Skip to content

The Unified and Autonomous Development Platform

Support RENAME, COPY, TYPE change type.

robin shinea550effb14c8
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 68e84c3a3d..52d26ff56c 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
@@ -4,19 +4,27 @@ import java.io.Serializable;
@SuppressWarnings("serial")
public class FileChange implements Serializable {
- public enum Action {ADD, MODIFY, DELETE}
+ public enum Action {ADD, MODIFY, DELETE, COPY, RENAME, TYPE}
- private final String path;
+ private final String oldPath;
+
+ private final String newPath;
private final Action action;
- public FileChange(Action action, String path) {
+ public FileChange(Action action, String oldPath, String newPath) {
this.action = action;
- this.path = path;
+
+ this.oldPath = oldPath;
+ this.newPath = newPath;
}
- public String getPath() {
- return path;
+ public String getOldPath() {
+ return oldPath;
+ }
+
+ public String getNewPath() {
+ return newPath;
}
public Action getAction() {
@@ -25,7 +33,10 @@ public class FileChange implements Serializable {
@Override
public String toString() {
- return action.name() + " " + path;
+ if (action == Action.COPY || action == Action.RENAME)
+ return action.name() + "\t" + oldPath + "->" + newPath;
+ else
+ return action.name() + "\t" + newPath;
}
}
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 3015d7b739..2a3b4aeb83 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
@@ -3,6 +3,8 @@ package com.pmease.commons.git;
import java.util.ArrayList;
import java.util.List;
+import javax.annotation.Nullable;
+
import com.pmease.commons.util.diff.DiffChunk;
@SuppressWarnings("serial")
@@ -10,22 +12,30 @@ public class FileChangeWithDiffs extends FileChange {
private final boolean binary;
- private final String commitHash1;
+ private final TreeNode.Type type;
+
+ private final String oldCommit;
- private final String commitHash2;
+ private final String newCommit;
private final List<DiffChunk> diffChunks;
- public FileChangeWithDiffs(Action action, String path, boolean binary,
- String commitHash1, String commitHash2, List<DiffChunk> diffChunks) {
- super(action, path);
+ public FileChangeWithDiffs(Action action, String oldPath, String newPath,
+ @Nullable TreeNode.Type type, boolean binary, @Nullable String oldCommit,
+ @Nullable String newCommit, List<DiffChunk> diffChunks) {
+ super(action, oldPath, newPath);
+ this.type = type;
this.binary = binary;
- this.commitHash1 = commitHash1;
- this.commitHash2 = commitHash2;
+ this.oldCommit = oldCommit;
+ this.newCommit = newCommit;
this.diffChunks = new ArrayList<>(diffChunks);
}
+ public @Nullable TreeNode.Type getType() {
+ return type;
+ }
+
public boolean isBinary() {
return binary;
}
@@ -34,12 +44,12 @@ public class FileChangeWithDiffs extends FileChange {
return diffChunks;
}
- public String getCommitHash1() {
- return commitHash1;
+ public @Nullable String getOldCommit() {
+ return oldCommit;
}
- public String getCommitHash2() {
- return commitHash2;
+ public @Nullable String getNewCommit() {
+ return newCommit;
}
}
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 0bb2263546..7b444f0db2 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
@@ -55,6 +55,16 @@ public class Git implements Serializable {
return repoDir;
}
+ /**
+ * Create branch even in a bare repository.
+ *
+ * @param branchName
+ * name of the branch to create
+ * @param commitHash
+ * commit hash of the branch
+ * @return
+ * this git object
+ */
public Git createBranch(String branchName, String commitHash) {
if (new ListBranchesCommand(repoDir).call().contains(branchName))
throw new GeneralException("Branch %s already exists.", branchName);
@@ -65,6 +75,14 @@ public class Git implements Serializable {
return this;
}
+ /**
+ * delete branch even in a bare repository.
+ *
+ * @param branchName
+ * name of the branch to delete
+ * @return
+ * this git object
+ */
public Git deleteBranch(String branchName) {
new DeleteRefCommand(repoDir).refName("refs/heads/" + branchName).call();
return this;
@@ -98,13 +116,13 @@ public class Git implements Serializable {
return this;
}
- public Git add(String path) {
- new AddCommand(repoDir).addPath(path).call();
+ public Git add(String... paths) {
+ new AddCommand(repoDir).addPaths(paths).call();
return this;
}
- public Git remove(String path) {
- new RemoveCommand(repoDir).removePath(path).call();
+ public Git remove(String... paths) {
+ new RemoveCommand(repoDir).removePaths(paths).call();
return this;
}
@@ -227,6 +245,21 @@ public class Git implements Serializable {
return log(null, revision, path, 1).get(0);
}
+ /**
+ * General diff between specified revisions for specified path.
+ *
+ * @param fromRev
+ * calculate diffs since this revision (not include changes of this revision)
+ * @param toRev
+ * calculate diffs till this revision (include changes of this revision)
+ * @param path
+ * optionally specify directory or file for diff. Use <tt>null</tt> to diff
+ * all files in the repository
+ * @param contextLines
+ * number of not changed lines before and after the difference lines
+ * @return
+ * list of file changes with diff information included
+ */
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();
}
@@ -236,6 +269,18 @@ public class Git implements Serializable {
return this;
}
+ /**
+ * Display commit information for every region of specified file and revision.
+ *
+ * @param file
+ * file for blame
+ * @param revision
+ * revision of the file for blame
+ * @return
+ * list of blame objects. The blame object consists of commit information
+ * and lines associated with this commit. All lines of all blame objects
+ * consist the whole file.
+ */
public List<Blame> blame(String file, String revision) {
return new BlameCommand(repoDir).file(file).revision(revision).call();
}
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
index 6452011d43..371b9a4209 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.java
@@ -14,8 +14,9 @@ public class AddCommand extends GitCommand<Void> {
super(repoDir);
}
- public AddCommand addPath(String path) {
- paths.add(path);
+ public AddCommand addPaths(String... paths) {
+ for (String path: paths)
+ this.paths.add(path);
return this;
}
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 0e41ff6d05..0c86257d5f 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
@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.FileChange;
+import com.pmease.commons.git.TreeNode;
import com.pmease.commons.git.FileChange.Action;
import com.pmease.commons.git.FileChangeWithDiffs;
import com.pmease.commons.util.diff.DiffUtils;
@@ -54,7 +55,8 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
Preconditions.checkNotNull(toRev, "toRev has to be specified.");
Commandline cmd = cmd();
- cmd.addArgs("diff", fromRev + ".." + toRev, "--full-index");
+ cmd.addArgs("diff", fromRev + ".." + toRev, "--full-index", "--no-color", "--find-renames",
+ "--find-copies", "--src-prefix=#gitop_old/", "--dst-prefix=#gitop_new/");
if (contextLines != 0)
cmd.addArgs("--unified=" + contextLines);
if (path != null)
@@ -68,25 +70,40 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
@Override
public void consume(String line) {
if (line.startsWith("diff --git")) {
- if (changeBuilder.path != null)
+ if (changeBuilder.newPath != null)
fileChanges.add(changeBuilder.buildFileChange());
changeBuilder.action = FileChange.Action.MODIFY;
changeBuilder.binary = false;
+ changeBuilder.type = null;
+ changeBuilder.oldCommit = null;
+ changeBuilder.newCommit = null;
changeBuilder.diffLines.clear();
- changeBuilder.path = StringUtils.substringBefore(line.substring("diff --git a/".length()), " ");
- } else if (line.startsWith("deleted file")) {
+ line = line.substring("diff --git #gitop_old/".length());
+
+ changeBuilder.oldPath = StringUtils.substringBefore(line, " #gitop_new/");
+ changeBuilder.newPath = StringUtils.substringAfter(line, " #gitop_new/");
+ } else if (line.startsWith("deleted file mode ")) {
changeBuilder.action = FileChange.Action.DELETE;
- } else if (line.startsWith("new file")) {
+ changeBuilder.type = TreeNode.Type.fromMode(line.substring("deleted file mode ".length()));
+ } else if (line.startsWith("new file mode ")) {
changeBuilder.action = FileChange.Action.ADD;
+ changeBuilder.type = TreeNode.Type.fromMode(line.substring("new file mode ".length()));
} else if (line.startsWith("Binary files")) {
changeBuilder.binary = true;
+ } else if (line.startsWith("rename from ") || line.startsWith("rename to ")) {
+ changeBuilder.action = FileChange.Action.RENAME;
+ } else if (line.startsWith("copy from ") || line.startsWith("copy to ")) {
+ changeBuilder.action = FileChange.Action.COPY;
} 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, " ");
+ changeBuilder.oldCommit = StringUtils.substringBefore(line, "..");
+ changeBuilder.newCommit = StringUtils.substringAfter(line, "..");
+ if (changeBuilder.newCommit.indexOf(' ') != -1) {
+ changeBuilder.newCommit = StringUtils.substringBefore(changeBuilder.newCommit, " ");
+ changeBuilder.type = TreeNode.Type.fromMode(StringUtils.substringAfterLast(line, " "));
+ }
} else if (line.startsWith("@@") || line.startsWith("+") || line.startsWith("-")
|| line.startsWith(" ") || line.startsWith("\\")) {
changeBuilder.diffLines.add(line);
@@ -96,7 +113,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
}, errorLogger()).checkReturnCode();
- if (changeBuilder.path != null)
+ if (changeBuilder.newPath != null)
fileChanges.add(changeBuilder.buildFileChange());
return fileChanges;
@@ -105,19 +122,23 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
private static class ChangeBuilder {
private Action action;
- private String path;
+ private String oldPath;
+
+ private String newPath;
private boolean binary;
- private String commitHash1;
+ private TreeNode.Type type;
+
+ private String oldCommit;
- private String commitHash2;
+ private String newCommit;
private List<String> diffLines = new ArrayList<>();
private FileChangeWithDiffs buildFileChange() {
- return new FileChangeWithDiffs(action, path, binary, commitHash1, commitHash2,
- DiffUtils.parseUnifiedDiff(diffLines));
+ return new FileChangeWithDiffs(action, oldPath, newPath, type, binary,
+ oldCommit, newCommit, 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 b0b18d4448..60667d23c4 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
@@ -59,7 +59,7 @@ public class LogCommand extends GitCommand<List<Commit>> {
+ "*** 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");
+ "--name-status", "--find-renames", "--find-copies", "--date=iso");
if (fromRev != null) {
if (toRev != null)
cmd.addArgs(fromRev + ".." + toRev);
@@ -126,10 +126,24 @@ public class LogCommand extends GitCommand<List<Commit>> {
action = FileChange.Action.MODIFY;
else if (line.startsWith("D"))
action = FileChange.Action.DELETE;
+ else if (line.startsWith("C"))
+ action = FileChange.Action.COPY;
+ else if (line.startsWith("R"))
+ action = FileChange.Action.RENAME;
+ else if (line.startsWith("T"))
+ action = FileChange.Action.TYPE;
if (action != null) {
String path = StringUtils.substringAfter(line, "\t").trim();
- FileChange fileChange = new FileChange(action, path);
+ String path1;
+ String path2;
+ if (path.indexOf('\t') != -1) {
+ path1 = StringUtils.substringBefore(path, "\t").trim();
+ path2 = StringUtils.substringAfter(path, "\t").trim();
+ } else {
+ path1 = path2 = path;
+ }
+ FileChange fileChange = new FileChange(action, path1, path2);
commitBuilder.fileChanges.add(fileChange);
}
} else if (line.startsWith("subject:")) {
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
index e37e5f4600..c10d61ddef 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,8 +14,9 @@ public class RemoveCommand extends GitCommand<Void> {
super(repoDir);
}
- public RemoveCommand removePath(String path) {
- paths.add(path);
+ public RemoveCommand removePaths(String... paths) {
+ for (String path: paths)
+ this.paths.add(path);
return this;
}
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 4832b636da..4788cc9eb7 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
@@ -87,10 +87,10 @@ public class DiffCommandTest {
assertEquals(fileChanges.size(), 3);
for (FileChangeWithDiffs fileChange: fileChanges) {
- assertEquals(40, fileChange.getCommitHash1().length());
- assertEquals(40, fileChange.getCommitHash2().length());
+ assertEquals(40, fileChange.getOldCommit().length());
+ assertEquals(40, fileChange.getNewCommit().length());
if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {
- assertEquals(fileChange.getPath(), "dir/file1");
+ assertEquals(fileChange.getNewPath(), "dir/file1");
assertEquals(fileChange.getDiffChunks().get(0).toString(),
"@@ -1 +1 @@\n"
+ "-1st line of file1\n"
@@ -113,4 +113,43 @@ public class DiffCommandTest {
}
}
+ @Test
+ public void shouldHandleRenameAndCopyCorrectly() {
+ assertTrue(GitCommand.checkError() == null);
+ File tempDir = FileUtils.createTempDir();
+
+ try {
+ Git git = new Git(tempDir);
+ git.init(false);
+
+ 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);
+
+ 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);
+ }
+ }
+
}
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 46c7fdca62..754d9ac68e 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
@@ -62,10 +62,10 @@ public class LogCommandTest {
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).getMessage(), "add dir/file to test files under a directory");
+ assertEquals(commits.get(0).getMessage(), "add dir/file\n\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).getPath(), "dir/file");
+ 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());
@@ -94,4 +94,40 @@ public class LogCommandTest {
}
}
+ @Test
+ public void shouldHandleRenameAndCopyCorrectly() {
+ assertTrue(GitCommand.checkError() == null);
+ File tempDir = FileUtils.createTempDir();
+
+ try {
+ Git git = new Git(tempDir);
+ git.init(false);
+
+ 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);
+
+ List<Commit> commits = git.log("master~3", "master", null, 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);
+ }
+ }
}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
index d396d795cd..134f5a9187 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
@@ -179,6 +179,38 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
}
}
+ public static void deleteFile(File file) {
+ /*
+ * Do not test existence of file here as broken symbol link will report file as non-existent
+ if (!file.exists())
+ return;
+ */
+
+ int maxTries = 10;
+ int numTries = 1;
+
+ while (true) {
+ if (file.delete())
+ break;
+
+ if (file.exists()) {
+ if (numTries == maxTries) {
+ throw new GeneralException("Failed to delete file '" +
+ file.getAbsolutePath() + "'.");
+ } else {
+ System.gc();
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e2) {
+ }
+ numTries++;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+
public static void writeFile(File file, String content, String encoding) {
try {
org.apache.commons.io.FileUtils.writeStringToFile(file, content, encoding);