Search in sources :

Example 1 with TestRepository

use of org.eclipse.jgit.junit.TestRepository in project gerrit by GerritCodeReview.

the class AbstractPushForReview method accidentallyPushNewPatchSetDirectlyToBranch.

private Change.Id accidentallyPushNewPatchSetDirectlyToBranch() throws Exception {
    PushOneCommit.Result r = createChange();
    RevCommit ps1Commit = r.getCommit();
    Change c = r.getChange().change();
    RevCommit ps2Commit;
    try (Repository repo = repoManager.openRepository(project)) {
        // Create a new patch set of the change directly in Gerrit's repository,
        // without pushing it. In reality it's more likely that the client would
        // create and push this behind Gerrit's back (e.g. an admin accidentally
        // using direct ssh access to the repo), but that's harder to do in tests.
        TestRepository<?> tr = new TestRepository<>(repo);
        ps2Commit = tr.branch("refs/heads/master").commit().message(ps1Commit.getShortMessage() + " v2").insertChangeId(r.getChangeId().substring(1)).create();
    }
    testRepo.git().fetch().setRefSpecs(new RefSpec("refs/heads/master")).call();
    testRepo.reset(ps2Commit);
    ChangeData cd = byCommit(ps1Commit);
    assertThat(cd.change().getStatus()).isEqualTo(Change.Status.NEW);
    assertThat(getPatchSetRevisions(cd)).containsExactlyEntriesIn(ImmutableMap.of(1, ps1Commit.name()));
    return c.getId();
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) RefSpec(org.eclipse.jgit.transport.RefSpec) Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with TestRepository

use of org.eclipse.jgit.junit.TestRepository in project gerrit by GerritCodeReview.

the class AbstractPushForReview method mergedOptionWithExistingChangeInsertsPatchSet.

@Test
public void mergedOptionWithExistingChangeInsertsPatchSet() throws Exception {
    String master = "refs/heads/master";
    grant(project, master, Permission.PUSH, true);
    PushOneCommit.Result r = pushTo("refs/for/master");
    r.assertOkStatus();
    ObjectId c1 = r.getCommit().copy();
    // Create a PS2 commit directly on master in the server's repo. This
    // simulates the client amending locally and pushing directly to the branch,
    // expecting the change to be auto-closed, but the change metadata update
    // fails.
    ObjectId c2;
    try (Repository repo = repoManager.openRepository(project)) {
        TestRepository<?> tr = new TestRepository<>(repo);
        RevCommit commit2 = tr.amend(c1).message("New subject").insertChangeId(r.getChangeId().substring(1)).create();
        c2 = commit2.copy();
        tr.update(master, c2);
    }
    testRepo.git().fetch().setRefSpecs(new RefSpec("refs/heads/master")).call();
    testRepo.reset(c2);
    String ref = "refs/for/master%merged";
    assertPushOk(pushHead(testRepo, ref, false), ref);
    EnumSet<ListChangesOption> opts = EnumSet.of(ListChangesOption.ALL_REVISIONS);
    ChangeInfo info = gApi.changes().id(r.getChangeId()).get(opts);
    assertThat(info.currentRevision).isEqualTo(c2.name());
    assertThat(info.revisions.keySet()).containsExactly(c1.name(), c2.name());
    // TODO(dborowitz): Fix ReceiveCommits to also auto-close the change.
    assertThat(info.status).isEqualTo(ChangeStatus.NEW);
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) RefSpec(org.eclipse.jgit.transport.RefSpec) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 3 with TestRepository

use of org.eclipse.jgit.junit.TestRepository in project gerrit by GerritCodeReview.

the class CreateChangeIT method createNewCommitWithoutChangeId.

private RevCommit createNewCommitWithoutChangeId() throws Exception {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk walk = new RevWalk(repo)) {
        Ref ref = repo.exactRef("refs/heads/master");
        RevCommit tip = null;
        if (ref != null) {
            tip = walk.parseCommit(ref.getObjectId());
        }
        TestRepository<?> testSrcRepo = new TestRepository<>(repo);
        TestRepository<?>.BranchBuilder<?> builder = testSrcRepo.branch("refs/heads/master");
        RevCommit revCommit = tip == null ? builder.commit().message("commit 1").add("a.txt", "content").create() : builder.commit().parent(tip).message("commit 1").add("a.txt", "content").create();
        assertThat(GitUtil.getChangeId(testSrcRepo, revCommit)).isEmpty();
        return revCommit;
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) RefNames.changeMetaRef(com.google.gerrit.reviewdb.client.RefNames.changeMetaRef) Ref(org.eclipse.jgit.lib.Ref) TestRepository(org.eclipse.jgit.junit.TestRepository) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with TestRepository

use of org.eclipse.jgit.junit.TestRepository in project gerrit by GerritCodeReview.

the class MoveChangeIT method moveMergeCommitChange.

@Test
public void moveMergeCommitChange() throws Exception {
    // Move a change which has a merge commit as the current PS
    // Create a merge commit and push for review
    PushOneCommit.Result r1 = createChange();
    PushOneCommit.Result r2 = createChange();
    TestRepository<?>.CommitBuilder<?> commitBuilder = testRepo.branch("HEAD").commit().insertChangeId();
    commitBuilder.parent(r1.getCommit()).parent(r2.getCommit()).message("Move change Merge Commit").author(admin.getIdent()).committer(new PersonIdent(admin.getIdent(), testRepo.getDate()));
    RevCommit c = commitBuilder.create();
    pushHead(testRepo, "refs/for/master", false, false);
    // Try to move the merge commit to another branch
    Branch.NameKey newBranch = new Branch.NameKey(r1.getChange().change().getProject(), "moveTest");
    createBranch(newBranch);
    exception.expect(ResourceConflictException.class);
    exception.expectMessage("Merge commit cannot be moved");
    move(GitUtil.getChangeId(testRepo, c).get(), newBranch.get());
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Branch(com.google.gerrit.reviewdb.client.Branch) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with TestRepository

use of org.eclipse.jgit.junit.TestRepository in project gerrit by GerritCodeReview.

the class RepoSequenceTest method failOnWrongType.

@Test
public void failOnWrongType() throws Exception {
    try (Repository repo = repoManager.openRepository(project)) {
        TestRepository<Repository> tr = new TestRepository<>(repo);
        tr.branch(RefNames.REFS_SEQUENCES + "id").commit().create();
        try {
            newSequence("id", 1, 3).next();
            fail();
        } catch (OrmException e) {
            assertThat(e.getCause()).isInstanceOf(ExecutionException.class);
            assertThat(e.getCause().getCause()).isInstanceOf(IncorrectObjectTypeException.class);
        }
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) OrmException(com.google.gwtorm.server.OrmException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

TestRepository (org.eclipse.jgit.junit.TestRepository)22 Repository (org.eclipse.jgit.lib.Repository)14 Test (org.junit.Test)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)9 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)7 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 Change (com.google.gerrit.reviewdb.client.Change)6 ObjectId (org.eclipse.jgit.lib.ObjectId)6 PersonIdent (org.eclipse.jgit.lib.PersonIdent)6 ArrayList (java.util.ArrayList)4 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)4 Ref (org.eclipse.jgit.lib.Ref)4 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)3 Sandboxed (com.google.gerrit.acceptance.Sandboxed)2 DraftInput (com.google.gerrit.extensions.api.changes.DraftInput)2 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)2 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)2 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2