ls-files.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * This merges the file listing in the directory cache index
  3. * with the actual working directory list, and shows different
  4. * combinations of the two.
  5. *
  6. * Copyright (C) Linus Torvalds, 2005
  7. */
  8. #include "cache.h"
  9. #include "repository.h"
  10. #include "config.h"
  11. #include "quote.h"
  12. #include "dir.h"
  13. #include "builtin.h"
  14. #include "tree.h"
  15. #include "parse-options.h"
  16. #include "resolve-undo.h"
  17. #include "string-list.h"
  18. #include "pathspec.h"
  19. #include "run-command.h"
  20. #include "submodule.h"
  21. #include "submodule-config.h"
  22. static int abbrev;
  23. static int show_deleted;
  24. static int show_cached;
  25. static int show_others;
  26. static int show_stage;
  27. static int show_unmerged;
  28. static int show_resolve_undo;
  29. static int show_modified;
  30. static int show_killed;
  31. static int show_valid_bit;
  32. static int show_fsmonitor_bit;
  33. static int line_terminator = '\n';
  34. static int debug_mode;
  35. static int show_eol;
  36. static int recurse_submodules;
  37. static const char *prefix;
  38. static int max_prefix_len;
  39. static int prefix_len;
  40. static struct pathspec pathspec;
  41. static int error_unmatch;
  42. static char *ps_matched;
  43. static const char *with_tree;
  44. static int exc_given;
  45. static int exclude_args;
  46. static const char *tag_cached = "";
  47. static const char *tag_unmerged = "";
  48. static const char *tag_removed = "";
  49. static const char *tag_other = "";
  50. static const char *tag_killed = "";
  51. static const char *tag_modified = "";
  52. static const char *tag_skip_worktree = "";
  53. static const char *tag_resolve_undo = "";
  54. static void write_eolinfo(const struct index_state *istate,
  55. const struct cache_entry *ce, const char *path)
  56. {
  57. if (show_eol) {
  58. struct stat st;
  59. const char *i_txt = "";
  60. const char *w_txt = "";
  61. const char *a_txt = get_convert_attr_ascii(istate, path);
  62. if (ce && S_ISREG(ce->ce_mode))
  63. i_txt = get_cached_convert_stats_ascii(istate,
  64. ce->name);
  65. if (!lstat(path, &st) && S_ISREG(st.st_mode))
  66. w_txt = get_wt_convert_stats_ascii(path);
  67. printf("i/%-5s w/%-5s attr/%-17s\t", i_txt, w_txt, a_txt);
  68. }
  69. }
  70. static void write_name(const char *name)
  71. {
  72. /*
  73. * With "--full-name", prefix_len=0; this caller needs to pass
  74. * an empty string in that case (a NULL is good for "").
  75. */
  76. write_name_quoted_relative(name, prefix_len ? prefix : NULL,
  77. stdout, line_terminator);
  78. }
  79. static const char *get_tag(const struct cache_entry *ce, const char *tag)
  80. {
  81. static char alttag[4];
  82. if (tag && *tag && ((show_valid_bit && (ce->ce_flags & CE_VALID)) ||
  83. (show_fsmonitor_bit && (ce->ce_flags & CE_FSMONITOR_VALID)))) {
  84. memcpy(alttag, tag, 3);
  85. if (isalpha(tag[0])) {
  86. alttag[0] = tolower(tag[0]);
  87. } else if (tag[0] == '?') {
  88. alttag[0] = '!';
  89. } else {
  90. alttag[0] = 'v';
  91. alttag[1] = tag[0];
  92. alttag[2] = ' ';
  93. alttag[3] = 0;
  94. }
  95. tag = alttag;
  96. }
  97. return tag;
  98. }
  99. static void print_debug(const struct cache_entry *ce)
  100. {
  101. if (debug_mode) {
  102. const struct stat_data *sd = &ce->ce_stat_data;
  103. printf(" ctime: %u:%u\n", sd->sd_ctime.sec, sd->sd_ctime.nsec);
  104. printf(" mtime: %u:%u\n", sd->sd_mtime.sec, sd->sd_mtime.nsec);
  105. printf(" dev: %u\tino: %u\n", sd->sd_dev, sd->sd_ino);
  106. printf(" uid: %u\tgid: %u\n", sd->sd_uid, sd->sd_gid);
  107. printf(" size: %u\tflags: %x\n", sd->sd_size, ce->ce_flags);
  108. }
  109. }
  110. static void show_dir_entry(const struct index_state *istate,
  111. const char *tag, struct dir_entry *ent)
  112. {
  113. int len = max_prefix_len;
  114. if (len > ent->len)
  115. die("git ls-files: internal error - directory entry not superset of prefix");
  116. /* If ps_matches is non-NULL, figure out which pathspec(s) match. */
  117. if (ps_matched)
  118. dir_path_match(istate, ent, &pathspec, len, ps_matched);
  119. fputs(tag, stdout);
  120. write_eolinfo(istate, NULL, ent->name);
  121. write_name(ent->name);
  122. }
  123. static void show_other_files(const struct index_state *istate,
  124. const struct dir_struct *dir)
  125. {
  126. int i;
  127. for (i = 0; i < dir->nr; i++) {
  128. struct dir_entry *ent = dir->entries[i];
  129. if (!index_name_is_other(istate, ent->name, ent->len))
  130. continue;
  131. show_dir_entry(istate, tag_other, ent);
  132. }
  133. }
  134. static void show_killed_files(const struct index_state *istate,
  135. const struct dir_struct *dir)
  136. {
  137. int i;
  138. for (i = 0; i < dir->nr; i++) {
  139. struct dir_entry *ent = dir->entries[i];
  140. char *cp, *sp;
  141. int pos, len, killed = 0;
  142. for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
  143. sp = strchr(cp, '/');
  144. if (!sp) {
  145. /* If ent->name is prefix of an entry in the
  146. * cache, it will be killed.
  147. */
  148. pos = index_name_pos(istate, ent->name, ent->len);
  149. if (0 <= pos)
  150. BUG("killed-file %.*s not found",
  151. ent->len, ent->name);
  152. pos = -pos - 1;
  153. while (pos < istate->cache_nr &&
  154. ce_stage(istate->cache[pos]))
  155. pos++; /* skip unmerged */
  156. if (istate->cache_nr <= pos)
  157. break;
  158. /* pos points at a name immediately after
  159. * ent->name in the cache. Does it expect
  160. * ent->name to be a directory?
  161. */
  162. len = ce_namelen(istate->cache[pos]);
  163. if ((ent->len < len) &&
  164. !strncmp(istate->cache[pos]->name,
  165. ent->name, ent->len) &&
  166. istate->cache[pos]->name[ent->len] == '/')
  167. killed = 1;
  168. break;
  169. }
  170. if (0 <= index_name_pos(istate, ent->name, sp - ent->name)) {
  171. /* If any of the leading directories in
  172. * ent->name is registered in the cache,
  173. * ent->name will be killed.
  174. */
  175. killed = 1;
  176. break;
  177. }
  178. }
  179. if (killed)
  180. show_dir_entry(istate, tag_killed, dir->entries[i]);
  181. }
  182. }
  183. static void show_files(struct repository *repo, struct dir_struct *dir);
  184. static void show_submodule(struct repository *superproject,
  185. struct dir_struct *dir, const char *path)
  186. {
  187. struct repository subrepo;
  188. const struct submodule *sub = submodule_from_path(superproject,
  189. &null_oid, path);
  190. if (repo_submodule_init(&subrepo, superproject, sub))
  191. return;
  192. if (repo_read_index(&subrepo) < 0)
  193. die("index file corrupt");
  194. show_files(&subrepo, dir);
  195. repo_clear(&subrepo);
  196. }
  197. static void show_ce(struct repository *repo, struct dir_struct *dir,
  198. const struct cache_entry *ce, const char *fullname,
  199. const char *tag)
  200. {
  201. if (max_prefix_len > strlen(fullname))
  202. die("git ls-files: internal error - cache entry not superset of prefix");
  203. if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
  204. is_submodule_active(repo, ce->name)) {
  205. show_submodule(repo, dir, ce->name);
  206. } else if (match_pathspec(repo->index, &pathspec, fullname, strlen(fullname),
  207. max_prefix_len, ps_matched,
  208. S_ISDIR(ce->ce_mode) ||
  209. S_ISGITLINK(ce->ce_mode))) {
  210. tag = get_tag(ce, tag);
  211. if (!show_stage) {
  212. fputs(tag, stdout);
  213. } else {
  214. printf("%s%06o %s %d\t",
  215. tag,
  216. ce->ce_mode,
  217. find_unique_abbrev(&ce->oid, abbrev),
  218. ce_stage(ce));
  219. }
  220. write_eolinfo(repo->index, ce, fullname);
  221. write_name(fullname);
  222. print_debug(ce);
  223. }
  224. }
  225. static void show_ru_info(const struct index_state *istate)
  226. {
  227. struct string_list_item *item;
  228. if (!istate->resolve_undo)
  229. return;
  230. for_each_string_list_item(item, istate->resolve_undo) {
  231. const char *path = item->string;
  232. struct resolve_undo_info *ui = item->util;
  233. int i, len;
  234. len = strlen(path);
  235. if (len < max_prefix_len)
  236. continue; /* outside of the prefix */
  237. if (!match_pathspec(istate, &pathspec, path, len,
  238. max_prefix_len, ps_matched, 0))
  239. continue; /* uninterested */
  240. for (i = 0; i < 3; i++) {
  241. if (!ui->mode[i])
  242. continue;
  243. printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
  244. find_unique_abbrev(&ui->oid[i], abbrev),
  245. i + 1);
  246. write_name(path);
  247. }
  248. }
  249. }
  250. static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
  251. const char *fullname, const struct cache_entry *ce)
  252. {
  253. int dtype = ce_to_dtype(ce);
  254. return is_excluded(dir, istate, fullname, &dtype);
  255. }
  256. static void construct_fullname(struct strbuf *out, const struct repository *repo,
  257. const struct cache_entry *ce)
  258. {
  259. strbuf_reset(out);
  260. if (repo->submodule_prefix)
  261. strbuf_addstr(out, repo->submodule_prefix);
  262. strbuf_addstr(out, ce->name);
  263. }
  264. static void show_files(struct repository *repo, struct dir_struct *dir)
  265. {
  266. int i;
  267. struct strbuf fullname = STRBUF_INIT;
  268. /* For cached/deleted files we don't need to even do the readdir */
  269. if (show_others || show_killed) {
  270. if (!show_others)
  271. dir->flags |= DIR_COLLECT_KILLED_ONLY;
  272. fill_directory(dir, repo->index, &pathspec);
  273. if (show_others)
  274. show_other_files(repo->index, dir);
  275. if (show_killed)
  276. show_killed_files(repo->index, dir);
  277. }
  278. if (show_cached || show_stage) {
  279. for (i = 0; i < repo->index->cache_nr; i++) {
  280. const struct cache_entry *ce = repo->index->cache[i];
  281. construct_fullname(&fullname, repo, ce);
  282. if ((dir->flags & DIR_SHOW_IGNORED) &&
  283. !ce_excluded(dir, repo->index, fullname.buf, ce))
  284. continue;
  285. if (show_unmerged && !ce_stage(ce))
  286. continue;
  287. if (ce->ce_flags & CE_UPDATE)
  288. continue;
  289. show_ce(repo, dir, ce, fullname.buf,
  290. ce_stage(ce) ? tag_unmerged :
  291. (ce_skip_worktree(ce) ? tag_skip_worktree :
  292. tag_cached));
  293. }
  294. }
  295. if (show_deleted || show_modified) {
  296. for (i = 0; i < repo->index->cache_nr; i++) {
  297. const struct cache_entry *ce = repo->index->cache[i];
  298. struct stat st;
  299. int err;
  300. construct_fullname(&fullname, repo, ce);
  301. if ((dir->flags & DIR_SHOW_IGNORED) &&
  302. !ce_excluded(dir, repo->index, fullname.buf, ce))
  303. continue;
  304. if (ce->ce_flags & CE_UPDATE)
  305. continue;
  306. if (ce_skip_worktree(ce))
  307. continue;
  308. err = lstat(fullname.buf, &st);
  309. if (show_deleted && err)
  310. show_ce(repo, dir, ce, fullname.buf, tag_removed);
  311. if (show_modified && ie_modified(repo->index, ce, &st, 0))
  312. show_ce(repo, dir, ce, fullname.buf, tag_modified);
  313. }
  314. }
  315. strbuf_release(&fullname);
  316. }
  317. /*
  318. * Prune the index to only contain stuff starting with "prefix"
  319. */
  320. static void prune_index(struct index_state *istate,
  321. const char *prefix, size_t prefixlen)
  322. {
  323. int pos;
  324. unsigned int first, last;
  325. if (!prefix || !istate->cache_nr)
  326. return;
  327. pos = index_name_pos(istate, prefix, prefixlen);
  328. if (pos < 0)
  329. pos = -pos-1;
  330. first = pos;
  331. last = istate->cache_nr;
  332. while (last > first) {
  333. int next = first + ((last - first) >> 1);
  334. const struct cache_entry *ce = istate->cache[next];
  335. if (!strncmp(ce->name, prefix, prefixlen)) {
  336. first = next+1;
  337. continue;
  338. }
  339. last = next;
  340. }
  341. MOVE_ARRAY(istate->cache, istate->cache + pos, last - pos);
  342. istate->cache_nr = last - pos;
  343. }
  344. static int get_common_prefix_len(const char *common_prefix)
  345. {
  346. int common_prefix_len;
  347. if (!common_prefix)
  348. return 0;
  349. common_prefix_len = strlen(common_prefix);
  350. /*
  351. * If the prefix has a trailing slash, strip it so that submodules wont
  352. * be pruned from the index.
  353. */
  354. if (common_prefix[common_prefix_len - 1] == '/')
  355. common_prefix_len--;
  356. return common_prefix_len;
  357. }
  358. /*
  359. * Read the tree specified with --with-tree option
  360. * (typically, HEAD) into stage #1 and then
  361. * squash them down to stage #0. This is used for
  362. * --error-unmatch to list and check the path patterns
  363. * that were given from the command line. We are not
  364. * going to write this index out.
  365. */
  366. void overlay_tree_on_index(struct index_state *istate,
  367. const char *tree_name, const char *prefix)
  368. {
  369. struct tree *tree;
  370. struct object_id oid;
  371. struct pathspec pathspec;
  372. struct cache_entry *last_stage0 = NULL;
  373. int i;
  374. if (get_oid(tree_name, &oid))
  375. die("tree-ish %s not found.", tree_name);
  376. tree = parse_tree_indirect(&oid);
  377. if (!tree)
  378. die("bad tree-ish %s", tree_name);
  379. /* Hoist the unmerged entries up to stage #3 to make room */
  380. for (i = 0; i < istate->cache_nr; i++) {
  381. struct cache_entry *ce = istate->cache[i];
  382. if (!ce_stage(ce))
  383. continue;
  384. ce->ce_flags |= CE_STAGEMASK;
  385. }
  386. if (prefix) {
  387. static const char *(matchbuf[1]);
  388. matchbuf[0] = NULL;
  389. parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
  390. PATHSPEC_PREFER_CWD, prefix, matchbuf);
  391. } else
  392. memset(&pathspec, 0, sizeof(pathspec));
  393. if (read_tree(the_repository, tree, 1, &pathspec, istate))
  394. die("unable to read tree entries %s", tree_name);
  395. for (i = 0; i < istate->cache_nr; i++) {
  396. struct cache_entry *ce = istate->cache[i];
  397. switch (ce_stage(ce)) {
  398. case 0:
  399. last_stage0 = ce;
  400. /* fallthru */
  401. default:
  402. continue;
  403. case 1:
  404. /*
  405. * If there is stage #0 entry for this, we do not
  406. * need to show it. We use CE_UPDATE bit to mark
  407. * such an entry.
  408. */
  409. if (last_stage0 &&
  410. !strcmp(last_stage0->name, ce->name))
  411. ce->ce_flags |= CE_UPDATE;
  412. }
  413. }
  414. }
  415. static const char * const ls_files_usage[] = {
  416. N_("git ls-files [<options>] [<file>...]"),
  417. NULL
  418. };
  419. static int option_parse_exclude(const struct option *opt,
  420. const char *arg, int unset)
  421. {
  422. struct string_list *exclude_list = opt->value;
  423. BUG_ON_OPT_NEG(unset);
  424. exc_given = 1;
  425. string_list_append(exclude_list, arg);
  426. return 0;
  427. }
  428. static int option_parse_exclude_from(const struct option *opt,
  429. const char *arg, int unset)
  430. {
  431. struct dir_struct *dir = opt->value;
  432. BUG_ON_OPT_NEG(unset);
  433. exc_given = 1;
  434. add_patterns_from_file(dir, arg);
  435. return 0;
  436. }
  437. static int option_parse_exclude_standard(const struct option *opt,
  438. const char *arg, int unset)
  439. {
  440. struct dir_struct *dir = opt->value;
  441. BUG_ON_OPT_NEG(unset);
  442. BUG_ON_OPT_ARG(arg);
  443. exc_given = 1;
  444. setup_standard_excludes(dir);
  445. return 0;
  446. }
  447. int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
  448. {
  449. int require_work_tree = 0, show_tag = 0, i;
  450. const char *max_prefix;
  451. struct dir_struct dir;
  452. struct pattern_list *pl;
  453. struct string_list exclude_list = STRING_LIST_INIT_NODUP;
  454. struct option builtin_ls_files_options[] = {
  455. /* Think twice before adding "--nul" synonym to this */
  456. OPT_SET_INT('z', NULL, &line_terminator,
  457. N_("paths are separated with NUL character"), '\0'),
  458. OPT_BOOL('t', NULL, &show_tag,
  459. N_("identify the file status with tags")),
  460. OPT_BOOL('v', NULL, &show_valid_bit,
  461. N_("use lowercase letters for 'assume unchanged' files")),
  462. OPT_BOOL('f', NULL, &show_fsmonitor_bit,
  463. N_("use lowercase letters for 'fsmonitor clean' files")),
  464. OPT_BOOL('c', "cached", &show_cached,
  465. N_("show cached files in the output (default)")),
  466. OPT_BOOL('d', "deleted", &show_deleted,
  467. N_("show deleted files in the output")),
  468. OPT_BOOL('m', "modified", &show_modified,
  469. N_("show modified files in the output")),
  470. OPT_BOOL('o', "others", &show_others,
  471. N_("show other files in the output")),
  472. OPT_BIT('i', "ignored", &dir.flags,
  473. N_("show ignored files in the output"),
  474. DIR_SHOW_IGNORED),
  475. OPT_BOOL('s', "stage", &show_stage,
  476. N_("show staged contents' object name in the output")),
  477. OPT_BOOL('k', "killed", &show_killed,
  478. N_("show files on the filesystem that need to be removed")),
  479. OPT_BIT(0, "directory", &dir.flags,
  480. N_("show 'other' directories' names only"),
  481. DIR_SHOW_OTHER_DIRECTORIES),
  482. OPT_BOOL(0, "eol", &show_eol, N_("show line endings of files")),
  483. OPT_NEGBIT(0, "empty-directory", &dir.flags,
  484. N_("don't show empty directories"),
  485. DIR_HIDE_EMPTY_DIRECTORIES),
  486. OPT_BOOL('u', "unmerged", &show_unmerged,
  487. N_("show unmerged files in the output")),
  488. OPT_BOOL(0, "resolve-undo", &show_resolve_undo,
  489. N_("show resolve-undo information")),
  490. OPT_CALLBACK_F('x', "exclude", &exclude_list, N_("pattern"),
  491. N_("skip files matching pattern"),
  492. PARSE_OPT_NONEG, option_parse_exclude),
  493. OPT_CALLBACK_F('X', "exclude-from", &dir, N_("file"),
  494. N_("exclude patterns are read from <file>"),
  495. PARSE_OPT_NONEG, option_parse_exclude_from),
  496. OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, N_("file"),
  497. N_("read additional per-directory exclude patterns in <file>")),
  498. OPT_CALLBACK_F(0, "exclude-standard", &dir, NULL,
  499. N_("add the standard git exclusions"),
  500. PARSE_OPT_NOARG | PARSE_OPT_NONEG,
  501. option_parse_exclude_standard),
  502. OPT_SET_INT_F(0, "full-name", &prefix_len,
  503. N_("make the output relative to the project top directory"),
  504. 0, PARSE_OPT_NONEG),
  505. OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
  506. N_("recurse through submodules")),
  507. OPT_BOOL(0, "error-unmatch", &error_unmatch,
  508. N_("if any <file> is not in the index, treat this as an error")),
  509. OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
  510. N_("pretend that paths removed since <tree-ish> are still present")),
  511. OPT__ABBREV(&abbrev),
  512. OPT_BOOL(0, "debug", &debug_mode, N_("show debugging data")),
  513. OPT_END()
  514. };
  515. if (argc == 2 && !strcmp(argv[1], "-h"))
  516. usage_with_options(ls_files_usage, builtin_ls_files_options);
  517. dir_init(&dir);
  518. prefix = cmd_prefix;
  519. if (prefix)
  520. prefix_len = strlen(prefix);
  521. git_config(git_default_config, NULL);
  522. if (repo_read_index(the_repository) < 0)
  523. die("index file corrupt");
  524. argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
  525. ls_files_usage, 0);
  526. pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
  527. for (i = 0; i < exclude_list.nr; i++) {
  528. add_pattern(exclude_list.items[i].string, "", 0, pl, --exclude_args);
  529. }
  530. if (show_tag || show_valid_bit || show_fsmonitor_bit) {
  531. tag_cached = "H ";
  532. tag_unmerged = "M ";
  533. tag_removed = "R ";
  534. tag_modified = "C ";
  535. tag_other = "? ";
  536. tag_killed = "K ";
  537. tag_skip_worktree = "S ";
  538. tag_resolve_undo = "U ";
  539. }
  540. if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
  541. require_work_tree = 1;
  542. if (show_unmerged)
  543. /*
  544. * There's no point in showing unmerged unless
  545. * you also show the stage information.
  546. */
  547. show_stage = 1;
  548. if (dir.exclude_per_dir)
  549. exc_given = 1;
  550. if (require_work_tree && !is_inside_work_tree())
  551. setup_work_tree();
  552. if (recurse_submodules &&
  553. (show_stage || show_deleted || show_others || show_unmerged ||
  554. show_killed || show_modified || show_resolve_undo || with_tree))
  555. die("ls-files --recurse-submodules unsupported mode");
  556. if (recurse_submodules && error_unmatch)
  557. die("ls-files --recurse-submodules does not support "
  558. "--error-unmatch");
  559. parse_pathspec(&pathspec, 0,
  560. PATHSPEC_PREFER_CWD,
  561. prefix, argv);
  562. /*
  563. * Find common prefix for all pathspec's
  564. * This is used as a performance optimization which unfortunately cannot
  565. * be done when recursing into submodules because when a pathspec is
  566. * given which spans repository boundaries you can't simply remove the
  567. * submodule entry because the pathspec may match something inside the
  568. * submodule.
  569. */
  570. if (recurse_submodules)
  571. max_prefix = NULL;
  572. else
  573. max_prefix = common_prefix(&pathspec);
  574. max_prefix_len = get_common_prefix_len(max_prefix);
  575. prune_index(the_repository->index, max_prefix, max_prefix_len);
  576. /* Treat unmatching pathspec elements as errors */
  577. if (pathspec.nr && error_unmatch)
  578. ps_matched = xcalloc(pathspec.nr, 1);
  579. if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
  580. die("ls-files --ignored needs some exclude pattern");
  581. /* With no flags, we default to showing the cached files */
  582. if (!(show_stage || show_deleted || show_others || show_unmerged ||
  583. show_killed || show_modified || show_resolve_undo))
  584. show_cached = 1;
  585. if (with_tree) {
  586. /*
  587. * Basic sanity check; show-stages and show-unmerged
  588. * would not make any sense with this option.
  589. */
  590. if (show_stage || show_unmerged)
  591. die("ls-files --with-tree is incompatible with -s or -u");
  592. overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
  593. }
  594. show_files(the_repository, &dir);
  595. if (show_resolve_undo)
  596. show_ru_info(the_repository->index);
  597. if (ps_matched) {
  598. int bad;
  599. bad = report_path_error(ps_matched, &pathspec);
  600. if (bad)
  601. fprintf(stderr, "Did you forget to 'git add'?\n");
  602. return bad ? 1 : 0;
  603. }
  604. dir_clear(&dir);
  605. return 0;
  606. }