Skip to content

Commit 4c5c8d2

Browse files
committed
Lists subdirectories at the top of the source tree
Feature: Issue 158 Change-Id: I9799f30f727057599ce4ddea387070a03bb1d30a
1 parent e54582f commit 4c5c8d2

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

java/com/google/gitiles/TreeSoyData.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class TreeSoyData {
4141
*/
4242
private static final int MAX_SYMLINK_TARGET_LENGTH = 72;
4343

44+
private static final Map<String, Integer> TYPE_WEIGHT =
45+
Map.of(
46+
"TREE", 0,
47+
"GITLINK", 1,
48+
"SYMLINK", 2,
49+
"REGULAR_FILE", 3,
50+
"EXECUTABLE_FILE", 3);
51+
4452
/**
4553
* Maximum number of bytes to load from a blob that claims to be a symlink. If the blob is larger
4654
* than this byte limit it will be displayed as a binary file instead of as a symlink.
@@ -65,6 +73,10 @@ static String getTargetDisplayName(String target) {
6573
return lastSlash >= 0 ? "..." + target.substring(lastSlash) : target;
6674
}
6775

76+
static int sortByType(Map<String, String> m1, Map<String, String> m2) {
77+
return TYPE_WEIGHT.get(m1.get("type")).compareTo(TYPE_WEIGHT.get(m2.get("type")));
78+
}
79+
6880
private final ObjectReader reader;
6981
private final GitilesView view;
7082
private final Config cfg;
@@ -90,7 +102,7 @@ public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw)
90102
throws MissingObjectException, IOException {
91103
ReadmeHelper readme =
92104
new ReadmeHelper(reader, view, MarkdownConfig.get(cfg), rootTree, requestUri);
93-
List<Object> entries = Lists.newArrayList();
105+
List<Map<String, String>> entries = Lists.newArrayList();
94106
GitilesView.Builder urlBuilder = GitilesView.path().copyFrom(view);
95107
while (tw.next()) {
96108
FileType type = FileType.forEntry(tw);
@@ -129,6 +141,8 @@ public Map<String, Object> toSoyData(ObjectId treeId, TreeWalk tw)
129141
entries.add(entry);
130142
}
131143

144+
entries.sort(TreeSoyData::sortByType);
145+
132146
Map<String, Object> data = Maps.newHashMapWithExpectedSize(3);
133147
data.put("sha", treeId.name());
134148
data.put("entries", entries);

javatests/com/google/gitiles/PathServletTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public void subTreeHtml() throws Exception {
6464
assertThat(data).containsEntry("type", "TREE");
6565
List<Map<String, ?>> entries = getTreeEntries(data);
6666
assertThat(entries).hasSize(2);
67-
assertThat(entries.get(0).get("name")).isEqualTo("baz");
68-
assertThat(entries.get(1).get("name")).isEqualTo("foo/");
67+
assertThat(entries.get(0).get("name")).isEqualTo("foo/");
68+
assertThat(entries.get(1).get("name")).isEqualTo("baz");
6969

7070
data = buildData("/repo/+/master/foo");
7171
assertThat(data).containsEntry("type", "TREE");

javatests/com/google/gitiles/TreeSoyDataTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.google.gitiles.TreeSoyData.getTargetDisplayName;
1919
import static com.google.gitiles.TreeSoyData.resolveTargetUrl;
20+
import static com.google.gitiles.TreeSoyData.sortByType;
2021

2122
import com.google.common.base.Strings;
23+
import java.util.Map;
24+
import java.util.HashMap;
2225
import org.eclipse.jgit.lib.ObjectId;
2326
import org.junit.Test;
2427
import org.junit.runner.RunWith;
@@ -64,4 +67,25 @@ public void resolveTargetUrlReturnsUrl() throws Exception {
6467
assertThat(resolveTargetUrl(view, "../../../../")).isNull();
6568
assertThat(resolveTargetUrl(view, "../../a/../../..")).isNull();
6669
}
70+
71+
@Test
72+
public void sortByTypeSortsCorrect() throws Exception {
73+
Map<String, String> m1 = new HashMap<String, String>();
74+
Map<String, String> m2 = new HashMap<String, String>();
75+
Map<String, String> m3 = new HashMap<String, String>();
76+
Map<String, String> m4 = new HashMap<String, String>();
77+
Map<String, String> m5 = new HashMap<String, String>();
78+
m1.put("type", "TREE");
79+
m2.put("type", "TREE");
80+
m3.put("type", "SYMLINK");
81+
m4.put("type", "REGULAR_FILE");
82+
m5.put("type", "GITLINK");
83+
assertThat(sortByType(m1, m2)).isEqualTo(0);
84+
assertThat(sortByType(m2, m3)).isEqualTo(-1);
85+
assertThat(sortByType(m3, m4)).isEqualTo(-1);
86+
assertThat(sortByType(m4, m1)).isEqualTo(1);
87+
assertThat(sortByType(m1, m4)).isEqualTo(-1);
88+
assertThat(sortByType(m5, m2)).isEqualTo(1);
89+
assertThat(sortByType(m2, m5)).isEqualTo(-1);
90+
}
6791
}

0 commit comments

Comments
 (0)