List of usage examples for org.eclipse.jgit.api Git commit
public CommitCommand commit()
From source file:org.wise.portal.presentation.web.controllers.author.project.JGitUtils.java
License:Open Source License
/** * Adds all changes in specified directory to git index and then commits them * with the specified commit message// w w w.j a va 2 s . com * * @param directoryPath * @param author author username * @param commitMessage * @throws IOException * @throws GitAPIException */ public static void commitAllChangesToCurriculumHistory(String directoryPath, String author, String commitMessage) throws IOException, GitAPIException { boolean doCreate = true; Repository gitRepository = getGitRepository(directoryPath, doCreate); Git git = new Git(gitRepository); // add all changes in directory git.add().addFilepattern(".").call(); String email = ""; // don't save any emails for now. // commit all changes git.commit().setAll(true).setAuthor(author, email).setMessage(commitMessage).call(); gitRepository.close(); }
From source file:org.wso2.carbon.appfactory.repository.mgt.git.JGitAgent.java
License:Apache License
/** * Git commit//from w w w.j a va2 s .c o m * * @param commitMsg the commit message * @param all If set to true the Commit command automatically stages files that have been modified * and deleted, but new files not known by the repository are not affected. * @param repositoryDirectory repository directory where .git exists. * @return success * @throws RepositoryMgtException if error while branching */ @Override public boolean commit(String commitMsg, boolean all, File repositoryDirectory) throws RepositoryMgtException { try { Repository repository = getLocalRepository(repositoryDirectory); Git git = new Git(repository); git.commit().setMessage(commitMsg).setAll(all).call(); return true; } catch (IOException e) { String msg = "Error while committing due to " + e.getMessage() + " from IOException"; log.error(msg, e); throw new RepositoryMgtException(msg, e); } catch (GitAPIException e) { String msg = "Error while committing due to " + e.getMessage() + " from GitAPIException"; log.error(msg, e); throw new RepositoryMgtException(msg, e); } }
From source file:org.xwiki.git.GitHelper.java
License:Open Source License
public void add(File repo, String path, String content, PersonIdent author, PersonIdent committer, String message) throws Exception { File file = new File(repo.getParentFile(), path); if (!file.getParentFile().exists()) { Assert.assertTrue(file.getParentFile().mkdirs()); }/* w w w . java 2s.com*/ if (!file.exists()) { Assert.assertTrue(file.createNewFile()); } PrintWriter writer = new PrintWriter(file); if (content == null) content = ""; try { writer.print(content); } finally { writer.close(); } Git git = Git.open(repo); git.add().addFilepattern(path).call(); RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor(author).setCommitter(committer) .call(); Assert.assertNotNull(commit); }
From source file:org.xwiki.git.internal.GitScriptServiceTest.java
License:Open Source License
private void add(File repo, String path, String content, String message) throws Exception { File file = new File(repo.getParentFile(), path); if (!file.getParentFile().exists()) { Assert.assertTrue(file.getParentFile().mkdirs()); }/*from w w w . j av a 2 s. c om*/ if (!file.exists()) { Assert.assertTrue(file.createNewFile()); } PrintWriter writer = new PrintWriter(file); if (content == null) content = ""; try { writer.print(content); } finally { writer.close(); } Git git = Git.open(repo); git.add().addFilepattern(path).call(); RevCommit commit = git.commit().setOnly(path).setMessage(message).setAuthor("test author", "john@does.com") .setCommitter("test committer", "john@does.com").call(); Assert.assertNotNull(commit); }
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 {//from w w w . j a v a2s . co m 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:org.zend.sdkcli.internal.commands.GitPushApplicationCommand.java
License:Open Source License
@Override protected boolean doExecute() { Repository repo = null;/*from w ww. j av a 2s . c o m*/ try { File gitDir = getRepo(); if (!gitDir.exists()) { getLogger().error("Git repository is not available in provided location"); return false; } repo = FileRepositoryBuilder.create(getRepo()); } catch (IOException e) { getLogger().error(e); return false; } if (repo != null) { Git git = new Git(repo); String remote = doGetRemote(repo); if (remote == null) { getLogger().error("Invalid remote value: " + getRemote()); return false; } // perform operation only if it is clone of phpCloud repository String repoUrl = git.getRepository().getConfig().getString("remote", remote, "url"); AddCommand addCommand = git.add(); addCommand.setUpdate(false); // add all new files addCommand.addFilepattern("."); try { addCommand.call(); } catch (NoFilepatternException e) { // should not occur because '.' is used getLogger().error(e); return false; } catch (GitAPIException e) { getLogger().error(e); return false; } CommitCommand commitCommand = git.commit(); // automatically stage files that have been modified and deleted commitCommand.setAll(true); PersonIdent ident = getPersonalIdent(repoUrl); if (ident == null) { getLogger().error("Invalid author information provided: " + getAuthor()); return false; } commitCommand.setAuthor(ident); commitCommand.setInsertChangeId(true); commitCommand.setMessage(getMessage()); try { commitCommand.call(); } catch (Exception e) { getLogger().error(e); return false; } // at the end push all changes PushCommand pushCommand = git.push(); pushCommand.setPushAll(); pushCommand.setRemote(remote); pushCommand.setProgressMonitor(new TextProgressMonitor(new PrintWriter(System.out))); try { URIish uri = new URIish(repoUrl); if (GITHUB_HOST.equals(uri.getHost()) && "git".equals(uri.getUser())) { if (!prepareSSHFactory()) { return false; } } else { CredentialsProvider credentials = getCredentials(repoUrl); if (credentials != null) { pushCommand.setCredentialsProvider(credentials); } } Iterable<PushResult> result = pushCommand.call(); for (PushResult pushResult : result) { pushResult.getAdvertisedRefs(); Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates(); for (RemoteRefUpdate remoteRefUpdate : updates) { TrackingRefUpdate trackingRefUpdate = remoteRefUpdate.getTrackingRefUpdate(); getLogger().info(MessageFormat.format("Remote name: {0}, status: {1}", remoteRefUpdate.getRemoteName(), remoteRefUpdate.getStatus().toString())); getLogger().info(MessageFormat.format("Remote name: {0}, result: {1}", trackingRefUpdate.getRemoteName(), trackingRefUpdate.getResult().toString())); } } } catch (JGitInternalException e) { getLogger().error(e); return false; } catch (InvalidRemoteException e) { // should not occur because selected remote is available getLogger().error(e); return false; } catch (URISyntaxException e) { getLogger().error(e); return false; } catch (TransportException e) { getLogger().error(e); return false; } catch (GitAPIException e) { getLogger().error(e); return false; } } return true; }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void getPatch() throws IOException, GitAPIException { // given/*from ww w.j a v a 2 s . co m*/ String userName = "yobi"; String projectName = "testProject"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName; String repoPath = wcPath + "/.git"; File repoDir = new File(repoPath); Repository repo = new RepositoryBuilder().setGitDir(repoDir).build(); repo.create(false); Git git = new Git(repo); String testFilePath = wcPath + "/readme.txt"; BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath)); // when out.write("hello 1"); out.flush(); git.add().addFilepattern("readme.txt").call(); git.commit().setMessage("commit 1").call(); out.write("hello 2"); out.flush(); git.add().addFilepattern("readme.txt").call(); RevCommit commit = git.commit().setMessage("commit 2").call(); GitRepository gitRepo = new GitRepository(userName, projectName + "/"); String patch = gitRepo.getPatch(commit.getId().getName()); // then assertThat(patch).contains("-hello 1"); assertThat(patch).contains("+hello 1hello 2"); }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void getHistory() throws IOException, GitAPIException { // given/*from ww w. jav a 2 s. c o m*/ String userName = "yobi"; String projectName = "testProject"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName; String repoPath = wcPath + "/.git"; File repoDir = new File(repoPath); Repository repo = new RepositoryBuilder().setGitDir(repoDir).build(); repo.create(false); Git git = new Git(repo); String testFilePath = wcPath + "/readme.txt"; BufferedWriter out = new BufferedWriter(new FileWriter(testFilePath)); // when out.write("hello 1"); out.flush(); git.add().addFilepattern("readme.txt").call(); git.commit().setMessage("commit 1").call(); out.write("hello 2"); out.flush(); git.add().addFilepattern("readme.txt").call(); git.commit().setMessage("commit 2").call(); git.tag().setName("tag").setAnnotated(true).call(); out.write("hello 3"); out.flush(); git.add().addFilepattern("readme.txt").call(); git.commit().setMessage("commit 3").call(); GitRepository gitRepo = new GitRepository(userName, projectName + "/"); List<Commit> history2 = gitRepo.getHistory(0, 2, "HEAD", null); List<Commit> history5 = gitRepo.getHistory(0, 5, null, null); List<Commit> tagHistory2 = gitRepo.getHistory(0, 2, "tag", null); // then assertThat(history2.size()).isEqualTo(2); assertThat(history2.get(0).getMessage()).isEqualTo("commit 3"); assertThat(history2.get(1).getMessage()).isEqualTo("commit 2"); assertThat(history5.size()).isEqualTo(3); assertThat(history5.get(0).getMessage()).isEqualTo("commit 3"); assertThat(history5.get(1).getMessage()).isEqualTo("commit 2"); assertThat(history5.get(2).getMessage()).isEqualTo("commit 1"); assertThat(tagHistory2.size()).isEqualTo(2); assertThat(tagHistory2.get(0).getMessage()).isEqualTo("commit 2"); assertThat(tagHistory2.get(1).getMessage()).isEqualTo("commit 1"); }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
@Test public void getMetaDataFromPath() throws Exception { // Given//from ww w . j a v a 2s .c o m final String userName = "yobi"; final String projectName = "mytest"; final String branchName = "branch"; final String lightWeightTagName = "tag1"; final String annotatedTagName = "tag2"; String wcPath = GitRepository.getRepoPrefix() + userName + "/" + projectName; Repository repository = GitRepository.buildGitRepository(userName, projectName + "/"); repository.create(); Git git = new Git(repository); FileUtils.touch(new File(wcPath + "/hello")); FileUtils.touch(new File(wcPath + "/dir/world")); git.add().addFilepattern("hello").call(); git.add().addFilepattern("dir").call(); git.commit().setAuthor("yobi", "yobi@yobi.io").setMessage("test").call(); git.branchCreate().setName(branchName).call(); git.tag().setName(lightWeightTagName).setAnnotated(false).call(); git.tag().setName(annotatedTagName).setAnnotated(true).setMessage("annotated tag").call(); repository.close(); running(support.Helpers.makeTestApplication(), new Runnable() { @Override public void run() { try { // When GitRepository gitRepository = new GitRepository(userName, projectName + "/"); ObjectNode notExistBranch = gitRepository.getMetaDataFromPath("not_exist_branch", ""); ObjectNode root = gitRepository.getMetaDataFromPath(""); ObjectNode dir = gitRepository.getMetaDataFromPath("dir"); ObjectNode file = gitRepository.getMetaDataFromPath("hello"); ObjectNode branch = gitRepository.getMetaDataFromPath(branchName, ""); ObjectNode lightWeightTag = gitRepository.getMetaDataFromPath(lightWeightTagName, ""); ObjectNode annotatedTag = gitRepository.getMetaDataFromPath(annotatedTagName, ""); // Then assertThat(notExistBranch).isNull(); assertThat(root.get("type").textValue()).isEqualTo("folder"); assertThat(root.get("data").get("hello").get("type").textValue()).isEqualTo("file"); assertThat(root.get("data").get("dir").get("type").textValue()).isEqualTo("folder"); assertThat(dir.get("type").textValue()).isEqualTo("folder"); assertThat(dir.get("data").get("world").get("type").textValue()).isEqualTo("file"); assertThat(file.get("type").textValue()).isEqualTo("file"); assertThat(branch.toString()).isEqualTo(root.toString()); assertThat(lightWeightTag.toString()).isEqualTo(root.toString()); assertThat(annotatedTag.toString()).isEqualTo(root.toString()); } catch (Exception e) { throw new RuntimeException(e); } } }); }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
public RevCommit addCommit(Git git, String fileName, String contents, String commitMessage, User author) throws IOException, GitAPIException { File newFile = new File(git.getRepository().getWorkTree().getAbsolutePath(), fileName); BufferedWriter out = new BufferedWriter(new FileWriter(newFile)); out.write(contents);// ww w. j a v a 2 s .co m out.flush(); git.add().addFilepattern(fileName).call(); CommitCommand commit = git.commit().setMessage(commitMessage); if (author != null) { commit.setAuthor(author.loginId, author.email).setCommitter(author.loginId, author.email); } return commit.call(); }