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.

LsTree.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.pgm;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.jgit.lib.Constants;
  17. import org.eclipse.jgit.lib.FileMode;
  18. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  19. import org.eclipse.jgit.treewalk.TreeWalk;
  20. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  21. import org.eclipse.jgit.util.QuotedString;
  22. import org.kohsuke.args4j.Argument;
  23. import org.kohsuke.args4j.Option;
  24. import org.kohsuke.args4j.spi.StopOptionHandler;
  25. @Command(common = true, usage = "usage_LsTree")
  26. class LsTree extends TextBuiltin {
  27. @Option(name = "--recursive", usage = "usage_recurseIntoSubtrees", aliases = { "-r" })
  28. private boolean recursive;
  29. @Argument(index = 0, required = true, metaVar = "metaVar_treeish")
  30. private AbstractTreeIterator tree;
  31. @Argument(index = 1)
  32. @Option(name = "--", metaVar = "metaVar_paths", handler = StopOptionHandler.class)
  33. private List<String> paths = new ArrayList<>();
  34. /** {@inheritDoc} */
  35. @Override
  36. protected void run() {
  37. try (TreeWalk walk = new TreeWalk(db)) {
  38. walk.reset(); // drop the first empty tree, which we do not need here
  39. if (!paths.isEmpty()) {
  40. walk.setFilter(PathFilterGroup.createFromStrings(paths));
  41. }
  42. walk.setRecursive(recursive);
  43. walk.addTree(tree);
  44. while (walk.next()) {
  45. final FileMode mode = walk.getFileMode(0);
  46. if (mode == FileMode.TREE) {
  47. outw.print('0');
  48. }
  49. outw.print(mode);
  50. outw.print(' ');
  51. outw.print(Constants.typeString(mode.getObjectType()));
  52. outw.print(' ');
  53. outw.print(walk.getObjectId(0).name());
  54. outw.print('\t');
  55. outw.print(QuotedString.GIT_PATH.quote(walk.getPathString()));
  56. outw.println();
  57. }
  58. } catch (IOException e) {
  59. throw die(e.getMessage(), e);
  60. }
  61. }
  62. }