1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.pgm;
44
45 import static java.util.function.Predicate.isEqual;
46 import static org.eclipse.jgit.lib.FileMode.EXECUTABLE_FILE;
47 import static org.eclipse.jgit.lib.FileMode.GITLINK;
48 import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
49 import static org.eclipse.jgit.lib.FileMode.SYMLINK;
50
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.List;
54
55 import org.eclipse.jgit.dircache.DirCacheIterator;
56 import org.eclipse.jgit.lib.Constants;
57 import org.eclipse.jgit.lib.FileMode;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.revwalk.RevCommit;
60 import org.eclipse.jgit.revwalk.RevWalk;
61 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
62 import org.eclipse.jgit.treewalk.TreeWalk;
63 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
64 import org.eclipse.jgit.util.QuotedString;
65 import org.kohsuke.args4j.Option;
66 import org.kohsuke.args4j.spi.StopOptionHandler;
67
68 @Command(common = true, usage = "usage_LsFiles")
69 class LsFiles extends TextBuiltin {
70
71 @Option(name = "--", metaVar = "metaVar_paths", handler = StopOptionHandler.class)
72 private List<String> paths = new ArrayList<>();
73
74 @Override
75 protected void run() throws Exception {
76 try (RevWalk rw = new RevWalk(db);
77 TreeWalk tw = new TreeWalk(db)) {
78 final ObjectId head = db.resolve(Constants.HEAD);
79 if (head == null) {
80 return;
81 }
82 RevCommit c = rw.parseCommit(head);
83 CanonicalTreeParser p = new CanonicalTreeParser();
84 p.reset(rw.getObjectReader(), c.getTree());
85 tw.reset();
86 if (paths.size() > 0) {
87 tw.setFilter(PathFilterGroup.createFromStrings(paths));
88 }
89 tw.addTree(p);
90 tw.addTree(new DirCacheIterator(db.readDirCache()));
91 tw.setRecursive(true);
92 while (tw.next()) {
93 if (filterFileMode(tw, EXECUTABLE_FILE, GITLINK, REGULAR_FILE,
94 SYMLINK)) {
95 outw.println(
96 QuotedString.GIT_PATH.quote(tw.getPathString()));
97 }
98 }
99 }
100 }
101
102 private boolean filterFileMode(TreeWalk tw, FileMode... modes) {
103 return Arrays.stream(modes).anyMatch(isEqual(tw.getFileMode(0))
104 .or(isEqual(tw.getFileMode(1))));
105 }
106 }