Skip to content

The Unified and Autonomous Development Platform

Add tests for git log and tree browse commands.

robin shine2b71eaaa56f1
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Commit.java b/commons.git/src/main/java/com/pmease/commons/git/Commit.java
index 8b4ce8c143..604e3e7cf3 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Commit.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Commit.java
@@ -7,6 +7,8 @@ import java.util.HashSet;
import javax.annotation.Nullable;
+import com.google.common.base.Objects;
+
public class Commit {
private final Date date;
@@ -61,4 +63,13 @@ public class Commit {
public Collection<String> getParentHashes() {
return Collections.unmodifiableCollection(parentHashes);
}
+
+ @Override
+ public String toString() {
+ return Objects.toStringHelper(this)
+ .add("hash", getHash())
+ .add("subject", getSubject())
+ .toString();
+ }
+
}
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.java
index 41b60f4ed6..dc8752bcb0 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
@@ -11,10 +11,9 @@ import com.pmease.commons.git.command.AddCommand;
import com.pmease.commons.git.command.CalcMergeBaseCommand;
import com.pmease.commons.git.command.CheckAncestorCommand;
import com.pmease.commons.git.command.CheckoutCommand;
+import com.pmease.commons.git.command.CloneCommand;
import com.pmease.commons.git.command.CommitCommand;
import com.pmease.commons.git.command.DeleteRefCommand;
-import com.pmease.commons.git.command.ReadFileCommand;
-import com.pmease.commons.git.command.ResolveCommitCommand;
import com.pmease.commons.git.command.InitCommand;
import com.pmease.commons.git.command.ListBranchesCommand;
import com.pmease.commons.git.command.ListChangedFilesCommand;
@@ -22,6 +21,9 @@ import com.pmease.commons.git.command.ListTagsCommand;
import com.pmease.commons.git.command.ListTreeCommand;
import com.pmease.commons.git.command.LogCommand;
import com.pmease.commons.git.command.MergeCommand;
+import com.pmease.commons.git.command.ReadFileCommand;
+import com.pmease.commons.git.command.RemoveCommand;
+import com.pmease.commons.git.command.ResolveCommitCommand;
import com.pmease.commons.git.command.UpdateRefCommand;
import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.GeneralException;
@@ -42,27 +44,32 @@ public class Git implements Serializable {
return repoDir;
}
- public void createBranch(String branchName, String commitHash) {
+ public Git createBranch(String branchName, String commitHash) {
if (new ListBranchesCommand(repoDir).call().contains(branchName))
throw new GeneralException("Branch %s already exists.", branchName);
new UpdateRefCommand(repoDir).refName("refs/heads/" + branchName).revision(commitHash)
.call();
+
+ return this;
}
- public void deleteBranch(String branchName) {
+ public Git deleteBranch(String branchName) {
new DeleteRefCommand(repoDir).refName("refs/heads/" + branchName).call();
+ return this;
}
- public void createTag(String tagName, String commitHash) {
+ public Git createTag(String tagName, String commitHash) {
if (new ListTagsCommand(repoDir).call().contains(tagName))
throw new GeneralException("Tag %s already exists.", tagName);
new UpdateRefCommand(repoDir).refName("refs/tags/" + tagName).revision(commitHash).call();
+ return this;
}
- public void deleteTag(String tagName) {
+ public Git deleteTag(String tagName) {
new DeleteRefCommand(repoDir).refName("refs/tags/" + tagName).call();
+ return this;
}
public Commit resolveCommit(String revision) {
@@ -73,38 +80,55 @@ public class Git implements Serializable {
return new ListTreeCommand(repoDir).revision(revision).path(path).recursive(recursive).call();
}
- public void init(boolean bare) {
+ public Git init(boolean bare) {
new InitCommand(repoDir).bare(bare).call();
+ return this;
}
- public void add(String path) {
+ public Git add(String path) {
new AddCommand(repoDir).addPath(path).call();
+ return this;
+ }
+
+ public Git remove(String path) {
+ new RemoveCommand(repoDir).removePath(path).call();
+ return this;
+ }
+
+ public Git clone(String from, boolean bare) {
+ new CloneCommand(repoDir).from(from).bare(bare).call();
+ return this;
}
- public void commit(String message, boolean amend) {
+ public Git commit(String message, boolean amend) {
new CommitCommand(repoDir).message(message).amend(amend).call();
+ return this;
}
public Collection<String> listChangedFiles(String fromRev, String toRev) {
return new ListChangedFilesCommand(repoDir).fromRev(fromRev).toRev(toRev).call();
}
- public void checkout(String revision, boolean newBranch) {
+ public Git checkout(String revision, boolean newBranch) {
new CheckoutCommand(repoDir).revision(revision).newBranch(newBranch).call();
+ return this;
}
- public void updateRef(String refName, String revision,
+ public Git updateRef(String refName, String revision,
@Nullable String oldRevision, @Nullable String reason) {
new UpdateRefCommand(repoDir).refName(refName).revision(revision).oldRevision(oldRevision)
.reason(reason).call();
+ return this;
}
- public void deleteRef(String refName) {
+ public Git deleteRef(String refName) {
new DeleteRefCommand(repoDir).refName(refName).call();
+ return this;
}
- public void merge(String revision) {
+ public Git merge(String revision) {
new MergeCommand(repoDir).revision(revision).call();
+ return this;
}
public String calcMergeBase(String rev1, String rev2) {
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.java
index dc388890f3..bc4374b69e 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
@@ -75,4 +75,10 @@ public class TreeNode implements Serializable {
public void setParent(@Nullable DirNode parent) {
this.parentNode = Optional.fromNullable(parent);
}
+
+ @Override
+ public String toString() {
+ return getPath();
+ }
+
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java
new file mode 100644
index 0000000000..fadfc2a3bf
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CloneCommand.java
@@ -0,0 +1,43 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.util.execution.Commandline;
+
+public class CloneCommand extends GitCommand<Void> {
+
+ private String from;
+
+ private boolean bare;
+
+ public CloneCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public CloneCommand from(String from) {
+ this.from = from;
+ return this;
+ }
+
+ public CloneCommand bare(boolean bare) {
+ this.bare = bare;
+ return this;
+ }
+
+ @Override
+ public Void call() {
+ Preconditions.checkNotNull(from, "from has to be specified.");
+
+ Commandline cmd = cmd().addArgs("clone");
+ if (bare)
+ cmd.addArgs("--bare");
+ cmd.addArgs(from);
+ cmd.addArgs(".");
+
+ cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+
+ return null;
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
index 4475ae7ed3..aaac895d21 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
@@ -63,8 +63,8 @@ public class ListTreeCommand extends GitCommand<List<TreeNode>> {
line = StringUtils.substringAfter(line, " ");
String hash = StringUtils.substringBefore(line, " ");
line = StringUtils.substringAfter(line, " ");
- String size = StringUtils.substringBefore(line, " ");
- String path = StringUtils.substringAfter(line, " ");
+ String size = StringUtils.substringBefore(line.trim(), "\t");
+ String path = StringUtils.substringAfter(line.trim(), "\t");
TreeNode treeNode;
if (type.equals("tree")) {
@@ -77,7 +77,7 @@ public class ListTreeCommand extends GitCommand<List<TreeNode>> {
}
}, errorLogger()).checkReturnCode();
-
+
return treeNodes;
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
index 9997196eb2..b14967f47f 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
@@ -52,7 +52,7 @@ public class LogCommand extends GitCommand<List<Commit>> {
public List<Commit> call() {
Commandline cmd = cmd();
cmd.addArgs("log",
- "--format=\"%B%n*** commit_message_end ***%nhash:%H%nauthor:%an%ncommitter:%cn%nparents:%P%ndate:%cd %n*** commit_end ***\"",
+ "--format=%B%n*** commit_message_end ***%nhash:%H%nauthor:%an%ncommitter:%cn%nparents:%P%ndate:%cd %n*** commit_end ***",
"--quiet", "--date=iso");
if (fromRevision != null) {
if (toRevision != null)
@@ -84,6 +84,8 @@ public class LogCommand extends GitCommand<List<Commit>> {
commitBuilder.setSubject(null);
commitBuilder.setBody(null);
commitMessageBlock[0] = true;
+ } else if (line.equals("*** commit_message_end ***")) {
+ commitMessageBlock[0] = false;
} else if (commitMessageBlock[0]) {
if (commitBuilder.getSubject() == null)
commitBuilder.setSubject(line);
@@ -91,19 +93,17 @@ public class LogCommand extends GitCommand<List<Commit>> {
commitBuilder.setBody(line);
else
commitBuilder.setBody(commitBuilder.getBody() + "\n" + line);
- } else if (line.equals("*** commit_message_end ***"))
- commitMessageBlock[0] = false;
- else if (line.startsWith("subject:"))
+ } else if (line.startsWith("subject:")) {
commitBuilder.setSubject(line.substring("subject:".length()));
- else if (line.startsWith("hash:"))
+ } else if (line.startsWith("hash:")) {
commitBuilder.setHash(line.substring("hash:".length()));
- else if (line.startsWith("author:"))
+ } else if (line.startsWith("author:")) {
commitBuilder.setAuthor(line.substring("author:".length()));
- else if (line.startsWith("committer:"))
+ } else if (line.startsWith("committer:")) {
commitBuilder.setCommitter(line.substring("committer:".length()));
- else if (line.startsWith("date:"))
- commitBuilder.setDate(dateFormatter.parseDateTime(line.substring("date:".length())).toDate());
- else if (line.startsWith("parents:")) {
+ } else if (line.startsWith("date:")) {
+ commitBuilder.setDate(dateFormatter.parseDateTime(line.substring("date:".length()).trim()).toDate());
+ } else if (line.startsWith("parents:")) {
for (String each: StringUtils.split(line.substring("parents:".length()), " "))
commitBuilder.getParentHashes().add(each);
}
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.java
new file mode 100644
index 0000000000..e37e5f4600
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java
@@ -0,0 +1,35 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.pmease.commons.util.execution.Commandline;
+
+public class RemoveCommand extends GitCommand<Void> {
+
+ private List<String> paths = new ArrayList<String>();
+
+ public RemoveCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public RemoveCommand removePath(String path) {
+ paths.add(path);
+ return this;
+ }
+
+ @Override
+ public Void call() {
+ Commandline cmd = cmd().addArgs("rm");
+
+ for (String path: paths) {
+ cmd.addArgs(path);
+ }
+
+ cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+
+ return null;
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ResolveCommitCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ResolveCommitCommand.java
index 787bff4c03..c5433717b3 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ResolveCommitCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ResolveCommitCommand.java
@@ -31,7 +31,7 @@ public class ResolveCommitCommand extends GitCommand<Commit> {
Preconditions.checkNotNull(revision, "revision has to be specified.");
Commandline cmd = cmd().addArgs("show",
- "--format=\"%B%n*** commit_message_end ***%nhash:%H%nauthor:%an%ncommitter:%cn%nparents:%P%ndate:%cd\"",
+ "--format=%B%n*** commit_message_end ***%nhash:%H%nauthor:%an%ncommitter:%cn%nparents:%P%ndate:%cd",
"--quiet", "--date=iso");
cmd.addArgs(revision);
diff --git a/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java
similarity index 94%
rename from commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java
rename to commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java
index bbac4e34bf..6522f0a2f2 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/ListChangedFilesCommandTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListChangedFilesCommandTest.java
@@ -1,4 +1,4 @@
-package com.pmease.commons.git;
+package com.pmease.commons.git.command;
import java.io.File;
import java.util.Collection;
@@ -7,6 +7,7 @@ import org.junit.Assert;
import org.junit.Test;
import com.google.common.collect.Sets;
+import com.pmease.commons.git.Git;
import com.pmease.commons.git.command.GitCommand;
import com.pmease.commons.util.FileUtils;
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.java
new file mode 100644
index 0000000000..966ce07329
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java
@@ -0,0 +1,84 @@
+package com.pmease.commons.git.command;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.pmease.commons.git.DirNode;
+import com.pmease.commons.git.FileNode;
+import com.pmease.commons.git.Git;
+import com.pmease.commons.git.TreeNode;
+import com.pmease.commons.util.FileUtils;
+
+public class ListTreeCommandTest {
+
+ @Test
+ public void shouldListTreeAndReadFileCorrectly() {
+ Assert.assertTrue(GitCommand.checkError() == null);
+ File tempDir = FileUtils.createTempDir();
+
+ Git workGit = new Git(new File(tempDir, "work"));
+ workGit.init(false);
+
+ try {
+ FileUtils.touchFile(new File(workGit.repoDir(), "a"));
+ workGit.add("a");
+ workGit.commit("commit", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "b"));
+ workGit.add("b");
+ workGit.commit("commit", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "c"));
+ workGit.add("c");
+ workGit.commit("commit", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "d"));
+ workGit.add("d");
+ workGit.commit("commit", false);
+
+ FileUtils.writeFile(new File(workGit.repoDir(), "a"), "a");
+ workGit.add("a");
+ workGit.commit("commit", 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);
+
+ workGit.checkout("dev", true);
+ workGit.remove("dir/file");
+ workGit.commit("commit", false);
+
+ Git bareGit = new Git(new File(tempDir, "bare"));
+ bareGit.clone(workGit.repoDir().getAbsolutePath(), true);
+
+ List<TreeNode> treeNodes = bareGit.listTree("master", null, false);
+
+ assertEquals(treeNodes.size(), 5);
+ assertEquals(treeNodes.get(4).getPath(), "dir");
+ assertEquals(treeNodes.get(4).getName(), "dir");
+ assertTrue(treeNodes.get(4) instanceof DirNode);
+
+ DirNode dirNode = (DirNode) treeNodes.get(4);
+ treeNodes = dirNode.listChildren();
+
+ assertEquals(treeNodes.size(), 1);
+ assertEquals(treeNodes.get(0).getPath(), "dir/file");
+ assertEquals(treeNodes.get(0).getName(), "file");
+
+ FileNode fileNode = (FileNode) treeNodes.get(0);
+ assertEquals(new String(fileNode.read()), "hello world");
+ assertEquals(fileNode.getParent().getName(), "dir");
+
+ } finally {
+ FileUtils.deleteDir(tempDir);
+ }
+ }
+
+}
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.java
new file mode 100644
index 0000000000..cf5d710283
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/command/LogCommandTest.java
@@ -0,0 +1,84 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.util.List;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import com.pmease.commons.git.Commit;
+import com.pmease.commons.git.Git;
+import com.pmease.commons.util.FileUtils;
+
+public class LogCommandTest {
+
+ @Test
+ public void shouldParseLogCorrectly() {
+ assertTrue(GitCommand.checkError() == null);
+ File tempDir = FileUtils.createTempDir();
+
+ Git workGit = new Git(new File(tempDir, "work"));
+ workGit.init(false);
+
+ try {
+ FileUtils.touchFile(new File(workGit.repoDir(), "a"));
+ workGit.add("a");
+ workGit.commit("add a", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "b"));
+ workGit.add("b");
+ workGit.commit("add b", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "c"));
+ workGit.add("c");
+ workGit.commit("add c", false);
+
+ FileUtils.touchFile(new File(workGit.repoDir(), "d"));
+ workGit.add("d");
+ workGit.commit("add d", false);
+
+ FileUtils.writeFile(new File(workGit.repoDir(), "a"), "a");
+ workGit.add("a");
+ workGit.commit("modify a", false);
+
+ FileUtils.createDir(new File(workGit.repoDir(), "dir"));
+ FileUtils.writeFile(new File(workGit.repoDir(), "dir/file"), "hello world");
+ workGit.add("dir/file");
+ workGit.commit("add dir/file\nadd dir/file to test files under a directory", false);
+
+ workGit.checkout("dev", true);
+ workGit.remove("dir/file");
+ workGit.commit("remove dir/file", false);
+
+ Git bareGit = new Git(new File(tempDir, "bare"));
+ bareGit.clone(workGit.repoDir().getAbsolutePath(), true);
+
+ List<Commit> commits = bareGit.log(null, "master", null, 0);
+ assertEquals(commits.size(), 6);
+ assertEquals(commits.get(0).getSubject(), "add dir/file");
+ assertEquals(commits.get(0).getBody(), "add dir/file to test files under a directory");
+
+ assertEquals(commits.get(0).getParentHashes().size(), 1);
+ assertEquals(commits.get(0).getParentHashes().iterator().next(), commits.get(1).getHash());
+
+ workGit.checkout("master", false).remove("a").commit("remove a", false);
+ FileUtils.writeFile(new File(workGit.repoDir(), "dir/file2"), "file2");
+ workGit.add("dir/file2").commit("add dir/file2", false);
+ workGit.merge("dev");
+
+ commits = workGit.log(null, "master", null, 0);
+
+ assertEquals(commits.get(0).getParentHashes().size(), 2);
+
+ commits = workGit.log(null, "master", "a", 0);
+ assertEquals(commits.size(), 3);
+
+ commits = workGit.log("dev", "master", "dir", 0);
+ assertEquals(commits.size(), 2);
+
+ } finally {
+ FileUtils.deleteDir(tempDir);
+ }
+ }
+
+}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/GeneralException.java b/commons.util/src/main/java/com/pmease/commons/util/GeneralException.java
index ddaf4587f8..8babbd9bca 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/GeneralException.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/GeneralException.java
@@ -7,4 +7,8 @@ public class GeneralException extends RuntimeException {
super(String.format(format, args));
}
+ public GeneralException(String message) {
+ super(message);
+ }
+
}