The Unified and Autonomous Development Platform
Modify various git test to utilize common git repository setup in AbstractGitTest.
robin shine029dd1ded1a5
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.javaindex b7cdb67947..08003c8f82 100644--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java@@ -43,6 +43,7 @@ import com.pmease.commons.git.command.LogCommand;import com.pmease.commons.git.command.MergeCommand;import com.pmease.commons.git.command.MergeCommand.FastForwardMode;import com.pmease.commons.git.command.ParseRevisionCommand;+import com.pmease.commons.git.command.PullCommand;import com.pmease.commons.git.command.PushCommand;import com.pmease.commons.git.command.RemoveCommand;import com.pmease.commons.git.command.ResetCommand;@@ -240,8 +241,8 @@ public class Git implements Serializable {return this;}- public Git remove(String... paths) {- new RemoveCommand(repoDir).removePaths(paths).call();+ public Git rm(String... paths) {+ new RemoveCommand(repoDir).paths(paths).call();return this;}@@ -343,22 +344,32 @@ public class Git implements Serializable {.strategy(strategy).strategyOption(strategyOption).message(message).call();}- public Git fetch(String from, String refspec) {+ public Git fetch(String from, String... refspec) {new FetchCommand(repoDir).from(from).refspec(refspec).call();return this;}- public Git fetch(Git from, @Nullable String refspec) {+ public Git fetch(Git from, String... refspec) {new FetchCommand(repoDir).from(from.repoDir.getAbsolutePath()).refspec(refspec).call();return this;}- public Git push(String to, String refspec) {+ public Git pull(String from, String... refspec) {+ new PullCommand(repoDir).from(from).refspec(refspec).call();+ return this;+ }++ public Git pull(Git from, String... refspec) {+ new PullCommand(repoDir).from(from.repoDir.getAbsolutePath()).refspec(refspec).call();+ return this;+ }++ public Git push(String to, String... refspec) {new PushCommand(repoDir).to(to).refspec(refspec).call();return this;}- public Git push(Git to, String refspec) {+ public Git push(Git to, String... refspec) {new PushCommand(repoDir).to(to.repoDir.getAbsolutePath()).refspec(refspec).call();return this;}diff --git a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.javaindex f7854ac65c..e0c3efdd34 100644--- a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java+++ b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java@@ -124,13 +124,13 @@ public class TreeNode implements Comparable<TreeNode>, Serializable {@Overridepublic int compareTo(TreeNode node) {- if (FileMode.TREE.equals(modeBits)) {- if (FileMode.TREE.equals(node.getModeBits()))+ if (getMode() == FileMode.TREE) {+ if (node.getMode() == FileMode.TREE)return getName().compareTo(node.getName());else- return 1;- } else if (FileMode.TREE.equals(node.getModeBits())) {- return -1;+ return -1;+ } else if (node.getMode() == FileMode.TREE) {+ return 1;} else {return getName().compareTo(node.getName());}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.javaindex 71c9b5d021..444100a969 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/FetchCommand.java@@ -10,7 +10,7 @@ public class FetchCommand extends GitCommand<Void> {private String from;- private String refspec;+ private String[] refspec = new String[0];public FetchCommand(final File repoDir) {super(repoDir);@@ -21,7 +21,7 @@ public class FetchCommand extends GitCommand<Void> {return this;}- public FetchCommand refspec(String refspec) {+ public FetchCommand refspec(String... refspec) {this.refspec = refspec;return this;}@@ -33,8 +33,8 @@ public class FetchCommand extends GitCommand<Void> {Commandline cmd = cmd().addArgs("fetch");cmd.addArgs(from);- if (refspec != null)- cmd.addArgs(refspec);+ for (String each: refspec)+ cmd.addArgs(each);cmd.execute(debugLogger, new LineConsumer() {diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/PullCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/PullCommand.javanew file mode 100644index 0000000000..01a19562d8--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/command/PullCommand.java@@ -0,0 +1,68 @@+package com.pmease.commons.git.command;++import java.io.File;++import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++import com.google.common.base.Preconditions;+import com.pmease.commons.util.execution.Commandline;+import com.pmease.commons.util.execution.LineConsumer;++public class PullCommand extends GitCommand<Void> {++ private static final Logger logger = LoggerFactory.getLogger(PullCommand.class);++ private String from;++ private String refspec[] = new String[0];++ public PullCommand(final File repoDir) {+ super(repoDir);+ }++ public PullCommand from(String from) {+ this.from = from;+ return this;+ }++ public PullCommand refspec(String... refspec) {+ this.refspec = refspec;+ return this;+ }++ @Override+ public Void call() {+ Preconditions.checkNotNull(from, "from param has to be specified.");+ Preconditions.checkNotNull(refspec, "refspec param has to be specified.");++ Commandline cmd = cmd().addArgs("pull", from);++ for (String each: refspec)+ cmd.addArgs(each);++ cmd.execute(new LineConsumer() {++ @Override+ public void consume(String line) {+ logger.debug(line);+ }++ }, new LineConsumer() {++ @Override+ public void consume(String line) {+ if (line.startsWith("To "))+ info(line);+ else if (line.startsWith(" * [new branch]"))+ info(line);+ else+ error(line);+ }++ }).checkReturnCode();++ return null;+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.javaindex e16ef71099..2a214915f9 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/PushCommand.java@@ -10,7 +10,7 @@ public class PushCommand extends GitCommand<Void> {private String to;- private String refspec;+ private String refspec[] = new String[0];public PushCommand(final File repoDir) {super(repoDir);@@ -21,7 +21,7 @@ public class PushCommand extends GitCommand<Void> {return this;}- public PushCommand refspec(String refspec) {+ public PushCommand refspec(String... refspec) {this.refspec = refspec;return this;}@@ -31,8 +31,10 @@ public class PushCommand extends GitCommand<Void> {Preconditions.checkNotNull(to, "to param has to be specified.");Preconditions.checkNotNull(refspec, "refspec param has to be specified.");- Commandline cmd = cmd().addArgs("push");- cmd.addArgs(to, refspec);+ Commandline cmd = cmd().addArgs("push", to);++ for (String each: refspec)+ cmd.addArgs(each);cmd.execute(debugLogger, new LineConsumer() {diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.javaindex 25109a398f..58fa3690fe 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java@@ -14,7 +14,7 @@ public class RemoveCommand extends GitCommand<Void> {super(repoDir);}- public RemoveCommand removePaths(String... paths) {+ public RemoveCommand paths(String... paths) {for (String path: paths)this.paths.add(path);return this;diff --git a/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java b/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.javaindex d110437362..314bf9fbd3 100644--- a/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/AbstractGitTest.java@@ -1,14 +1,20 @@package com.pmease.commons.git;+import java.io.File;+import org.junit.Assert;import org.mockito.Mockito;-import com.pmease.commons.git.command.GitCommand;import com.pmease.commons.loader.AppLoader;import com.pmease.commons.loader.AppLoaderMocker;+import com.pmease.commons.util.FileUtils;public abstract class AbstractGitTest extends AppLoaderMocker {+ protected File tempDir;++ protected Git git;+/*** Subclass should call super.setup() as first statement if it wants to override this method.*/@@ -24,15 +30,52 @@ public abstract class AbstractGitTest extends AppLoaderMocker {});- Assert.assertTrue(GitCommand.checkError("git") == null);- }+ Assert.assertTrue(com.pmease.commons.git.command.GitCommand.checkError("git") == null);++ tempDir = FileUtils.createTempDir();- /**- * Subclass should call super.teardown() as last statement if it wants to override this method.- */- @Override+ git = new Git(new File(tempDir, "repo"));+ git.init(false);+ }++ protected void createDir(String path) {+ FileUtils.createDir(new File(git.repoDir(), path));+ }++ protected void writeFile(String path, String content) {+ File file = new File(git.repoDir(), path);+ FileUtils.writeFile(file, content);+ }++ protected void add(String...paths) {+ git.add(paths);+ }++ protected void rm(String...paths) {+ git.rm(paths);+ }++ protected void commit(String comment) {+ git.commit(comment, false, false);+ }++ 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);+ }+protected void teardown() {-+ FileUtils.deleteDir(tempDir);}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/GitTest.java b/commons.git/src/test/java/com/pmease/commons/git/GitTest.javaindex 62c2cf787b..9406c0794c 100644--- a/commons.git/src/test/java/com/pmease/commons/git/GitTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/GitTest.java@@ -1,14 +1,12 @@package com.pmease.commons.git;-import static org.junit.Assert.*;-import org.junit.Test;public class GitTest {@Testpublic void test() {- fail("Not yet implemented");+}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/BlameCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/BlameCommandTest.javaindex 0b4855e18a..a4abc04eb8 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/BlameCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/BlameCommandTest.java@@ -10,80 +10,72 @@ import org.junit.Test;import com.pmease.commons.git.AbstractGitTest;import com.pmease.commons.git.Blame;import com.pmease.commons.git.Git;-import com.pmease.commons.util.FileUtils;public class BlameCommandTest extends AbstractGitTest {@Testpublic void test() {- File tempDir = FileUtils.createTempDir();+ addFileAndCommit(+ "file",+ "1st line\n"+ + "2nd line\n"+ + "3rd line\n"+ + "4th line\n"+ + "5th line\n"+ + "6th line\n"+ + "7th line\n"+ + "8th line\n"+ + "9th line\n",+ "initial commit");- try {- Git workGit = new Git(new File(tempDir, "work"));- workGit.init(false);-- File file = new File(workGit.repoDir(), "file");- FileUtils.writeFile(file,- "1st line\n"- + "2nd line\n"- + "3rd line\n"- + "4th line\n"- + "5th line\n"- + "6th line\n"- + "7th line\n"- + "8th line\n"- + "9th line\n");- workGit.add("file").commit("initial commit", false, false);-- List<Blame> blames = workGit.blame("file", "master", 3, -1);- assertEquals(1, blames.size());- assertEquals(7, blames.get(0).getLines().size());-- FileUtils.writeFile(file,- "first line\n"- + "2nd line\n"- + "3rd line\n"- + "4th line\n"- + "5th line\n"- + "6th line\n"- + "7th line\n"- + "8th line\n"- + "nineth line\n");- workGit.add("file").commit("second commit", false, false);-- blames = workGit.blame("file", "master", -1, -1);- assertEquals(3, blames.size());- assertEquals(1, blames.get(0).getLines().size());- assertEquals(7, blames.get(1).getLines().size());- assertEquals(1, blames.get(2).getLines().size());- assertEquals(blames.get(0).getCommit().getHash(), blames.get(2).getCommit().getHash());+ List<Blame> blames = git.blame("file", "master", 3, -1);+ assertEquals(1, blames.size());+ assertEquals(7, blames.get(0).getLines().size());++ addFileAndCommit(+ "file",+ "first line\n"+ + "2nd line\n"+ + "3rd line\n"+ + "4th line\n"+ + "5th line\n"+ + "6th line\n"+ + "7th line\n"+ + "8th line\n"+ + "nineth line\n",+ "second commit");++ blames = git.blame("file", "master", -1, -1);+ assertEquals(3, blames.size());+ assertEquals(1, blames.get(0).getLines().size());+ assertEquals(7, blames.get(1).getLines().size());+ assertEquals(1, blames.get(2).getLines().size());+ assertEquals(blames.get(0).getCommit().getHash(), blames.get(2).getCommit().getHash());- blames = workGit.blame("file", "master", 5, 9);- assertEquals(2, blames.size());- assertEquals(4, blames.get(0).getLines().size());+ blames = git.blame("file", "master", 5, 9);+ assertEquals(2, blames.size());+ assertEquals(4, blames.get(0).getLines().size());- blames = workGit.blame("file", "master", 10, 9);- assertEquals(0, blames.size());+ blames = git.blame("file", "master", 10, 9);+ assertEquals(0, blames.size());- blames = workGit.blame("file", "master", 8, 100);- assertEquals(2, blames.size());+ blames = git.blame("file", "master", 8, 100);+ assertEquals(2, blames.size());- blames = workGit.blame("file", "master", 80, 100);- assertEquals(0, blames.size());+ blames = git.blame("file", "master", 80, 100);+ assertEquals(0, blames.size());- FileUtils.writeFile(file,- "first line\n"- + "nineth line\n");- workGit.add("file").commit("third commit", false, false);-- Git bareGit = new Git(new File(tempDir, "bare"));- bareGit.clone(workGit.repoDir().getAbsolutePath(), true, false, false, null);- blames = bareGit.blame("file", "master", -1, -1);-- assertEquals(1, blames.size());- } finally {- FileUtils.deleteDir(tempDir);- }+ addFileAndCommit(+ "file",+ "first line\n"+ + "nineth line\n",+ "third commit");++ Git bareGit = new Git(new File(tempDir, "bare"));+ bareGit.clone(git, true, false, false, null);+ blames = bareGit.blame("file", "master", -1, -1);++ assertEquals(1, blames.size());}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.javaindex 4ba8433d93..48a1258fb7 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java@@ -10,143 +10,101 @@ import org.junit.Test;import com.pmease.commons.git.AbstractGitTest;import com.pmease.commons.git.FileChangeWithDiffs;import com.pmease.commons.git.Git;-import com.pmease.commons.util.FileUtils;public class DiffCommandTest extends AbstractGitTest {@Testpublic void shouldGenerateDiffCorrectly() {- File tempDir = FileUtils.createTempDir();-- try {- Git workGit = new Git(new File(tempDir, "work"));- workGit.init(false);-- File readme = new File(workGit.repoDir(), "readme");- FileUtils.writeFile(readme, "readme");- workGit.add("readme");-- File dir = new File(workGit.repoDir(), "dir");- FileUtils.createDir(dir);- File file1 = new File(dir, "file1");- FileUtils.writeFile(file1,- "1st line of file1\n"- + "2nd line of file1\n"- + "3rd line of file1\n"- + "4th line of file1\n"- + "5th line of file1\n"- + "6th line of file1\n"- + "7th line of file1\n"- + "8th line of file1\n"- + "9th line of file1\n"- + "10th line of file1\n");-- File file2 = new File(dir, "file2");- FileUtils.writeFile(file2, "file2");-- workGit.add("dir").commit("add readme, dir/file1 and dir/file2", false, false);-- workGit.checkout("head", "dev");+ addFile("readme", "readme");+ createDir("dir");- FileUtils.writeFile(readme, "reame for dev");- workGit.add("readme");-- FileUtils.writeFile(file1,- "first line of file1\n"- + "2nd line of file1\n"- + "3rd line of file1\n"- + "4th line of file1\n"- + "5th line of file1\n"- + "6th line of file1\n"- + "7th line of file1\n"- + "some info here\n"- + "8th line of file1\n"- + "9th line of file1\n"- + "10th line of file1\n");-- workGit.add("dir/file1");-- workGit.remove("dir/file2");-- File file3 = new File(dir, "file3");- FileUtils.writeFile(file3, "file3");-- workGit.add("dir/file3");-- workGit.commit("modify readme, modify dir/file1, remove dir/file2, and add dir/file3", false, false);-- Git bareGit = new Git(new File(tempDir, "bare"));- bareGit.clone(workGit.repoDir().getAbsolutePath(), true, false, false, null);-- List<FileChangeWithDiffs> fileChanges = bareGit.diff("master..dev", null, 3);- assertEquals(fileChanges.size(), 4);-- fileChanges = bareGit.diff("master..dev", "dir", 3);- assertEquals(fileChanges.size(), 3);-- for (FileChangeWithDiffs fileChange: fileChanges) {- assertEquals(40, fileChange.getOldCommit().length());- assertEquals(40, fileChange.getNewCommit().length());- if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {- assertEquals(fileChange.getNewPath(), "dir/file1");- assertEquals(fileChange.getDiffChunks().get(0).toString(),- "@@ -1 +1 @@\n"- + "-1st line of file1\n"- + "+first line of file1\n"- + " 2nd line of file1\n"- + " 3rd line of file1\n"- + " 4th line of file1\n"- + " 5th line of file1\n"- + " 6th line of file1\n"- + " 7th line of file1\n"- + "+some info here\n"- + " 8th line of file1\n"- + " 9th line of file1\n"- + " 10th line of file1\n");- }- }+ addFile("dir/file1",+ "1st line of file1\n"+ + "2nd line of file1\n"+ + "3rd line of file1\n"+ + "4th line of file1\n"+ + "5th line of file1\n"+ + "6th line of file1\n"+ + "7th line of file1\n"+ + "8th line of file1\n"+ + "9th line of file1\n"+ + "10th line of file1\n");++ addFile("dir/file2", "file2");++ commit("add readme, dir/file1 and dir/file2");++ git.checkout("head", "dev");- } finally {- FileUtils.deleteDir(tempDir);- }+ addFile("readme", "readme for dev");+ addFile("dir/file1",+ "first line of file1\n"+ + "2nd line of file1\n"+ + "3rd line of file1\n"+ + "4th line of file1\n"+ + "5th line of file1\n"+ + "6th line of file1\n"+ + "7th line of file1\n"+ + "some info here\n"+ + "8th line of file1\n"+ + "9th line of file1\n"+ + "10th line of file1\n");+ rm("dir/file2");+ addFile("dir/file3", "file3");++ commit("modify readme, modify dir/file1, remove dir/file2, and add dir/file3");++ Git bareGit = new Git(new File(tempDir, "bare"));+ bareGit.clone(git, true, false, false, null);++ List<FileChangeWithDiffs> fileChanges = bareGit.diff("master..dev", null, 3);+ assertEquals(fileChanges.size(), 4);++ fileChanges = bareGit.diff("master..dev", "dir", 3);+ assertEquals(fileChanges.size(), 3);++ for (FileChangeWithDiffs fileChange: fileChanges) {+ assertEquals(40, fileChange.getOldCommit().length());+ assertEquals(40, fileChange.getNewCommit().length());+ if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {+ assertEquals(fileChange.getNewPath(), "dir/file1");+ assertEquals(fileChange.getDiffChunks().get(0).toString(),+ "@@ -1 +1 @@\n"+ + "-1st line of file1\n"+ + "+first line of file1\n"+ + " 2nd line of file1\n"+ + " 3rd line of file1\n"+ + " 4th line of file1\n"+ + " 5th line of file1\n"+ + " 6th line of file1\n"+ + " 7th line of file1\n"+ + "+some info here\n"+ + " 8th line of file1\n"+ + " 9th line of file1\n"+ + " 10th line of file1\n");+ }+ }}@Testpublic void shouldHandleRenameAndCopyCorrectly() {- File tempDir = FileUtils.createTempDir();-- try {- Git git = new Git(tempDir);- git.init(false);+ addFileAndCommit("a", "1111\n2222\n3333\n", "add a");++ addFile("a", "1111\n2222\n3333\n4444\n");+ addFile("a2", "1111\n2222\n3333\n4444\n");+ commit("copy a to a2");- FileUtils.writeFile(new File(tempDir, "a"), "1111\n2222\n3333\n");- git.add("a");- git.commit("add a", false, false);-- FileUtils.writeFile(new File(tempDir, "a"), "1111\n2222\n3333\n4444\n");- FileUtils.writeFile(new File(tempDir, "a2"), "1111\n2222\n3333\n4444\n");- git.add("a", "a2");- git.commit("copy a to a2", false, false);-- FileUtils.writeFile(new File(tempDir, "b"), "1111\n2222\n3333\n4444\n");- git.add("b");- git.commit("add b", false, false);+ addFileAndCommit("b", "1111\n2222\n3333\n4444\n", "add b");- FileUtils.writeFile(new File(tempDir, "b2"), "1111\n2222\n3333\n4444\n");- git.remove("b");- git.add("b2");- git.commit("move b to b2", false, false);+ rm("b");+ addFileAndCommit("b2", "1111\n2222\n3333\n4444\n", "move b to b2");- List<FileChangeWithDiffs> changes = git.diff("master~3..master", null, 4);- assertEquals("COPY\ta->a2", changes.get(1).toString());- assertEquals("COPY\ta->b2", changes.get(2).toString());-- changes = git.diff("master~1..master", null, 4);- assertEquals("RENAME\tb->b2", changes.get(0).toString());-- } finally {- FileUtils.deleteDir(tempDir);- }+ List<FileChangeWithDiffs> changes = git.diff("master~3..master", null, 4);+ assertEquals("COPY\ta->a2", changes.get(1).toString());+ assertEquals("COPY\ta->b2", changes.get(2).toString());++ changes = git.diff("master~1..master", null, 4);+ assertEquals("RENAME\tb->b2", changes.get(0).toString());}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/IsBinaryCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/IsBinaryCommandTest.javaindex 17abcfa035..8c6463b5f0 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/IsBinaryCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/IsBinaryCommandTest.java@@ -11,33 +11,22 @@ import java.io.OutputStream;import org.junit.Test;import com.pmease.commons.git.AbstractGitTest;-import com.pmease.commons.git.Git;-import com.pmease.commons.util.FileUtils;public class IsBinaryCommandTest extends AbstractGitTest {@Testpublic void test() throws IOException {- Git git = new Git(FileUtils.createTempDir());- try {- git.init(false);- File file = new File(git.repoDir(), "file");- FileUtils.writeFile(file, "hello world");- git.add("file").commit("initial commit", false, false);-- byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};- OutputStream os = new FileOutputStream(file);- os.write(bytes);- os.close();-- git.add("file").commit("second commit", false, false);-- assertFalse(git.isBinary("file", "master~1"));- assertTrue(git.isBinary("file", "master"));-- } finally {- FileUtils.deleteDir(git.repoDir());- }+ addFileAndCommit("file", "hello world", "initial commit");++ byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};+ OutputStream os = new FileOutputStream(new File(git.repoDir(), "file"));+ os.write(bytes);+ os.close();++ git.add("file").commit("second commit", false, false);++ assertFalse(git.isBinary("file", "master~1"));+ assertTrue(git.isBinary("file", "master"));}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.javaindex f4a058bf53..d5f2f75d11 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/IsTreeLinkCommandTest.java@@ -10,28 +10,24 @@ import org.junit.Test;import com.pmease.commons.git.AbstractGitTest;import com.pmease.commons.git.Git;import com.pmease.commons.util.ClassUtils;-import com.pmease.commons.util.FileUtils;import com.pmease.commons.util.ZipUtils;public class IsTreeLinkCommandTest extends AbstractGitTest {@Testpublic void test() throws IOException {- File tempDir = FileUtils.createTempDir();- try {- // Use a pre-built repo to test symbol link behaviors as Windows does not- // support symbol links.- try (InputStream is = ClassUtils.getResourceAsStream(IsTreeLinkCommandTest.class, "symlink_repo.zip");) {- ZipUtils.unzip(is, tempDir, null);-- Git git = new Git(tempDir);- Assert.assertFalse(git.isTreeLink("file_link", "master"));- Assert.assertFalse(git.isTreeLink("link_link", "master"));- Assert.assertFalse(git.isTreeLink("external_link", "master"));- Assert.assertTrue(git.isTreeLink("dir_link", "master"));- }- } finally {- FileUtils.deleteDir(tempDir);+ // Use a pre-built repo to test symbol link behaviors as Windows does not+ // support symbol links.+ File repoDir = new File(tempDir, "unzip");++ try (InputStream is = ClassUtils.getResourceAsStream(IsTreeLinkCommandTest.class, "symlink_repo.zip");) {+ ZipUtils.unzip(is, repoDir, null);++ Git git = new Git(repoDir);+ Assert.assertFalse(git.isTreeLink("file_link", "master"));+ Assert.assertFalse(git.isTreeLink("link_link", "master"));+ Assert.assertFalse(git.isTreeLink("external_link", "master"));+ Assert.assertTrue(git.isTreeLink("dir_link", "master"));}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.javaindex d55cda754e..2759514b6e 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java@@ -1,6 +1,5 @@package com.pmease.commons.git.command;-import java.io.File;import java.util.Collection;import org.junit.Assert;@@ -8,43 +7,20 @@ import org.junit.Test;import com.google.common.collect.Sets;import com.pmease.commons.git.AbstractGitTest;-import com.pmease.commons.git.Git;-import com.pmease.commons.util.FileUtils;public class ListChangedFilesCommandTest extends AbstractGitTest {@Testpublic void shouldListChangedFiles() {- Git git = new Git(FileUtils.createTempDir());- git.init(false);-- try {- FileUtils.touchFile(new File(git.repoDir(), "a"));- git.add("a");- git.commit("commit", false, false);-- FileUtils.touchFile(new File(git.repoDir(), "b"));- git.add("b");- git.commit("commit", false, false);-- FileUtils.touchFile(new File(git.repoDir(), "c"));- git.add("c");- git.commit("commit", false, false);-- FileUtils.touchFile(new File(git.repoDir(), "d"));- git.add("d");- git.commit("commit", false, false);-- FileUtils.writeFile(new File(git.repoDir(), "a"), "a");- git.add("a");- git.commit("commit", false, false);-- Collection<String> changedFiles = git.listChangedFiles("HEAD~4", "HEAD", null);-- Assert.assertTrue(changedFiles.containsAll(Sets.newHashSet("a", "b", "c", "d")));- } finally {- FileUtils.deleteDir(git.repoDir());- }+ addFileAndCommit("a", "", "commit");+ addFileAndCommit("b", "", "commit");+ addFileAndCommit("c", "", "commit");+ addFileAndCommit("d", "", "commit");+ addFileAndCommit("a", "a", "commit");++ Collection<String> changedFiles = git.listChangedFiles("HEAD~4", "HEAD", null);++ Assert.assertTrue(changedFiles.containsAll(Sets.newHashSet("a", "b", "c", "d")));}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListSubModulesCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListSubModulesCommandTest.javaindex 9a0d418062..6a0aef2905 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListSubModulesCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListSubModulesCommandTest.java@@ -16,44 +16,35 @@ public class ListSubModulesCommandTest extends AbstractGitTest {@Testpublic void test() throws IOException {- File tempDir = FileUtils.createTempDir();- try {- Git work = new Git(new File(tempDir, "work"));- Git module1 = new Git(new File(tempDir, "module1"));- Git module2 = new Git(new File(tempDir, "module2"));-- work.init(false);- module1.init(false);- module2.init(false);-- FileUtils.writeFile(new File(work.repoDir(), "readme"), "readme");- work.add("readme").commit("initial commit", false, false);- FileUtils.writeFile(new File(module1.repoDir(), "readme"), "readme");- module1.add("readme").commit("initial commit", false, false);- FileUtils.writeFile(new File(module2.repoDir(), "readme"), "readme");- module2.add("readme").commit("initial commit", false, false);-- work.addSubModule(module1.repoDir().getAbsolutePath(), "module1");- work.commit("add submodule1", false, false);-- Map<String, String> subModules = work.listSubModules("master");- assertEquals(module1.repoDir().getCanonicalPath(),- new File(subModules.get("module1")).getCanonicalPath());-- File dir = new File(work.repoDir(), "dir");- FileUtils.createDir(dir);-- work.addSubModule(module2.repoDir().getAbsolutePath(), "dir/module2");- work.commit("add submodule2", false, false);- subModules = work.listSubModules("master");- assertEquals(module1.repoDir().getCanonicalPath(),- new File(subModules.get("module1")).getCanonicalPath());- assertEquals(module2.repoDir().getCanonicalPath(),- new File(subModules.get("dir/module2")).getCanonicalPath());-- } finally {- FileUtils.deleteDir(tempDir);- }+ Git module1 = new Git(new File(tempDir, "module1"));+ Git module2 = new Git(new File(tempDir, "module2"));++ module1.init(false);+ module2.init(false);++ addFileAndCommit("readme", "readme", "initial commit");++ FileUtils.writeFile(new File(module1.repoDir(), "readme"), "readme");+ module1.add("readme").commit("initial commit", false, false);+ FileUtils.writeFile(new File(module2.repoDir(), "readme"), "readme");+ module2.add("readme").commit("initial commit", false, false);++ git.addSubModule(module1.repoDir().getAbsolutePath(), "module1");+ git.commit("add submodule1", false, false);++ Map<String, String> subModules = git.listSubModules("master");+ assertEquals(module1.repoDir().getCanonicalPath(),+ new File(subModules.get("module1")).getCanonicalPath());++ createDir("dir");++ git.addSubModule(module2.repoDir().getAbsolutePath(), "dir/module2");+ commit("add submodule2");+ subModules = git.listSubModules("master");+ assertEquals(module1.repoDir().getCanonicalPath(),+ new File(subModules.get("module1")).getCanonicalPath());+ assertEquals(module2.repoDir().getCanonicalPath(),+ new File(subModules.get("dir/module2")).getCanonicalPath());}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.javaindex f31982a414..831f354a60 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java@@ -18,74 +18,48 @@ public class ListTreeCommandTest extends AbstractGitTest {@Testpublic void shouldListTreeAndReadFileCorrectly() throws IOException {- File tempDir = FileUtils.createTempDir();-- try {- Git workGit = new Git(new File(tempDir, "work"));- workGit.init(false);-- FileUtils.touchFile(new File(workGit.repoDir(), "a"));- workGit.add("a");- workGit.commit("commit", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "b"));- workGit.add("b");- workGit.commit("commit", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "c"));- workGit.add("c");- workGit.commit("commit", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "d"));- workGit.add("d");- workGit.commit("commit", false, false);-- FileUtils.writeFile(new File(workGit.repoDir(), "a"), "a");- workGit.add("a");- workGit.commit("commit", false, false);-- FileUtils.createDir(new File(workGit.repoDir(), "dir"));- FileUtils.writeFile(new File(workGit.repoDir(), "dir/file"), "hello world");- workGit.add("dir/file");- workGit.commit("commit", false, false);-- Git moduleGit = new Git(new File(tempDir, "module"));- moduleGit.init(false);- FileUtils.writeFile(new File(moduleGit.repoDir(), "readme"), "readme");- moduleGit.add("readme").commit("commit", false, false);-- workGit.addSubModule(moduleGit.repoDir().getAbsolutePath(), "module");- workGit.commit("commit", false, false);+ addFileAndCommit("a", "", "commit");+ addFileAndCommit("b", "", "commit");+ addFileAndCommit("c", "", "commit");+ addFileAndCommit("d", "", "commit");+ addFileAndCommit("a", "a", "commit");+ createDir("dir");+ addFileAndCommit("dir/file", "hello world", "commit");++ Git moduleGit = new Git(new File(tempDir, "module"));+ moduleGit.init(false);+ FileUtils.writeFile(new File(moduleGit.repoDir(), "readme"), "readme");+ moduleGit.add("readme").commit("commit", false, false);++ git.addSubModule(moduleGit.repoDir().getAbsolutePath(), "module");+ git.commit("commit", false, false);- workGit.checkout("head", "dev");- workGit.remove("dir/file");- workGit.commit("commit", false, false);-- Git bareGit = new Git(new File(tempDir, "bare"));- bareGit.clone(workGit.repoDir().getAbsolutePath(), true, false, false, null);+ git.checkout("head", "dev");+ rm("dir/file");+ commit("commit");++ Git bareGit = new Git(new File(tempDir, "bare"));+ bareGit.clone(git, true, false, false, null);++ List<TreeNode> treeNodes = bareGit.listTree("master", null);- List<TreeNode> treeNodes = bareGit.listTree("master", null);-- assertEquals(7, treeNodes.size());- assertEquals("dir", treeNodes.get(5).getPath());- assertEquals("dir", treeNodes.get(5).getName());- assertEquals(FileMode.TREE, treeNodes.get(5).getMode());-- assertEquals(moduleGit.repoDir().getAbsolutePath(),- new File(new String(treeNodes.get(6).show(bareGit))).getCanonicalPath());-- TreeNode dirNode = treeNodes.get(5);- treeNodes = bareGit.listTree(dirNode.getRevision(), dirNode.getPath());-- assertEquals(1, treeNodes.size());- assertEquals("dir/file", treeNodes.get(0).getPath());- assertEquals("file", treeNodes.get(0).getName());-- TreeNode fileNode = treeNodes.get(0);- assertEquals("hello world", new String(fileNode.show(bareGit)));- } finally {- FileUtils.deleteDir(tempDir);- }+ assertEquals(7, treeNodes.size());+ assertEquals("dir", treeNodes.get(0).getPath());+ assertEquals("dir", treeNodes.get(0).getName());+ assertEquals(FileMode.TREE, treeNodes.get(0).getMode());++ assertEquals(moduleGit.repoDir().getAbsolutePath(),+ new File(new String(treeNodes.get(6).show(bareGit))).getCanonicalPath());++ TreeNode dirNode = treeNodes.get(0);+ treeNodes = bareGit.listTree(dirNode.getRevision(), dirNode.getPath());++ assertEquals(1, treeNodes.size());+ assertEquals("dir/file", treeNodes.get(0).getPath());+ assertEquals("file", treeNodes.get(0).getName());++ TreeNode fileNode = treeNodes.get(0);+ assertEquals("hello world", new String(fileNode.show(bareGit)));}}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.javaindex 47450bcde1..04636326d0 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java@@ -11,122 +11,78 @@ import com.pmease.commons.git.AbstractGitTest;import com.pmease.commons.git.Commit;import com.pmease.commons.git.FileChange;import com.pmease.commons.git.Git;-import com.pmease.commons.util.FileUtils;public class LogCommandTest extends AbstractGitTest {@Testpublic void shouldParseLogCorrectly() {- File tempDir = FileUtils.createTempDir();-- try {- Git workGit = new Git(new File(tempDir, "work"));- workGit.init(false);-- FileUtils.touchFile(new File(workGit.repoDir(), "a"));- workGit.add("a");- workGit.commit("add a", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "b"));- workGit.add("b");- workGit.commit("add b", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "c"));- workGit.add("c");- workGit.commit("add c", false, false);-- FileUtils.touchFile(new File(workGit.repoDir(), "d"));- workGit.add("d");- workGit.commit("add d", false, false);-- FileUtils.writeFile(new File(workGit.repoDir(), "a"), "a");- workGit.add("a");- workGit.commit("modify a", false, false);-- FileUtils.createDir(new File(workGit.repoDir(), "dir"));- FileUtils.writeFile(new File(workGit.repoDir(), "dir/file"), "hello world");- FileUtils.writeFile(new File(workGit.repoDir(), "dir/file2"), "hello world");- workGit.add("dir/file");- workGit.add("dir/file2");- workGit.commit("add dir/file\nadd dir/file to test files under a directory", false, false);-- workGit.checkout("head", "dev");- workGit.remove("dir/file");- workGit.commit("remove dir/file", false, false);-- Git bareGit = new Git(new File(tempDir, "bare"));- bareGit.clone(workGit.repoDir().getAbsolutePath(), true, false, false, null);+ addFileAndCommit("a", "", "add a");+ addFileAndCommit("b", "", "add b");+ addFileAndCommit("c", "", "add c");+ addFileAndCommit("d", "", "add d");+ addFileAndCommit("a", "a", "modify a");++ createDir("dir");+ addFile("dir/file", "hello world");+ addFile("dir/file2", "hello world");+ commit("add dir/file\nadd dir/file to test files under a directory");++ git.checkout("head", "dev");+ rm("dir/file");+ commit("remove dir/file");++ Git bareGit = new Git(new File(tempDir, "bare"));+ bareGit.clone(git, true, false, false, null);- bareGit.addNote("master", "hello\nworld");+ bareGit.addNote("master", "hello\nworld");- List<Commit> commits = bareGit.log(null, "master", null, 0, 0);- assertEquals(commits.size(), 6);- assertEquals(commits.get(0).getSubject(), "add dir/file");- assertEquals(commits.get(0).getMessage(), "add dir/file\nadd dir/file to test files under a directory");- assertEquals("hello\nworld", commits.get(0).getNote());- assertEquals(commits.get(0).getFileChanges().size(), 2);- assertEquals(commits.get(0).getFileChanges().get(0).getNewPath(), "dir/file");- assertEquals(commits.get(0).getFileChanges().get(0).getAction(), FileChange.Action.ADD);- assertEquals(commits.get(0).getParentHashes().size(), 1);- assertEquals(commits.get(0).getParentHashes().iterator().next(), commits.get(1).getHash());-- assertEquals(null, commits.get(1).getNote());-- workGit.checkout("master", null).remove("a").commit("remove a", false, false);- FileUtils.writeFile(new File(workGit.repoDir(), "dir/file2"), "file2");- workGit.add("dir/file2").commit("add dir/file2", false, false);- workGit.merge("dev", null, null, null, null);-- commits = workGit.log(null, "master", null, 0, 0);-- assertEquals(commits.get(0).getParentHashes().size(), 2);-- commits = workGit.log(null, "master", "a", 0, 0);- assertEquals(commits.size(), 3);-- commits = workGit.log("dev", "master", "dir", 0, 0);- assertEquals(commits.size(), 2);+ List<Commit> commits = bareGit.log(null, "master", null, 0, 0);+ assertEquals(commits.size(), 6);+ assertEquals(commits.get(0).getSubject(), "add dir/file");+ assertEquals(commits.get(0).getMessage(), "add dir/file\nadd dir/file to test files under a directory");+ assertEquals("hello\nworld", commits.get(0).getNote());+ assertEquals(commits.get(0).getFileChanges().size(), 2);+ assertEquals(commits.get(0).getFileChanges().get(0).getNewPath(), "dir/file");+ assertEquals(commits.get(0).getFileChanges().get(0).getAction(), FileChange.Action.ADD);+ assertEquals(commits.get(0).getParentHashes().size(), 1);+ assertEquals(commits.get(0).getParentHashes().iterator().next(), commits.get(1).getHash());++ assertEquals(null, commits.get(1).getNote());++ git.checkout("master", null).rm("a").commit("remove a", false, false);+ addFileAndCommit("dir/file2", "file2", "add dir/file2");+ git.merge("dev", null, null, null, null);++ commits = git.log(null, "master", null, 0, 0);++ assertEquals(commits.get(0).getParentHashes().size(), 2);++ commits = git.log(null, "master", "a", 0, 0);+ assertEquals(commits.size(), 3);++ commits = git.log("dev", "master", "dir", 0, 0);+ assertEquals(commits.size(), 2);- assertEquals(workGit.showRevision(commits.get(0).getHash()).getHash(), commits.get(0).getHash());- assertEquals(workGit.showRevision(commits.get(1).getHash()).getHash(), commits.get(1).getHash());- } finally {- FileUtils.deleteDir(tempDir);- }+ assertEquals(git.showRevision(commits.get(0).getHash()).getHash(), commits.get(0).getHash());+ assertEquals(git.showRevision(commits.get(1).getHash()).getHash(), commits.get(1).getHash());}@Testpublic void shouldHandleRenameAndCopyCorrectly() {- File tempDir = FileUtils.createTempDir();-- try {- Git git = new Git(tempDir);- git.init(false);+ addFileAndCommit("a", "1111\n2222\n3333\n", "add a");+ addFile("a", "1111\n2222\n3333\n4444\n");+ addFile("a2", "1111\n2222\n3333\n4444\n");+ commit("copy a to a2");- FileUtils.writeFile(new File(tempDir, "a"), "1111\n2222\n3333\n");- git.add("a");- git.commit("add a", false, false);-- FileUtils.writeFile(new File(tempDir, "a"), "1111\n2222\n3333\n4444\n");- FileUtils.writeFile(new File(tempDir, "a2"), "1111\n2222\n3333\n4444\n");- git.add("a", "a2");- git.commit("copy a to a2", false, false);-- FileUtils.writeFile(new File(tempDir, "b"), "1111\n2222\n3333\n4444\n");- git.add("b");- git.commit("add b", false, false);-- FileUtils.writeFile(new File(tempDir, "b2"), "1111\n2222\n3333\n4444\n");- git.remove("b");- git.add("b2");- git.commit("move b to b2", false, false);+ addFileAndCommit("b", "1111\n2222\n3333\n4444\n", "add b");++ rm("b");+ addFileAndCommit("b2", "1111\n2222\n3333\n4444\n", "move b to b2");- List<Commit> commits = git.log("master~3", "master", null, 0, 0);- FileChange change = commits.get(0).getFileChanges().get(0);- assertEquals("RENAME\tb->b2", change.toString());- change = commits.get(2).getFileChanges().get(1);- assertEquals("COPY\ta->a2", change.toString());- } finally {- FileUtils.deleteDir(tempDir);- }+ List<Commit> commits = git.log("master~3", "master", null, 0, 0);+ FileChange change = commits.get(0).getFileChanges().get(0);+ assertEquals("RENAME\tb->b2", change.toString());+ change = commits.get(2).getFileChanges().get(1);+ assertEquals("COPY\ta->a2", change.toString());}}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/NewRequestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/NewRequestPage.javaindex 9263ae5b4a..a45aece21f 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/NewRequestPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/NewRequestPage.java@@ -129,7 +129,7 @@ public class NewRequestPage extends RepositoryInfoPage implements CommitCommentssandbox.clone(target.getRepository().git(), false, true, true, pullRequest.getTarget().getName());sandbox.reset(null, null);- sandbox.fetch(source.getRepository().git(), null);+ sandbox.fetch(source.getRepository().git());if (sandbox.isAncestor(source.getHeadCommit(), target.getHeadCommit())) {CloseInfo closeInfo = new CloseInfo();