List of usage examples for org.eclipse.jgit.lib PersonIdent PersonIdent
private PersonIdent(final String aName, final String aEmailAddress, long when)
From source file:com.google.gerrit.server.patch.AutoMerger.java
License:Apache License
private RevCommit commit(Repository repo, RevWalk rw, ObjectInserter ins, String refName, ObjectId tree, RevCommit merge) throws IOException { rw.parseHeaders(merge);/*www . j a v a 2 s .co m*/ // For maximum stability, choose a single ident using the committer time of // the input commit, using the server name and timezone. PersonIdent ident = new PersonIdent(gerritIdent, merge.getCommitterIdent().getWhen(), gerritIdent.getTimeZone()); CommitBuilder cb = new CommitBuilder(); cb.setAuthor(ident); cb.setCommitter(ident); cb.setTreeId(tree); cb.setMessage("Auto-merge of " + merge.name() + '\n'); for (RevCommit p : merge.getParents()) { cb.addParentId(p); } ObjectId commitId; commitId = ins.insert(cb); if (save) { ins.flush(); RefUpdate ru = repo.updateRef(refName); ru.setNewObjectId(commitId); ru.disableRefLog(); ru.forceUpdate(); } return rw.parseCommit(commitId); }
From source file:org.eclipse.egit.core.op.CommitOperation.java
License:Open Source License
public void execute(IProgressMonitor m) throws CoreException { IProgressMonitor monitor;/* ww w .jav a 2 s. c om*/ if (m == null) monitor = new NullProgressMonitor(); else monitor = m; IWorkspaceRunnable action = new IWorkspaceRunnable() { public void run(IProgressMonitor actMonitor) throws CoreException { final Date commitDate = new Date(); final TimeZone timeZone = TimeZone.getDefault(); final PersonIdent authorIdent = RawParseUtils.parsePersonIdent(author); final PersonIdent committerIdent = RawParseUtils.parsePersonIdent(committer); if (commitAll) { for (Repository repo : repos) { Git git = new Git(repo); try { git.commit().setAll(true).setAuthor(new PersonIdent(authorIdent, commitDate, timeZone)) .setCommitter(new PersonIdent(committerIdent, commitDate, timeZone)) .setMessage(message).call(); } catch (NoHeadException e) { throw new TeamException(e.getLocalizedMessage(), e); } catch (NoMessageException e) { throw new TeamException(e.getLocalizedMessage(), e); } catch (UnmergedPathException e) { throw new TeamException(e.getLocalizedMessage(), e); } catch (ConcurrentRefUpdateException e) { throw new TeamException(CoreText.MergeOperation_InternalError, e); } catch (JGitInternalException e) { throw new TeamException(CoreText.MergeOperation_InternalError, e); } catch (WrongRepositoryStateException e) { throw new TeamException(e.getLocalizedMessage(), e); } } } else if (amending || filesToCommit != null && filesToCommit.length > 0) { actMonitor.beginTask(CoreText.CommitOperation_PerformingCommit, filesToCommit.length * 2); actMonitor.setTaskName(CoreText.CommitOperation_PerformingCommit); HashMap<Repository, Tree> treeMap = new HashMap<Repository, Tree>(); try { if (!prepareTrees(filesToCommit, treeMap, actMonitor)) { // reread the indexes, they were changed in memory for (Repository repo : treeMap.keySet()) repo.getIndex().read(); return; } } catch (IOException e) { throw new TeamException(CoreText.CommitOperation_errorPreparingTrees, e); } try { doCommits(message, treeMap); actMonitor.worked(filesToCommit.length); } catch (IOException e) { throw new TeamException(CoreText.CommitOperation_errorCommittingChanges, e); } } else if (commitWorkingDirChanges) { // TODO commit -a } else { // TODO commit } } }; ResourcesPlugin.getWorkspace().run(action, monitor); }
From source file:org.eclipse.egit.core.op.CommitOperation.java
License:Open Source License
private void doCommits(String actMessage, HashMap<Repository, Tree> treeMap) throws IOException, TeamException { String commitMessage = actMessage; final Date commitDate = new Date(); final TimeZone timeZone = TimeZone.getDefault(); final PersonIdent authorIdent = RawParseUtils.parsePersonIdent(author); final PersonIdent committerIdent = RawParseUtils.parsePersonIdent(committer); for (java.util.Map.Entry<Repository, Tree> entry : treeMap.entrySet()) { Tree tree = entry.getValue();//from ww w.j ava 2 s.c o m Repository repo = tree.getRepository(); repo.getIndex().write(); writeTreeWithSubTrees(tree); ObjectId currentHeadId = repo.resolve(Constants.HEAD); ObjectId[] parentIds; if (amending) { RevCommit[] parents = previousCommit.getParents(); parentIds = new ObjectId[parents.length]; for (int i = 0; i < parents.length; i++) parentIds[i] = parents[i].getId(); } else { if (currentHeadId != null) parentIds = new ObjectId[] { currentHeadId }; else parentIds = new ObjectId[0]; } if (createChangeId) { ObjectId parentId; if (parentIds.length > 0) parentId = parentIds[0]; else parentId = null; ObjectId changeId = ChangeIdUtil.computeChangeId(tree.getId(), parentId, authorIdent, committerIdent, commitMessage); commitMessage = ChangeIdUtil.insertId(commitMessage, changeId); if (changeId != null) commitMessage = commitMessage.replaceAll( "\nChange-Id: I0000000000000000000000000000000000000000\n", //$NON-NLS-1$ "\nChange-Id: I" + changeId.getName() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ } CommitBuilder commit = new CommitBuilder(); commit.setTreeId(tree.getTreeId()); commit.setParentIds(parentIds); commit.setMessage(commitMessage); commit.setAuthor(new PersonIdent(authorIdent, commitDate, timeZone)); commit.setCommitter(new PersonIdent(committerIdent, commitDate, timeZone)); ObjectInserter inserter = repo.newObjectInserter(); ObjectId commitId; try { commitId = inserter.insert(commit); inserter.flush(); } finally { inserter.release(); } final RefUpdate ru = repo.updateRef(Constants.HEAD); ru.setNewObjectId(commitId); ru.setRefLogMessage(buildReflogMessage(commitMessage), false); if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) { throw new TeamException(NLS.bind(CoreText.CommitOperation_failedToUpdate, ru.getName(), commitId)); } } }
From source file:org.eclipse.egit.core.test.internal.mapping.T0002_HistoryTest.java
License:Open Source License
@Before public void setUp() throws Exception { super.setUp(); project.createSourceFolder();/* www .j av a2s .co m*/ gitDir = new File(project.getProject().getWorkspace().getRoot().getRawLocation().toFile(), Constants.DOT_GIT); thisGit = new Repository(gitDir); workDir = thisGit.getWorkDir(); thisGit.create(); objectWriter = new ObjectWriter(thisGit); tree = new Tree(thisGit); Tree projectTree = tree.addTree("Project-1"); File project1_a_txt = createFile("Project-1/A.txt", "A.txt - first version\n"); addFile(projectTree, project1_a_txt); projectTree.setId(objectWriter.writeTree(projectTree)); File project1_b_txt = createFile("Project-1/B.txt", "B.txt - first version\n"); addFile(projectTree, project1_b_txt); projectTree.setId(objectWriter.writeTree(projectTree)); tree.setId(objectWriter.writeTree(tree)); Commit commit = new Commit(thisGit); commit.setAuthor(new PersonIdent(jauthor, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setCommitter(new PersonIdent(jcommitter, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setMessage("Foo\n\nMessage"); commit.setTree(tree); ObjectId commitId = objectWriter.writeCommit(commit); tree = new Tree(thisGit); projectTree = tree.addTree("Project-1"); addFile(projectTree, project1_a_txt); File project1_b_v2_txt = createFile("Project-1/B.txt", "B.txt - second version\n"); addFile(projectTree, project1_b_v2_txt); projectTree.setId(objectWriter.writeTree(projectTree)); tree.setId(objectWriter.writeTree(tree)); commit = new Commit(thisGit); commit.setAuthor(new PersonIdent(jauthor, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setCommitter(new PersonIdent(jcommitter, new Date(0L), TimeZone.getTimeZone("GMT+1"))); commit.setMessage("Modified"); commit.setParentIds(new ObjectId[] { commitId }); commit.setTree(tree); commitId = objectWriter.writeCommit(commit); RefUpdate lck = thisGit.updateRef("refs/heads/master"); assertNotNull("obtained lock", lck); lck.setNewObjectId(commitId); assertEquals(RefUpdate.Result.NEW, lck.forceUpdate()); ConnectProviderOperation operation = new ConnectProviderOperation(project.getProject(), gitDir); operation.execute(null); }
From source file:org.flowerplatform.web.git.operation.CommitOperation.java
License:Open Source License
public boolean commit(String repositoryLocation, List<CommitResourceDto> files, String author, String committer, String message, boolean amending) { ProgressMonitor monitor = ProgressMonitor .create(GitPlugin.getInstance().getMessage("git.commit.monitor.title"), channel); try {/*from ww w. jav a2s . c o m*/ Repository repo = GitPlugin.getInstance().getUtils().getRepository(new File(repositoryLocation)); Collection<String> notTracked = new HashSet<String>(); Collection<String> resources = new HashSet<String>(); for (CommitResourceDto file : files) { resources.add(file.getPath()); if (file.getState() == CommitResourceDto.UNTRACKED) { notTracked.add(file.getPath()); } } monitor.beginTask(GitPlugin.getInstance().getMessage("git.commit.monitor.message"), 10); addUntracked(notTracked, repo); monitor.worked(1); CommitCommand commitCommand = new Git(repo).commit(); commitCommand.setAmend(amending).setMessage(message); for (String path : resources) { commitCommand.setOnly(path); } Date commitDate = new Date(); TimeZone timeZone = TimeZone.getDefault(); PersonIdent enteredAuthor = RawParseUtils.parsePersonIdent(author); PersonIdent enteredCommitter = RawParseUtils.parsePersonIdent(committer); if (enteredAuthor == null) { channel.appendOrSendCommand(new DisplaySimpleMessageClientCommand( CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance() .getMessage("git.commit.errorParsingPersonIdent", new Object[] { author }), DisplaySimpleMessageClientCommand.ICON_ERROR)); return false; } if (enteredCommitter == null) { channel.appendOrSendCommand(new DisplaySimpleMessageClientCommand( CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance() .getMessage("git.commit.errorParsingPersonIdent", new Object[] { committer }), DisplaySimpleMessageClientCommand.ICON_ERROR)); return false; } PersonIdent authorIdent = new PersonIdent(enteredAuthor, commitDate, timeZone); PersonIdent committerIdent = new PersonIdent(enteredCommitter, commitDate, timeZone); if (amending) { RevCommit headCommit = GitPlugin.getInstance().getUtils().getHeadCommit(repo); if (headCommit != null) { PersonIdent headAuthor = headCommit.getAuthorIdent(); authorIdent = new PersonIdent(enteredAuthor, headAuthor.getWhen(), headAuthor.getTimeZone()); } } commitCommand.setAuthor(authorIdent); commitCommand.setCommitter(committerIdent); monitor.worked(1); commitCommand.call(); if (monitor.isCanceled()) { return false; } monitor.worked(8); // GitLightweightDecorator.refresh(); // // updateDispatcher.dispatchContentUpdate(null, repo, GitTreeUpdateDispatcher.COMMIT, null); return true; } catch (Exception e) { logger.debug(GitPlugin.getInstance().getMessage("git.commit.error"), e); channel.appendOrSendCommand( new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"), e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR)); return false; } finally { monitor.done(); } }
From source file:org.nbgit.client.CommitBuilder.java
License:Open Source License
/** * Set the author and commit time for the commit. * * @param time to use for the commit.//from ww w . ja v a2 s .c o m * @param timeZone to use for the commit. * @return the builder. */ public CommitBuilder time(long time, int timeZone) { personIdent = new PersonIdent(personIdent, time, timeZone); return this; }