List of usage examples for org.eclipse.jgit.api Git open
public static Git open(File dir) throws IOException
From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java
License:Open Source License
private static boolean hasAtLeastOneReference(File folder) { try {/*from w w w. j a va 2 s . c om*/ Repository repo = Git.open(folder).getRepository(); return repo.getAllRefs().values().stream().anyMatch(ref -> ref.getObjectId() != null); } catch (IOException e) { return false; } }
From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java
License:Open Source License
private void checkOutBranch(File destPath, String branch) { try {//from www.ja v a2 s. c o m Git git = Git.open(destPath); List<Ref> refs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); /* refs will have name like these: refs/heads/master refs/remotes/origin/master refs/remotes/origin/zanata */ Optional<Ref> localBranchRef = Optional.empty(); Optional<Ref> remoteBranchRef = Optional.empty(); for (Ref ref : refs) { String refName = ref.getName(); if (refName.equals("refs/heads/" + branch)) { localBranchRef = Optional.of(ref); } if (refName.equals("refs/remotes/origin/" + branch)) { remoteBranchRef = Optional.of(ref); } } if (branch.equals("master")) { log.debug("merging origin/master"); git.checkout().setName("master").call(); git.merge().setFastForward(MergeCommand.FastForwardMode.FF_ONLY).include(remoteBranchRef.get()) .call(); return; } /** * If branch found in local and is not master, delete it, create new local branch from remote. * If branch does not exists in remote, create new local branch based on master branch. */ Ref ref; if (localBranchRef.isPresent()) { git.checkout().setName("master").setForce(true).call(); git.branchDelete().setBranchNames(branch).call(); } if (remoteBranchRef.isPresent()) { ref = git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/" + branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM).call(); } else { ref = git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/master") .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM).call(); } log.debug("checked out {}", ref); git.checkout().setName(branch).call(); if (log.isDebugEnabled()) { log.debug("current branch is: {}", git.getRepository().getBranch()); } } catch (IOException | GitAPIException e) { throw new RepoSyncException(e); } }
From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java
License:Open Source License
@Override public void syncTranslationToRepo(String repoUrl, String branch, File baseDir) throws RepoSyncException { try {//w w w . j a va2 s .c om Git git = Git.open(baseDir); StatusCommand statusCommand = git.status(); Status status = statusCommand.call(); Set<String> uncommittedChanges = status.getUncommittedChanges(); uncommittedChanges.addAll(status.getUntracked()); if (!uncommittedChanges.isEmpty()) { log.info("uncommitted files in git repo: {}", uncommittedChanges); AddCommand addCommand = git.add(); addCommand.addFilepattern("."); addCommand.call(); log.info("commit changed files"); CommitCommand commitCommand = git.commit(); commitCommand.setCommitter("Zanata Auto Repo Sync", "zanata-users@redhat.com"); commitCommand.setMessage("Zanata Auto Repo Sync (pushing translations)"); commitCommand.call(); log.info("push to remote repo"); PushCommand pushCommand = git.push(); UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider( getCredentials().getUsername(), getCredentials().getSecret()); pushCommand.setCredentialsProvider(user); pushCommand.call(); } else { log.info("nothing changed so nothing to do"); } } catch (IOException e) { throw new RepoSyncException("failed opening " + baseDir + " as git repo", e); } catch (GitAPIException e) { throw new RepoSyncException("Failed committing translations into the repo", e); } }
From source file:pl.project13.maven.git.GitIntegrationTest.java
License:Open Source License
protected Git git() throws IOException, InterruptedException { return Git.open(dotGitDir(projectDir())); }
From source file:pt.up.fe.specs.git.SpecsGit.java
License:Apache License
public static File parseRepositoryUrl(String repositoryPath) { String repoName = getRepoName(repositoryPath); // Get repo folder File eclipseBuildFolder = getRepositoriesFolder(); File repoFolder = new File(eclipseBuildFolder, repoName); // If folder does not exist, or if it exists and is empty, clone repository if (!repoFolder.exists() || SpecsIo.isEmptyFolder(repoFolder)) { try {//from w w w .j a v a2 s .c o m SpecsLogs.msgInfo("Cloning repo '" + repositoryPath + "' to folder '" + repoFolder + "'"); Git.cloneRepository().setURI(repositoryPath).setDirectory(repoFolder).call(); return repoFolder; } catch (GitAPIException e) { throw new RuntimeException("Could not clone repository '" + repositoryPath + "'", e); } } // Repository already exists, pull try { SpecsLogs.msgInfo("Pulling repo '" + repositoryPath + "' in folder '" + repoFolder + "'"); Git gitRepo = Git.open(repoFolder); PullCommand pullCmd = gitRepo.pull(); pullCmd.call(); } catch (GitAPIException | IOException e) { throw new RuntimeException("Could not pull repository '" + repositoryPath + "'", e); } return repoFolder; }
From source file:se.kth.karamel.backend.github.GithubApi.java
/** * Adds a file to the Github repo's index. If the file already exists, it will delete it and replace its contents with * the new contents. You wil subsequenty need to commit the change and push the commit to github. * * @param owner// w w w .j ava 2 s . co m * @param repoName * @param fileName * @param contents * @throws KaramelException */ public synchronized static void addFile(String owner, String repoName, String fileName, String contents) throws KaramelException { File repoDir = getRepoDirectory(repoName); Git git = null; try { git = Git.open(repoDir); new File(repoDir + File.separator + fileName).delete(); new File(repoDir + File.separator + fileName).getParentFile().mkdirs(); try (PrintWriter out = new PrintWriter(repoDir + File.separator + fileName)) { out.println(contents); } git.add().addFilepattern(fileName).call(); } catch (IOException | GitAPIException ex) { throw new KaramelException(ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:se.kth.karamel.backend.github.GithubApi.java
public synchronized static void removeFile(String owner, String repoName, String fileName) throws KaramelException { File repoDir = getRepoDirectory(repoName); Git git = null;/*from www . java 2 s . c o m*/ try { git = Git.open(repoDir); new File(repoDir + File.separator + fileName).delete(); git.add().addFilepattern(fileName).call(); git.commit().setAuthor(user, email).setMessage("File removed by Karamel.").setAll(true).call(); git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call(); RepoItem toRemove = null; List<RepoItem> repos = cachedRepos.get(owner); for (RepoItem r : repos) { if (r.getName().compareToIgnoreCase(repoName) == 0) { toRemove = r; } } if (toRemove != null) { repos.remove(toRemove); } } catch (IOException | GitAPIException ex) { throw new KaramelException(ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:se.kth.karamel.backend.github.GithubApi.java
/** * Scaffolds a Karamel/chef project for an experiment and adds it to the github repo. You still need to commit and * push the changes to github.//from w w w . j a v a 2s. com * * @param repoName * @throws KaramelException */ public static void scaffoldRepo(String repoName) throws KaramelException { File repoDir = getRepoDirectory(repoName); Git git = null; try { git = Git.open(repoDir); CookbookScaffolder.create(repoName); git.add().addFilepattern("Berksfile").addFilepattern("metadata.rb").addFilepattern("Karamelfile") .addFilepattern(".kitchen.yml").addFilepattern("attributes").addFilepattern("recipes") .addFilepattern("templates").addFilepattern("README.md").call(); } catch (IOException | GitAPIException ex) { throw new KaramelException("Problem scaffolding a new Repository: " + ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:se.kth.karamel.backend.github.GithubApi.java
/** * Synchronizes your updates on your local repository with github. * * @param owner//from w w w .j av a2 s .c o m * @param repoName * @throws KaramelException */ public synchronized static void commitPush(String owner, String repoName) throws KaramelException { if (email == null || user == null) { throw new KaramelException("You forgot to call registerCredentials. You must call this method first."); } File repoDir = getRepoDirectory(repoName); Git git = null; try { git = Git.open(repoDir); git.commit().setAuthor(user, email).setMessage("Code generated by Karamel.").setAll(true).call(); git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call(); } catch (IOException | GitAPIException ex) { logger.error("error during github push", ex); throw new KaramelException(ex.getMessage()); } finally { if (git != null) { git.close(); } } }
From source file:services.player.ZoneManager.java
License:Open Source License
private void loadCommitHistory() { File repoDir = new File("./" + Constants.DOT_GIT); int commitCount = 3; int iterations = 0; try {// w w w.j av a 2 s. c om Git git = Git.open(repoDir); Repository repo = git.getRepository(); try { commitHistory = "The " + commitCount + " most recent commits in branch '" + repo.getBranch() + "':\n"; for (RevCommit commit : git.log().setMaxCount(commitCount).call()) { commitHistory += commit.getName().substring(0, 7) + " " + commit.getShortMessage(); if (commitCount > iterations++) commitHistory += "\n"; } } catch (GitAPIException e) { e.printStackTrace(); } } catch (IOException e) { System.err.println("ZoneManager: Failed to open " + repoDir + " to read commit history"); // An exception is thrown if bash isn't installed. // https://www.eclipse.org/forums/index.php/t/1031740/ } }