blob: 15701233e29b240cfa1abdb56bd489306e74c677 [file] [log] [blame]
Linus Torvalds8695c8b2005-04-11 18:55:38 -07001/*
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 */
Linus Torvalds8695c8b2005-04-11 18:55:38 -07008#include "cache.h"
Junio C Hamano22ddf712005-10-14 21:56:46 -07009#include "quote.h"
Linus Torvalds453ec4b2006-05-16 19:02:14 -070010#include "dir.h"
Peter Eriksen0864f262006-05-23 14:15:29 +020011#include "builtin.h"
Junio C Hamano64586e72007-09-12 16:04:22 -070012#include "tree.h"
Miklos Vajnace8e8802009-02-17 15:27:11 +010013#include "parse-options.h"
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080014#include "resolve-undo.h"
15#include "string-list.h"
Linus Torvalds8695c8b2005-04-11 18:55:38 -070016
David Rientjes96f1e582006-08-15 10:23:48 -070017static int abbrev;
18static int show_deleted;
19static int show_cached;
20static int show_others;
21static int show_stage;
22static int show_unmerged;
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080023static int show_resolve_undo;
David Rientjes96f1e582006-08-15 10:23:48 -070024static int show_modified;
25static int show_killed;
26static int show_valid_bit;
Junio C Hamanob83c8342005-04-15 11:11:01 -070027static int line_terminator = '\n';
Thomas Rast84974212010-07-31 00:35:59 +020028static int debug_mode;
Linus Torvalds8695c8b2005-04-11 18:55:38 -070029
Clemens Buchacherefad1a52010-06-03 15:39:18 +020030static const char *prefix;
31static int max_prefix_len;
David Rientjes96f1e582006-08-15 10:23:48 -070032static int prefix_len;
David Rientjes96f1e582006-08-15 10:23:48 -070033static const char **pathspec;
34static int error_unmatch;
35static char *ps_matched;
Junio C Hamano64586e72007-09-12 16:04:22 -070036static const char *with_tree;
Miklos Vajnace8e8802009-02-17 15:27:11 +010037static int exc_given;
Linus Torvalds5be4efb2005-08-21 12:55:33 -070038
Petr Baudis20d37ef2005-04-21 19:47:08 -070039static const char *tag_cached = "";
40static const char *tag_unmerged = "";
41static const char *tag_removed = "";
42static const char *tag_other = "";
Junio C Hamano6ca45942005-05-12 17:17:54 -070043static const char *tag_killed = "";
Junio C Hamanob0391892005-09-19 15:11:15 -070044static const char *tag_modified = "";
Nguyễn Thái Ngọc Duy44a36912009-08-20 20:46:57 +070045static const char *tag_skip_worktree = "";
Junio C Hamano9d9a2f42009-12-25 10:08:04 -080046static const char *tag_resolve_undo = "";
Petr Baudis20d37ef2005-04-21 19:47:08 -070047
Clemens Buchacherefad1a52010-06-03 15:39:18 +020048static void write_name(const char* name, size_t len)
49{
50 write_name_quoted_relative(name, len, prefix, prefix_len, stdout,
51 line_terminator);
52}
53
Linus Torvalds453ec4b2006-05-16 19:02:14 -070054static void show_dir_entry(const char *tag, struct dir_entry *ent)
Linus Torvalds5be4efb2005-08-21 12:55:33 -070055{
Clemens Buchacherefad1a52010-06-03 15:39:18 +020056 int len = max_prefix_len;
Linus Torvalds5be4efb2005-08-21 12:55:33 -070057
58 if (len >= ent->len)
Junio C Hamano7e44c932008-08-31 09:39:19 -070059 die("git ls-files: internal error - directory entry not superset of prefix");
Linus Torvalds5be4efb2005-08-21 12:55:33 -070060
Clemens Buchacher0b509222009-01-14 15:54:35 +010061 if (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))
Linus Torvalds5be4efb2005-08-21 12:55:33 -070062 return;
63
Junio C Hamano22ddf712005-10-14 21:56:46 -070064 fputs(tag, stdout);
Clemens Buchacherefad1a52010-06-03 15:39:18 +020065 write_name(ent->name, ent->len);
Linus Torvalds5be4efb2005-08-21 12:55:33 -070066}
67
Linus Torvalds453ec4b2006-05-16 19:02:14 -070068static void show_other_files(struct dir_struct *dir)
Junio C Hamanofcbc3082005-11-06 17:26:31 -080069{
70 int i;
Linus Torvalds56984542007-04-14 16:22:08 -070071
Linus Torvalds453ec4b2006-05-16 19:02:14 -070072 for (i = 0; i < dir->nr; i++) {
Linus Torvalds453ec4b2006-05-16 19:02:14 -070073 struct dir_entry *ent = dir->entries[i];
Jeff King98fa4732008-10-16 11:07:26 -040074 if (!cache_name_is_other(ent->name, ent->len))
75 continue;
Junio C Hamanofcbc3082005-11-06 17:26:31 -080076 show_dir_entry(tag_other, ent);
77 }
78}
79
Linus Torvalds453ec4b2006-05-16 19:02:14 -070080static void show_killed_files(struct dir_struct *dir)
Junio C Hamano6ca45942005-05-12 17:17:54 -070081{
82 int i;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070083 for (i = 0; i < dir->nr; i++) {
84 struct dir_entry *ent = dir->entries[i];
Junio C Hamano6ca45942005-05-12 17:17:54 -070085 char *cp, *sp;
86 int pos, len, killed = 0;
87
88 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
89 sp = strchr(cp, '/');
90 if (!sp) {
91 /* If ent->name is prefix of an entry in the
92 * cache, it will be killed.
93 */
94 pos = cache_name_pos(ent->name, ent->len);
95 if (0 <= pos)
96 die("bug in show-killed-files");
97 pos = -pos - 1;
98 while (pos < active_nr &&
99 ce_stage(active_cache[pos]))
100 pos++; /* skip unmerged */
101 if (active_nr <= pos)
102 break;
103 /* pos points at a name immediately after
104 * ent->name in the cache. Does it expect
105 * ent->name to be a directory?
106 */
107 len = ce_namelen(active_cache[pos]);
108 if ((ent->len < len) &&
109 !strncmp(active_cache[pos]->name,
110 ent->name, ent->len) &&
111 active_cache[pos]->name[ent->len] == '/')
112 killed = 1;
113 break;
114 }
115 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
116 /* If any of the leading directories in
117 * ent->name is registered in the cache,
118 * ent->name will be killed.
119 */
120 killed = 1;
121 break;
122 }
123 }
124 if (killed)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700125 show_dir_entry(tag_killed, dir->entries[i]);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700126 }
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700127}
128
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700129static void show_ce_entry(const char *tag, struct cache_entry *ce)
130{
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200131 int len = max_prefix_len;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700132
133 if (len >= ce_namelen(ce))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700134 die("git ls-files: internal error - cache entry not superset of prefix");
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700135
Clemens Buchacher0b509222009-01-14 15:54:35 +0100136 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700137 return;
138
Junio C Hamano8bb2e032006-02-12 01:47:57 -0800139 if (tag && *tag && show_valid_bit &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800140 (ce->ce_flags & CE_VALID)) {
Junio C Hamano2bcab242006-02-08 21:50:18 -0800141 static char alttag[4];
142 memcpy(alttag, tag, 3);
143 if (isalpha(tag[0]))
144 alttag[0] = tolower(tag[0]);
145 else if (tag[0] == '?')
146 alttag[0] = '!';
147 else {
148 alttag[0] = 'v';
149 alttag[1] = tag[0];
150 alttag[2] = ' ';
151 alttag[3] = 0;
152 }
153 tag = alttag;
154 }
155
Junio C Hamano22ddf712005-10-14 21:56:46 -0700156 if (!show_stage) {
157 fputs(tag, stdout);
Pierre Habouzit663af342007-09-20 00:42:15 +0200158 } else {
Junio C Hamano22ddf712005-10-14 21:56:46 -0700159 printf("%s%06o %s %d\t",
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700160 tag,
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800161 ce->ce_mode,
Erik Faye-Lund531e7582010-03-20 19:55:28 +0100162 find_unique_abbrev(ce->sha1,abbrev),
Junio C Hamano22ddf712005-10-14 21:56:46 -0700163 ce_stage(ce));
Junio C Hamano22ddf712005-10-14 21:56:46 -0700164 }
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200165 write_name(ce->name, ce_namelen(ce));
Thomas Rast84974212010-07-31 00:35:59 +0200166 if (debug_mode) {
167 printf(" ctime: %d:%d\n", ce->ce_ctime.sec, ce->ce_ctime.nsec);
168 printf(" mtime: %d:%d\n", ce->ce_mtime.sec, ce->ce_mtime.nsec);
169 printf(" dev: %d\tino: %d\n", ce->ce_dev, ce->ce_ino);
170 printf(" uid: %d\tgid: %d\n", ce->ce_uid, ce->ce_gid);
171 printf(" size: %d\tflags: %x\n", ce->ce_size, ce->ce_flags);
172 }
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700173}
174
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200175static void show_ru_info(void)
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800176{
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200177 struct string_list_item *item;
178
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800179 if (!the_index.resolve_undo)
180 return;
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200181
182 for_each_string_list_item(item, the_index.resolve_undo) {
183 const char *path = item->string;
184 struct resolve_undo_info *ui = item->util;
185 int i, len;
186
187 len = strlen(path);
188 if (len < max_prefix_len)
189 continue; /* outside of the prefix */
190 if (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))
191 continue; /* uninterested */
192 for (i = 0; i < 3; i++) {
193 if (!ui->mode[i])
194 continue;
195 printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i],
196 find_unique_abbrev(ui->sha1[i], abbrev),
197 i + 1);
198 write_name(path, len);
199 }
200 }
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800201}
202
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200203static void show_files(struct dir_struct *dir)
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700204{
205 int i;
206
207 /* For cached/deleted files we don't need to even do the readdir */
Junio C Hamano6ca45942005-05-12 17:17:54 -0700208 if (show_others || show_killed) {
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700209 fill_directory(dir, pathspec);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700210 if (show_others)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700211 show_other_files(dir);
Junio C Hamano6ca45942005-05-12 17:17:54 -0700212 if (show_killed)
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700213 show_killed_files(dir);
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700214 }
Junio C Hamanoaee46192005-04-16 08:33:23 -0700215 if (show_cached | show_stage) {
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700216 for (i = 0; i < active_nr; i++) {
217 struct cache_entry *ce = active_cache[i];
Jeff King500348a2009-10-30 15:05:52 -0400218 int dtype = ce_to_dtype(ce);
219 if (dir->flags & DIR_SHOW_IGNORED &&
220 !excluded(dir, ce->name, &dtype))
221 continue;
Linus Torvaldseec8c632005-04-16 12:43:32 -0700222 if (show_unmerged && !ce_stage(ce))
223 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800224 if (ce->ce_flags & CE_UPDATE)
Junio C Hamano64586e72007-09-12 16:04:22 -0700225 continue;
Nguyễn Thái Ngọc Duy44a36912009-08-20 20:46:57 +0700226 show_ce_entry(ce_stage(ce) ? tag_unmerged :
227 (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700228 }
229 }
Junio C Hamanob0391892005-09-19 15:11:15 -0700230 if (show_deleted | show_modified) {
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700231 for (i = 0; i < active_nr; i++) {
232 struct cache_entry *ce = active_cache[i];
233 struct stat st;
Junio C Hamanob0391892005-09-19 15:11:15 -0700234 int err;
Jeff King500348a2009-10-30 15:05:52 -0400235 int dtype = ce_to_dtype(ce);
236 if (dir->flags & DIR_SHOW_IGNORED &&
237 !excluded(dir, ce->name, &dtype))
238 continue;
Junio C Hamano4b4e26d2008-11-16 00:10:25 -0800239 if (ce->ce_flags & CE_UPDATE)
240 continue;
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +0700241 if (ce_skip_worktree(ce))
242 continue;
Junio C Hamanob0391892005-09-19 15:11:15 -0700243 err = lstat(ce->name, &st);
244 if (show_deleted && err)
245 show_ce_entry(tag_removed, ce);
Junio C Hamano2bcab242006-02-08 21:50:18 -0800246 if (show_modified && ce_modified(ce, &st, 0))
Junio C Hamanob0391892005-09-19 15:11:15 -0700247 show_ce_entry(tag_modified, ce);
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700248 }
249 }
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700250}
251
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700252/*
253 * Prune the index to only contain stuff starting with "prefix"
254 */
Linus Torvalds3e042282006-07-31 13:13:55 -0700255static void prune_cache(const char *prefix)
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700256{
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200257 int pos = cache_name_pos(prefix, max_prefix_len);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700258 unsigned int first, last;
259
260 if (pos < 0)
261 pos = -pos-1;
Keith Packard95af39f2007-10-02 22:44:15 -0700262 memmove(active_cache, active_cache + pos,
263 (active_nr - pos) * sizeof(struct cache_entry *));
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700264 active_nr -= pos;
265 first = 0;
266 last = active_nr;
267 while (last > first) {
268 int next = (last + first) >> 1;
269 struct cache_entry *ce = active_cache[next];
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200270 if (!strncmp(ce->name, prefix, max_prefix_len)) {
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700271 first = next+1;
272 continue;
273 }
274 last = next;
275 }
276 active_nr = last;
277}
278
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200279static const char *pathspec_prefix(const char *prefix)
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700280{
Linus Torvalds56fc5102005-08-21 17:27:50 -0700281 const char **p, *n, *prev;
Linus Torvalds56fc5102005-08-21 17:27:50 -0700282 unsigned long max;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700283
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200284 if (!pathspec) {
285 max_prefix_len = prefix ? strlen(prefix) : 0;
286 return prefix;
287 }
288
Linus Torvalds56fc5102005-08-21 17:27:50 -0700289 prev = NULL;
290 max = PATH_MAX;
291 for (p = pathspec; (n = *p) != NULL; p++) {
292 int i, len = 0;
293 for (i = 0; i < max; i++) {
294 char c = n[i];
295 if (prev && prev[i] != c)
296 break;
Linus Torvalds56906142005-08-23 17:14:13 -0700297 if (!c || c == '*' || c == '?')
Linus Torvalds56fc5102005-08-21 17:27:50 -0700298 break;
299 if (c == '/')
300 len = i+1;
301 }
302 prev = n;
303 if (len < max) {
304 max = len;
305 if (!max)
306 break;
307 }
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700308 }
Linus Torvalds56fc5102005-08-21 17:27:50 -0700309
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200310 max_prefix_len = max;
Pierre Habouzit182af832007-09-16 00:32:36 +0200311 return max ? xmemdupz(prev, max) : NULL;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700312}
313
Johannes Schindelinf3670a52009-02-07 14:43:03 +0100314static void strip_trailing_slash_from_submodules(void)
315{
316 const char **p;
317
318 for (p = pathspec; *p != NULL; p++) {
319 int len = strlen(*p), pos;
320
321 if (len < 1 || (*p)[len - 1] != '/')
322 continue;
323 pos = cache_name_pos(*p, len - 1);
324 if (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))
325 *p = xstrndup(*p, len - 1);
326 }
327}
328
Junio C Hamano64586e72007-09-12 16:04:22 -0700329/*
330 * Read the tree specified with --with-tree option
331 * (typically, HEAD) into stage #1 and then
332 * squash them down to stage #0. This is used for
333 * --error-unmatch to list and check the path patterns
334 * that were given from the command line. We are not
335 * going to write this index out.
336 */
Junio C Hamanoee425e42007-11-18 01:13:32 -0800337void overlay_tree_on_cache(const char *tree_name, const char *prefix)
Junio C Hamano64586e72007-09-12 16:04:22 -0700338{
339 struct tree *tree;
340 unsigned char sha1[20];
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700341 struct pathspec pathspec;
Junio C Hamano64586e72007-09-12 16:04:22 -0700342 struct cache_entry *last_stage0 = NULL;
343 int i;
344
345 if (get_sha1(tree_name, sha1))
346 die("tree-ish %s not found.", tree_name);
347 tree = parse_tree_indirect(sha1);
348 if (!tree)
349 die("bad tree-ish %s", tree_name);
350
351 /* Hoist the unmerged entries up to stage #3 to make room */
352 for (i = 0; i < active_nr; i++) {
353 struct cache_entry *ce = active_cache[i];
354 if (!ce_stage(ce))
355 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800356 ce->ce_flags |= CE_STAGEMASK;
Junio C Hamano64586e72007-09-12 16:04:22 -0700357 }
358
359 if (prefix) {
360 static const char *(matchbuf[2]);
361 matchbuf[0] = prefix;
Junio C Hamano07e77e42008-11-16 00:15:43 -0800362 matchbuf[1] = NULL;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700363 init_pathspec(&pathspec, matchbuf);
Junio C Hamano33e0f622011-04-05 09:30:36 -0700364 pathspec.items[0].use_wildcard = 0;
Junio C Hamano64586e72007-09-12 16:04:22 -0700365 } else
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700366 init_pathspec(&pathspec, NULL);
367 if (read_tree(tree, 1, &pathspec))
Junio C Hamano64586e72007-09-12 16:04:22 -0700368 die("unable to read tree entries %s", tree_name);
369
370 for (i = 0; i < active_nr; i++) {
371 struct cache_entry *ce = active_cache[i];
372 switch (ce_stage(ce)) {
373 case 0:
374 last_stage0 = ce;
375 /* fallthru */
376 default:
377 continue;
378 case 1:
379 /*
380 * If there is stage #0 entry for this, we do not
381 * need to show it. We use CE_UPDATE bit to mark
382 * such an entry.
383 */
384 if (last_stage0 &&
385 !strcmp(last_stage0->name, ce->name))
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800386 ce->ce_flags |= CE_UPDATE;
Junio C Hamano64586e72007-09-12 16:04:22 -0700387 }
388 }
389}
390
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200391int report_path_error(const char *ps_matched, const char **pathspec, int prefix_len)
Junio C Hamanoee425e42007-11-18 01:13:32 -0800392{
393 /*
394 * Make sure all pathspec matched; otherwise it is an error.
395 */
396 int num, errors = 0;
397 for (num = 0; pathspec[num]; num++) {
398 int other, found_dup;
399
400 if (ps_matched[num])
401 continue;
402 /*
403 * The caller might have fed identical pathspec
404 * twice. Do not barf on such a mistake.
405 */
406 for (found_dup = other = 0;
407 !found_dup && pathspec[other];
408 other++) {
409 if (other == num || !ps_matched[other])
410 continue;
411 if (!strcmp(pathspec[other], pathspec[num]))
412 /*
413 * Ok, we have a match already.
414 */
415 found_dup = 1;
416 }
417 if (found_dup)
418 continue;
419
420 error("pathspec '%s' did not match any file(s) known to git.",
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200421 pathspec[num] + prefix_len);
Junio C Hamanoee425e42007-11-18 01:13:32 -0800422 errors++;
423 }
424 return errors;
425}
426
Miklos Vajnace8e8802009-02-17 15:27:11 +0100427static const char * const ls_files_usage[] = {
Štěpán Němec0adda932010-10-08 19:31:17 +0200428 "git ls-files [options] [<file>...]",
Miklos Vajnace8e8802009-02-17 15:27:11 +0100429 NULL
430};
431
432static int option_parse_z(const struct option *opt,
433 const char *arg, int unset)
434{
435 line_terminator = unset ? '\n' : '\0';
436
437 return 0;
438}
439
440static int option_parse_exclude(const struct option *opt,
441 const char *arg, int unset)
442{
443 struct exclude_list *list = opt->value;
444
445 exc_given = 1;
446 add_exclude(arg, "", 0, list);
447
448 return 0;
449}
450
451static int option_parse_exclude_from(const struct option *opt,
452 const char *arg, int unset)
453{
454 struct dir_struct *dir = opt->value;
455
456 exc_given = 1;
457 add_excludes_from_file(dir, arg);
458
459 return 0;
460}
461
462static int option_parse_exclude_standard(const struct option *opt,
463 const char *arg, int unset)
464{
465 struct dir_struct *dir = opt->value;
466
467 exc_given = 1;
468 setup_standard_excludes(dir);
469
470 return 0;
471}
Nicolas Pitrecf9a1132005-04-28 15:06:25 -0700472
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200473int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700474{
Miklos Vajnace8e8802009-02-17 15:27:11 +0100475 int require_work_tree = 0, show_tag = 0;
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200476 const char *max_prefix;
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700477 struct dir_struct dir;
Miklos Vajnace8e8802009-02-17 15:27:11 +0100478 struct option builtin_ls_files_options[] = {
479 { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
480 "paths are separated with NUL character",
481 PARSE_OPT_NOARG, option_parse_z },
482 OPT_BOOLEAN('t', NULL, &show_tag,
483 "identify the file status with tags"),
484 OPT_BOOLEAN('v', NULL, &show_valid_bit,
485 "use lowercase letters for 'assume unchanged' files"),
486 OPT_BOOLEAN('c', "cached", &show_cached,
487 "show cached files in the output (default)"),
488 OPT_BOOLEAN('d', "deleted", &show_deleted,
489 "show deleted files in the output"),
490 OPT_BOOLEAN('m', "modified", &show_modified,
491 "show modified files in the output"),
492 OPT_BOOLEAN('o', "others", &show_others,
493 "show other files in the output"),
494 OPT_BIT('i', "ignored", &dir.flags,
495 "show ignored files in the output",
496 DIR_SHOW_IGNORED),
497 OPT_BOOLEAN('s', "stage", &show_stage,
498 "show staged contents' object name in the output"),
499 OPT_BOOLEAN('k', "killed", &show_killed,
500 "show files on the filesystem that need to be removed"),
501 OPT_BIT(0, "directory", &dir.flags,
502 "show 'other' directories' name only",
503 DIR_SHOW_OTHER_DIRECTORIES),
Jeff Kinge9008b92009-05-08 01:01:17 -0400504 OPT_NEGBIT(0, "empty-directory", &dir.flags,
Jeff King2fb6d6d2009-03-07 20:27:22 -0500505 "don't show empty directories",
Miklos Vajnace8e8802009-02-17 15:27:11 +0100506 DIR_HIDE_EMPTY_DIRECTORIES),
507 OPT_BOOLEAN('u', "unmerged", &show_unmerged,
508 "show unmerged files in the output"),
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800509 OPT_BOOLEAN(0, "resolve-undo", &show_resolve_undo,
510 "show resolve-undo information"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100511 { OPTION_CALLBACK, 'x', "exclude", &dir.exclude_list[EXC_CMDL], "pattern",
512 "skip files matching pattern",
513 0, option_parse_exclude },
514 { OPTION_CALLBACK, 'X', "exclude-from", &dir, "file",
515 "exclude patterns are read from <file>",
516 0, option_parse_exclude_from },
517 OPT_STRING(0, "exclude-per-directory", &dir.exclude_per_dir, "file",
518 "read additional per-directory exclude patterns in <file>"),
519 { OPTION_CALLBACK, 0, "exclude-standard", &dir, NULL,
520 "add the standard git exclusions",
521 PARSE_OPT_NOARG, option_parse_exclude_standard },
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200522 { OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
Miklos Vajnace8e8802009-02-17 15:27:11 +0100523 "make the output relative to the project top directory",
524 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
525 OPT_BOOLEAN(0, "error-unmatch", &error_unmatch,
526 "if any <file> is not in the index, treat this as an error"),
527 OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
528 "pretend that paths removed since <tree-ish> are still present"),
529 OPT__ABBREV(&abbrev),
Thomas Rast84974212010-07-31 00:35:59 +0200530 OPT_BOOLEAN(0, "debug", &debug_mode, "show debugging data"),
Miklos Vajnace8e8802009-02-17 15:27:11 +0100531 OPT_END()
532 };
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700533
Nguyễn Thái Ngọc Duycbb31672010-10-22 01:48:14 -0500534 if (argc == 2 && !strcmp(argv[1], "-h"))
535 usage_with_options(ls_files_usage, builtin_ls_files_options);
536
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700537 memset(&dir, 0, sizeof(dir));
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200538 prefix = cmd_prefix;
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700539 if (prefix)
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200540 prefix_len = strlen(prefix);
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100541 git_config(git_default_config, NULL);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700542
Nguyễn Thái Ngọc Duyc28b3d62009-08-20 20:47:01 +0700543 if (read_cache() < 0)
544 die("index file corrupt");
545
Stephen Boyd37782922009-05-23 11:53:12 -0700546 argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
Miklos Vajnace8e8802009-02-17 15:27:11 +0100547 ls_files_usage, 0);
548 if (show_tag || show_valid_bit) {
549 tag_cached = "H ";
550 tag_unmerged = "M ";
551 tag_removed = "R ";
552 tag_modified = "C ";
553 tag_other = "? ";
554 tag_killed = "K ";
Nguyễn Thái Ngọc Duy44a36912009-08-20 20:46:57 +0700555 tag_skip_worktree = "S ";
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800556 tag_resolve_undo = "U ";
Nicolas Pitre9ff768e2005-04-28 11:44:04 -0700557 }
Junio C Hamanode2e3b02009-03-20 14:30:51 -0700558 if (show_modified || show_others || show_deleted || (dir.flags & DIR_SHOW_IGNORED) || show_killed)
Miklos Vajnace8e8802009-02-17 15:27:11 +0100559 require_work_tree = 1;
560 if (show_unmerged)
561 /*
562 * There's no point in showing unmerged unless
563 * you also show the stage information.
564 */
565 show_stage = 1;
566 if (dir.exclude_per_dir)
567 exc_given = 1;
Nicolas Pitre9ff768e2005-04-28 11:44:04 -0700568
Mike Hommey7d8ae932007-11-03 12:23:12 +0100569 if (require_work_tree && !is_inside_work_tree())
570 setup_work_tree();
Johannes Schindelin6d9ba672007-01-23 13:30:20 +0100571
Miklos Vajnace8e8802009-02-17 15:27:11 +0100572 pathspec = get_pathspec(prefix, argv);
Linus Torvalds56fc5102005-08-21 17:27:50 -0700573
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100574 /* be nice with submodule paths ending in a slash */
Johannes Schindelinf3670a52009-02-07 14:43:03 +0100575 if (pathspec)
576 strip_trailing_slash_from_submodules();
577
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200578 /* Find common prefix for all pathspec's */
579 max_prefix = pathspec_prefix(prefix);
Linus Torvalds5be4efb2005-08-21 12:55:33 -0700580
Junio C Hamanobba319b2006-02-14 12:40:20 -0800581 /* Treat unmatching pathspec elements as errors */
582 if (pathspec && error_unmatch) {
583 int num;
584 for (num = 0; pathspec[num]; num++)
585 ;
586 ps_matched = xcalloc(1, num);
587 }
588
Ben Waltonac78b002009-10-08 21:53:35 -0400589 if ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)
590 die("ls-files --ignored needs some exclude pattern");
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700591
592 /* With no flags, we default to showing the cached files */
Junio C Hamanob0391892005-09-19 15:11:15 -0700593 if (!(show_stage | show_deleted | show_others | show_unmerged |
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800594 show_killed | show_modified | show_resolve_undo))
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700595 show_cached = 1;
596
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200597 if (max_prefix)
598 prune_cache(max_prefix);
Junio C Hamano64586e72007-09-12 16:04:22 -0700599 if (with_tree) {
600 /*
601 * Basic sanity check; show-stages and show-unmerged
602 * would not make any sense with this option.
603 */
604 if (show_stage || show_unmerged)
605 die("ls-files --with-tree is incompatible with -s or -u");
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200606 overlay_tree_on_cache(with_tree, max_prefix);
Junio C Hamano64586e72007-09-12 16:04:22 -0700607 }
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200608 show_files(&dir);
Junio C Hamano9d9a2f42009-12-25 10:08:04 -0800609 if (show_resolve_undo)
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200610 show_ru_info();
Junio C Hamanobba319b2006-02-14 12:40:20 -0800611
612 if (ps_matched) {
Junio C Hamanoee425e42007-11-18 01:13:32 -0800613 int bad;
Clemens Buchacherefad1a52010-06-03 15:39:18 +0200614 bad = report_path_error(ps_matched, pathspec, prefix_len);
Junio C Hamanoee425e42007-11-18 01:13:32 -0800615 if (bad)
Andreas Ericssonced7b822006-11-30 12:28:28 +0100616 fprintf(stderr, "Did you forget to 'git add'?\n");
617
Junio C Hamanoee425e42007-11-18 01:13:32 -0800618 return bad ? 1 : 0;
Junio C Hamanobba319b2006-02-14 12:40:20 -0800619 }
620
Linus Torvalds8695c8b2005-04-11 18:55:38 -0700621 return 0;
622}