The Unified and Autonomous Development Platform
Enhance FileChange to include file mode.
robin shinef95cf97bece8
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.javaindex 52d26ff56c..0c8771adb7 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@@ -1,22 +1,41 @@package com.pmease.commons.git;import java.io.Serializable;+import java.util.StringTokenizer;++import org.eclipse.jgit.util.QuotedString;++import com.google.common.base.Preconditions;@SuppressWarnings("serial")public class FileChange implements Serializable {- public enum Action {ADD, MODIFY, DELETE, COPY, RENAME, TYPE}++ public enum Status {ADD, MODIFY, DELETE, RENAME, TYPE}private final String oldPath;private final String newPath;- private final Action action;+ private final int oldMode;- public FileChange(Action action, String oldPath, String newPath) {- this.action = action;-+ private final int newMode;++ private final Status status;++ public FileChange(Status status, String oldPath, String newPath, int oldMode, int newMode) {+ this.status = status;this.oldPath = oldPath;this.newPath = newPath;+ this.oldMode = oldMode;+ this.newMode = newMode;+ }++ public FileChange(FileChange copy) {+ this.status = copy.status;+ this.oldPath = copy.oldPath;+ this.newPath = copy.newPath;+ this.oldMode = copy.oldMode;+ this.newMode = copy.newMode;}public String getOldPath() {@@ -27,16 +46,61 @@ public class FileChange implements Serializable {return newPath;}- public Action getAction() {- return action;+ public int getOldMode() {+ return oldMode;+ }++ public int getNewMode() {+ return newMode;+ }++ public Status getStatus() {+ return status;}@Overridepublic String toString() {- if (action == Action.COPY || action == Action.RENAME)- return action.name() + "\t" + oldPath + "->" + newPath;+ if (status == Status.RENAME)+ return status.name() + "\t" + oldPath + "->" + newPath;+ else if (status == Status.DELETE)+ return status.name() + "\t" + oldPath;else- return action.name() + "\t" + newPath;+ return status.name() + "\t" + newPath;}+ private static String dequoteFileName(String quotedFileName) {+ return QuotedString.GIT_PATH.dequote(quotedFileName);+ }++ public static FileChange parseRawLine(String rawLine) {+ Preconditions.checkArgument(rawLine.startsWith(":"));++ StringTokenizer tokenizer = new StringTokenizer(rawLine.substring(1));+ int oldMode = Integer.parseInt(tokenizer.nextToken(), 8);+ int newMode = Integer.parseInt(tokenizer.nextToken(), 8);+ tokenizer.nextToken();+ tokenizer.nextToken();+ String statusCode = tokenizer.nextToken();+ if (statusCode.startsWith("R")) {+ String oldPath = dequoteFileName(tokenizer.nextToken("\t"));+ String newPath = dequoteFileName(tokenizer.nextToken("\t"));+ return new FileChange(FileChange.Status.RENAME, oldPath, newPath, oldMode, newMode);+ } else if (statusCode.equals("M")) {+ String oldPath, newPath;+ oldPath = newPath = dequoteFileName(tokenizer.nextToken("\t"));+ return new FileChange(FileChange.Status.MODIFY, oldPath, newPath, oldMode, newMode);+ } else if (statusCode.equals("D")) {+ String oldPath = dequoteFileName(tokenizer.nextToken("\t"));+ return new FileChange(FileChange.Status.DELETE, oldPath, null, oldMode, newMode);+ } else if (statusCode.equals("T")) {+ String oldPath, newPath;+ oldPath = newPath = dequoteFileName(tokenizer.nextToken("\t"));+ return new FileChange(FileChange.Status.TYPE, oldPath, newPath, oldMode, newMode);+ } else if (statusCode.equals("A")) {+ String newPath = dequoteFileName(tokenizer.nextToken("\t"));+ return new FileChange(FileChange.Status.ADD, null, newPath, oldMode, newMode);+ } else {+ throw new RuntimeException("Unexpected status code: " + statusCode);+ }+ }}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.javadeleted file mode 100644index f38f1c3592..0000000000--- a/commons.git/src/main/java/com/pmease/commons/git/FileChangeWithDiffs.java+++ /dev/null@@ -1,57 +0,0 @@-package com.pmease.commons.git;--import java.util.ArrayList;-import java.util.List;--import javax.annotation.Nullable;--import org.eclipse.jgit.lib.FileMode;--import com.pmease.commons.util.diff.DiffChunk;--@SuppressWarnings("serial")-public class FileChangeWithDiffs extends FileChange {-- private final boolean binary;-- private final FileMode mode;-- private final String oldCommit;-- private final String newCommit;-- private final List<DiffChunk> diffChunks;-- public FileChangeWithDiffs(Action action, String oldPath, String newPath,- FileMode mode, boolean binary, @Nullable String oldCommit,- @Nullable String newCommit, List<DiffChunk> diffChunks) {- super(action, oldPath, newPath);-- this.mode = mode;- this.binary = binary;- this.oldCommit = oldCommit;- this.newCommit = newCommit;- this.diffChunks = new ArrayList<>(diffChunks);- }-- public FileMode getMode() {- return mode;- }-- public boolean isBinary() {- return binary;- }-- public List<DiffChunk> getDiffChunks() {- return diffChunks;- }-- public @Nullable String getOldCommit() {- return oldCommit;- }-- 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.javaindex 0f86e13d08..95f39dd66a 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@@ -28,7 +28,6 @@ import com.pmease.commons.git.command.CherryPickCommand;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.FetchCommand;import com.pmease.commons.git.command.InitCommand;import com.pmease.commons.git.command.IsAncestorCommand;@@ -166,22 +165,22 @@ public class Git implements Serializable {public List<DiffTreeNode> listTreeWithDiff(String fromRev, String toRev, @Nullable String path) {path = GitUtils.normalizeTreePath(path);- Collection<FileChange> changes = listFileChanges(fromRev, toRev, path, false, false);+ Collection<FileChange> changes = listFileChanges(fromRev, toRev, path, false);Set<String> addedFiles = new HashSet<>();Set<String> deletedFiles = new HashSet<>();Set<String> modifiedFiles = new HashSet<>();Set<String> changedFiles = new HashSet<>();for (FileChange change: changes) {- if (change.getAction() == FileChange.Action.ADD) {+ if (change.getStatus() == FileChange.Status.ADD) {addedFiles.add(change.getNewPath());- } else if (change.getAction() == FileChange.Action.DELETE) {+ } else if (change.getStatus() == FileChange.Status.DELETE) {deletedFiles.add(change.getOldPath());- } else if (change.getAction() == FileChange.Action.MODIFY- || change.getAction() == FileChange.Action.TYPE) {+ } else if (change.getStatus() == FileChange.Status.MODIFY+ || change.getStatus() == FileChange.Status.TYPE) {modifiedFiles.add(change.getOldPath());} else {- throw new IllegalStateException("Unexpected action: " + change.getAction());+ throw new IllegalStateException("Unexpected action: " + change.getStatus());}}@@ -276,10 +275,10 @@ public class Git implements Serializable {return new ListChangedFilesCommand(repoDir).fromRev(fromRev).toRev(toRev).path(path).call();}- public Collection<FileChange> listFileChanges(String fromRev, String toRev, @Nullable String path,- boolean findRenames, boolean findCopies) {+ public List<FileChange> listFileChanges(String fromRev, String toRev, @Nullable String path,+ boolean findRenames) {return new ListFileChangesCommand(repoDir).fromRev(fromRev).toRev(toRev).path(path)- .findRenames(findRenames).findCopies(findCopies).call();+ .findRenames(findRenames).call();}public Git checkout(String revision, @Nullable String newBranch) {@@ -474,23 +473,6 @@ public class Git implements Serializable {return log(null, revision, path, 1, 0).get(0);}- /**- * General diff between specified revisions for specified path.- *- * @param revisions- * calculate diffs using this revisions- * @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 revisions, @Nullable String path, int contextLines) {- return new DiffCommand(repoDir).revisions(revisions).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/AddCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AddCommand.javaindex 0ffdb19f1a..c74b4061fd 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@@ -1,8 +1,6 @@package com.pmease.commons.git.command;import java.io.File;-import java.util.ArrayList;-import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -14,15 +12,14 @@ public class AddCommand extends GitCommand<Void> {private static final Logger logger = LoggerFactory.getLogger(AddCommand.class);- private List<String> paths = new ArrayList<String>();+ private String paths[] = new String[0];public AddCommand(File repoDir) {super(repoDir);}public AddCommand addPaths(String... paths) {- for (String path: paths)- this.paths.add(path);+ this.paths = paths;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.javadeleted file mode 100644index 0f9def48e2..0000000000--- a/commons.git/src/main/java/com/pmease/commons/git/command/DiffCommand.java+++ /dev/null@@ -1,154 +0,0 @@-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 org.eclipse.jgit.lib.FileMode;-import org.slf4j.Logger;-import org.slf4j.LoggerFactory;--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 static final Logger logger = LoggerFactory.getLogger(DiffCommand.class);-- private String revisions;-- private String path;-- private int contextLines;-- public DiffCommand(File repoDir) {- super(repoDir);- }-- public DiffCommand revisions(String revisions) {- this.revisions = revisions;- 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(revisions, "revisions has to be specified.");-- Commandline cmd = cmd();- cmd.addArgs("diff", revisions, "--full-index", "--no-color", "--find-renames",- "--find-copies", "--src-prefix=#gitplex_old/", "--dst-prefix=#gitplex_new/");- 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.newPath != null)- fileChanges.add(changeBuilder.buildFileChange());-- changeBuilder.action = FileChange.Action.MODIFY;- changeBuilder.binary = false;- changeBuilder.mode = FileMode.MISSING;- changeBuilder.oldCommit = null;- changeBuilder.newCommit = null;- changeBuilder.diffLines.clear();-- line = line.substring("diff --git #gitplex_old/".length());-- changeBuilder.oldPath = StringUtils.substringBefore(line, " #gitplex_new/");- changeBuilder.newPath = StringUtils.substringAfter(line, " #gitplex_new/");- } else if (line.startsWith("deleted file mode ")) {- changeBuilder.action = FileChange.Action.DELETE;- changeBuilder.mode = FileMode.fromBits(Integer.parseInt(line.substring("deleted file mode ".length()), 8));- } else if (line.startsWith("new file mode ")) {- changeBuilder.action = FileChange.Action.ADD;- changeBuilder.mode = FileMode.fromBits(Integer.parseInt(line.substring("new file mode ".length()), 8));- } 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.oldCommit = StringUtils.substringBefore(line, "..");- changeBuilder.newCommit = StringUtils.substringAfter(line, "..");- if (changeBuilder.newCommit.indexOf(' ') != -1) {- changeBuilder.newCommit = StringUtils.substringBefore(changeBuilder.newCommit, " ");- changeBuilder.mode = FileMode.fromBits(Integer.parseInt(StringUtils.substringAfterLast(line, " "), 8));- }- } else if (line.startsWith("@@") || line.startsWith("+") || line.startsWith("-")- || line.startsWith(" ") || line.startsWith("\\")) {- changeBuilder.diffLines.add(line);- }-- }-- }, new LineConsumer() {-- @Override- public void consume(String line) {- if (line.startsWith("warning: "))- logger.warn(line.substring("warning: ".length()));- else if (line.startsWith("The file will have its original line endings"))- logger.warn(line);- else if (line.startsWith("The file will have its original line endings in your working directory"))- logger.warn(line);- else- logger.error(line);- }-- }).checkReturnCode();-- if (changeBuilder.newPath != null)- fileChanges.add(changeBuilder.buildFileChange());-- return fileChanges;- }-- private static class ChangeBuilder {- private Action action;-- private String oldPath;-- private String newPath;-- private boolean binary;-- private FileMode mode;-- private String oldCommit;-- private String newCommit;-- private List<String> diffLines = new ArrayList<>();-- private FileChangeWithDiffs buildFileChange() {- return new FileChangeWithDiffs(action, oldPath, newPath, mode, binary,- oldCommit, newCommit, DiffUtils.parseUnifiedDiff(diffLines));- }- }-}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ListFileChangesCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListFileChangesCommand.javaindex 11626437b7..140c1f72ed 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListFileChangesCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListFileChangesCommand.java@@ -2,9 +2,8 @@ package com.pmease.commons.git.command;import java.io.File;import java.util.ArrayList;-import java.util.Collection;+import java.util.List;-import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -13,7 +12,7 @@ import com.pmease.commons.git.FileChange;import com.pmease.commons.util.execution.Commandline;import com.pmease.commons.util.execution.LineConsumer;-public class ListFileChangesCommand extends GitCommand<Collection<FileChange>> {+public class ListFileChangesCommand extends GitCommand<List<FileChange>> {private static final Logger logger = LoggerFactory.getLogger(ListFileChangesCommand.class);@@ -25,8 +24,6 @@ public class ListFileChangesCommand extends GitCommand<Collection<FileChange>> {private boolean findRenames;- private boolean findCopies;-public ListFileChangesCommand(final File repoDir) {super(repoDir);}@@ -51,26 +48,19 @@ public class ListFileChangesCommand extends GitCommand<Collection<FileChange>> {return this;}- public ListFileChangesCommand findCopies(final boolean findCopies) {- this.findCopies = findCopies;- return this;- }-@Override- public Collection<FileChange> call() {+ public List<FileChange> call() {Preconditions.checkNotNull(toRev, "toRev has to be specified.");Preconditions.checkNotNull(fromRev, "fromRev has to be specified.");- final Collection<FileChange> fileChanges = new ArrayList<>();+ final List<FileChange> fileChanges = new ArrayList<>();Commandline cmd = cmd();- cmd.addArgs("diff", "--name-status");+ cmd.addArgs("diff", "--raw");if (findRenames)cmd.addArgs("--find-renames");- if (findCopies)- cmd.addArgs("--find-copies");cmd.addArgs(fromRev + ".." + toRev);@@ -81,33 +71,8 @@ public class ListFileChangesCommand extends GitCommand<Collection<FileChange>> {@Overridepublic void consume(String line) {- 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;- 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();- 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);- fileChanges.add(fileChange);- }+ if (line.startsWith(":"))+ fileChanges.add(FileChange.parseRawLine(line));}}, new LineConsumer() {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 b683e13ee3..b355748c22 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@@ -77,7 +77,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", "--find-renames", "--find-copies", "--date=iso");+ "--raw", "--find-renames", "--date=iso");if (fromRev != null) {if (toRev != null)cmd.addArgs(fromRev + ".." + toRev);@@ -105,7 +105,7 @@ public class LogCommand extends GitCommand<List<Commit>> {final boolean[] commitNoteBlock = new boolean[1];final boolean[] commitMessageBlock = new boolean[1];- final boolean[] fileChangesBlock = new boolean[1];+ final boolean[] rawBlock = new boolean[1];cmd.execute(new LineConsumer() {@@ -121,14 +121,14 @@ public class LogCommand extends GitCommand<List<Commit>> {commitBuilder.note = null;}commitMessageBlock[0] = true;- fileChangesBlock[0] = false;+ rawBlock[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;+ rawBlock[0] = true;} else if (commitMessageBlock[0]) {if (commitBuilder.summary == null)commitBuilder.summary = line;@@ -141,34 +141,9 @@ public class LogCommand extends GitCommand<List<Commit>> {commitBuilder.note = line;elsecommitBuilder.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;- 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();- 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 (rawBlock[0]) {+ if (line.startsWith(":"))+ commitBuilder.fileChanges.add(FileChange.parseRawLine(line));} else if (line.startsWith("subject:")) {commitBuilder.summary = line.substring("subject:".length());} else if (line.startsWith("hash:")) {diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/RemoveCommand.javaindex f0fd74d8a8..dbc8d572c4 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@@ -1,8 +1,6 @@package com.pmease.commons.git.command;import java.io.File;-import java.util.ArrayList;-import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -14,15 +12,14 @@ public class RemoveCommand extends GitCommand<Void> {private static final Logger logger = LoggerFactory.getLogger(RemoveCommand.class);- private List<String> paths = new ArrayList<String>();+ private String paths[] = new String[0];public RemoveCommand(File repoDir) {super(repoDir);}public RemoveCommand paths(String... paths) {- for (String path: paths)- this.paths.add(path);+ this.paths = paths;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.javadeleted file mode 100644index 48a1258fb7..0000000000--- a/commons.git/src/test/java/com/pmease/commons/git/command/DiffCommandTest.java+++ /dev/null@@ -1,110 +0,0 @@-package com.pmease.commons.git.command;--import static org.junit.Assert.assertEquals;--import java.io.File;-import java.util.List;--import org.junit.Test;--import com.pmease.commons.git.AbstractGitTest;-import com.pmease.commons.git.FileChangeWithDiffs;-import com.pmease.commons.git.Git;--public class DiffCommandTest extends AbstractGitTest {-- @Test- public void shouldGenerateDiffCorrectly() {- addFile("readme", "readme");- createDir("dir");-- addFile("dir/file1",- "1st line of file1\n"- + "2nd line of file1\n"- + "3rd line of file1\n"- + "4th line of file1\n"- + "5th line of file1\n"- + "6th line of file1\n"- + "7th line of file1\n"- + "8th line of file1\n"- + "9th line of file1\n"- + "10th line of file1\n");-- addFile("dir/file2", "file2");-- commit("add readme, dir/file1 and dir/file2");-- git.checkout("head", "dev");-- addFile("readme", "readme for dev");- addFile("dir/file1",- "first line of file1\n"- + "2nd line of file1\n"- + "3rd line of file1\n"- + "4th line of file1\n"- + "5th line of file1\n"- + "6th line of file1\n"- + "7th line of file1\n"- + "some info here\n"- + "8th line of file1\n"- + "9th line of file1\n"- + "10th line of file1\n");- rm("dir/file2");- addFile("dir/file3", "file3");-- commit("modify readme, modify dir/file1, remove dir/file2, and add dir/file3");-- Git bareGit = new Git(new File(tempDir, "bare"));- bareGit.clone(git, true, false, false, null);-- List<FileChangeWithDiffs> fileChanges = bareGit.diff("master..dev", null, 3);- assertEquals(fileChanges.size(), 4);-- fileChanges = bareGit.diff("master..dev", "dir", 3);- assertEquals(fileChanges.size(), 3);-- for (FileChangeWithDiffs fileChange: fileChanges) {- assertEquals(40, fileChange.getOldCommit().length());- assertEquals(40, fileChange.getNewCommit().length());- if (fileChange.getAction() == FileChangeWithDiffs.Action.MODIFY) {- assertEquals(fileChange.getNewPath(), "dir/file1");- assertEquals(fileChange.getDiffChunks().get(0).toString(),- "@@ -1 +1 @@\n"- + "-1st line of file1\n"- + "+first line of file1\n"- + " 2nd line of file1\n"- + " 3rd line of file1\n"- + " 4th line of file1\n"- + " 5th line of file1\n"- + " 6th line of file1\n"- + " 7th line of file1\n"- + "+some info here\n"- + " 8th line of file1\n"- + " 9th line of file1\n"- + " 10th line of file1\n");- }- }- }-- @Test- public void shouldHandleRenameAndCopyCorrectly() {- addFileAndCommit("a", "1111\n2222\n3333\n", "add a");-- addFile("a", "1111\n2222\n3333\n4444\n");- addFile("a2", "1111\n2222\n3333\n4444\n");- commit("copy a to a2");-- addFileAndCommit("b", "1111\n2222\n3333\n4444\n", "add b");-- rm("b");- addFileAndCommit("b2", "1111\n2222\n3333\n4444\n", "move b to b2");-- List<FileChangeWithDiffs> changes = git.diff("master~3..master", null, 4);- assertEquals("COPY\ta->a2", changes.get(1).toString());- assertEquals("COPY\ta->b2", changes.get(2).toString());-- changes = git.diff("master~1..master", null, 4);- assertEquals("RENAME\tb->b2", changes.get(0).toString());- }--}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListFileChangesCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListFileChangesCommandTest.javanew file mode 100644index 0000000000..423d3e43ba--- /dev/null+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListFileChangesCommandTest.java@@ -0,0 +1,46 @@+package com.pmease.commons.git.command;++import static org.junit.Assert.assertEquals;++import java.util.List;++import org.apache.commons.lang3.SystemUtils;+import org.junit.Test;++import com.pmease.commons.git.AbstractGitTest;+import com.pmease.commons.git.FileChange;++public class ListFileChangesCommandTest extends AbstractGitTest {++ @Test+ public void test() {+ addFileAndCommit("initial", "", "0");+ addFileAndCommit("file name with space", "hello world", "1");+ removeFileAndCommit("file name with space", "2");+ addFileAndCommit("another file name with space", "hello world", "3");++ List<FileChange> changes = git.listFileChanges("master~3", "master~2", null, true);+ assertEquals(1, changes.size());+ assertEquals("file name with space", changes.get(0).getNewPath());++ changes = git.listFileChanges("master~2", "master", null, true);+ assertEquals(1, changes.size());+ assertEquals(FileChange.Status.RENAME, changes.get(0).getStatus());+ assertEquals("file name with space", changes.get(0).getOldPath());+ assertEquals("another file name with space", changes.get(0).getNewPath());++ if (!SystemUtils.IS_OS_WINDOWS) {+ addFileAndCommit("file name with \"quote\" ", "hello world", "1");+ rm("file name with \"quote\" ");+ addFile(" another file name with \"quote\" ", "hello world");+ commit("2");++ changes = git.listFileChanges("master~1", "master", null, true);+ assertEquals(1, changes.size());+ assertEquals(FileChange.Status.RENAME, changes.get(0).getStatus());+ assertEquals("file name with \"quote\" ", changes.get(0).getOldPath());+ assertEquals(" another file name with \"quote\" ", changes.get(0).getNewPath());+ }++ }+}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 04636326d0..8932065e3a 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@@ -43,7 +43,7 @@ public class LogCommandTest extends AbstractGitTest {assertEquals("hello\nworld", commits.get(0).getNote());assertEquals(commits.get(0).getFileChanges().size(), 2);assertEquals(commits.get(0).getFileChanges().get(0).getNewPath(), "dir/file");- assertEquals(commits.get(0).getFileChanges().get(0).getAction(), FileChange.Action.ADD);+ assertEquals(commits.get(0).getFileChanges().get(0).getStatus(), FileChange.Status.ADD);assertEquals(commits.get(0).getParentHashes().size(), 1);assertEquals(commits.get(0).getParentHashes().iterator().next(), commits.get(1).getHash());@@ -83,6 +83,6 @@ public class LogCommandTest extends AbstractGitTest {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());+ assertEquals("ADD\ta2", change.toString());}}diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/GitPlexMappings.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/GitPlexMappings.javaindex 954a43b86d..03599a5694 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/GitPlexMappings.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/GitPlexMappings.java@@ -198,8 +198,8 @@ public class GitPlexMappings extends CompoundRequestMapper {// --------------------------------------------------------------------// system shared resources// --------------------------------------------------------------------- addResource("raw/${user}/${repo}/${revision}", new RawBlobResourceReference());- addResource("archive/${user}/${repo}/${file}",new GitArchiveResourceReference());+ addResource("raw/${user}/${repo}", new RawBlobResourceReference());+ addResource("archive/${user}/${repo}",new GitArchiveResourceReference());}private void addPage(String path, Class<? extends Page> page) {diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/code/blob/renderer/RawBlobResource.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/code/blob/renderer/RawBlobResource.javaindex ea541ba060..9b7d3f141a 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/code/blob/renderer/RawBlobResource.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/code/blob/renderer/RawBlobResource.java@@ -1,7 +1,6 @@package com.pmease.gitplex.web.page.repository.info.code.blob.renderer;import java.io.IOException;-import java.util.List;import javax.persistence.EntityNotFoundException;@@ -16,7 +15,6 @@ import org.eclipse.jgit.lib.ObjectStream;import com.google.common.base.Preconditions;import com.google.common.base.Strings;import com.google.common.base.Throwables;-import com.google.common.collect.Lists;import com.google.common.io.ByteStreams;import com.pmease.commons.hibernate.dao.Dao;import com.pmease.commons.util.MediaTypes;@@ -29,7 +27,6 @@ import com.pmease.gitplex.web.page.account.AccountPage;import com.pmease.gitplex.web.page.repository.RepositoryPage;import com.pmease.gitplex.web.service.FileBlob;import com.pmease.gitplex.web.service.FileBlobService;-import com.pmease.gitplex.web.util.UrlUtils;public class RawBlobResource extends AbstractResource {private static final long serialVersionUID = 1L;@@ -43,17 +40,13 @@ public class RawBlobResource extends AbstractResource {final String username = params.get(AccountPage.PARAM_USER).toString();final String repositoryName = params.get(RepositoryPage.PARAM_REPO).toString();- final String revision = params.get("objectId").toString();-- List<String> paths = Lists.newArrayList();- for (int i = 0; i < params.getIndexedCount(); i++) {- paths.add(params.get(i).toString());- }+ final String revision = params.get(RepositoryPage.PARAM_REVISION).toString();+ final String path = params.get(RepositoryPage.PARAM_PATH).toString();Preconditions.checkArgument(username != null);Preconditions.checkArgument(!Strings.isNullOrEmpty(repositoryName));Preconditions.checkArgument(!Strings.isNullOrEmpty(revision));- Preconditions.checkArgument(!paths.isEmpty());+ Preconditions.checkArgument(path != null);Repository repository = GitPlex.getInstance(RepositoryManager.class).findBy(username, repositoryName);if (repository == null) {@@ -69,7 +62,6 @@ public class RawBlobResource extends AbstractResource {ResourceResponse response = new ResourceResponse();if (response.dataNeedsToBeWritten(attributes)) {- final String path = UrlUtils.concatSegments(paths);FileBlob blob = FileBlob.of(repository, revision, path);response.setContentLength(blob.getSize());String fileName = FilenameUtils.getName(blob.getFilePath());