/** * * @param lastCommitId * @param treeId * @param repo * @param lastTreeId * @return * @throws IOException */ public static ObjectId processCommit(ObjectId lastCommitId, ObjectId treeId, Repository repo, ObjectId lastTreeId) throws IOException { CommitBuilder commitBuilder = new CommitBuilder(); ObjectId commitId = null; if (lastCommitId == null) { commitId = Commands.commit(commitBuilder, treeId, repo); System.out.println("Initial Commit: " + commitId); } else { if (lastTreeId.equals(treeId)) { // Do nothing, because there is no changes in the tree System.out.println("Did nothing, because there is no changes in the commited tree!\n"); } else { commitBuilder.setParentId(lastCommitId); commitId = Commands.commit(commitBuilder, treeId, repo); System.out.println("Current Commit: " + commitId); } } return commitId; }
@Override public void execute() throws SVNException { DAVResource resource = getRequestedDAVResource(false, false); FSRepository repos = (FSRepository) resource.getRepository(); myGitFS = (GitFS) repos.getFSFS(); readInput(true); if (commitBuilders.containsKey(resource.getActivityID())) { throw new DAVException("<DAV:resource-must-be-null/>", HttpServletResponse.SC_CONFLICT, SVNLogType.NETWORK); } // FIXME: I don't know what it is if (!resource.canBeActivity()) { throw new DAVException("<DAV:activity-location-ok/>", HttpServletResponse.SC_FORBIDDEN, SVNLogType.NETWORK); } CommitBuilder builder = new CommitBuilder(); commitBuilders.put(resource.getActivityID(), builder); setResponseHeader(CACHE_CONTROL_HEADER, CACHE_CONTROL_VALUE); handleDAVCreated(resource.getResourceURI().getURI(), "Activity", false); }
private ObjectId createCommit( Repository repository, RevCommit basePatchSetCommit, ObjectId tree, String commitMessage, Timestamp timestamp) throws IOException { try (ObjectInserter objectInserter = repository.newObjectInserter()) { CommitBuilder builder = new CommitBuilder(); builder.setTreeId(tree); builder.setParentIds(basePatchSetCommit.getParents()); builder.setAuthor(basePatchSetCommit.getAuthorIdent()); builder.setCommitter(getCommitterIdent(timestamp)); builder.setMessage(commitMessage); ObjectId newCommitId = objectInserter.insert(builder); objectInserter.flush(); return newCommitId; } }
@Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { checkLoaded(); if (revision != null) { commit.setMessage("Update account\n"); } else if (account != null) { commit.setMessage("Create account\n"); commit.setAuthor(new PersonIdent(commit.getAuthor(), registeredOn)); commit.setCommitter(new PersonIdent(commit.getCommitter(), registeredOn)); } Config cfg = readConfig(ACCOUNT_CONFIG); writeToConfig(account, cfg); saveConfig(ACCOUNT_CONFIG, cfg); return true; }
private static RevCommit newCommit( ObjectInserter oi, RevWalk rw, PersonIdent authorIdent, RevCommit mergeTip, String commitMessage) throws IOException { CommitBuilder commit = new CommitBuilder(); if (mergeTip == null) { commit.setTreeId(emptyTreeId(oi)); } else { commit.setTreeId(mergeTip.getTree().getId()); commit.setParentId(mergeTip); } commit.setAuthor(authorIdent); commit.setCommitter(authorIdent); commit.setMessage(commitMessage); return rw.parseCommit(insert(oi, commit)); }
private ObjectId createCommit( ObjectInserter objectInserter, RevCommit basePatchSetCommit, String commitMessage, Timestamp timestamp) throws IOException { CommitBuilder builder = new CommitBuilder(); builder.setTreeId(basePatchSetCommit.getTree()); builder.setParentIds(basePatchSetCommit.getParents()); builder.setAuthor(basePatchSetCommit.getAuthorIdent()); builder.setCommitter( currentUserProvider.get().asIdentifiedUser().newCommitterIdent(timestamp, tz)); builder.setMessage(commitMessage); ObjectId newCommitId = objectInserter.insert(builder); objectInserter.flush(); return newCommitId; }
private void createGroupBranch(Project.NameKey project, String ref) throws IOException { try (Repository r = repoManager.openRepository(project); ObjectInserter oi = r.newObjectInserter(); RevWalk rw = new RevWalk(r)) { ObjectId emptyTree = oi.insert(Constants.OBJ_TREE, new byte[] {}); PersonIdent ident = new PersonIdent(serverIdent.get(), TimeUtil.nowTs()); CommitBuilder cb = new CommitBuilder(); cb.setTreeId(emptyTree); cb.setCommitter(ident); cb.setAuthor(ident); cb.setMessage("Create group"); ObjectId emptyCommit = oi.insert(cb); oi.flush(); RefUpdate updateRef = r.updateRef(ref); updateRef.setExpectedOldObjectId(ObjectId.zeroId()); updateRef.setNewObjectId(emptyCommit); assertThat(updateRef.update(rw)).isEqualTo(RefUpdate.Result.NEW); } }
private RevCommit writeCommit(String body, PersonIdent author, boolean initWorkInProgress) throws Exception { Change change = newChange(initWorkInProgress); ChangeNotes notes = newNotes(change).load(); try (ObjectInserter ins = testRepo.getRepository().newObjectInserter()) { CommitBuilder cb = new CommitBuilder(); cb.setParentId(notes.getRevision()); cb.setAuthor(author); cb.setCommitter(new PersonIdent(serverIdent, author.getWhen())); cb.setTreeId(testRepo.tree()); cb.setMessage(body); ObjectId id = ins.insert(cb); ins.flush(); RevCommit commit = walk.parseCommit(id); walk.parseBody(commit); return commit; } }
private void setHEAD() throws Exception { try (ObjectInserter oi = repository.newObjectInserter()) { final CommitBuilder commit = new CommitBuilder(); commit.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {})); commit.setAuthor(author); commit.setCommitter(committer); commit.setMessage("test\n"); ObjectId commitId = oi.insert(commit); final RefUpdate ref = repository.updateRef(Constants.HEAD); ref.setNewObjectId(commitId); Result result = ref.forceUpdate(); assertWithMessage(Constants.HEAD + " did not change: " + ref.getResult()) .that(result) .isAnyOf(Result.FAST_FORWARD, Result.FORCED, Result.NEW, Result.NO_CHANGE); } }
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; } }
@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())) { CommitBuilder newCommit = new CommitBuilder(); newCommit.setAuthor(commit.getAuthorIdent()); newCommit.setCommitter(committer); newCommit.setParentId(headCommit); newCommit.setMessage(commit.getFullMessage()); newCommit.setTreeId(merger.getResultTreeId()); headCommit = revWalk.parseCommit(inserter.insert(newCommit)); } } else { return null; } } inserter.flush(); return headCommit.copy(); } catch (IOException e) { throw new RuntimeException(e); } }
@Nullable public static ObjectId merge(Repository repository, ObjectId source, ObjectId target, boolean squash, PersonIdent committer, String commitMessage) { try ( RevWalk revWalk = new RevWalk(repository); ObjectInserter inserter = repository.newObjectInserter();) { RevCommit sourceCommit = revWalk.parseCommit(source); RevCommit targetCommit = revWalk.parseCommit(target); Merger merger = MergeStrategy.RECURSIVE.newMerger(repository, true); if (merger.merge(targetCommit, sourceCommit)) { CommitBuilder mergedCommit = new CommitBuilder(); mergedCommit.setAuthor(sourceCommit.getAuthorIdent()); 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); } }
public static void updateProjectRef(ObjectId treeId, ObjectInserter objectInserter, Repository repository, CurrentUser currentUser) throws IOException, NoFilepatternException, GitAPIException { // Create a branch Ref gerritCiRef = repository.getRef("refs/meta/gerrit-ci"); CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setTreeId(treeId); logger.info("treeId: " + treeId); if (gerritCiRef != null) { ObjectId prevCommit = gerritCiRef.getObjectId(); logger.info("prevCommit: " + prevCommit); commitBuilder.setParentId(prevCommit); } // build commit logger.info("Adding git tree : " + treeId); commitBuilder.setMessage("Modify project build rules."); final IdentifiedUser iUser = (IdentifiedUser) currentUser; PersonIdent user = new PersonIdent(currentUser.getUserName(), iUser.getEmailAddresses().iterator().next()); commitBuilder.setAuthor(user); commitBuilder.setCommitter(user); ObjectId commitId = objectInserter.insert(commitBuilder); objectInserter.flush(); logger.info(" Making new commit: " + commitId); RefUpdate newRef = repository.updateRef("refs/meta/gerrit-ci"); newRef.setNewObjectId(commitId); newRef.update(); repository.close(); }
/** * 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); }
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); }
@Override protected boolean onSave(CommitBuilder commit) throws IOException { if (Strings.isNullOrEmpty(commit.getMessage())) { commit.setMessage("Updated SSH keys\n"); } saveUTF8(AuthorizedKeys.FILE_NAME, AuthorizedKeys.serialize(keys)); return true; }
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException { File path = getPath(); if (path == null) { throw new IOException(project + " does not exist."); } try (Repository repo = new FileRepository(path); ObjectInserter i = repo.newObjectInserter(); ObjectReader r = repo.newObjectReader(); RevWalk rw = new RevWalk(r)) { inserter = i; reader = r; RevTree srcTree = revision != null ? rw.parseTree(revision) : null; newTree = readTree(srcTree); CommitBuilder commit = new CommitBuilder(); commit.setAuthor(ident); commit.setCommitter(ident); commit.setMessage(msg); onSave(commit); ObjectId res = newTree.writeTree(inserter); if (res.equals(srcTree)) { return; } commit.setTreeId(res); if (revision != null) { commit.addParentId(revision); } ObjectId newRevision = inserter.insert(commit); updateRef(repo, ident, newRevision, "commit: " + msg); revision = rw.parseCommit(newRevision); } finally { inserter = null; reader = null; } }
private static RevCommit writeSquashedCommit( RevWalk rw, ObjectInserter inserter, RevCommit parent, RevCommit edit) throws IOException { CommitBuilder mergeCommit = new CommitBuilder(); for (int i = 0; i < parent.getParentCount(); i++) { mergeCommit.addParentId(parent.getParent(i)); } mergeCommit.setAuthor(parent.getAuthorIdent()); mergeCommit.setMessage(edit.getFullMessage()); mergeCommit.setCommitter(edit.getCommitterIdent()); mergeCommit.setTreeId(edit.getTree()); return rw.parseCommit(commit(inserter, mergeCommit)); }
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 != null ? author : gerritIdent); b.setCommitter(gerritIdent); if (parents.length > 0) { b.setParentIds(parents); } b.setMessage(message); ObjectId commitId = inserter.insert(b); inserter.flush(); return revWalk.parseCommit(commitId); }
@Inject public MetaDataUpdate( GitReferenceUpdated gitRefUpdated, @Assisted Project.NameKey projectName, @Assisted Repository repository, @Assisted @Nullable BatchRefUpdate batch) { this.gitRefUpdated = gitRefUpdated; this.projectName = projectName; this.repository = repository; this.batch = batch; this.commit = new CommitBuilder(); }
@Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { if (commit.getMessage() == null || "".equals(commit.getMessage())) { commit.setMessage("Updated configuration\n"); } saveConfig(fileName, cfg); return true; }
public CodeReviewCommit createCherryPickFromCommit( ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, PersonIdent cherryPickCommitterIdent, String commitMsg, CodeReviewRevWalk rw, int parentIndex, boolean ignoreIdenticalTree) throws MissingObjectException, IncorrectObjectTypeException, IOException, MergeIdenticalTreeException, MergeConflictException { final ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig); m.setBase(originalCommit.getParent(parentIndex)); if (m.merge(mergeTip, originalCommit)) { ObjectId tree = m.getResultTreeId(); if (tree.equals(mergeTip.getTree()) && !ignoreIdenticalTree) { throw new MergeIdenticalTreeException("identical tree"); } CommitBuilder mergeCommit = new CommitBuilder(); mergeCommit.setTreeId(tree); mergeCommit.setParentId(mergeTip); mergeCommit.setAuthor(originalCommit.getAuthorIdent()); mergeCommit.setCommitter(cherryPickCommitterIdent); mergeCommit.setMessage(commitMsg); matchAuthorToCommitterDate(project, mergeCommit); return rw.parseCommit(inserter.insert(mergeCommit)); } throw new MergeConflictException("merge conflict"); }
public static RevCommit createMergeCommit( ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, String mergeStrategy, PersonIdent committerIndent, String commitMsg, RevWalk rw) throws IOException, MergeIdenticalTreeException, MergeConflictException { if (!MergeStrategy.THEIRS.getName().equals(mergeStrategy) && rw.isMergedInto(originalCommit, mergeTip)) { throw new ChangeAlreadyMergedException( "'" + originalCommit.getName() + "' has already been merged"); } Merger m = newMerger(inserter, repoConfig, mergeStrategy); if (m.merge(false, mergeTip, originalCommit)) { ObjectId tree = m.getResultTreeId(); CommitBuilder mergeCommit = new CommitBuilder(); mergeCommit.setTreeId(tree); mergeCommit.setParentIds(mergeTip, originalCommit); mergeCommit.setAuthor(committerIndent); mergeCommit.setCommitter(committerIndent); mergeCommit.setMessage(commitMsg); return rw.parseCommit(inserter.insert(mergeCommit)); } List<String> conflicts = ImmutableList.of(); if (m instanceof ResolveMerger) { conflicts = ((ResolveMerger) m).getUnmergedPaths(); } throw new MergeConflictException(createConflictMessage(conflicts)); }
private static void matchAuthorToCommitterDate(ProjectState project, CommitBuilder commit) { if (project.isMatchAuthorToCommitterDate()) { commit.setAuthor( new PersonIdent( commit.getAuthor(), commit.getCommitter().getWhen(), commit.getCommitter().getTimeZone())); } }
/** * Apply this update to the given inserter. * * @param rw walk for reading back any objects needed for the update. * @param ins inserter to write to; callers should not flush. * @param curr the current tip of the branch prior to this update. * @return commit ID produced by inserting this update's commit, or null if this update is a no-op * and should be skipped. The zero ID is a valid return value, and indicates the ref should be * deleted. * @throws OrmException if a Gerrit-level error occurred. * @throws IOException if a lower-level error occurred. */ final ObjectId apply(RevWalk rw, ObjectInserter ins, ObjectId curr) throws OrmException, IOException { if (isEmpty()) { return null; } // Allow this method to proceed even if migration.failChangeWrites() = true. // This may be used by an auto-rebuilding step that the caller does not plan // to actually store. checkArgument(rw.getObjectReader().getCreatedFromInserter() == ins); checkNotReadOnly(); ObjectId z = ObjectId.zeroId(); CommitBuilder cb = applyImpl(rw, ins, curr); if (cb == null) { result = z; return z; // Impl intends to delete the ref. } else if (cb == NO_OP_UPDATE) { return null; // Impl is a no-op. } cb.setAuthor(authorIdent); cb.setCommitter(new PersonIdent(serverIdent, when)); if (!curr.equals(z)) { cb.setParentId(curr); } else { cb.setParentIds(); // Ref is currently nonexistent, commit has no parents. } if (cb.getTreeId() == null) { if (curr.equals(z)) { cb.setTreeId(emptyTree(ins)); // No parent, assume empty tree. } else { RevCommit p = rw.parseCommit(curr); cb.setTreeId(p.getTree()); // Copy tree from parent. } } result = ins.insert(cb); return result; }
@Override protected CommitBuilder applyImpl(RevWalk rw, ObjectInserter ins, ObjectId curr) throws OrmException, IOException { CommitBuilder cb = new CommitBuilder(); cb.setMessage("Update robot comments"); try { return storeCommentsInNotes(rw, ins, curr, cb); } catch (ConfigInvalidException e) { throw new OrmException(e); } }
@Override protected CommitBuilder applyImpl(RevWalk rw, ObjectInserter ins, ObjectId curr) throws OrmException, IOException { CommitBuilder cb = new CommitBuilder(); cb.setMessage("Update draft comments"); try { return storeCommentsInNotes(rw, ins, curr, cb); } catch (ConfigInvalidException e) { throw new OrmException(e); } }
@Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { if (Strings.isNullOrEmpty(commit.getMessage())) { commit.setMessage("Updated preferences\n"); } saveConfig(PREFERENCES, cfg); return true; }
/** * Rebase a commit. * * @param ctx repo context. * @param original the commit to rebase. * @param base base to rebase against. * @return the rebased commit. * @throws MergeConflictException the rebase failed due to a merge conflict. * @throws IOException the merge failed for another reason. */ private RevCommit rebaseCommit( RepoContext ctx, RevCommit original, ObjectId base, String commitMessage) throws ResourceConflictException, IOException { RevCommit parentCommit = original.getParent(0); if (base.equals(parentCommit)) { throw new ResourceConflictException("Change is already up to date."); } ThreeWayMerger merger = newMergeUtil().newThreeWayMerger(ctx.getInserter(), ctx.getRepoView().getConfig()); merger.setBase(parentCommit); merger.merge(original, base); if (merger.getResultTreeId() == null) { throw new MergeConflictException( "The change could not be rebased due to a conflict during merge."); } CommitBuilder cb = new CommitBuilder(); cb.setTreeId(merger.getResultTreeId()); cb.setParentId(base); cb.setAuthor(original.getAuthorIdent()); cb.setMessage(commitMessage); if (committerIdent != null) { cb.setCommitter(committerIdent); } else { cb.setCommitter(ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone())); } if (matchAuthorToCommitterDate) { cb.setAuthor( new PersonIdent( cb.getAuthor(), cb.getCommitter().getWhen(), cb.getCommitter().getTimeZone())); } ObjectId objectId = ctx.getInserter().insert(cb); ctx.getInserter().flush(); return ctx.getRevWalk().parseCommit(objectId); }
private ObjectId createInitialEmptyCommit( ObjectInserter oi, ObjectId emptyTree, Timestamp registrationDate) throws IOException { PersonIdent ident = new PersonIdent(serverIdent, registrationDate); CommitBuilder cb = new CommitBuilder(); cb.setTreeId(emptyTree); cb.setCommitter(ident); cb.setAuthor(ident); cb.setMessage(CREATE_ACCOUNT_MSG); return oi.insert(cb); }
@Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { if (!dirty) { return false; } commit.setMessage("Remove \"My Drafts\" menu items"); return super.onSave(commit); }
@Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { TestSubmitRuleInput in = new TestSubmitRuleInput(); in.rule = rule; try { gApi.changes().id(testChangeId.get()).current().testSubmitType(in); } catch (RestApiException e) { throw new ConfigInvalidException("Invalid submit type rule", e); } saveUTF8(FILENAME, rule); return true; }
private ObjectId newCommit(RevWalk rw, ObjectInserter ins, ObjectId parent, String msg) throws IOException { PersonIdent ident = serverIdent.get(); CommitBuilder cb = new CommitBuilder(); cb.setParentId(parent); cb.setTreeId(rw.parseCommit(parent).getTree()); cb.setMessage(msg); cb.setAuthor(ident); cb.setCommitter(ident); return ins.insert(Constants.OBJ_COMMIT, cb.build()); }
private MetaDataUpdate newMetaDataUpdate(@Nullable BatchRefUpdate bru) { MetaDataUpdate u = new MetaDataUpdate(GitReferenceUpdated.DISABLED, project, repo, bru); CommitBuilder cb = newCommitBuilder(); u.getCommitBuilder().setAuthor(cb.getAuthor()); u.getCommitBuilder().setCommitter(cb.getCommitter()); return u; }