Search in sources :

Example 61 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class AbstractSubmit method getLatestRemoteDiff.

private String getLatestRemoteDiff() throws Exception {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        ObjectId oldTreeId = repo.resolve("refs/heads/master~1^{tree}");
        ObjectId newTreeId = repo.resolve("refs/heads/master^{tree}");
        return getLatestDiff(repo, oldTreeId, newTreeId);
    }
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 62 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class AbstractSubmit method submitWithCommitAndItsMergeCommitTogether.

@Test
public void submitWithCommitAndItsMergeCommitTogether() throws Exception {
    assume().that(isSubmitWholeTopicEnabled()).isTrue();
    RevCommit initialHead = getRemoteHead();
    // Create a stable branch and bootstrap it.
    gApi.projects().name(project.get()).branch("stable").create(new BranchInput());
    PushOneCommit push = pushFactory.create(db, user.getIdent(), testRepo, "initial commit", "a.txt", "a");
    PushOneCommit.Result change = push.to("refs/heads/stable");
    RevCommit stable = getRemoteHead(project, "stable");
    RevCommit master = getRemoteHead(project, "master");
    assertThat(master).isEqualTo(initialHead);
    assertThat(stable).isEqualTo(change.getCommit());
    testRepo.git().fetch().call();
    testRepo.git().branchCreate().setName("stable").setStartPoint(stable).call();
    testRepo.git().branchCreate().setName("master").setStartPoint(master).call();
    // Create a fix in stable branch.
    testRepo.reset(stable);
    RevCommit fix = testRepo.commit().parent(stable).message("small fix").add("b.txt", "b").insertChangeId().create();
    testRepo.branch("refs/heads/stable").update(fix);
    testRepo.git().push().setRefSpecs(new RefSpec("refs/heads/stable:refs/for/stable/" + name("topic"))).call();
    // Merge the fix into master.
    testRepo.reset(master);
    RevCommit merge = testRepo.commit().parent(master).parent(fix).message("Merge stable into master").insertChangeId().create();
    testRepo.branch("refs/heads/master").update(merge);
    testRepo.git().push().setRefSpecs(new RefSpec("refs/heads/master:refs/for/master/" + name("topic"))).call();
    // Submit together.
    String fixId = GitUtil.getChangeId(testRepo, fix).get();
    String mergeId = GitUtil.getChangeId(testRepo, merge).get();
    approve(fixId);
    approve(mergeId);
    submit(mergeId);
    assertMerged(fixId);
    assertMerged(mergeId);
    testRepo.git().fetch().call();
    RevWalk rw = testRepo.getRevWalk();
    master = rw.parseCommit(getRemoteHead(project, "master"));
    assertThat(rw.isMergedInto(merge, master)).isTrue();
    assertThat(rw.isMergedInto(fix, master)).isTrue();
}
Also used : RefSpec(org.eclipse.jgit.transport.RefSpec) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 63 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class AbstractSubmitByMerge method repairChangeStateAfterFailure.

@Test
public void repairChangeStateAfterFailure() throws Exception {
    // In NoteDb-only mode, repo and meta updates are atomic (at least in InMemoryRepository).
    assume().that(notesMigration.disableChangeReviewDb()).isFalse();
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result change = createChange("Change 1", "a.txt", "content");
    submit(change.getChangeId());
    RevCommit afterChange1Head = getRemoteHead();
    testRepo.reset(initialHead);
    PushOneCommit.Result change2 = createChange("Change 2", "b.txt", "other content");
    Change.Id id2 = change2.getChange().getId();
    SubmitInput failAfterRefUpdates = new TestSubmitInput(new SubmitInput(), true);
    submit(change2.getChangeId(), failAfterRefUpdates, ResourceConflictException.class, "Failing after ref updates");
    // Bad: ref advanced but change wasn't updated.
    PatchSet.Id psId1 = new PatchSet.Id(id2, 1);
    ChangeInfo info = gApi.changes().id(id2.get()).get();
    assertThat(info.status).isEqualTo(ChangeStatus.NEW);
    assertThat(info.revisions.get(info.currentRevision)._number).isEqualTo(1);
    RevCommit tip;
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        ObjectId rev1 = repo.exactRef(psId1.toRefName()).getObjectId();
        assertThat(rev1).isNotNull();
        tip = rw.parseCommit(repo.exactRef("refs/heads/master").getObjectId());
        assertThat(tip.getParentCount()).isEqualTo(2);
        assertThat(tip.getParent(0)).isEqualTo(afterChange1Head);
        assertThat(tip.getParent(1)).isEqualTo(change2.getCommit());
    }
    submit(change2.getChangeId(), new SubmitInput(), null, null);
    // Change status and patch set entities were updated, and branch tip stayed
    // the same.
    info = gApi.changes().id(id2.get()).get();
    assertThat(info.status).isEqualTo(ChangeStatus.MERGED);
    assertThat(info.revisions.get(info.currentRevision)._number).isEqualTo(1);
    assertThat(Iterables.getLast(info.messages).message).isEqualTo("Change has been successfully merged by Administrator");
    try (Repository repo = repoManager.openRepository(project)) {
        assertThat(repo.exactRef("refs/heads/master").getObjectId()).isEqualTo(tip);
    }
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) TestSubmitInput(com.google.gerrit.server.change.Submit.TestSubmitInput) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) TestSubmitInput(com.google.gerrit.server.change.Submit.TestSubmitInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 64 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class AbstractSubmitByRebase method repairChangeStateAfterFailure.

@Test
public void repairChangeStateAfterFailure() throws Exception {
    // In NoteDb-only mode, repo and meta updates are atomic (at least in InMemoryRepository).
    assume().that(notesMigration.disableChangeReviewDb()).isFalse();
    RevCommit initialHead = getRemoteHead();
    PushOneCommit.Result change = createChange("Change 1", "a.txt", "content");
    submit(change.getChangeId());
    RevCommit headAfterFirstSubmit = getRemoteHead();
    testRepo.reset(initialHead);
    PushOneCommit.Result change2 = createChange("Change 2", "b.txt", "other content");
    Change.Id id2 = change2.getChange().getId();
    SubmitInput failAfterRefUpdates = new TestSubmitInput(new SubmitInput(), true);
    submit(change2.getChangeId(), failAfterRefUpdates, ResourceConflictException.class, "Failing after ref updates");
    RevCommit headAfterFailedSubmit = getRemoteHead();
    // Bad: ref advanced but change wasn't updated.
    PatchSet.Id psId1 = new PatchSet.Id(id2, 1);
    PatchSet.Id psId2 = new PatchSet.Id(id2, 2);
    ChangeInfo info = gApi.changes().id(id2.get()).get();
    assertThat(info.status).isEqualTo(ChangeStatus.NEW);
    assertThat(info.revisions.get(info.currentRevision)._number).isEqualTo(1);
    assertThat(getPatchSet(psId2)).isNull();
    ObjectId rev2;
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        ObjectId rev1 = repo.exactRef(psId1.toRefName()).getObjectId();
        assertThat(rev1).isNotNull();
        rev2 = repo.exactRef(psId2.toRefName()).getObjectId();
        assertThat(rev2).isNotNull();
        assertThat(rev2).isNotEqualTo(rev1);
        assertThat(rw.parseCommit(rev2).getParent(0)).isEqualTo(headAfterFirstSubmit);
        assertThat(repo.exactRef("refs/heads/master").getObjectId()).isEqualTo(rev2);
    }
    submit(change2.getChangeId());
    RevCommit headAfterSecondSubmit = getRemoteHead();
    assertThat(headAfterSecondSubmit).isEqualTo(headAfterFailedSubmit);
    // Change status and patch set entities were updated, and branch tip stayed
    // the same.
    info = gApi.changes().id(id2.get()).get();
    assertThat(info.status).isEqualTo(ChangeStatus.MERGED);
    assertThat(info.revisions.get(info.currentRevision)._number).isEqualTo(2);
    PatchSet ps2 = getPatchSet(psId2);
    assertThat(ps2).isNotNull();
    assertThat(ps2.getRevision().get()).isEqualTo(rev2.name());
    assertThat(Iterables.getLast(info.messages).message).isEqualTo("Change has been successfully rebased and submitted as " + rev2.name() + " by Administrator");
    try (Repository repo = repoManager.openRepository(project)) {
        assertThat(repo.exactRef("refs/heads/master").getObjectId()).isEqualTo(rev2);
    }
    assertRefUpdatedEvents(initialHead, headAfterFirstSubmit);
    assertChangeMergedEvents(change.getChangeId(), headAfterFirstSubmit.name(), change2.getChangeId(), headAfterSecondSubmit.name());
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) TestSubmitInput(com.google.gerrit.server.change.Submit.TestSubmitInput) Repository(org.eclipse.jgit.lib.Repository) GitUtil.getChangeId(com.google.gerrit.acceptance.GitUtil.getChangeId) ObjectId(org.eclipse.jgit.lib.ObjectId) TestSubmitInput(com.google.gerrit.server.change.Submit.TestSubmitInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 65 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class PublicKeyStoreTest method saveAppendsToExistingList.

@Test
public void saveAppendsToExistingList() throws Exception {
    TestKey key1 = validKeyWithoutExpiration();
    TestKey key2 = validKeyWithExpiration();
    tr.branch(REFS_GPG_KEYS).commit().add(keyObjectId(key1.getKeyId()).name(), key2.getPublicKeyArmored()).create();
    store.add(key1.getPublicKeyRing());
    assertEquals(RefUpdate.Result.FAST_FORWARD, store.save(newCommitBuilder()));
    assertKeys(key1.getKeyId(), key1, key2);
    try (ObjectReader reader = tr.getRepository().newObjectReader();
        RevWalk rw = new RevWalk(reader)) {
        NoteMap notes = NoteMap.read(reader, tr.getRevWalk().parseCommit(tr.getRepository().exactRef(REFS_GPG_KEYS).getObjectId()));
        String contents = new String(reader.open(notes.get(keyObjectId(key1.getKeyId()))).getBytes(), UTF_8);
        String header = "-----BEGIN PGP PUBLIC KEY BLOCK-----";
        int i1 = contents.indexOf(header);
        assertTrue(i1 >= 0);
        int i2 = contents.indexOf(header, i1 + header.length());
        assertTrue(i2 >= 0);
    }
}
Also used : TestKey(com.google.gerrit.gpg.testutil.TestKey) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Test(org.junit.Test)

Aggregations

RevWalk (org.eclipse.jgit.revwalk.RevWalk)217 RevCommit (org.eclipse.jgit.revwalk.RevCommit)114 Repository (org.eclipse.jgit.lib.Repository)100 ObjectId (org.eclipse.jgit.lib.ObjectId)92 IOException (java.io.IOException)67 Ref (org.eclipse.jgit.lib.Ref)35 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)33 Test (org.junit.Test)33 Change (com.google.gerrit.reviewdb.client.Change)31 ObjectReader (org.eclipse.jgit.lib.ObjectReader)30 ArrayList (java.util.ArrayList)25 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 OrmException (com.google.gwtorm.server.OrmException)24 RevTree (org.eclipse.jgit.revwalk.RevTree)24 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)21 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)16 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)15 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)15 PersonIdent (org.eclipse.jgit.lib.PersonIdent)15 Project (com.google.gerrit.reviewdb.client.Project)14