Search in sources :

Example 6 with RevCommit

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()));
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 7 with RevCommit

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);
}
Also used : GroupCollector(com.google.gerrit.server.git.GroupCollector) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ObjectId(org.eclipse.jgit.lib.ObjectId) RefDatabase(org.eclipse.jgit.lib.RefDatabase) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with RevCommit

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);
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) DiffInfo(com.google.gerrit.extensions.common.DiffInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with RevCommit

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;
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) ArrayList(java.util.ArrayList) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with RevCommit

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);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) LockFailureException(com.google.gerrit.server.git.LockFailureException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

RevCommit (org.eclipse.jgit.revwalk.RevCommit)1300 Test (org.junit.Test)650 RevWalk (org.eclipse.jgit.revwalk.RevWalk)332 ObjectId (org.eclipse.jgit.lib.ObjectId)292 Repository (org.eclipse.jgit.lib.Repository)272 IOException (java.io.IOException)221 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)190 Ref (org.eclipse.jgit.lib.Ref)174 ArrayList (java.util.ArrayList)134 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)133 File (java.io.File)133 Git (org.eclipse.jgit.api.Git)133 PersonIdent (org.eclipse.jgit.lib.PersonIdent)105 Change (com.google.gerrit.entities.Change)87 TestRepository (org.eclipse.jgit.junit.TestRepository)72 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)69 ObjectReader (org.eclipse.jgit.lib.ObjectReader)64 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)61 List (java.util.List)61 HashMap (java.util.HashMap)57