org.eclipse.jgit.lib.CommitBuilder Java Examples
The following examples show how to use
org.eclipse.jgit.lib.CommitBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubtreeMerger.java From git-merge-repos with Apache License 2.0 | 6 votes |
public ObjectId createMergeCommit(Map<SubtreeConfig, RevCommit> parentCommits, String message) throws IOException { PersonIdent latestIdent = getLatestPersonIdent(parentCommits.values()); DirCache treeDirCache = createTreeDirCache(parentCommits, message); List<? extends ObjectId> parentIds = new ArrayList<>(parentCommits.values()); try (ObjectInserter inserter = repository.newObjectInserter()) { ObjectId treeId = treeDirCache.writeTree(inserter); PersonIdent repositoryUser = new PersonIdent(repository); PersonIdent ident = new PersonIdent(repositoryUser, latestIdent.getWhen().getTime(), latestIdent.getTimeZoneOffset()); CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setTreeId(treeId); commitBuilder.setAuthor(ident); commitBuilder.setCommitter(ident); commitBuilder.setMessage(message); commitBuilder.setParentIds(parentIds); ObjectId mergeCommit = inserter.insert(commitBuilder); inserter.flush(); return mergeCommit; } }
Example #2
Source File: GitUtils.java From onedev with MIT License | 5 votes |
@Nullable public static ObjectId rebase(Repository repository, ObjectId source, ObjectId target, PersonIdent committer) { try ( RevWalk revWalk = new RevWalk(repository); ObjectInserter inserter = repository.newObjectInserter();) { RevCommit sourceCommit = revWalk.parseCommit(source); RevCommit targetCommit = revWalk.parseCommit(target); revWalk.setRevFilter(RevFilter.NO_MERGES); List<RevCommit> commits = RevWalkUtils.find(revWalk, sourceCommit, targetCommit); Collections.reverse(commits); RevCommit headCommit = targetCommit; for (RevCommit commit: commits) { ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE.newMerger(repository, true); merger.setBase(commit.getParent(0)); if (merger.merge(headCommit, commit)) { if (!headCommit.getTree().getId().equals(merger.getResultTreeId())) { if (!commit.getTree().getId().equals(merger.getResultTreeId()) || !commit.getParent(0).equals(headCommit)) { CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setAuthor(commit.getAuthorIdent()); commitBuilder.setCommitter(committer); commitBuilder.setParentId(headCommit); commitBuilder.setMessage(commit.getFullMessage()); commitBuilder.setTreeId(merger.getResultTreeId()); headCommit = revWalk.parseCommit(inserter.insert(commitBuilder)); } else { headCommit = commit; } } } else { return null; } } inserter.flush(); return headCommit.copy(); } catch (IOException e) { throw new RuntimeException(e); } }
Example #3
Source File: GitUtils.java From onedev with MIT License | 5 votes |
@Nullable public static ObjectId merge(Repository repository, ObjectId targetCommitId, ObjectId sourceCommitId, boolean squash, PersonIdent committer, PersonIdent author, String commitMessage, boolean useOursOnConflict) { boolean prevUseOursOnConflict = UseOursOnConflict.get(); UseOursOnConflict.set(useOursOnConflict); try ( RevWalk revWalk = new RevWalk(repository); ObjectInserter inserter = repository.newObjectInserter();) { RevCommit sourceCommit = revWalk.parseCommit(sourceCommitId); RevCommit targetCommit = revWalk.parseCommit(targetCommitId); Merger merger = MergeStrategy.RECURSIVE.newMerger(repository, true); if (merger.merge(targetCommit, sourceCommit)) { CommitBuilder mergedCommit = new CommitBuilder(); mergedCommit.setAuthor(author); mergedCommit.setCommitter(committer); if (squash) mergedCommit.setParentId(targetCommit); else mergedCommit.setParentIds(targetCommit, sourceCommit); mergedCommit.setMessage(commitMessage); mergedCommit.setTreeId(merger.getResultTreeId()); ObjectId mergedCommitId = inserter.insert(mergedCommit); inserter.flush(); return mergedCommitId; } else { return null; } } catch (IOException e) { throw new RuntimeException(e); } finally { UseOursOnConflict.set(prevUseOursOnConflict); } }
Example #4
Source File: AppraiseGitReviewClient.java From git-appraise-eclipse with Eclipse Public License 1.0 | 5 votes |
/** * Creates a merged notes commit. */ private RevCommit createNotesCommit(NoteMap map, ObjectInserter inserter, RevWalk revWalk, String message, RevCommit... parents) throws IOException { CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setTreeId(map.writeTree(inserter)); commitBuilder.setAuthor(author); commitBuilder.setCommitter(author); if (parents.length > 0) { commitBuilder.setParentIds(parents); } commitBuilder.setMessage(message); ObjectId commitId = inserter.insert(commitBuilder); inserter.flush(); return revWalk.parseCommit(commitId); }
Example #5
Source File: GitNoteWriter.java From git-appraise-eclipse with Eclipse Public License 1.0 | 5 votes |
private RevCommit createCommit( NoteMap map, PersonIdent author, String message, RevCommit... parents) throws IOException { CommitBuilder b = new CommitBuilder(); b.setTreeId(map.writeTree(inserter)); b.setAuthor(author); b.setCommitter(author); if (parents.length > 0) { b.setParentIds(parents); } b.setMessage(message); ObjectId commitId = inserter.insert(b); inserter.flush(); return revWalk.parseCommit(commitId); }
Example #6
Source File: RecursiveMerger.java From onedev with MIT License | 3 votes |
/** * Create a new commit by explicitly specifying the content tree and the * parents. The commit message is not set and author/committer are set to * the current user. * * @param tree * the tree this commit should capture * @param parents * the list of parent commits * @return a new commit visible only within this merger's RevWalk. * @throws IOException */ private RevCommit createCommitForTree(ObjectId tree, List<RevCommit> parents) throws IOException { CommitBuilder c = new CommitBuilder(); c.setTreeId(tree); c.setParentIds(parents); c.setAuthor(mockAuthor(parents)); c.setCommitter(c.getAuthor()); return RevCommit.parse(walk, c.build()); }