ls-tree.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #include "cache.h"
  7. #include "config.h"
  8. #include "object-store.h"
  9. #include "blob.h"
  10. #include "tree.h"
  11. #include "commit.h"
  12. #include "quote.h"
  13. #include "builtin.h"
  14. #include "parse-options.h"
  15. #include "pathspec.h"
  16. static int line_termination = '\n';
  17. #define LS_RECURSIVE 1
  18. #define LS_TREE_ONLY 2
  19. #define LS_SHOW_TREES 4
  20. #define LS_NAME_ONLY 8
  21. #define LS_SHOW_SIZE 16
  22. static int abbrev;
  23. static int ls_options;
  24. static struct pathspec pathspec;
  25. static int chomp_prefix;
  26. static const char *ls_tree_prefix;
  27. static const char * const ls_tree_usage[] = {
  28. N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
  29. NULL
  30. };
  31. static int show_recursive(const char *base, int baselen, const char *pathname)
  32. {
  33. int i;
  34. if (ls_options & LS_RECURSIVE)
  35. return 1;
  36. if (!pathspec.nr)
  37. return 0;
  38. for (i = 0; i < pathspec.nr; i++) {
  39. const char *spec = pathspec.items[i].match;
  40. int len, speclen;
  41. if (strncmp(base, spec, baselen))
  42. continue;
  43. len = strlen(pathname);
  44. spec += baselen;
  45. speclen = strlen(spec);
  46. if (speclen <= len)
  47. continue;
  48. if (spec[len] && spec[len] != '/')
  49. continue;
  50. if (memcmp(pathname, spec, len))
  51. continue;
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int show_tree(const struct object_id *oid, struct strbuf *base,
  57. const char *pathname, unsigned mode, int stage, void *context)
  58. {
  59. int retval = 0;
  60. int baselen;
  61. const char *type = blob_type;
  62. if (S_ISGITLINK(mode)) {
  63. /*
  64. * Maybe we want to have some recursive version here?
  65. *
  66. * Something similar to this incomplete example:
  67. *
  68. if (show_subprojects(base, baselen, pathname))
  69. retval = READ_TREE_RECURSIVE;
  70. *
  71. */
  72. type = commit_type;
  73. } else if (S_ISDIR(mode)) {
  74. if (show_recursive(base->buf, base->len, pathname)) {
  75. retval = READ_TREE_RECURSIVE;
  76. if (!(ls_options & LS_SHOW_TREES))
  77. return retval;
  78. }
  79. type = tree_type;
  80. }
  81. else if (ls_options & LS_TREE_ONLY)
  82. return 0;
  83. if (!(ls_options & LS_NAME_ONLY)) {
  84. if (ls_options & LS_SHOW_SIZE) {
  85. char size_text[24];
  86. if (!strcmp(type, blob_type)) {
  87. unsigned long size;
  88. if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
  89. xsnprintf(size_text, sizeof(size_text),
  90. "BAD");
  91. else
  92. xsnprintf(size_text, sizeof(size_text),
  93. "%"PRIuMAX, (uintmax_t)size);
  94. } else
  95. xsnprintf(size_text, sizeof(size_text), "-");
  96. printf("%06o %s %s %7s\t", mode, type,
  97. find_unique_abbrev(oid, abbrev),
  98. size_text);
  99. } else
  100. printf("%06o %s %s\t", mode, type,
  101. find_unique_abbrev(oid, abbrev));
  102. }
  103. baselen = base->len;
  104. strbuf_addstr(base, pathname);
  105. write_name_quoted_relative(base->buf,
  106. chomp_prefix ? ls_tree_prefix : NULL,
  107. stdout, line_termination);
  108. strbuf_setlen(base, baselen);
  109. return retval;
  110. }
  111. int cmd_ls_tree(int argc, const char **argv, const char *prefix)
  112. {
  113. struct object_id oid;
  114. struct tree *tree;
  115. int i, full_tree = 0;
  116. const struct option ls_tree_options[] = {
  117. OPT_BIT('d', NULL, &ls_options, N_("only show trees"),
  118. LS_TREE_ONLY),
  119. OPT_BIT('r', NULL, &ls_options, N_("recurse into subtrees"),
  120. LS_RECURSIVE),
  121. OPT_BIT('t', NULL, &ls_options, N_("show trees when recursing"),
  122. LS_SHOW_TREES),
  123. OPT_SET_INT('z', NULL, &line_termination,
  124. N_("terminate entries with NUL byte"), 0),
  125. OPT_BIT('l', "long", &ls_options, N_("include object size"),
  126. LS_SHOW_SIZE),
  127. OPT_BIT(0, "name-only", &ls_options, N_("list only filenames"),
  128. LS_NAME_ONLY),
  129. OPT_BIT(0, "name-status", &ls_options, N_("list only filenames"),
  130. LS_NAME_ONLY),
  131. OPT_SET_INT(0, "full-name", &chomp_prefix,
  132. N_("use full path names"), 0),
  133. OPT_BOOL(0, "full-tree", &full_tree,
  134. N_("list entire tree; not just current directory "
  135. "(implies --full-name)")),
  136. OPT__ABBREV(&abbrev),
  137. OPT_END()
  138. };
  139. git_config(git_default_config, NULL);
  140. ls_tree_prefix = prefix;
  141. if (prefix && *prefix)
  142. chomp_prefix = strlen(prefix);
  143. argc = parse_options(argc, argv, prefix, ls_tree_options,
  144. ls_tree_usage, 0);
  145. if (full_tree) {
  146. ls_tree_prefix = prefix = NULL;
  147. chomp_prefix = 0;
  148. }
  149. /* -d -r should imply -t, but -d by itself should not have to. */
  150. if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
  151. ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
  152. ls_options |= LS_SHOW_TREES;
  153. if (argc < 1)
  154. usage_with_options(ls_tree_usage, ls_tree_options);
  155. if (get_oid(argv[0], &oid))
  156. die("Not a valid object name %s", argv[0]);
  157. /*
  158. * show_recursive() rolls its own matching code and is
  159. * generally ignorant of 'struct pathspec'. The magic mask
  160. * cannot be lifted until it is converted to use
  161. * match_pathspec() or tree_entry_interesting()
  162. */
  163. parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC &
  164. ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),
  165. PATHSPEC_PREFER_CWD,
  166. prefix, argv + 1);
  167. for (i = 0; i < pathspec.nr; i++)
  168. pathspec.items[i].nowildcard_len = pathspec.items[i].len;
  169. pathspec.has_wildcard = 0;
  170. tree = parse_tree_indirect(&oid);
  171. if (!tree)
  172. die("not a tree object");
  173. return !!read_tree_recursive(the_repository, tree, "", 0, 0,
  174. &pathspec, show_tree, NULL);
  175. }