List of usage examples for org.eclipse.jgit.api CommitCommand setAuthor
public CommitCommand setAuthor(PersonIdent author)
From source file:com.barchart.jenkins.cascade.PluginScmGit.java
License:BSD License
/** * See {@link Git#commit()}// w w w. j a v a2 s. c o m */ public static RevCommit doCommit(final File workspace, final PersonIdent person, final String message) { try { final Git git = Git.open(workspace); final CommitCommand command = git.commit(); if (person != null) { command.setAuthor(person).setCommitter(person); } return command.setMessage(message).call(); } catch (final Throwable e) { throw new RuntimeException(e); } }
From source file:com.google.gerrit.acceptance.git.GitUtil.java
License:Apache License
private static Commit createCommit(Git git, PersonIdent i, String msg, String changeId) throws GitAPIException, IOException { final CommitCommand commitCmd = git.commit(); commitCmd.setAmend(changeId != null); commitCmd.setAuthor(i); commitCmd.setCommitter(i);//from ww w. ja va 2 s.c o m if (changeId == null) { ObjectId id = computeChangeId(git, i, msg); changeId = "I" + id.getName(); } msg = ChangeIdUtil.insertId(msg, ObjectId.fromString(changeId.substring(1))); commitCmd.setMessage(msg); RevCommit c = commitCmd.call(); return new Commit(c, changeId); }
From source file:com.google.gerrit.acceptance.git.ssh.GitUtil.java
License:Apache License
public static String createCommit(Git git, PersonIdent i, String msg, boolean insertChangeId) throws GitAPIException, IOException { ObjectId changeId = null;//w w w. jav a 2 s .c o m if (insertChangeId) { changeId = computeChangeId(git, i, msg); msg = ChangeIdUtil.insertId(msg, changeId); } final CommitCommand commitCmd = git.commit(); commitCmd.setAuthor(i); commitCmd.setCommitter(i); commitCmd.setMessage(msg); commitCmd.call(); return changeId != null ? "I" + changeId.getName() : null; }
From source file:io.fabric8.collector.git.GitHelpers.java
License:Apache License
public static RevCommit doCommitAndPush(Git git, String message, UserDetails userDetails, PersonIdent author, String branch, String origin, boolean pushOnCommit) throws GitAPIException { CommitCommand commit = git.commit().setAll(true).setMessage(message); if (author != null) { commit = commit.setAuthor(author); }/* w w w. j av a 2 s . c o m*/ RevCommit answer = commit.call(); if (LOG.isDebugEnabled()) { LOG.debug("Committed " + answer.getId() + " " + answer.getFullMessage()); } if (pushOnCommit) { PushCommand push = git.push(); configureCommand(push, userDetails); Iterable<PushResult> results = push.setRemote(origin).call(); for (PushResult result : results) { if (LOG.isDebugEnabled()) { LOG.debug("Pushed " + result.getMessages() + " " + result.getURI() + " branch: " + branch + " updates: " + toString(result.getRemoteUpdates())); } } } return answer; }
From source file:io.fabric8.forge.rest.main.GitCommandCompletePostProcessor.java
License:Apache License
protected RevCommit doCommitAndPush(Git git, String message, CredentialsProvider credentials, PersonIdent author, String remote, String branch, String origin) throws IOException, GitAPIException { CommitCommand commit = git.commit().setAll(true).setMessage(message); if (author != null) { commit = commit.setAuthor(author); }/*from www . j ava2 s . c o m*/ RevCommit answer = commit.call(); if (LOG.isDebugEnabled()) { LOG.debug("Committed " + answer.getId() + " " + answer.getFullMessage()); } if (isPushOnCommit()) { Iterable<PushResult> results = git.push().setCredentialsProvider(credentials).setRemote(origin).call(); for (PushResult result : results) { if (LOG.isDebugEnabled()) { LOG.debug("Pushed " + result.getMessages() + " " + result.getURI() + " branch: " + branch + " updates: " + GitHelpers.toString(result.getRemoteUpdates())); } } } return answer; }
From source file:io.fabric8.maven.HelmPushMojo.java
License:Apache License
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (!isRootReactorBuild()) { getLog().info("Not the root reactor build so not committing changes"); return;/*from ww w. j a va 2 s. c o m*/ } File outputDir = getHelmRepoFolder(); if (!Files.isDirectory(outputDir)) { throw new MojoExecutionException( "No helm repository exists for " + outputDir + ". Did you run `mvn fabric8:helm` yet?"); } File gitFolder = new File(outputDir, ".git"); if (!Files.isDirectory(gitFolder)) { throw new MojoExecutionException( "No helm git repository exists for " + gitFolder + ". Did you run `mvn fabric8:helm` yet?"); } FileRepositoryBuilder builder = new FileRepositoryBuilder(); Git git = null; try { Repository repository = builder.setGitDir(gitFolder).readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); git = new Git(repository); git.add().addFilepattern(".").call(); } catch (Exception e) { throw new MojoExecutionException("Failed to add files to the helm git repository: " + e, e); } CommitCommand commit = git.commit().setAll(true).setMessage(commitMessage); PersonIdent author = null; if (Strings.isNotBlank(userName) && Strings.isNotBlank(emailAddress)) { author = new PersonIdent(userName, emailAddress); } if (author != null) { commit = commit.setAuthor(author); } try { RevCommit answer = commit.call(); getLog().info("Committed " + answer.getId() + " " + answer.getFullMessage()); } catch (GitAPIException e) { throw new MojoExecutionException("Failed to commit changes to help repository: " + e, e); } if (pushChanges) { PushCommand push = git.push(); try { push.setRemote(remoteRepoName).call(); getLog().info("Pushed commits upstream to " + getHelmGitUrl()); } catch (GitAPIException e) { throw new MojoExecutionException( "Failed to push helm git changes to remote repository " + remoteRepoName + ": " + e, e); } } }
From source file:io.hawt.git.GitFacadeSupport.java
License:Apache License
/** * Performs a write operation on the file *//* w ww. j a va 2s. c o m*/ protected <T> T doWriteFile(Git git, File rootDir, String branch, String pathOrEmpty, WriteCallback callback) throws Exception { checkoutBranch(git, branch); String path = Strings.isBlank(pathOrEmpty) ? "/" : pathOrEmpty; File file = getFile(rootDir, path); WriteContext context = new WriteContext(git, rootDir, file); T results = (T) callback.apply(context); if (context.isRequiresCommit()) { PersonIdent author = context.getAuthor(); String message = context.getMessage(); if (Strings.isBlank(message)) { message = "Updated " + Files.getRelativePath(rootDir, file); } CommitCommand command = git.commit().setAll(true).setMessage(message); if (author != null) { command = command.setAuthor(author); } RevCommit revCommit = commitThenPush(git, branch, command); createCommitInfo(revCommit); } return results; }
From source file:net.polydawn.mdm.commands.MdmReleaseCommand.java
License:Open Source License
public MdmExitMessage call() throws IOException, MdmException, MdmExitMessage { MdmModuleRelease relModule = loadReleaseModule(); Repository relRepo = relModule.getRepo(); relModule.assertPresentsAsReleaseRepo(); assertReleaseRepoDoesntAlreadyContain(relModule, version); assertReleaseRepoClean(relModule);//from ww w .ja v a2 s. c om List<String> inputFiles = selectInputFiles(); // create a branch for the release commit. depending on whether or not infix mode is enabled, this is either branching from the infix branch, or it's founding a new root of history. boolean infixMode = relRepo.getRef("refs/heads/mdm/infix") != null; if (infixMode) try { new Git(relRepo).checkout().setCreateBranch(true).setStartPoint("mdm/infix") .setName("mdm/release/" + version).call(); } catch (RefAlreadyExistsException e) { return new MdmExitMessage(":'(", "the releases repo already has a release point labeled version " + version + " !"); } catch (RefNotFoundException e) { throw new MdmException("aborted due to concurrent modification of repo"); } catch (InvalidRefNameException e) { return new MdmExitMessage(":(", "you can't use version names that git rejects as branch names."); } catch (CheckoutConflictException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } else { Plumbing.createOrphanBranch(relRepo, "mdm/release/" + version); try { new Git(relRepo).checkout().setName("mdm/release/" + version).call(); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } } // enumerate and copy in artifact files. File inputBase = new File(inputPath).getCanonicalFile(); if (inputBase.isFile()) inputBase = inputBase.getParentFile(); File relRepoFile = new File(relRepoPath).getCanonicalFile(); for (String input : inputFiles) { File inputFull = new File(inputBase, input); File dest = new File(relRepoFile, input); if (inputFull.isDirectory()) FileUtils.copyDirectory(inputFull, dest, new FileFilter() { public boolean accept(File file) { return !(file.isDirectory() && file.listFiles().length == 0); } }, true, false); else FileUtils.copyFile(inputFull, dest, true, false); } // commit the changes try { new Git(relRepo).add().addFilepattern(".").call(); } catch (NoFilepatternException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } try { CommitCommand commit = new Git(relRepo).commit().setMessage("release version " + version); if (!infixMode) { commit.setAmend(true); // because our mechanism for creating an orphan branch starts us with an empty commit. PersonIdent convergenceIdent = new PersonIdent("mdm", "", new Date(0), TimeZone.getTimeZone("GMT")); commit.setAuthor(convergenceIdent); commit.setCommitter(convergenceIdent); } commit.call(); // this tag will be removed in a future release, as it's no longer required for any structural purpose // (long long ago in a galaxy far away, this was used as an easy way for `mdm status` to report version names... but execing `git describe --tags` hasn't been the way we do this anymore for a long time now) new Git(relRepo).tag().setName("release/" + version).setAnnotated(false).call(); } catch (NoHeadException e) { throw new MdmException("your repository is in an invalid state!", e); } catch (ConcurrentRefUpdateException e) { throw new MdmException("aborted due to concurrent modification of repo"); } catch (NoMessageException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? } catch (UnmergedPathsException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (WrongRepositoryStateException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } // generate an accumulation commit. do this from the master branch, but don't submit it yet, because when we roll in the artifacts we want them in a subdirectory so that when master is checked out all the versions are splayed out in the working tree at once. try { new Git(relRepo).checkout().setName("master").call(); } catch (RefAlreadyExistsException e) { throw new MajorBug(e); // not even valid unless we're creating a new branch, which we aren't. } catch (RefNotFoundException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (InvalidRefNameException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (CheckoutConflictException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } try { new Git(relRepo).merge().include(relRepo.getRef("mdm/release/" + version)) .setFastForward(FastForwardMode.NO_FF).setCommit(false).call(); } catch (NoHeadException e) { throw new MdmException("your repository is in an invalid state!", e); } catch (ConcurrentRefUpdateException e) { throw new MdmException("aborted due to concurrent modification of repo"); } catch (CheckoutConflictException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (InvalidMergeHeadsException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (WrongRepositoryStateException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (NoMessageException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? also, we're not even making a commit here. } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } // move the artifact files into a version-named directory File artifactDestFile = new File(relRepoFile, version); if (!artifactDestFile.mkdir()) return new MdmExitMessage(":'(", "couldn't make the directory named \"" + version + "\" to put the releases into because there was already something there."); for (String input : inputFiles) new File(relRepoFile, input).renameTo(new File(artifactDestFile, input)); // now fire off the accumulation commit, and that commit now becomes head of the master branch. try { RmCommand rmc = new Git(relRepo).rm(); for (String input : inputFiles) rmc.addFilepattern(input); rmc.call(); new Git(relRepo).add().addFilepattern(version).call(); } catch (NoFilepatternException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } try { new Git(relRepo).commit().setMessage("merge release version " + version + " to master").call(); new Git(relRepo).tag().setName("mdm/master/" + version).setAnnotated(false).call(); } catch (NoHeadException e) { throw new MdmException("your repository is in an invalid state!", e); } catch (ConcurrentRefUpdateException e) { throw new MdmException("aborted due to concurrent modification of repo"); } catch (NoMessageException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? } catch (UnmergedPathsException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (WrongRepositoryStateException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } // commit the new hash of the releases-repo into the project main repo (if we are operating in a canonically placed releases submodule) if (isInRepoRoot() && relRepoPath.equals("releases") && Plumbing.isCommitedGitlink(repo, "releases")) { try { new Git(repo).commit().setOnly("releases").setMessage("release version " + version).call(); new Git(repo).tag().setName("release/" + version).setAnnotated(false).call(); } catch (NoHeadException e) { throw new MdmException("your repository is in an invalid state!", e); } catch (ConcurrentRefUpdateException e) { throw new MdmException("aborted due to concurrent modification of repo"); } catch (NoMessageException e) { throw new MajorBug(e); // why would an api throw exceptions like this *checked*? } catch (UnmergedPathsException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (WrongRepositoryStateException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } catch (GitAPIException e) { throw new MajorBug("an unrecognized problem occurred. please file a bug report.", e); } } return new MdmExitMessage(":D", "release version " + version + " complete"); }
From source file:org.ajoberstar.gradle.git.tasks.GitCommit.java
License:Apache License
/** * Commits changes to the Git repository. *//*from w w w.ja va 2 s .c o m*/ @TaskAction public void commit() { final CommitCommand cmd = getGit().commit(); cmd.setMessage(getMessage()); cmd.setAll(getCommitAll()); if (committer != null) { cmd.setCommitter(getCommitter()); } if (author != null) { cmd.setAuthor(getAuthor()); } if (!patternSet.getExcludes().isEmpty() || !patternSet.getIncludes().isEmpty()) { getSource().visit(new FileVisitor() { public void visitDir(FileVisitDetails arg0) { visitFile(arg0); } public void visitFile(FileVisitDetails arg0) { cmd.setOnly(arg0.getPath()); } }); } try { cmd.call(); } catch (Exception e) { throw new GradleException("Problem committing changes.", e); } }
From source file:org.archicontribs.modelrepository.grafico.GraficoUtils.java
License:Open Source License
/** * Commit a model with any changes to local repo * @param model// ww w .ja v a 2 s .c o m * @param localGitFolder * @param personIdent * @param commitMessage * @return * @throws GitAPIException * @throws IOException */ public static RevCommit commitModel(IArchimateModel model, File localGitFolder, PersonIdent personIdent, String commitMessage) throws GitAPIException, IOException { GraficoModelExporter exporter = new GraficoModelExporter(); exporter.exportModelToLocalGitRepository(model, localGitFolder); try (Git git = Git.open(localGitFolder)) { Status status = git.status().call(); // Nothing changed if (status.isClean()) { return null; } // Add modified files to index AddCommand addCommand = git.add(); addCommand.addFilepattern("."); //$NON-NLS-1$ addCommand.setUpdate(false); addCommand.call(); // Add missing files to index for (String s : status.getMissing()) { git.rm().addFilepattern(s).call(); } // Commit CommitCommand commitCommand = git.commit(); commitCommand.setAuthor(personIdent); commitCommand.setMessage(commitMessage); return commitCommand.call(); } }