You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LsFiles.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2017, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import static java.util.function.Predicate.isEqual;
  12. import static org.eclipse.jgit.lib.FileMode.EXECUTABLE_FILE;
  13. import static org.eclipse.jgit.lib.FileMode.GITLINK;
  14. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  15. import static org.eclipse.jgit.lib.FileMode.SYMLINK;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import org.eclipse.jgit.dircache.DirCacheIterator;
  21. import org.eclipse.jgit.errors.RevisionSyntaxException;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.FileMode;
  24. import org.eclipse.jgit.lib.ObjectId;
  25. import org.eclipse.jgit.revwalk.RevCommit;
  26. import org.eclipse.jgit.revwalk.RevWalk;
  27. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  28. import org.eclipse.jgit.treewalk.TreeWalk;
  29. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  30. import org.eclipse.jgit.util.QuotedString;
  31. import org.kohsuke.args4j.Option;
  32. import org.kohsuke.args4j.spi.StopOptionHandler;
  33. @Command(common = true, usage = "usage_LsFiles")
  34. class LsFiles extends TextBuiltin {
  35. @Option(name = "--", metaVar = "metaVar_paths", handler = StopOptionHandler.class)
  36. private List<String> paths = new ArrayList<>();
  37. @Override
  38. protected void run() {
  39. try (RevWalk rw = new RevWalk(db);
  40. TreeWalk tw = new TreeWalk(db)) {
  41. final ObjectId head = db.resolve(Constants.HEAD);
  42. if (head == null) {
  43. return;
  44. }
  45. RevCommit c = rw.parseCommit(head);
  46. CanonicalTreeParser p = new CanonicalTreeParser();
  47. p.reset(rw.getObjectReader(), c.getTree());
  48. tw.reset(); // drop the first empty tree, which we do not need here
  49. if (!paths.isEmpty()) {
  50. tw.setFilter(PathFilterGroup.createFromStrings(paths));
  51. }
  52. tw.addTree(p);
  53. tw.addTree(new DirCacheIterator(db.readDirCache()));
  54. tw.setRecursive(true);
  55. while (tw.next()) {
  56. if (filterFileMode(tw, EXECUTABLE_FILE, GITLINK, REGULAR_FILE,
  57. SYMLINK)) {
  58. outw.println(
  59. QuotedString.GIT_PATH.quote(tw.getPathString()));
  60. }
  61. }
  62. } catch (RevisionSyntaxException | IOException e) {
  63. throw die(e.getMessage(), e);
  64. }
  65. }
  66. private boolean filterFileMode(TreeWalk tw, FileMode... modes) {
  67. return Arrays.stream(modes).anyMatch(isEqual(tw.getFileMode(0))
  68. .or(isEqual(tw.getFileMode(1))));
  69. }
  70. }