Search in sources :

Example 36 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class CheckMergeabilityIT method checkAlreadyMergedCommit.

@Test
public void checkAlreadyMergedCommit() throws Exception {
    ObjectId c0 = testRepo.branch("HEAD").commit().insertChangeId().message("first commit").add("a.txt", "a contents ").create();
    testRepo.git().push().setRemote("origin").setRefSpecs(new RefSpec("HEAD:refs/heads/master")).call();
    testRepo.branch("HEAD").commit().insertChangeId().message("second commit").add("b.txt", "b contents ").create();
    testRepo.git().push().setRemote("origin").setRefSpecs(new RefSpec("HEAD:refs/heads/master")).call();
    assertCommitMerged("master", c0.getName(), "");
}
Also used : RefSpec(org.eclipse.jgit.transport.RefSpec) ObjectId(org.eclipse.jgit.lib.ObjectId) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 37 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class CheckMergeabilityIT method checkContentMergedCommit.

@Test
public void checkContentMergedCommit() throws Exception {
    testRepo.branch("HEAD").commit().insertChangeId().message("first commit").add("a.txt", "a contents ").create();
    testRepo.git().push().setRemote("origin").setRefSpecs(new RefSpec("HEAD:refs/heads/master")).call();
    // create a change, and cherrypick into master
    PushOneCommit.Result cId = createChange();
    RevCommit commitId = cId.getCommit();
    CherryPickInput cpi = new CherryPickInput();
    cpi.destination = "master";
    cpi.message = "cherry pick the commit";
    ChangeApi orig = gApi.changes().id(cId.getChangeId());
    ChangeApi cherry = orig.current().cherryPick(cpi);
    cherry.current().review(ReviewInput.approve());
    cherry.current().submit();
    ObjectId remoteId = getRemoteHead();
    assertThat(remoteId).isNotEqualTo(commitId);
    assertContentMerged("master", commitId.getName(), "recursive");
}
Also used : RefSpec(org.eclipse.jgit.transport.RefSpec) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) ObjectId(org.eclipse.jgit.lib.ObjectId) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 38 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class NoteDbOnlyIT method noRetryOnLockFailureWithoutAtomicUpdates.

@Test
public void noRetryOnLockFailureWithoutAtomicUpdates() throws Exception {
    assume().that(notesMigration.fuseUpdates()).isFalse();
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    String master = "refs/heads/master";
    ObjectId initial;
    try (Repository repo = repoManager.openRepository(project)) {
        initial = repo.exactRef(master).getObjectId();
    }
    AtomicInteger updateRepoCalledCount = new AtomicInteger();
    AtomicInteger updateChangeCalledCount = new AtomicInteger();
    AtomicInteger afterUpdateReposCalledCount = new AtomicInteger();
    try {
        retryHelper.execute(batchUpdateFactory -> {
            try (BatchUpdate bu = newBatchUpdate(batchUpdateFactory)) {
                bu.addOp(id, new UpdateRefAndAddMessageOp(updateRepoCalledCount, updateChangeCalledCount));
                bu.execute(new ConcurrentWritingListener(afterUpdateReposCalledCount));
            }
            return null;
        });
        assert_().fail("expected RestApiException");
    } catch (RestApiException e) {
    // Expected.
    }
    assertThat(updateRepoCalledCount.get()).isEqualTo(1);
    assertThat(afterUpdateReposCalledCount.get()).isEqualTo(1);
    assertThat(updateChangeCalledCount.get()).isEqualTo(0);
    // updateChange was never called, so no message was ever added.
    assertThat(getMessages(id)).doesNotContain(UpdateRefAndAddMessageOp.CHANGE_MESSAGE);
    try (Repository repo = repoManager.openRepository(project)) {
        // Op lost the race, so the other writer's commit happened first. Op didn't retry, because the
        // ref updates weren't atomic, so it didn't throw LockFailureException on failure.
        assertThat(commitMessages(repo, initial, repo.exactRef(master).getObjectId())).containsExactly(ConcurrentWritingListener.MSG_PREFIX + "1");
    }
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Change(com.google.gerrit.reviewdb.client.Change) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 39 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class NoteDbOnlyIT method updateChangeFailureRollsBackRefUpdate.

@Test
public void updateChangeFailureRollsBackRefUpdate() throws Exception {
    assume().that(notesMigration.fuseUpdates()).isTrue();
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    String master = "refs/heads/master";
    String backup = "refs/backup/master";
    ObjectId master1 = getRef(master).get();
    assertThat(getRef(backup)).isEmpty();
    // Toy op that copies the value of refs/heads/master to refs/backup/master.
    BatchUpdateOp backupMasterOp = new BatchUpdateOp() {

        ObjectId newId;

        @Override
        public void updateRepo(RepoContext ctx) throws IOException {
            ObjectId oldId = ctx.getRepoView().getRef(backup).orElse(ObjectId.zeroId());
            newId = ctx.getRepoView().getRef(master).get();
            ctx.addRefUpdate(oldId, newId, backup);
        }

        @Override
        public boolean updateChange(ChangeContext ctx) {
            ctx.getUpdate(ctx.getChange().currentPatchSetId()).setChangeMessage("Backed up master branch to " + newId.name());
            return true;
        }
    };
    try (BatchUpdate bu = newBatchUpdate(batchUpdateFactory)) {
        bu.addOp(id, backupMasterOp);
        bu.execute();
    }
    // Ensure backupMasterOp worked.
    assertThat(getRef(backup)).hasValue(master1);
    assertThat(getMessages(id)).contains("Backed up master branch to " + master1.name());
    // Advance master by submitting the change.
    gApi.changes().id(id.get()).current().review(ReviewInput.approve());
    gApi.changes().id(id.get()).current().submit();
    ObjectId master2 = getRef(master).get();
    assertThat(master2).isNotEqualTo(master1);
    int msgCount = getMessages(id).size();
    try (BatchUpdate bu = newBatchUpdate(batchUpdateFactory)) {
        // This time, we attempt to back up master, but we fail during updateChange.
        bu.addOp(id, backupMasterOp);
        String msg = "Change is bad";
        bu.addOp(id, new BatchUpdateOp() {

            @Override
            public boolean updateChange(ChangeContext ctx) throws ResourceConflictException {
                throw new ResourceConflictException(msg);
            }
        });
        try {
            bu.execute();
            assert_().fail("expected ResourceConflictException");
        } catch (ResourceConflictException e) {
            assertThat(e).hasMessageThat().isEqualTo(msg);
        }
    }
    // If updateChange hadn't failed, backup would have been updated to master2.
    assertThat(getRef(backup)).hasValue(master1);
    assertThat(getMessages(id)).hasSize(msgCount);
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ChangeContext(com.google.gerrit.server.update.ChangeContext) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 40 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gerrit by GerritCodeReview.

the class NoteDbOnlyIT method retryOnLockFailureWithAtomicUpdates.

@Test
public void retryOnLockFailureWithAtomicUpdates() throws Exception {
    assume().that(notesMigration.fuseUpdates()).isTrue();
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    String master = "refs/heads/master";
    ObjectId initial;
    try (Repository repo = repoManager.openRepository(project)) {
        ((InMemoryRepository) repo).setPerformsAtomicTransactions(true);
        initial = repo.exactRef(master).getObjectId();
    }
    AtomicInteger updateRepoCalledCount = new AtomicInteger();
    AtomicInteger updateChangeCalledCount = new AtomicInteger();
    AtomicInteger afterUpdateReposCalledCount = new AtomicInteger();
    String result = retryHelper.execute(batchUpdateFactory -> {
        try (BatchUpdate bu = newBatchUpdate(batchUpdateFactory)) {
            bu.addOp(id, new UpdateRefAndAddMessageOp(updateRepoCalledCount, updateChangeCalledCount));
            bu.execute(new ConcurrentWritingListener(afterUpdateReposCalledCount));
        }
        return "Done";
    });
    assertThat(result).isEqualTo("Done");
    assertThat(updateRepoCalledCount.get()).isEqualTo(2);
    assertThat(afterUpdateReposCalledCount.get()).isEqualTo(2);
    assertThat(updateChangeCalledCount.get()).isEqualTo(2);
    List<String> messages = getMessages(id);
    assertThat(Iterables.getLast(messages)).isEqualTo(UpdateRefAndAddMessageOp.CHANGE_MESSAGE);
    assertThat(Collections.frequency(messages, UpdateRefAndAddMessageOp.CHANGE_MESSAGE)).isEqualTo(1);
    try (Repository repo = repoManager.openRepository(project)) {
        // Op lost the race, so the other writer's commit happened first. Then op retried and wrote
        // its commit with the other writer's commit as parent.
        assertThat(commitMessages(repo, initial, repo.exactRef(master).getObjectId())).containsExactly(ConcurrentWritingListener.MSG_PREFIX + "1", UpdateRefAndAddMessageOp.COMMIT_MESSAGE).inOrder();
    }
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Repository(org.eclipse.jgit.lib.Repository) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)357 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)125 RevWalk (org.eclipse.jgit.revwalk.RevWalk)86 IOException (java.io.IOException)63 Repository (org.eclipse.jgit.lib.Repository)63 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)40 ArrayList (java.util.ArrayList)36 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)34 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)34 Ref (org.eclipse.jgit.lib.Ref)34 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)33 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)26 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)23 NoteMap (org.eclipse.jgit.notes.NoteMap)22 Map (java.util.Map)21 OrmException (com.google.gwtorm.server.OrmException)20 ObjectReader (org.eclipse.jgit.lib.ObjectReader)20