use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.
the class Schema_146 method rewriteUserBranch.
private void rewriteUserBranch(Repository repo, RevWalk rw, ObjectInserter oi, ObjectId emptyTree, Ref ref, Account account) throws IOException {
ObjectId current = createInitialEmptyCommit(oi, emptyTree, account.getRegisteredOn());
rw.reset();
rw.sort(RevSort.TOPO);
rw.sort(RevSort.REVERSE, true);
rw.markStart(rw.parseCommit(ref.getObjectId()));
RevCommit c;
while ((c = rw.next()) != null) {
if (isInitialEmptyCommit(emptyTree, c)) {
return;
}
CommitBuilder cb = new CommitBuilder();
cb.setParentId(current);
cb.setTreeId(c.getTree());
cb.setAuthor(c.getAuthorIdent());
cb.setCommitter(c.getCommitterIdent());
cb.setMessage(c.getFullMessage());
cb.setEncoding(c.getEncoding());
current = oi.insert(cb);
}
oi.flush();
RefUpdate ru = repo.updateRef(ref.getName());
ru.setExpectedOldObjectId(ref.getObjectId());
ru.setNewObjectId(current);
ru.setForceUpdate(true);
ru.setRefLogIdent(serverIdent);
ru.setRefLogMessage(getClass().getSimpleName(), true);
Result result = ru.update();
if (result != Result.FORCED) {
throw new IOException(String.format("Failed to update ref %s: %s", ref.getName(), result.name()));
}
}
use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.
the class Schema_108 method updateProjectGroups.
private void updateProjectGroups(ReviewDb db, Repository repo, RevWalk rw, Set<Change.Id> changes, UpdateUI ui) throws OrmException, IOException {
// Match sorting in ReceiveCommits.
rw.reset();
rw.sort(RevSort.TOPO);
rw.sort(RevSort.REVERSE, true);
RefDatabase refdb = repo.getRefDatabase();
for (Ref ref : refdb.getRefs(Constants.R_HEADS).values()) {
RevCommit c = maybeParseCommit(rw, ref.getObjectId(), ui);
if (c != null) {
rw.markUninteresting(c);
}
}
ListMultimap<ObjectId, Ref> changeRefsBySha = MultimapBuilder.hashKeys().arrayListValues().build();
ListMultimap<ObjectId, PatchSet.Id> patchSetsBySha = MultimapBuilder.hashKeys().arrayListValues().build();
for (Ref ref : refdb.getRefs(RefNames.REFS_CHANGES).values()) {
ObjectId id = ref.getObjectId();
if (ref.getObjectId() == null) {
continue;
}
id = id.copy();
changeRefsBySha.put(id, ref);
PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
if (psId != null && changes.contains(psId.getParentKey())) {
patchSetsBySha.put(id, psId);
RevCommit c = maybeParseCommit(rw, id, ui);
if (c != null) {
rw.markStart(c);
}
}
}
GroupCollector collector = GroupCollector.createForSchemaUpgradeOnly(changeRefsBySha, db);
RevCommit c;
while ((c = rw.next()) != null) {
collector.visit(c);
}
updateGroups(db, collector, patchSetsBySha);
}
use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.
the class RevisionIT method assertDiffForNewFile.
private void assertDiffForNewFile(PushOneCommit.Result pushResult, String path, String expectedContentSideB) throws Exception {
DiffInfo diff = gApi.changes().id(pushResult.getChangeId()).revision(pushResult.getCommit().name()).file(path).diff();
List<String> headers = new ArrayList<>();
if (path.equals(COMMIT_MSG)) {
RevCommit c = pushResult.getCommit();
RevCommit parentCommit = c.getParents()[0];
String parentCommitId = testRepo.getRevWalk().getObjectReader().abbreviate(parentCommit.getId(), 8).name();
headers.add("Parent: " + parentCommitId + " (" + parentCommit.getShortMessage() + ")");
SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);
PersonIdent author = c.getAuthorIdent();
dtfmt.setTimeZone(author.getTimeZone());
headers.add("Author: " + author.getName() + " <" + author.getEmailAddress() + ">");
headers.add("AuthorDate: " + dtfmt.format(Long.valueOf(author.getWhen().getTime())));
PersonIdent committer = c.getCommitterIdent();
dtfmt.setTimeZone(committer.getTimeZone());
headers.add("Commit: " + committer.getName() + " <" + committer.getEmailAddress() + ">");
headers.add("CommitDate: " + dtfmt.format(Long.valueOf(committer.getWhen().getTime())));
headers.add("");
}
if (!headers.isEmpty()) {
String header = Joiner.on("\n").join(headers);
expectedContentSideB = header + "\n" + expectedContentSideB;
}
assertDiffForNewFile(diff, pushResult.getCommit(), path, expectedContentSideB);
}
use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.
the class AbstractPushForReview method createChanges.
private List<RevCommit> createChanges(int n, String refsFor, List<String> footerLines) throws Exception {
List<RevCommit> commits = new ArrayList<>(n);
for (int i = 1; i <= n; i++) {
String msg = "Change " + i;
if (!footerLines.isEmpty()) {
StringBuilder sb = new StringBuilder(msg).append("\n\n");
for (String line : footerLines) {
sb.append(line).append('\n');
}
msg = sb.toString();
}
TestRepository<?>.CommitBuilder<?> cb = testRepo.branch("HEAD").commit().message(msg).insertChangeId();
if (!commits.isEmpty()) {
cb.parent(commits.get(commits.size() - 1));
}
RevCommit c = cb.create();
testRepo.getRevWalk().parseBody(c);
commits.add(c);
}
assertPushOk(pushHead(testRepo, refsFor, false), refsFor);
return commits;
}
use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.
the class ExternalIdsUpdate method commit.
/** Commits updates to the external IDs. */
public static ObjectId commit(Repository repo, RevWalk rw, ObjectInserter ins, ObjectId rev, NoteMap noteMap, String commitMessage, PersonIdent committerIdent, PersonIdent authorIdent) throws IOException {
CommitBuilder cb = new CommitBuilder();
cb.setMessage(commitMessage);
cb.setTreeId(noteMap.writeTree(ins));
cb.setAuthor(authorIdent);
cb.setCommitter(committerIdent);
if (!rev.equals(ObjectId.zeroId())) {
cb.setParentId(rev);
} else {
// Ref is currently nonexistent, commit has no parents.
cb.setParentIds();
}
if (cb.getTreeId() == null) {
if (rev.equals(ObjectId.zeroId())) {
// No parent, assume empty tree.
cb.setTreeId(emptyTree(ins));
} else {
RevCommit p = rw.parseCommit(rev);
// Copy tree from parent.
cb.setTreeId(p.getTree());
}
}
ObjectId commitId = ins.insert(cb);
ins.flush();
RefUpdate u = repo.updateRef(RefNames.REFS_EXTERNAL_IDS);
u.setRefLogIdent(committerIdent);
u.setRefLogMessage("Update external IDs", false);
u.setExpectedOldObjectId(rev);
u.setNewObjectId(commitId);
RefUpdate.Result res = u.update();
switch(res) {
case NEW:
case FAST_FORWARD:
case NO_CHANGE:
case RENAMED:
case FORCED:
break;
case LOCK_FAILURE:
throw new LockFailureException("Updating external IDs failed with " + res);
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
default:
throw new IOException("Updating external IDs failed with " + res);
}
return rw.parseCommit(commitId);
}
Aggregations