Skip to content

Commit

Permalink
more CommitUtils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
junyi.bei committed Jun 8, 2016
1 parent 21a82e0 commit 33e2a24
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static com.beijunyi.parallelgit.utils.TreeUtils.normalizeNodePath;
import static java.util.Collections.unmodifiableList;
import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
import static org.eclipse.jgit.treewalk.filter.TreeFilter.ANY_DIFF;

public final class CommitUtils {
Expand Down Expand Up @@ -52,18 +51,18 @@ public static RevCommit getCommit(Ref ref, Repository repo) throws IOException {

@Nonnull
public static RevCommit getCommit(String id, Repository repo) throws IOException {
AnyObjectId commitId = repo.resolve(id);
ObjectId commitId = repo.resolve(id);
if(commitId == null)
throw new NoSuchCommitException(id);
return getCommit(commitId, repo);
}

public static boolean exists(String name, Repository repo) throws IOException {
AnyObjectId obj = repo.resolve(name);
ObjectId obj = repo.resolve(name);
if(obj == null)
return false;
try(RevWalk rw = new RevWalk(repo)) {
return rw.parseAny(obj).getType() == OBJ_COMMIT;
return rw.lookupCommit(obj) != null;
}
}

Expand Down Expand Up @@ -129,15 +128,15 @@ public static RevCommit getLatestFileRevision(String path, String start, Reposit
return id != null ? getLatestFileRevision(path, id, repo) : null;
}

public static boolean isMergedInto(AnyObjectId branchHead, AnyObjectId masterHead, ObjectReader reader) throws IOException {
public static boolean isMergedInto(AnyObjectId sourceHead, AnyObjectId masterHead, ObjectReader reader) throws IOException {
try(RevWalk rw = new RevWalk(reader)) {
return rw.isMergedInto(rw.lookupCommit(branchHead), rw.lookupCommit(masterHead));
return rw.isMergedInto(rw.lookupCommit(sourceHead), rw.lookupCommit(masterHead));
}
}

public static boolean isMergedInto(AnyObjectId branchHead, AnyObjectId masterHead, Repository repo) throws IOException {
public static boolean isMergedInto(AnyObjectId sourceHead, AnyObjectId masterHead, Repository repo) throws IOException {
try(ObjectReader reader = repo.newObjectReader()) {
return isMergedInto(branchHead, masterHead, reader);
return isMergedInto(sourceHead, masterHead, reader);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.beijunyi.parallelgit.utils;

import java.io.IOException;

import com.beijunyi.parallelgit.AbstractParallelGitTest;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class CommitUtilsExistsTest extends AbstractParallelGitTest {

@Before
public void setUp() throws IOException {
initRepository();
}

@Test
public void testCommitExistsWhenReferencedByCommitId_shouldReturnTrue() throws IOException {
writeSomethingToCache();
String id = commit().getName();
assertTrue(CommitUtils.exists(id, repo));
}

@Test
public void testHeadCommitExistsWhenReferencedByBranchName_shouldReturnTrue() throws IOException {
writeSomethingToCache();
commitToBranch("test_branch");
assertTrue(CommitUtils.exists("test_branch", repo));
}

@Test
public void testTagCommitExistsWhenReferencedByTagName_shouldReturnTrue() throws IOException {
writeSomethingToCache();
ObjectId id = commit();
TagUtils.tagCommit("test_tag", id, repo);
assertTrue(CommitUtils.exists("test_tag", repo));
}

@Test
public void testCommitExistsWhenReferenceDoesNotExist_shouldReturnFalse() throws IOException {
assertFalse(CommitUtils.exists("non_existent_reference", repo));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.junit.Assert.assertEquals;

public class CommitUtilsGetCommitHistoryTest extends AbstractParallelGitTest {
public class CommitUtilsGetHistoryTest extends AbstractParallelGitTest {

@Before
public void setUp() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.Before;
import org.junit.Test;

import static com.beijunyi.parallelgit.utils.BranchUtils.createBranch;
import static org.eclipse.jgit.lib.Constants.MASTER;
import static org.junit.Assert.*;

public class CommitUtilsIsMergedIntoTest extends AbstractParallelGitTest {
Expand All @@ -17,23 +19,34 @@ public void setUp() throws IOException {
}

@Test
public void testIfCommitMergedIntoItself_shouldReturnTrue() throws IOException {
RevCommit commit = commit();
assertTrue(CommitUtils.isMergedInto(commit, commit, repo));
public void testIfBranchIsMergedIntoItself_shouldReturnTrue() throws IOException {
writeSomethingToCache();
commitToBranch("test_branch");

assertTrue(CommitUtils.isMergedInto("test_branch", "test_branch", repo));
}

@Test
public void testIfCommitMergedIntoItsParent_shouldReturnFalse() throws IOException {
RevCommit parent = commit();
RevCommit commit = commit(parent);
assertFalse(CommitUtils.isMergedInto(commit, parent, repo));
public void testIfBranchIsMergedIntoMasterWhenBranchIsAheadOfMaster_shouldReturnFalse() throws IOException {
writeSomethingToCache();
RevCommit masterFirst = commitToMaster();
writeSomethingToCache();
commitToBranch("test_branch", masterFirst);
writeSomethingToCache();
commitToBranch("test_branch");

assertFalse(CommitUtils.isMergedInto("test_branch", MASTER, repo));
}

@Test
public void testIfCommitMergedIntoItsChild_shouldReturnTrue() throws IOException {
RevCommit commit = commit();
RevCommit child = commit(commit);
assertTrue(CommitUtils.isMergedInto(commit, child, repo));
public void testIfBranchIsMergedIntoMasterWhenMasterIsAheadOfBranch_shouldReturnTrue() throws IOException {
writeSomethingToCache();
RevCommit masterFirst = commitToMaster();
createBranch("test_branch", masterFirst, repo);
writeSomethingToCache();
commitToMaster();

assertTrue(CommitUtils.isMergedInto("test_branch", MASTER, repo));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void setUp() throws IOException {
}

@Test
public void whenBranchIsAheadOfMaster_shouldReturnTheNewCommitsInBranchInReverseOrder() throws IOException {
public void listUnmergedCommitsWhenBranchIsAheadOfMaster_shouldReturnTheNewCommitsInBranchInReverseOrder() throws IOException {
writeSomethingToCache();
RevCommit masterFirst = commitToMaster();
writeSomethingToCache();
Expand All @@ -35,7 +35,7 @@ public void whenBranchIsAheadOfMaster_shouldReturnTheNewCommitsInBranchInReverse
}

@Test
public void whenMasterIsSameAsBranch_shouldReturnEmptyList() throws IOException {
public void listUnmergedCommitsWhenMasterIsSameAsBranch_shouldReturnEmptyList() throws IOException {
writeSomethingToCache();
RevCommit masterFirst = commitToMaster();
createBranch("test_branch", masterFirst, repo);
Expand All @@ -45,7 +45,7 @@ public void whenMasterIsSameAsBranch_shouldReturnEmptyList() throws IOException
}

@Test
public void whenMasterIsAheadOfBranch_shouldReturnEmptyList() throws IOException {
public void listUnmergedCommitsWhenMasterIsAheadOfBranch_shouldReturnEmptyList() throws IOException {
writeSomethingToCache();
RevCommit masterFirst = commitToMaster();
createBranch("test_branch", masterFirst, repo);
Expand Down

0 comments on commit 33e2a24

Please sign in to comment.