Search in sources :

Example 21 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project gitblit by gitblit.

the class GitServletTest method testMergeCommitterVerification.

private void testMergeCommitterVerification(boolean expectedSuccess) throws Exception {
    UserModel user = getUser();
    delete(user);
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
    // fork from original to a temporary bare repo
    File verification = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-committer.git");
    if (verification.exists()) {
        FileUtils.delete(verification, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(verification);
    clone.setBare(true);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    // require push permissions and committer verification
    RepositoryModel model = repositories().getRepositoryModel("refchecks/verify-committer.git");
    model.authorizationControl = AuthorizationControl.NAMED;
    model.accessRestriction = AccessRestrictionType.PUSH;
    model.verifyCommitter = true;
    // grant user push permission
    user.setRepositoryPermission(model.name, AccessPermission.PUSH);
    gitblit().addUser(user);
    repositories().updateRepositoryModel(model.name, model, false);
    // clone temp bare repo to working copy
    File local = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-wc");
    if (local.exists()) {
        FileUtils.delete(local, FileUtils.RECURSIVE);
    }
    clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
    clone.setDirectory(local);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    Git git = Git.open(local);
    // checkout a mergetest branch
    git.checkout().setCreateBranch(true).setName("mergetest").call();
    // change identity
    git.getRepository().getConfig().setString("user", null, "name", "mergetest");
    git.getRepository().getConfig().setString("user", null, "email", "mergetest@merge.com");
    git.getRepository().getConfig().save();
    // commit a file
    File file = new File(local, "MERGECHK2");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    RevCommit mergeTip = git.commit().setMessage(file.getName() + " test").call();
    // return to master
    git.checkout().setName("master").call();
    // restore identity
    if (expectedSuccess) {
        git.getRepository().getConfig().setString("user", null, "name", user.username);
        git.getRepository().getConfig().setString("user", null, "email", user.emailAddress);
        git.getRepository().getConfig().save();
    }
    // commit a file
    file = new File(local, "MERGECHK1");
    os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage(file.getName() + " test").call();
    // merge the tip of the mergetest branch into master with --no-ff
    MergeResult mergeResult = git.merge().setFastForward(FastForwardMode.NO_FF).include(mergeTip.getId()).call();
    assertEquals(MergeResult.MergeStatus.MERGED, mergeResult.getMergeStatus());
    // push the merged master to the origin
    Iterable<PushResult> results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        Status status = ref.getStatus();
        if (expectedSuccess) {
            assertTrue("Verification failed! User was NOT able to push commit! " + status.name(), Status.OK.equals(status));
        } else {
            assertTrue("Verification failed! User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));
        }
    }
    GitBlitSuite.close(git);
    // close serving repository
    GitBlitSuite.close(verification);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Status(org.eclipse.jgit.transport.RemoteRefUpdate.Status) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) MergeResult(org.eclipse.jgit.api.MergeResult) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) UserModel(com.gitblit.models.UserModel) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 22 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project gitblit by gitblit.

the class TicketReferenceTest method commitTicketBranchDeletePostMergeReference.

@Test
public void commitTicketBranchDeletePostMergeReference() throws Exception {
    setPatchsetAvailable(false);
    TicketModel a = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-A"));
    TicketModel b = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-B"));
    TicketModel c = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-C"));
    assertFalse(c.hasPatchsets());
    String branchName = String.format("ticket/%d", c.number);
    git.checkout().setCreateBranch(true).setName(branchName).call();
    String message = String.format("commit before amend for #%d and #%d", a.number, b.number);
    final RevCommit revCommit1 = makeCommit(message);
    final String commit1Sha = revCommit1.name();
    assertPushSuccess(commit1Sha, branchName);
    a = ticketService.getTicket(repo, a.number);
    b = ticketService.getTicket(repo, b.number);
    c = ticketService.getTicket(repo, c.number);
    assertTrue(a.hasReferences());
    assertTrue(b.hasReferences());
    assertFalse(c.hasReferences());
    List<Reference> cRefA = a.getReferences();
    assertNotNull(cRefA);
    assertEquals(1, cRefA.size());
    assertNull(cRefA.get(0).ticketId);
    assertEquals(commit1Sha, cRefA.get(0).hash);
    List<Reference> cRefB = b.getReferences();
    assertNotNull(cRefB);
    assertEquals(1, cRefB.size());
    assertNull(cRefB.get(0).ticketId);
    assertEquals(commit1Sha, cRefB.get(0).hash);
    git.checkout().setCreateBranch(false).setName("refs/heads/master").call();
    // merge the tip of the branch into master
    MergeResult mergeResult = git.merge().setFastForward(FastForwardMode.NO_FF).include(revCommit1.getId()).call();
    assertEquals(MergeResult.MergeStatus.MERGED, mergeResult.getMergeStatus());
    // push the merged master to the origin
    Iterable<PushResult> results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        assertEquals(Status.OK, ref.getStatus());
    }
    // As everything has been merged no references should be changed
    assertDeleteBranch(branchName);
    a = ticketService.getTicket(repo, a.number);
    b = ticketService.getTicket(repo, b.number);
    c = ticketService.getTicket(repo, c.number);
    assertTrue(a.hasReferences());
    assertTrue(b.hasReferences());
    assertFalse(c.hasReferences());
    cRefA = a.getReferences();
    assertNotNull(cRefA);
    assertEquals(1, cRefA.size());
    assertNull(cRefA.get(0).ticketId);
    assertEquals(commit1Sha, cRefA.get(0).hash);
    cRefB = b.getReferences();
    assertNotNull(cRefB);
    assertEquals(1, cRefB.size());
    assertNull(cRefB.get(0).ticketId);
    assertEquals(commit1Sha, cRefB.get(0).hash);
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Reference(com.gitblit.models.TicketModel.Reference) MergeResult(org.eclipse.jgit.api.MergeResult) TicketModel(com.gitblit.models.TicketModel) PushResult(org.eclipse.jgit.transport.PushResult) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

MergeResult (org.eclipse.jgit.api.MergeResult)22 IOException (java.io.IOException)10 Test (org.junit.Test)9 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 Git (org.eclipse.jgit.api.Git)6 CoreException (org.eclipse.core.runtime.CoreException)4 IndexDiffData (org.eclipse.egit.core.internal.indexdiff.IndexDiffData)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)3 MergeStatus (org.eclipse.jgit.api.MergeResult.MergeStatus)3 Ref (org.eclipse.jgit.lib.Ref)3 File (java.io.File)2 IFile (org.eclipse.core.resources.IFile)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 NoRemoteRepositoryException (org.eclipse.jgit.errors.NoRemoteRepositoryException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 StoredConfig (org.eclipse.jgit.lib.StoredConfig)2