Skip to content

The Unified and Autonomous Development Platform

Add unit test for LastCommitsOfChildren

robin shine32a3005084b8
diff --git a/commons.git/src/main/java/org/eclipse/jgit/revwalk/LastCommitsOfChildren.java b/commons.git/src/main/java/org/eclipse/jgit/revwalk/LastCommitsOfChildren.java
index badb1312b0..755648f4e6 100644
--- a/commons.git/src/main/java/org/eclipse/jgit/revwalk/LastCommitsOfChildren.java
+++ b/commons.git/src/main/java/org/eclipse/jgit/revwalk/LastCommitsOfChildren.java
@@ -39,13 +39,17 @@ public final class LastCommitsOfChildren extends HashMap<String, Value> {
private static final int PARSED = RevWalk.PARSED;
public LastCommitsOfChildren(Repository repo, AnyObjectId until) {
- this(repo, until, null);
+ this(repo, until, null, null);
}
public LastCommitsOfChildren(Repository repo, AnyObjectId until, @Nullable String treePath) {
this(repo, until, treePath, null);
}
+ public LastCommitsOfChildren(Repository repo, AnyObjectId until, @Nullable Cache cache) {
+ this(repo, until, null, cache);
+ }
+
/**
* Constructs a hashmap with key representing child name under specified tree, and value
* represents last commit info of the child.
diff --git a/commons.git/src/test/java/com/pmease/commons/git/AbstractJGitTest.java b/commons.git/src/test/java/com/pmease/commons/git/AbstractJGitTest.java
new file mode 100644
index 0000000000..d052617639
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/AbstractJGitTest.java
@@ -0,0 +1,104 @@
+package com.pmease.commons.git;
+
+import java.io.File;
+
+import org.eclipse.jgit.api.AddCommand;
+import org.eclipse.jgit.api.CommitCommand;
+import org.eclipse.jgit.api.RmCommand;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Repository;
+import org.junit.After;
+import org.junit.Before;
+
+import com.pmease.commons.util.FileUtils;
+
+public abstract class AbstractJGitTest {
+
+ protected File tempDir;
+
+ protected File repoDir;
+
+ protected org.eclipse.jgit.api.Git git;
+
+ protected Repository repo;
+
+ /**
+ * Subclass should call super.setup() as first statement if it wants to override this method.
+ */
+ @Before
+ public void before() {
+ tempDir = FileUtils.createTempDir();
+ repoDir = new File(tempDir, "repo");
+
+ try {
+ git = org.eclipse.jgit.api.Git.init().setBare(false).setDirectory(repoDir).call();
+ } catch (IllegalStateException | GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ repo = git.getRepository();
+ }
+
+ protected void createDir(String path) {
+ FileUtils.createDir(new File(repoDir, path));
+ }
+
+ protected void writeFile(String path, String content) {
+ File file = new File(repoDir, path);
+ FileUtils.writeFile(file, content);
+ }
+
+ protected void add(String...paths) {
+ AddCommand add = git.add();
+ for (String path: paths)
+ add.addFilepattern(path);
+ try {
+ add.call();
+ } catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void rm(String...paths) {
+ RmCommand rm = git.rm();
+ for (String path: paths)
+ rm.addFilepattern(path);
+ try {
+ rm.call();
+ } catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void commit(String comment) {
+ CommitCommand ci = git.commit();
+ ci.setMessage(comment);
+ ci.setAuthor("foo", "[email protected]");
+ try {
+ ci.call();
+ } catch (GitAPIException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void addFile(String path, String content) {
+ writeFile(path, content);
+ add(path);
+ }
+
+ protected void addFileAndCommit(String path, String content, String comment) {
+ addFile(path, content);
+ commit(comment);
+ }
+
+ protected void removeFileAndCommit(String path, String comment) {
+ rm(path);
+ commit(comment);
+ }
+
+ @After
+ public void after() {
+ git.close();
+ FileUtils.deleteDir(tempDir);
+ }
+
+}
diff --git a/commons.git/src/test/java/org/eclipse/jgit/revwalk/LastCommitsOfChildrenTest.java b/commons.git/src/test/java/org/eclipse/jgit/revwalk/LastCommitsOfChildrenTest.java
new file mode 100644
index 0000000000..a86d0b0ebe
--- /dev/null
+++ b/commons.git/src/test/java/org/eclipse/jgit/revwalk/LastCommitsOfChildrenTest.java
@@ -0,0 +1,93 @@
+package org.eclipse.jgit.revwalk;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.revwalk.LastCommitsOfChildren.Cache;
+import org.eclipse.jgit.revwalk.LastCommitsOfChildren.Value;
+import org.junit.Test;
+
+import com.pmease.commons.git.AbstractJGitTest;
+
+public class LastCommitsOfChildrenTest extends AbstractJGitTest {
+
+ @Test
+ public void testMergeWithoutTouchingSameFile() throws Exception {
+ addFileAndCommit("initial", "", "initial commit");
+ git.checkout().setName("dev").setCreateBranch(true).call();
+ addFileAndCommit("d", "", "add a file to dev branch");
+ git.checkout().setName("master").call();
+ addFileAndCommit("m", "", "add a file to master branch");
+ git.merge().include(repo.resolve("dev")).setCommit(true).call();
+
+ LastCommitsOfChildren lastCommits = new LastCommitsOfChildren(repo, repo.resolve("master"));
+ assertEquals(repo.resolve("master~1"), lastCommits.get("m").getId());
+ assertEquals(repo.resolve("dev"), lastCommits.get("d").getId());
+ assertEquals(repo.resolve("master~2"), lastCommits.get("initial").getId());
+ }
+
+ @Test
+ public void testMergeTouchingSameFile() throws Exception {
+ addFileAndCommit("file", "1\n2\n3\n4\n5\n", "initial commit");
+ git.checkout().setName("dev").setCreateBranch(true).call();
+ addFileAndCommit("file", "0\n1\n2\n3\n4\n5\n", "add first line");
+ git.checkout().setName("master").call();
+ addFileAndCommit("file", "1\n2\n3\n4\n5\n6\n", "add last line");
+ git.merge().include(repo.resolve("dev")).setCommit(true).call();
+
+ LastCommitsOfChildren lastCommits = new LastCommitsOfChildren(repo, repo.resolve("master"));
+ assertEquals(repo.resolve("master"), lastCommits.get("file").getId());
+ }
+
+ @Test
+ public void testDirAndFile() throws Exception {
+ createDir("dir/dir1/subdir");
+ createDir("dir/dir2");
+ addFileAndCommit("dir/dir1/file", "", "commit");
+ addFileAndCommit("dir/dir2/file", "", "commit");
+ addFileAndCommit("dir/dir1/subdir/file", "", "commit");
+ addFileAndCommit("dir/file", "", "commit");
+
+ LastCommitsOfChildren lastCommits = new LastCommitsOfChildren(repo, repo.resolve("master"), "dir");
+ assertEquals(3, lastCommits.size());
+ assertEquals(repo.resolve("master~1"), lastCommits.get("dir1").getId());
+ assertEquals(repo.resolve("master~2"), lastCommits.get("dir2").getId());
+ assertEquals(repo.resolve("master"), lastCommits.get("file").getId());
+ }
+
+ @Test
+ public void testWithCache() throws Exception {
+ addFileAndCommit("initial", "", "initial commit");
+ git.checkout().setName("feature1").setCreateBranch(true).call();
+ addFileAndCommit("feature1", "", "add feature1");
+ git.checkout().setName("master").call();
+ addFileAndCommit("master", "", "add master");
+ git.merge().include(repo.resolve("feature1")).setCommit(true).call();
+
+ final ObjectId oldId = repo.resolve("master");
+ final LastCommitsOfChildren oldLastCommits = new LastCommitsOfChildren(repo, oldId);
+ Cache cache = new Cache() {
+
+ @Override
+ public Map<String, Value> getLastCommitsOfChildren(ObjectId commitId) {
+ if (commitId.equals(oldId))
+ return oldLastCommits;
+ else
+ return null;
+ }
+
+ };
+ assertEquals(oldLastCommits, new LastCommitsOfChildren(repo, oldId, cache));
+
+ git.checkout().setName("feature2").setCreateBranch(true).call();
+ addFileAndCommit("initial", "hello", "modify initial");
+ git.checkout().setName("master").call();
+ git.merge().include(repo.resolve("feature2")).setCommit(true).call();
+
+ ObjectId newId = repo.resolve("master");
+ assertEquals(new LastCommitsOfChildren(repo, newId), new LastCommitsOfChildren(repo, newId, cache));
+ }
+
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
index 30be5c1844..e89e8ff6c3 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
@@ -1,6 +1,5 @@
package com.pmease.gitplex.core;
-import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
@@ -17,7 +16,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.eventbus.EventBus;
-import com.pmease.commons.git.Git;
import com.pmease.commons.git.GitConfig;
import com.pmease.commons.git.command.GitCommand;
import com.pmease.commons.loader.AbstractPlugin;
@@ -98,20 +96,6 @@ public class GitPlex extends AbstractPlugin {
});
checkGit();
-
- String gitDir = "w:\\mozilla";
- String folder = "build";
- Git git = new Git(new File(gitDir + "/.git"));
- long time = System.currentTimeMillis();
- int size = 0;
- for (File file: new File(gitDir + "/" + folder).listFiles()) {
- if (folder.length() != 0)
- size += git.log(null, "master", folder + "/" + file.getName(), 1, 0).size();
- else
- size += git.log(null, "master", file.getName(), 1, 0).size();
- System.out.println(size);
- }
- System.out.println(System.currentTimeMillis()-time);
}
public void checkGit() {