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.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.List;
55
56 import org.eclipse.jgit.dircache.DirCacheIterator;
57 import org.eclipse.jgit.errors.RevisionSyntaxException;
58 import org.eclipse.jgit.lib.Constants;
59 import org.eclipse.jgit.lib.FileMode;
60 import org.eclipse.jgit.lib.ObjectId;
61 import org.eclipse.jgit.revwalk.RevCommit;
62 import org.eclipse.jgit.revwalk.RevWalk;
63 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
64 import org.eclipse.jgit.treewalk.TreeWalk;
65 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
66 import org.eclipse.jgit.util.QuotedString;
67 import org.kohsuke.args4j.Option;
68 import org.kohsuke.args4j.spi.StopOptionHandler;
69
70 @Command(common = true, usage = "usage_LsFiles")
71 class LsFiles extends TextBuiltin {
72
73 @Option(name = "--", metaVar = "metaVar_paths", handler = StopOptionHandler.class)
74 private List<String> paths = new ArrayList<>();
75
76 @Override
77 protected void run() {
78 try (RevWalk rw = new RevWalk(db);
79 TreeWalk tw = new TreeWalk(db)) {
80 final ObjectId head = db.resolve(Constants.HEAD);
81 if (head == null) {
82 return;
83 }
84 RevCommit c = rw.parseCommit(head);
85 CanonicalTreeParser p = new CanonicalTreeParser();
86 p.reset(rw.getObjectReader(), c.getTree());
87 tw.reset();
88 if (!paths.isEmpty()) {
89 tw.setFilter(PathFilterGroup.createFromStrings(paths));
90 }
91 tw.addTree(p);
92 tw.addTree(new DirCacheIterator(db.readDirCache()));
93 tw.setRecursive(true);
94 while (tw.next()) {
95 if (filterFileMode(tw, EXECUTABLE_FILE, GITLINK, REGULAR_FILE,
96 SYMLINK)) {
97 outw.println(
98 QuotedString.GIT_PATH.quote(tw.getPathString()));
99 }
100 }
101 } catch (RevisionSyntaxException | IOException e) {
102 throw die(e.getMessage(), e);
103 }
104 }
105
106 private boolean filterFileMode(TreeWalk tw, FileMode... modes) {
107 return Arrays.stream(modes).anyMatch(isEqual(tw.getFileMode(0))
108 .or(isEqual(tw.getFileMode(1))));
109 }
110 }