Skip to content

The Unified and Autonomous Development Platform

Add missing fields in commit object.

robin shineef0828cad8fb
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 fbb6533ce0..f5cd3ee5f6 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
@@ -11,58 +11,88 @@ import com.google.common.base.Objects;
public class Commit {
- private final Date date;
+ private final Date committerDate;
+
+ private final Date authorDate;
private final String author;
private final String committer;
+ private final String committerEmail;
+
+ private final String authorEmail;
+
private final String hash;
private final String subject;
private final String body;
+ private final String note;
+
private final List<String> parentHashes;
private final List<FileChange> fileChanges;
- public Commit(Date date, String author, String committer, String hash, String subject,
- String body, List<String> parentHashes, List<FileChange> fileChanges) {
- this.date = date;
+ public Commit(Date committerDate, Date authorDate, String author, String committer,
+ String authorEmail, String committerEmail, String hash, String subject,
+ @Nullable String body, String note, List<String> parentHashes,
+ List<FileChange> fileChanges) {
+ this.committerDate = committerDate;
+ this.authorDate = authorDate;
this.author = author;
this.committer = committer;
+ this.authorEmail = authorEmail;
+ this.committerEmail = committerEmail;
this.hash = hash;
this.subject = subject;
this.body = body;
+ this.note = note;
this.parentHashes = new ArrayList<>(parentHashes);
this.fileChanges = new ArrayList<>(fileChanges);
}
- public Date getDate() {
- return date;
- }
+ public Date getCommitterDate() {
+ return committerDate;
+ }
- public String getAuthor() {
- return author;
- }
+ public Date getAuthorDate() {
+ return authorDate;
+ }
- public String getCommitter() {
- return committer;
- }
+ public String getAuthor() {
+ return author;
+ }
- public String getHash() {
- return hash;
- }
+ public String getCommitter() {
+ return committer;
+ }
- public String getSubject() {
- return subject;
- }
+ public String getCommitterEmail() {
+ return committerEmail;
+ }
+
+ public String getAuthorEmail() {
+ return authorEmail;
+ }
+
+ public String getHash() {
+ return hash;
+ }
+
+ public String getSubject() {
+ return subject;
+ }
public @Nullable String getBody() {
return body;
}
+ public @Nullable String getNote() {
+ return note;
+ }
+
public List<String> getParentHashes() {
return Collections.unmodifiableList(parentHashes);
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FileChange.java b/commons.git/src/main/java/com/pmease/commons/git/FileChange.java
index b70731815d..68e84c3a3d 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FileChange.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/FileChange.java
@@ -10,9 +10,9 @@ public class FileChange implements Serializable {
private final Action action;
- public FileChange(String path, Action action) {
- this.path = path;
+ public FileChange(Action action, String path) {
this.action = action;
+ this.path = path;
}
public String getPath() {
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FileChangeWithDiffs.java b/commons.git/src/main/java/com/pmease/commons/git/FileChangeWithDiffs.java
index cb882e9c28..3015d7b739 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FileChangeWithDiffs.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/FileChangeWithDiffs.java
@@ -1,30 +1,45 @@
-package com.pmease.commons.git;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import com.pmease.commons.util.diff.DiffChunk;
-
-@SuppressWarnings("serial")
-public class FileChangeWithDiffs extends FileChange {
-
- private final boolean binary;
-
- private final List<DiffChunk> diffChunks;
-
- public FileChangeWithDiffs(String path, Action action, boolean binary, List<DiffChunk> diffChunks) {
- super(path, action);
-
- this.binary = binary;
- this.diffChunks = new ArrayList<>(diffChunks);
- }
-
- public boolean isBinary() {
- return binary;
- }
-
- public List<DiffChunk> getDiffChunks() {
- return diffChunks;
- }
-
-}
+package com.pmease.commons.git;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.pmease.commons.util.diff.DiffChunk;
+
+@SuppressWarnings("serial")
+public class FileChangeWithDiffs extends FileChange {
+
+ private final boolean binary;
+
+ private final String commitHash1;
+
+ private final String commitHash2;
+
+ private final List<DiffChunk> diffChunks;
+
+ public FileChangeWithDiffs(Action action, String path, boolean binary,
+ String commitHash1, String commitHash2, List<DiffChunk> diffChunks) {
+ super(action, path);
+
+ this.binary = binary;
+ this.commitHash1 = commitHash1;
+ this.commitHash2 = commitHash2;
+ this.diffChunks = new ArrayList<>(diffChunks);
+ }
+
+ public boolean isBinary() {
+ return binary;
+ }
+
+ public List<DiffChunk> getDiffChunks() {
+ return diffChunks;
+ }
+
+ public String getCommitHash1() {
+ return commitHash1;
+ }
+
+ public String getCommitHash2() {
+ return commitHash2;
+ }
+
+}
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 ef5c365c6e..1745714a2e 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
@@ -1,171 +1,177 @@
-package com.pmease.commons.git;
-
-import java.io.File;
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.List;
-
-import javax.annotation.Nullable;
-
-import com.google.common.base.Preconditions;
-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.DiffCommand;
-import com.pmease.commons.git.command.InitCommand;
-import com.pmease.commons.git.command.ListBranchesCommand;
-import com.pmease.commons.git.command.ListChangedFilesCommand;
-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.UpdateRefCommand;
-import com.pmease.commons.util.FileUtils;
-import com.pmease.commons.util.GeneralException;
-
-@SuppressWarnings("serial")
-public class Git implements Serializable {
-
- private final File repoDir;
-
- public Git(File repoDir) {
- this.repoDir = repoDir;
-
- if (!repoDir.exists())
- FileUtils.createDir(repoDir);
- }
-
- public File repoDir() {
- return repoDir;
- }
-
- 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 Git deleteBranch(String branchName) {
- new DeleteRefCommand(repoDir).refName("refs/heads/" + branchName).call();
- return this;
- }
-
- 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 Git deleteTag(String tagName) {
- new DeleteRefCommand(repoDir).refName("refs/tags/" + tagName).call();
- return this;
- }
-
- public Commit resolveCommit(String revision) {
- List<Commit> commits = new LogCommand(repoDir).toRev(revision).maxCommits(1).call();
- Preconditions.checkState(commits.size() == 1);
- return commits.get(0);
- }
-
- public List<TreeNode> listTree(String revision, @Nullable String path, boolean recursive) {
- return new ListTreeCommand(repoDir).revision(revision).path(path).recursive(recursive).call();
- }
-
- public Git init(boolean bare) {
- new InitCommand(repoDir).bare(bare).call();
- return this;
- }
-
- 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 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 Git checkout(String revision, boolean newBranch) {
- new CheckoutCommand(repoDir).revision(revision).newBranch(newBranch).call();
- return this;
- }
-
- 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 Git deleteRef(String refName) {
- new DeleteRefCommand(repoDir).refName(refName).call();
- return this;
- }
-
- public Git merge(String revision) {
- new MergeCommand(repoDir).revision(revision).call();
- return this;
- }
-
- public String calcMergeBase(String rev1, String rev2) {
- return new CalcMergeBaseCommand(repoDir).rev1(rev1).rev2(rev2).call();
- }
-
- public boolean checkAncestor(String ancestor, String descendant) {
- return new CheckAncestorCommand(repoDir).ancestor(ancestor).descendant(descendant).call();
- }
-
- public Collection<String> listBranches() {
- return new ListBranchesCommand(repoDir).call();
- }
-
- public Collection<String> listTags() {
- return new ListTagsCommand(repoDir).call();
- }
-
- public byte[] readFile(String revision, String path) {
- return new ReadFileCommand(repoDir).revision(revision).path(path).call();
- }
-
- public List<Commit> log(@Nullable String fromRev, @Nullable String toRev,
- @Nullable String path, int maxCommits) {
- return new LogCommand(repoDir).fromRev(fromRev).toRev(toRev)
- .path(path).maxCommits(maxCommits).call();
- }
-
- public Commit retrieveLastCommmit(String revision, @Nullable String path) {
- return log(null, revision, path, 1).get(0);
- }
-
- public List<FileChangeWithDiffs> diff(String fromRev, String toRev, @Nullable String path) {
- return new DiffCommand(repoDir).fromRev(fromRev).toRev(toRev).path(path).call();
- }
-
-}
+package com.pmease.commons.git;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.git.command.AddCommand;
+import com.pmease.commons.git.command.AddNoteCommand;
+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.DiffCommand;
+import com.pmease.commons.git.command.InitCommand;
+import com.pmease.commons.git.command.ListBranchesCommand;
+import com.pmease.commons.git.command.ListChangedFilesCommand;
+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.UpdateRefCommand;
+import com.pmease.commons.util.FileUtils;
+import com.pmease.commons.util.GeneralException;
+
+@SuppressWarnings("serial")
+public class Git implements Serializable {
+
+ private final File repoDir;
+
+ public Git(File repoDir) {
+ this.repoDir = repoDir;
+
+ if (!repoDir.exists())
+ FileUtils.createDir(repoDir);
+ }
+
+ public File repoDir() {
+ return repoDir;
+ }
+
+ 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 Git deleteBranch(String branchName) {
+ new DeleteRefCommand(repoDir).refName("refs/heads/" + branchName).call();
+ return this;
+ }
+
+ 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 Git deleteTag(String tagName) {
+ new DeleteRefCommand(repoDir).refName("refs/tags/" + tagName).call();
+ return this;
+ }
+
+ public Commit resolveCommit(String revision) {
+ List<Commit> commits = new LogCommand(repoDir).toRev(revision).maxCommits(1).call();
+ Preconditions.checkState(commits.size() == 1);
+ return commits.get(0);
+ }
+
+ public List<TreeNode> listTree(String revision, @Nullable String path, boolean recursive) {
+ return new ListTreeCommand(repoDir).revision(revision).path(path).recursive(recursive).call();
+ }
+
+ public Git init(boolean bare) {
+ new InitCommand(repoDir).bare(bare).call();
+ return this;
+ }
+
+ 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 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 Git checkout(String revision, boolean newBranch) {
+ new CheckoutCommand(repoDir).revision(revision).newBranch(newBranch).call();
+ return this;
+ }
+
+ 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 Git deleteRef(String refName) {
+ new DeleteRefCommand(repoDir).refName(refName).call();
+ return this;
+ }
+
+ public Git merge(String revision) {
+ new MergeCommand(repoDir).revision(revision).call();
+ return this;
+ }
+
+ public String calcMergeBase(String rev1, String rev2) {
+ return new CalcMergeBaseCommand(repoDir).rev1(rev1).rev2(rev2).call();
+ }
+
+ public boolean checkAncestor(String ancestor, String descendant) {
+ return new CheckAncestorCommand(repoDir).ancestor(ancestor).descendant(descendant).call();
+ }
+
+ public Collection<String> listBranches() {
+ return new ListBranchesCommand(repoDir).call();
+ }
+
+ public Collection<String> listTags() {
+ return new ListTagsCommand(repoDir).call();
+ }
+
+ public byte[] readFile(String revision, String path) {
+ return new ReadFileCommand(repoDir).revision(revision).path(path).call();
+ }
+
+ public List<Commit> log(@Nullable String fromRev, @Nullable String toRev,
+ @Nullable String path, int maxCommits) {
+ return new LogCommand(repoDir).fromRev(fromRev).toRev(toRev)
+ .path(path).maxCommits(maxCommits).call();
+ }
+
+ public Commit retrieveLastCommmit(String revision, @Nullable String path) {
+ return log(null, revision, path, 1).get(0);
+ }
+
+ public List<FileChangeWithDiffs> diff(String fromRev, String toRev, @Nullable String path, int contextLines) {
+ return new DiffCommand(repoDir).fromRev(fromRev).toRev(toRev).contextLines(contextLines).path(path).call();
+ }
+
+ public Git addNote(String object, String message) {
+ new AddNoteCommand(repoDir).object(object).message(message).call();
+ return this;
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java
new file mode 100644
index 0000000000..c820374f79
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AddNoteCommand.java
@@ -0,0 +1,39 @@
+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 AddNoteCommand extends GitCommand<Void> {
+
+ private String object;
+
+ private String message;
+
+ public AddNoteCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public AddNoteCommand object(String object) {
+ this.object = object;
+ return this;
+ }
+
+ public AddNoteCommand message(String message) {
+ this.message = message;
+ return this;
+ }
+
+ @Override
+ public Void call() {
+ Preconditions.checkNotNull(object, "object should be specified.");
+ Preconditions.checkNotNull(message, "message should be specified.");
+ Commandline cmd = cmd().addArgs("notes", "add", "-m", message, object);
+
+ cmd.execute(debugLogger(), errorLogger()).checkReturnCode();
+
+ return null;
+ }
+
+}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
index 5926b22a6b..0e41ff6d05 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java
@@ -1,104 +1,123 @@
-package com.pmease.commons.git.command;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-
-import com.google.common.base.Preconditions;
-import com.pmease.commons.git.FileChange;
-import com.pmease.commons.git.FileChange.Action;
-import com.pmease.commons.git.FileChangeWithDiffs;
-import com.pmease.commons.util.diff.DiffUtils;
-import com.pmease.commons.util.execution.Commandline;
-import com.pmease.commons.util.execution.LineConsumer;
-
-public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
-
- private String fromRev;
-
- private String toRev;
-
- private String path;
-
- public DiffCommand(File repoDir) {
- super(repoDir);
- }
-
- public DiffCommand fromRev(String fromRev) {
- this.fromRev = fromRev;
- return this;
- }
-
- public DiffCommand toRev(String toRev) {
- this.toRev = toRev;
- return this;
- }
-
- public DiffCommand path(String path) {
- this.path = path;
- return this;
- }
-
- @Override
- public List<FileChangeWithDiffs> call() {
- Preconditions.checkNotNull(fromRev, "fromRev has to be specified.");
- Preconditions.checkNotNull(toRev, "toRev has to be specified.");
-
- Commandline cmd = cmd();
- cmd.addArgs("diff", fromRev + ".." + toRev);
- if (path != null)
- cmd.addArgs("--", path);
-
- final List<FileChangeWithDiffs> fileChanges = new ArrayList<FileChangeWithDiffs>();
- final ChangeBuilder changeBuilder = new ChangeBuilder();
-
- cmd.execute(new LineConsumer() {
-
- @Override
- public void consume(String line) {
- if (line.startsWith("diff --git")) {
- if (changeBuilder.path != null)
- fileChanges.add(changeBuilder.buildFileChange());
-
- changeBuilder.action = FileChange.Action.MODIFY;
- changeBuilder.binary = false;
- changeBuilder.diffLines.clear();
-
- changeBuilder.path = StringUtils.substringBefore(line.substring("diff --git a/".length()), " ");
- } else if (line.startsWith("deleted file")) {
- changeBuilder.action = FileChange.Action.DELETE;
- } else if (line.startsWith("new file")) {
- changeBuilder.action = FileChange.Action.ADD;
- } else if (line.startsWith("Binary files")) {
- changeBuilder.binary = true;
- } else if (line.startsWith("@@") || line.startsWith("+") || line.startsWith("-")
- || line.startsWith(" ") || line.startsWith("\\")) {
- changeBuilder.diffLines.add(line);
- }
-
- }
-
- }, errorLogger()).checkReturnCode();
-
- if (changeBuilder.path != null)
- fileChanges.add(changeBuilder.buildFileChange());
-
- return fileChanges;
- }
-
- private static class ChangeBuilder {
- private Action action;
-
- private String path;
-
- private boolean binary;
-
- private List<String> diffLines = new ArrayList<>();
-
- private FileChangeWithDiffs buildFileChange() {
- return new FileChangeWithDiffs(path, action, binary, DiffUtils.parseUnifiedDiff(diffLines));
- }
- }
-}
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.git.FileChange;
+import com.pmease.commons.git.FileChange.Action;
+import com.pmease.commons.git.FileChangeWithDiffs;
+import com.pmease.commons.util.diff.DiffUtils;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
+
+ private String fromRev;
+
+ private String toRev;
+
+ private String path;
+
+ private int contextLines;
+
+ public DiffCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public DiffCommand fromRev(String fromRev) {
+ this.fromRev = fromRev;
+ return this;
+ }
+
+ public DiffCommand toRev(String toRev) {
+ this.toRev = toRev;
+ return this;
+ }
+
+ public DiffCommand path(String path) {
+ this.path = path;
+ return this;
+ }
+
+ public DiffCommand contextLines(int contextLines) {
+ this.contextLines = contextLines;
+ return this;
+ }
+
+ @Override
+ public List<FileChangeWithDiffs> call() {
+ Preconditions.checkNotNull(fromRev, "fromRev has to be specified.");
+ Preconditions.checkNotNull(toRev, "toRev has to be specified.");
+
+ Commandline cmd = cmd();
+ cmd.addArgs("diff", fromRev + ".." + toRev, "--full-index");
+ if (contextLines != 0)
+ cmd.addArgs("--unified=" + contextLines);
+ if (path != null)
+ cmd.addArgs("--", path);
+
+ final List<FileChangeWithDiffs> fileChanges = new ArrayList<FileChangeWithDiffs>();
+ final ChangeBuilder changeBuilder = new ChangeBuilder();
+
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ if (line.startsWith("diff --git")) {
+ if (changeBuilder.path != null)
+ fileChanges.add(changeBuilder.buildFileChange());
+
+ changeBuilder.action = FileChange.Action.MODIFY;
+ changeBuilder.binary = false;
+ changeBuilder.diffLines.clear();
+
+ changeBuilder.path = StringUtils.substringBefore(line.substring("diff --git a/".length()), " ");
+ } else if (line.startsWith("deleted file")) {
+ changeBuilder.action = FileChange.Action.DELETE;
+ } else if (line.startsWith("new file")) {
+ changeBuilder.action = FileChange.Action.ADD;
+ } else if (line.startsWith("Binary files")) {
+ changeBuilder.binary = true;
+ } else if (line.startsWith("index ")) {
+ line = line.substring("index ".length());
+ changeBuilder.commitHash1 = StringUtils.substringBefore(line, "..");
+ changeBuilder.commitHash2 = StringUtils.substringAfter(line, "..");
+ changeBuilder.commitHash2 = StringUtils.substringBefore(changeBuilder.commitHash2, " ");
+ } else if (line.startsWith("@@") || line.startsWith("+") || line.startsWith("-")
+ || line.startsWith(" ") || line.startsWith("\\")) {
+ changeBuilder.diffLines.add(line);
+ }
+
+ }
+
+ }, errorLogger()).checkReturnCode();
+
+ if (changeBuilder.path != null)
+ fileChanges.add(changeBuilder.buildFileChange());
+
+ return fileChanges;
+ }
+
+ private static class ChangeBuilder {
+ private Action action;
+
+ private String path;
+
+ private boolean binary;
+
+ private String commitHash1;
+
+ private String commitHash2;
+
+ private List<String> diffLines = new ArrayList<>();
+
+ private FileChangeWithDiffs buildFileChange() {
+ return new FileChangeWithDiffs(action, path, binary, commitHash1, commitHash2,
+ DiffUtils.parseUnifiedDiff(diffLines));
+ }
+ }
+}
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 f6ebedd50a..e2ad284672 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
@@ -1,167 +1,195 @@
-package com.pmease.commons.git.command;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
-
-import com.pmease.commons.git.Commit;
-import com.pmease.commons.git.FileChange;
-import com.pmease.commons.util.execution.Commandline;
-import com.pmease.commons.util.execution.LineConsumer;
-
-public class LogCommand extends GitCommand<List<Commit>> {
-
- private final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z");
-
- private String fromRev;
-
- private String toRev;
-
- private String path;
-
- private int maxCommits;
-
- public LogCommand(File repoDir) {
- super(repoDir);
- }
-
- public LogCommand fromRev(String fromRev) {
- this.fromRev = fromRev;
- return this;
- }
-
- public LogCommand toRev(String toRev) {
- this.toRev = toRev;
- return this;
- }
-
- public LogCommand path(String path) {
- this.path = path;
- return this;
- }
-
- public LogCommand maxCommits(int maxCommits) {
- this.maxCommits = maxCommits;
- return this;
- }
-
- @Override
- public List<Commit> call() {
- Commandline cmd = cmd();
- cmd.addArgs("log",
- "--format=*** commit_begin ***%n%B%n*** commit_message_end ***%nhash:%H%nauthor:%an%ncommitter:%cn%nparents:%P%ndate:%cd %n*** commit_info_end ***",
- "--name-status", "--no-renames", "--date=iso");
- if (fromRev != null) {
- if (toRev != null)
- cmd.addArgs(fromRev + ".." + toRev);
- else
- cmd.addArgs(fromRev + "..");
- } else if (toRev != null) {
- cmd.addArgs(toRev);
- }
-
- if (maxCommits != 0)
- cmd.addArgs("-" + maxCommits);
-
- if (path != null)
- cmd.addArgs("--", path);
-
- final List<Commit> commits = new ArrayList<>();
-
- final CommitBuilder commitBuilder = new CommitBuilder();
-
- final boolean[] commitMessageBlock = new boolean[1];
- final boolean[] fileChangesBlock = new boolean[1];
-
- cmd.execute(new LineConsumer() {
-
- @Override
- public void consume(String line) {
- if (line.equals("*** commit_begin ***")) {
- if (commitBuilder.hash != null) {
- commits.add(commitBuilder.build());
- commitBuilder.parentHashes.clear();
- commitBuilder.fileChanges.clear();
- commitBuilder.subject = null;
- commitBuilder.body = null;
- }
- commitMessageBlock[0] = true;
- fileChangesBlock[0] = false;
- } else if (line.equals("*** commit_message_end ***")) {
- commitMessageBlock[0] = false;
- } else if (line.equals("*** commit_info_end ***")) {
- fileChangesBlock[0] = true;
- } else if (commitMessageBlock[0]) {
- if (commitBuilder.subject == null)
- commitBuilder.subject = line;
- else if (commitBuilder.body == null)
- commitBuilder.body = line;
- else
- commitBuilder.body = commitBuilder.body + "\n" + line;
- } else if (fileChangesBlock[0]) {
- FileChange.Action action = null;
- if (line.startsWith("A"))
- action = FileChange.Action.ADD;
- else if (line.startsWith("M"))
- action = FileChange.Action.MODIFY;
- else if (line.startsWith("D"))
- action = FileChange.Action.DELETE;
-
- if (action != null) {
- String path = StringUtils.substringAfter(line, "\t").trim();
- FileChange fileChange = new FileChange(path, action);
- commitBuilder.fileChanges.add(fileChange);
- }
- } else if (line.startsWith("subject:")) {
- commitBuilder.subject = line.substring("subject:".length());
- } else if (line.startsWith("hash:")) {
- commitBuilder.hash = line.substring("hash:".length());
- } else if (line.startsWith("author:")) {
- commitBuilder.author = line.substring("author:".length());
- } else if (line.startsWith("committer:")) {
- commitBuilder.committer = line.substring("committer:".length());
- } else if (line.startsWith("date:")) {
- commitBuilder.date = dateFormatter.parseDateTime(line.substring("date:".length()).trim()).toDate();
- } else if (line.startsWith("parents:")) {
- for (String each: StringUtils.split(line.substring("parents:".length()), " "))
- commitBuilder.parentHashes.add(each);
- }
- }
-
- }, errorLogger()).checkReturnCode();
-
- if (commitBuilder.hash != null)
- commits.add(commitBuilder.build());
-
- return commits;
- }
-
- private static class CommitBuilder {
-
- private Date date;
-
- private String author;
-
- private String committer;
-
- private String hash;
-
- private String subject;
-
- private String body;
-
- private List<String> parentHashes = new ArrayList<>();
-
- private List<FileChange> fileChanges = new ArrayList<>();
-
- private Commit build() {
- return new Commit(date, author, committer, hash, subject.trim(),
- body!=null?body.trim():null, parentHashes, fileChanges);
- }
- }
-}
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import com.pmease.commons.git.Commit;
+import com.pmease.commons.git.FileChange;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class LogCommand extends GitCommand<List<Commit>> {
+
+ private final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z");
+
+ private String fromRev;
+
+ private String toRev;
+
+ private String path;
+
+ private int maxCommits;
+
+ public LogCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public LogCommand fromRev(String fromRev) {
+ this.fromRev = fromRev;
+ return this;
+ }
+
+ public LogCommand toRev(String toRev) {
+ this.toRev = toRev;
+ return this;
+ }
+
+ public LogCommand path(String path) {
+ this.path = path;
+ return this;
+ }
+
+ public LogCommand maxCommits(int maxCommits) {
+ this.maxCommits = maxCommits;
+ return this;
+ }
+
+ @Override
+ public List<Commit> call() {
+ Commandline cmd = cmd();
+ cmd.addArgs("log",
+ "--format=*** commit_begin ***%n%B%n*** commit_message_end ***%n%N"
+ + "*** commit_note_end ***%nhash:%H%nauthor:%aN%nauthorEmail:%aE%n"
+ + "committerEmail:%cE%ncommitter:%cN%nparents:%P%ncommitterDate:%cd %n"
+ + "authorDate:%ad %n*** commit_info_end ***",
+ "--name-status", "--no-renames", "--date=iso");
+ if (fromRev != null) {
+ if (toRev != null)
+ cmd.addArgs(fromRev + ".." + toRev);
+ else
+ cmd.addArgs(fromRev + "..");
+ } else if (toRev != null) {
+ cmd.addArgs(toRev);
+ }
+
+ if (maxCommits != 0)
+ cmd.addArgs("-" + maxCommits);
+
+ if (path != null)
+ cmd.addArgs("--", path);
+
+ final List<Commit> commits = new ArrayList<>();
+
+ final CommitBuilder commitBuilder = new CommitBuilder();
+
+ final boolean[] commitNoteBlock = new boolean[1];
+ final boolean[] commitMessageBlock = new boolean[1];
+ final boolean[] fileChangesBlock = new boolean[1];
+
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ if (line.equals("*** commit_begin ***")) {
+ if (commitBuilder.hash != null) {
+ commits.add(commitBuilder.build());
+ commitBuilder.parentHashes.clear();
+ commitBuilder.fileChanges.clear();
+ commitBuilder.subject = null;
+ commitBuilder.body = null;
+ commitBuilder.note = null;
+ }
+ commitMessageBlock[0] = true;
+ fileChangesBlock[0] = false;
+ } else if (line.equals("*** commit_message_end ***")) {
+ commitMessageBlock[0] = false;
+ commitNoteBlock[0] = true;
+ } else if (line.equals("*** commit_note_end ***")) {
+ commitNoteBlock[0] = false;
+ } else if (line.equals("*** commit_info_end ***")) {
+ fileChangesBlock[0] = true;
+ } else if (commitMessageBlock[0]) {
+ if (commitBuilder.subject == null)
+ commitBuilder.subject = line;
+ else if (commitBuilder.body == null)
+ commitBuilder.body = line;
+ else
+ commitBuilder.body += "\n" + line;
+ } else if (commitNoteBlock[0]) {
+ if (commitBuilder.note == null)
+ commitBuilder.note = line;
+ else
+ commitBuilder.note += "\n" + line;
+ } else if (fileChangesBlock[0]) {
+ FileChange.Action action = null;
+ if (line.startsWith("A"))
+ action = FileChange.Action.ADD;
+ else if (line.startsWith("M"))
+ action = FileChange.Action.MODIFY;
+ else if (line.startsWith("D"))
+ action = FileChange.Action.DELETE;
+
+ if (action != null) {
+ String path = StringUtils.substringAfter(line, "\t").trim();
+ FileChange fileChange = new FileChange(action, path);
+ commitBuilder.fileChanges.add(fileChange);
+ }
+ } else if (line.startsWith("subject:")) {
+ commitBuilder.subject = line.substring("subject:".length());
+ } else if (line.startsWith("hash:")) {
+ commitBuilder.hash = line.substring("hash:".length());
+ } else if (line.startsWith("author:")) {
+ commitBuilder.author = line.substring("author:".length());
+ } else if (line.startsWith("committer:")) {
+ commitBuilder.committer = line.substring("committer:".length());
+ } else if (line.startsWith("authorDate:")) {
+ commitBuilder.authorDate = dateFormatter.parseDateTime(line.substring("authorDate:".length()).trim()).toDate();
+ } else if (line.startsWith("committerDate:")) {
+ commitBuilder.committerDate = dateFormatter.parseDateTime(line.substring("committerDate:".length()).trim()).toDate();
+ } else if (line.startsWith("authorEmail:")) {
+ commitBuilder.authorEmail = line.substring("authorEmail:".length());
+ } else if (line.startsWith("committerEmail:")) {
+ commitBuilder.committerEmail = line.substring("committerEmail:".length());
+ } else if (line.startsWith("parents:")) {
+ for (String each: StringUtils.split(line.substring("parents:".length()), " "))
+ commitBuilder.parentHashes.add(each);
+ }
+ }
+
+ }, errorLogger()).checkReturnCode();
+
+ if (commitBuilder.hash != null)
+ commits.add(commitBuilder.build());
+
+ return commits;
+ }
+
+ private static class CommitBuilder {
+
+ private Date committerDate;
+
+ private Date authorDate;
+
+ private String author;
+
+ private String committer;
+
+ private String authorEmail;
+
+ private String committerEmail;
+
+ private String hash;
+
+ private String subject;
+
+ private String body;
+
+ private String note;
+
+ private List<String> parentHashes = new ArrayList<>();
+
+ private List<FileChange> fileChanges = new ArrayList<>();
+
+ private Commit build() {
+ return new Commit(committerDate, authorDate, author, committer, authorEmail,
+ committerEmail, hash, subject.trim(), StringUtils.isNotBlank(body)?body.trim():null,
+ StringUtils.isNotBlank(note)?note.trim():null, parentHashes, fileChanges);
+ }
+ }
+}
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.java
index 160bc98368..2c8a315bdf 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
@@ -1,113 +1,115 @@
-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.Test;
-
-import com.pmease.commons.git.FileChangeWithDiffs;
-import com.pmease.commons.git.Git;
-import com.pmease.commons.util.FileUtils;
-
-public class DiffCommandTest {
-
- @Test
- public void shouldGenerateDiffCorrectly() {
- assertTrue(GitCommand.checkError() == null);
- 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);
-
- workGit.checkout("dev", true);
-
- 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);
-
- Git bareGit = new Git(new File(tempDir, "bare"));
- bareGit.clone(workGit.repoDir().getAbsolutePath(), true);
-
- List<FileChangeWithDiffs> fileChanges = bareGit.diff("master", "dev", null);
- assertEquals(fileChanges.size(), 4);
-
- fileChanges = bareGit.diff("master", "dev", "dir");
- assertEquals(fileChanges.size(), 3);
-
- for (FileChangeWithDiffs fileChange: fileChanges) {
- if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {
- assertEquals(fileChange.getPath(), "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");
- }
- }
-
- } finally {
- FileUtils.deleteDir(tempDir);
- }
- }
-
-}
+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.Test;
+
+import com.pmease.commons.git.FileChangeWithDiffs;
+import com.pmease.commons.git.Git;
+import com.pmease.commons.util.FileUtils;
+
+public class DiffCommandTest {
+
+ @Test
+ public void shouldGenerateDiffCorrectly() {
+ assertTrue(GitCommand.checkError() == null);
+ 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);
+
+ workGit.checkout("dev", true);
+
+ 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);
+
+ Git bareGit = new Git(new File(tempDir, "bare"));
+ bareGit.clone(workGit.repoDir().getAbsolutePath(), true);
+
+ 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.getCommitHash1().length());
+ assertEquals(40, fileChange.getCommitHash2().length());
+ if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {
+ assertEquals(fileChange.getPath(), "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");
+ }
+ }
+
+ } 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
index a341eeeabb..cc7db7b067 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
@@ -1,93 +1,97 @@
-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.FileChange;
-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();
-
- 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);
-
- 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");
- 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);
-
- 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).getFileChanges().size(), 2);
- assertEquals(commits.get(0).getFileChanges().get(0).getPath(), "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());
-
- 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);
-
- assertEquals(workGit.resolveCommit(commits.get(0).getHash()).getHash(), commits.get(0).getHash());
- assertEquals(workGit.resolveCommit(commits.get(1).getHash()).getHash(), commits.get(1).getHash());
- } finally {
- FileUtils.deleteDir(tempDir);
- }
- }
-
-}
+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.FileChange;
+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();
+
+ 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);
+
+ 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");
+ 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);
+
+ 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);
+
+ bareGit.addNote("master", "hello\nworld");
+
+ 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("hello\nworld", commits.get(0).getNote());
+ assertEquals(commits.get(0).getFileChanges().size(), 2);
+ assertEquals(commits.get(0).getFileChanges().get(0).getPath(), "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", 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);
+
+ assertEquals(workGit.resolveCommit(commits.get(0).getHash()).getHash(), commits.get(0).getHash());
+ assertEquals(workGit.resolveCommit(commits.get(1).getHash()).getHash(), commits.get(1).getHash());
+ } finally {
+ FileUtils.deleteDir(tempDir);
+ }
+ }
+
+}