The Unified and Autonomous Development Platform
Retreived file change information from log command and put into commits.
robin shine43a787ee7a69
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.javaindex 604e3e7cf3..fbb6533ce0 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@@ -1,9 +1,9 @@package com.pmease.commons.git;-import java.util.Collection;+import java.util.ArrayList;import java.util.Collections;import java.util.Date;-import java.util.HashSet;+import java.util.List;import javax.annotation.Nullable;@@ -23,17 +23,20 @@ public class Commit {private final String body;- private final Collection<String> parentHashes;+ private final List<String> parentHashes;++ private final List<FileChange> fileChanges;public Commit(Date date, String author, String committer, String hash, String subject,- String body, Collection<String> parentHashes) {+ String body, List<String> parentHashes, List<FileChange> fileChanges) {this.date = date;this.author = author;this.committer = committer;this.hash = hash;this.subject = subject;this.body = body;- this.parentHashes = new HashSet<>(parentHashes);+ this.parentHashes = new ArrayList<>(parentHashes);+ this.fileChanges = new ArrayList<>(fileChanges);}public Date getDate() {@@ -60,8 +63,12 @@ public class Commit {return body;}- public Collection<String> getParentHashes() {- return Collections.unmodifiableCollection(parentHashes);+ public List<String> getParentHashes() {+ return Collections.unmodifiableList(parentHashes);+ }++ public List<FileChange> getFileChanges() {+ return Collections.unmodifiableList(fileChanges);}@Overridediff --git a/commons.git/src/main/java/com/pmease/commons/git/FileChange.java b/commons.git/src/main/java/com/pmease/commons/git/FileChange.javanew file mode 100644index 0000000000..b70731815d--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/FileChange.java@@ -0,0 +1,31 @@+package com.pmease.commons.git;++import java.io.Serializable;++@SuppressWarnings("serial")+public class FileChange implements Serializable {+ public enum Action {ADD, MODIFY, DELETE}++ private final String path;++ private final Action action;++ public FileChange(String path, Action action) {+ this.path = path;+ this.action = action;+ }++ public String getPath() {+ return path;+ }++ public Action getAction() {+ return action;+ }++ @Override+ public String toString() {+ return action.name() + " " + path;+ }++}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/CommitBuilder.java b/commons.git/src/main/java/com/pmease/commons/git/command/CommitBuilder.javaindex a21eb96605..8e80b08845 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/CommitBuilder.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/CommitBuilder.java@@ -1,10 +1,12 @@package com.pmease.commons.git.command;+import java.util.ArrayList;import java.util.Collection;import java.util.Date;-import java.util.HashSet;+import java.util.List;import com.pmease.commons.git.Commit;+import com.pmease.commons.git.FileChange;class CommitBuilder {@@ -20,7 +22,9 @@ class CommitBuilder {private String body;- private Collection<String> parentHashes = new HashSet<>();+ private List<String> parentHashes = new ArrayList<>();++ private List<FileChange> fileChanges = new ArrayList<>();public Date getDate() {return date;@@ -74,8 +78,12 @@ class CommitBuilder {return parentHashes;}+ public List<FileChange> getFileChanges() {+ return fileChanges;+ }+public Commit build() {return new Commit(date, author, committer, hash, subject.trim(),- body!=null?body.trim():null, parentHashes);+ body!=null?body.trim():null, parentHashes, fileChanges);}}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.javaindex b14967f47f..cfb7cb2716 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@@ -9,6 +9,7 @@ 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;@@ -52,8 +53,8 @@ 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 ***",- "--quiet", "--date=iso");+ "--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 (fromRevision != null) {if (toRevision != null)cmd.addArgs(fromRevision + ".." + toRevision);@@ -73,19 +74,27 @@ public class LogCommand extends GitCommand<List<Commit>> {final CommitBuilder commitBuilder = new CommitBuilder();- final boolean[] commitMessageBlock = new boolean[]{true};+ final boolean[] commitMessageBlock = new boolean[1];+ final boolean[] fileChangesBlock = new boolean[1];+cmd.execute(new LineConsumer() {@Overridepublic void consume(String line) {- if (line.equals("*** commit_end ***")) {- commits.add(commitBuilder.build());- commitBuilder.getParentHashes().clear();- commitBuilder.setSubject(null);- commitBuilder.setBody(null);+ if (line.equals("*** commit_begin ***")) {+ if (commitBuilder.getHash() != null) {+ commits.add(commitBuilder.build());+ commitBuilder.getParentHashes().clear();+ commitBuilder.getFileChanges().clear();+ commitBuilder.setSubject(null);+ commitBuilder.setBody(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.getSubject() == null)commitBuilder.setSubject(line);@@ -93,6 +102,20 @@ public class LogCommand extends GitCommand<List<Commit>> {commitBuilder.setBody(line);elsecommitBuilder.setBody(commitBuilder.getBody() + "\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.getFileChanges().add(fileChange);+ }} else if (line.startsWith("subject:")) {commitBuilder.setSubject(line.substring("subject:".length()));} else if (line.startsWith("hash:")) {@@ -111,6 +134,8 @@ public class LogCommand extends GitCommand<List<Commit>> {}, errorLogger()).checkReturnCode();+ commits.add(commitBuilder.build());+return commits;}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 cf5d710283..887cacd5e2 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@@ -4,9 +4,11 @@ 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;@@ -43,7 +45,9 @@ public class LogCommandTest {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);@@ -57,6 +61,9 @@ public class LogCommandTest {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());