Skip to content

The Unified and Autonomous Development Platform

Change TreeNode.Action to use JGit FileMode

robin shine02a800a481de
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 2a3b4aeb83..f38f1c3592 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
@@ -5,6 +5,8 @@ import java.util.List;
import javax.annotation.Nullable;
+import org.eclipse.jgit.lib.FileMode;
+
import com.pmease.commons.util.diff.DiffChunk;
@SuppressWarnings("serial")
@@ -12,7 +14,7 @@ public class FileChangeWithDiffs extends FileChange {
private final boolean binary;
- private final TreeNode.Type type;
+ private final FileMode mode;
private final String oldCommit;
@@ -21,19 +23,19 @@ public class FileChangeWithDiffs extends FileChange {
private final List<DiffChunk> diffChunks;
public FileChangeWithDiffs(Action action, String oldPath, String newPath,
- @Nullable TreeNode.Type type, boolean binary, @Nullable String oldCommit,
+ FileMode mode, boolean binary, @Nullable String oldCommit,
@Nullable String newCommit, List<DiffChunk> diffChunks) {
super(action, oldPath, newPath);
- this.type = type;
+ this.mode = mode;
this.binary = binary;
this.oldCommit = oldCommit;
this.newCommit = newCommit;
this.diffChunks = new ArrayList<>(diffChunks);
}
- public @Nullable TreeNode.Type getType() {
- return type;
+ public FileMode getMode() {
+ return mode;
}
public boolean isBinary() {
diff --git a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java
index 3f0bc2325b..0e245c8019 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java
@@ -7,6 +7,7 @@ import java.util.List;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
+import org.eclipse.jgit.lib.FileMode;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
@@ -16,25 +17,9 @@ import com.pmease.commons.git.command.ShowCommand;
@SuppressWarnings("serial")
public class TreeNode implements Serializable {
- public enum Type {
- DIRECTORY, FILE, SUBMODULE, SYMBOLLINK;
-
- public static Type fromMode(String mode) {
- if (mode.startsWith("040"))
- return DIRECTORY;
- else if (mode.startsWith("160"))
- return SUBMODULE;
- else if (mode.startsWith("120"))
- return SYMBOLLINK;
- else
- return FILE;
- }
-
- }
-
protected final File gitDir;
- private final Type type;
+ private final FileMode mode;
private final String path;
@@ -46,12 +31,12 @@ public class TreeNode implements Serializable {
private Optional<TreeNode> parentNode;
- public TreeNode(File gitDir, Type type, String path, String revision, String hash, int size) {
+ public TreeNode(File gitDir, FileMode mode, String path, String revision, String hash, int size) {
this.gitDir = gitDir;
this.path = path;
this.revision = revision;
this.hash = hash;
- this.type = type;
+ this.mode = mode;
this.size = size;
}
@@ -74,8 +59,8 @@ public class TreeNode implements Serializable {
return hash;
}
- public Type getType() {
- return type;
+ public FileMode getMode() {
+ return mode;
}
public int getSize() {
@@ -120,7 +105,7 @@ public class TreeNode implements Serializable {
* directory.
*/
public @Nullable List<TreeNode> listChildren() {
- if (type == Type.DIRECTORY) {
+ if (mode == FileMode.TREE) {
List<TreeNode> children = new ListTreeCommand(gitDir).revision(getRevision()).path(getPath() + "/").call();
for (TreeNode each: children) {
each.setParent(this);
@@ -141,7 +126,7 @@ public class TreeNode implements Serializable {
* represents a directory
*/
public byte[] show() {
- if (type == Type.SUBMODULE) {
+ if (mode == FileMode.GITLINK) {
return new Git(gitDir).listSubModules(revision).get(path).getBytes();
} else {
return new ShowCommand(gitDir).revision(getRevision()).path(getPath()).call();
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 0c86257d5f..998c6a26f0 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
@@ -5,10 +5,10 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
+import org.eclipse.jgit.lib.FileMode;
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;
@@ -75,7 +75,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
changeBuilder.action = FileChange.Action.MODIFY;
changeBuilder.binary = false;
- changeBuilder.type = null;
+ changeBuilder.mode = FileMode.MISSING;
changeBuilder.oldCommit = null;
changeBuilder.newCommit = null;
changeBuilder.diffLines.clear();
@@ -86,10 +86,10 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
changeBuilder.newPath = StringUtils.substringAfter(line, " #gitop_new/");
} else if (line.startsWith("deleted file mode ")) {
changeBuilder.action = FileChange.Action.DELETE;
- changeBuilder.type = TreeNode.Type.fromMode(line.substring("deleted file mode ".length()));
+ 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.type = TreeNode.Type.fromMode(line.substring("new file mode ".length()));
+ 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 ")) {
@@ -102,7 +102,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
changeBuilder.newCommit = StringUtils.substringAfter(line, "..");
if (changeBuilder.newCommit.indexOf(' ') != -1) {
changeBuilder.newCommit = StringUtils.substringBefore(changeBuilder.newCommit, " ");
- changeBuilder.type = TreeNode.Type.fromMode(StringUtils.substringAfterLast(line, " "));
+ changeBuilder.mode = FileMode.fromBits(Integer.parseInt(StringUtils.substringAfterLast(line, " "), 8));
}
} else if (line.startsWith("@@") || line.startsWith("+") || line.startsWith("-")
|| line.startsWith(" ") || line.startsWith("\\")) {
@@ -128,7 +128,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
private boolean binary;
- private TreeNode.Type type;
+ private FileMode mode;
private String oldCommit;
@@ -137,7 +137,7 @@ public class DiffCommand extends GitCommand<List<FileChangeWithDiffs>> {
private List<String> diffLines = new ArrayList<>();
private FileChangeWithDiffs buildFileChange() {
- return new FileChangeWithDiffs(action, oldPath, newPath, type, binary,
+ 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/ListTreeCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
index 45d8b39a02..5c806ad068 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ListTreeCommand.java
@@ -6,6 +6,7 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
+import org.eclipse.jgit.lib.FileMode;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.TreeNode;
@@ -67,7 +68,7 @@ public class ListTreeCommand extends GitCommand<List<TreeNode>> {
size = Integer.parseInt(sizeStr);
String path = StringUtils.substringAfter(line.trim(), "\t");
- treeNodes.add(new TreeNode(repoDir, TreeNode.Type.fromMode(mode), path, revision, hash, size));
+ treeNodes.add(new TreeNode(repoDir, FileMode.fromBits(Integer.parseInt(mode, 8)), path, revision, hash, size));
}
}, errorLogger()).checkReturnCode();
diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java
index 2f290b962c..4bc36ebf15 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java
@@ -6,6 +6,7 @@ import java.io.File;
import java.io.IOException;
import java.util.List;
+import org.eclipse.jgit.lib.FileMode;
import org.junit.Assert;
import org.junit.Test;
@@ -69,7 +70,7 @@ public class ListTreeCommandTest {
assertEquals(7, treeNodes.size());
assertEquals("dir", treeNodes.get(5).getPath());
assertEquals("dir", treeNodes.get(5).getName());
- assertEquals(TreeNode.Type.DIRECTORY, treeNodes.get(5).getType());
+ assertEquals(FileMode.TREE, treeNodes.get(5).getMode());
assertEquals(moduleGit.repoDir().getAbsolutePath(),
new File(new String(treeNodes.get(6).show())).getCanonicalPath());
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java
index 15f6e6004f..12978f6e5c 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java
@@ -14,6 +14,7 @@ import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.eclipse.jgit.lib.FileMode;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
@@ -125,11 +126,11 @@ public class SourceTreePanel extends AbstractSourcePagePanel {
@Override
public int compare(TreeNode o1, TreeNode o2) {
- if (o1.getType() == o2.getType()) {
+ if (o1.getMode() == o2.getMode()) {
return o1.getName().compareTo(o2.getName());
- } else if (o1.getType() == TreeNode.Type.DIRECTORY) {
+ } else if (o1.getMode() == FileMode.TREE) {
return -1;
- } else if (o2.getType() == TreeNode.Type.DIRECTORY) {
+ } else if (o2.getMode() == FileMode.TREE) {
return 1;
} else {
return o1.getName().compareTo(o2.getName());
@@ -147,26 +148,21 @@ public class SourceTreePanel extends AbstractSourcePagePanel {
@Override
protected void populateItem(ListItem<TreeNode> item) {
TreeNode node = item.getModelObject();
- final TreeNode.Type type = node.getType();
+ final FileMode mode = node.getMode();
Icon icon = new Icon("icon", new AbstractReadOnlyModel<String>() {
@Override
public String getObject() {
- switch (type) {
- case DIRECTORY:
+ if (mode == FileMode.TREE)
return "folder";
-
- case FILE:
+ else if (mode == FileMode.REGULAR_FILE)
return "file";
-
- case SUBMODULE:
+ else if (mode == FileMode.GITLINK)
return "submodule";
-
- case SYMBOLLINK:
+ else if (mode == FileMode.SYMLINK)
return "symbollink-folder";
- }
-
- return "";
+ else
+ return "";
}
});
@@ -185,7 +181,7 @@ public class SourceTreePanel extends AbstractSourcePagePanel {
}
AbstractLink link;
- if (type == TreeNode.Type.DIRECTORY) {
+ if (mode == FileMode.TREE) {
link = new BookmarkablePageLink<Void>("file", SourceTreePage.class, params);
} else {
link = new BookmarkablePageLink<Void>("file", SourceBlobPage.class, params);