Clean up work-tree handling
[git/git.git] / builtin-ls-files.c
CommitLineData
8695c8bf
LT
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 */
8695c8bf 8#include "cache.h"
22ddf719 9#include "quote.h"
453ec4bd 10#include "dir.h"
0864f264 11#include "builtin.h"
8695c8bf 12
96f1e58f
DR
13static int abbrev;
14static int show_deleted;
15static int show_cached;
16static int show_others;
17static int show_stage;
18static int show_unmerged;
19static int show_modified;
20static int show_killed;
21static int show_valid_bit;
b83c8345 22static int line_terminator = '\n';
8695c8bf 23
96f1e58f
DR
24static int prefix_len;
25static int prefix_offset;
26static const char **pathspec;
27static int error_unmatch;
28static char *ps_matched;
5be4efbe 29
20d37ef6
PB
30static const char *tag_cached = "";
31static const char *tag_unmerged = "";
32static const char *tag_removed = "";
33static const char *tag_other = "";
6ca45943 34static const char *tag_killed = "";
b0391890 35static const char *tag_modified = "";
20d37ef6 36
8695c8bf 37
56fc5108
LT
38/*
39 * Match a pathspec against a filename. The first "len" characters
40 * are the common prefix
41 */
bba319b5
JH
42static int match(const char **spec, char *ps_matched,
43 const char *filename, int len)
56fc5108
LT
44{
45 const char *m;
46
47 while ((m = *spec++) != NULL) {
48 int matchlen = strlen(m + len);
49
50 if (!matchlen)
bba319b5 51 goto matched;
56fc5108
LT
52 if (!strncmp(m + len, filename + len, matchlen)) {
53 if (m[len + matchlen - 1] == '/')
bba319b5 54 goto matched;
56fc5108
LT
55 switch (filename[len + matchlen]) {
56 case '/': case '\0':
bba319b5 57 goto matched;
56fc5108
LT
58 }
59 }
60 if (!fnmatch(m + len, filename + len, 0))
bba319b5
JH
61 goto matched;
62 if (ps_matched)
63 ps_matched++;
64 continue;
65 matched:
66 if (ps_matched)
67 *ps_matched = 1;
68 return 1;
56fc5108
LT
69 }
70 return 0;
71}
72
453ec4bd 73static void show_dir_entry(const char *tag, struct dir_entry *ent)
5be4efbe
LT
74{
75 int len = prefix_len;
76 int offset = prefix_offset;
77
78 if (len >= ent->len)
79 die("git-ls-files: internal error - directory entry not superset of prefix");
80
bba319b5 81 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
5be4efbe
LT
82 return;
83
22ddf719 84 fputs(tag, stdout);
9ef2b3cb 85 write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
22ddf719 86 putchar(line_terminator);
5be4efbe
LT
87}
88
453ec4bd 89static void show_other_files(struct dir_struct *dir)
fcbc3083
JH
90{
91 int i;
5698454e
LT
92
93
94 /*
95 * Skip matching and unmerged entries for the paths,
96 * since we want just "others".
97 *
98 * (Matching entries are normally pruned during
99 * the directory tree walk, but will show up for
100 * gitlinks because we don't necessarily have
101 * dir->show_other_directories set to suppress
102 * them).
103 */
453ec4bd 104 for (i = 0; i < dir->nr; i++) {
453ec4bd 105 struct dir_entry *ent = dir->entries[i];
5698454e 106 int len, pos;
fcbc3083 107 struct cache_entry *ce;
5698454e
LT
108
109 /*
110 * Remove the '/' at the end that directory
111 * walking adds for directory entries.
112 */
113 len = ent->len;
114 if (len && ent->name[len-1] == '/')
115 len--;
116 pos = cache_name_pos(ent->name, len);
fcbc3083 117 if (0 <= pos)
5698454e 118 continue; /* exact match */
fcbc3083 119 pos = -pos - 1;
a6080a0a 120 if (pos < active_nr) {
fcbc3083 121 ce = active_cache[pos];
5698454e
LT
122 if (ce_namelen(ce) == len &&
123 !memcmp(ce->name, ent->name, len))
fcbc3083
JH
124 continue; /* Yup, this one exists unmerged */
125 }
126 show_dir_entry(tag_other, ent);
127 }
128}
129
453ec4bd 130static void show_killed_files(struct dir_struct *dir)
6ca45943
JH
131{
132 int i;
453ec4bd
LT
133 for (i = 0; i < dir->nr; i++) {
134 struct dir_entry *ent = dir->entries[i];
6ca45943
JH
135 char *cp, *sp;
136 int pos, len, killed = 0;
137
138 for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
139 sp = strchr(cp, '/');
140 if (!sp) {
141 /* If ent->name is prefix of an entry in the
142 * cache, it will be killed.
143 */
144 pos = cache_name_pos(ent->name, ent->len);
145 if (0 <= pos)
146 die("bug in show-killed-files");
147 pos = -pos - 1;
148 while (pos < active_nr &&
149 ce_stage(active_cache[pos]))
150 pos++; /* skip unmerged */
151 if (active_nr <= pos)
152 break;
153 /* pos points at a name immediately after
154 * ent->name in the cache. Does it expect
155 * ent->name to be a directory?
156 */
157 len = ce_namelen(active_cache[pos]);
158 if ((ent->len < len) &&
159 !strncmp(active_cache[pos]->name,
160 ent->name, ent->len) &&
161 active_cache[pos]->name[ent->len] == '/')
162 killed = 1;
163 break;
164 }
165 if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
166 /* If any of the leading directories in
167 * ent->name is registered in the cache,
168 * ent->name will be killed.
169 */
170 killed = 1;
171 break;
172 }
173 }
174 if (killed)
453ec4bd 175 show_dir_entry(tag_killed, dir->entries[i]);
6ca45943 176 }
8695c8bf
LT
177}
178
5be4efbe
LT
179static void show_ce_entry(const char *tag, struct cache_entry *ce)
180{
181 int len = prefix_len;
182 int offset = prefix_offset;
183
184 if (len >= ce_namelen(ce))
185 die("git-ls-files: internal error - cache entry not superset of prefix");
186
bba319b5 187 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
5be4efbe
LT
188 return;
189
8bb2e03b
JH
190 if (tag && *tag && show_valid_bit &&
191 (ce->ce_flags & htons(CE_VALID))) {
2bcab240
JH
192 static char alttag[4];
193 memcpy(alttag, tag, 3);
194 if (isalpha(tag[0]))
195 alttag[0] = tolower(tag[0]);
196 else if (tag[0] == '?')
197 alttag[0] = '!';
198 else {
199 alttag[0] = 'v';
200 alttag[1] = tag[0];
201 alttag[2] = ' ';
202 alttag[3] = 0;
203 }
204 tag = alttag;
205 }
206
22ddf719
JH
207 if (!show_stage) {
208 fputs(tag, stdout);
9ef2b3cb
JH
209 write_name_quoted("", 0, ce->name + offset,
210 line_terminator, stdout);
22ddf719
JH
211 putchar(line_terminator);
212 }
213 else {
214 printf("%s%06o %s %d\t",
5be4efbe
LT
215 tag,
216 ntohl(ce->ce_mode),
ad0cae4c
EW
217 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
218 : sha1_to_hex(ce->sha1),
22ddf719 219 ce_stage(ce));
9ef2b3cb
JH
220 write_name_quoted("", 0, ce->name + offset,
221 line_terminator, stdout);
22ddf719
JH
222 putchar(line_terminator);
223 }
5be4efbe
LT
224}
225
3e04228b 226static void show_files(struct dir_struct *dir, const char *prefix)
8695c8bf
LT
227{
228 int i;
229
230 /* For cached/deleted files we don't need to even do the readdir */
6ca45943 231 if (show_others || show_killed) {
5be4efbe
LT
232 const char *path = ".", *base = "";
233 int baselen = prefix_len;
234
b4189aa8 235 if (baselen)
5be4efbe 236 path = base = prefix;
9fc42d60 237 read_directory(dir, path, base, baselen, pathspec);
6ca45943 238 if (show_others)
453ec4bd 239 show_other_files(dir);
6ca45943 240 if (show_killed)
453ec4bd 241 show_killed_files(dir);
8695c8bf 242 }
aee46198 243 if (show_cached | show_stage) {
8695c8bf
LT
244 for (i = 0; i < active_nr; i++) {
245 struct cache_entry *ce = active_cache[i];
453ec4bd 246 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 247 continue;
eec8c633
LT
248 if (show_unmerged && !ce_stage(ce))
249 continue;
5be4efbe 250 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
8695c8bf
LT
251 }
252 }
b0391890 253 if (show_deleted | show_modified) {
8695c8bf
LT
254 for (i = 0; i < active_nr; i++) {
255 struct cache_entry *ce = active_cache[i];
256 struct stat st;
b0391890 257 int err;
453ec4bd 258 if (excluded(dir, ce->name) != dir->show_ignored)
9ff768e9 259 continue;
b0391890
JH
260 err = lstat(ce->name, &st);
261 if (show_deleted && err)
262 show_ce_entry(tag_removed, ce);
2bcab240 263 if (show_modified && ce_modified(ce, &st, 0))
b0391890 264 show_ce_entry(tag_modified, ce);
5be4efbe
LT
265 }
266 }
267}
268
269/*
270 * Prune the index to only contain stuff starting with "prefix"
271 */
3e04228b 272static void prune_cache(const char *prefix)
5be4efbe
LT
273{
274 int pos = cache_name_pos(prefix, prefix_len);
275 unsigned int first, last;
276
277 if (pos < 0)
278 pos = -pos-1;
279 active_cache += pos;
280 active_nr -= pos;
281 first = 0;
282 last = active_nr;
283 while (last > first) {
284 int next = (last + first) >> 1;
285 struct cache_entry *ce = active_cache[next];
286 if (!strncmp(ce->name, prefix, prefix_len)) {
287 first = next+1;
288 continue;
8695c8bf 289 }
5be4efbe
LT
290 last = next;
291 }
292 active_nr = last;
293}
294
3e04228b 295static const char *verify_pathspec(const char *prefix)
5be4efbe 296{
56fc5108
LT
297 const char **p, *n, *prev;
298 char *real_prefix;
299 unsigned long max;
300
301 prev = NULL;
302 max = PATH_MAX;
303 for (p = pathspec; (n = *p) != NULL; p++) {
304 int i, len = 0;
305 for (i = 0; i < max; i++) {
306 char c = n[i];
307 if (prev && prev[i] != c)
308 break;
56906143 309 if (!c || c == '*' || c == '?')
56fc5108
LT
310 break;
311 if (c == '/')
312 len = i+1;
313 }
314 prev = n;
315 if (len < max) {
316 max = len;
317 if (!max)
318 break;
319 }
5be4efbe 320 }
56fc5108
LT
321
322 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
323 die("git-ls-files: cannot generate relative filenames containing '..'");
324
325 real_prefix = NULL;
326 prefix_len = max;
327 if (max) {
328 real_prefix = xmalloc(max + 1);
329 memcpy(real_prefix, prev, max);
330 real_prefix[max] = 0;
8695c8bf 331 }
3e04228b 332 return real_prefix;
8695c8bf
LT
333}
334
4d1f1190 335static const char ls_files_usage[] =
8bb2e03b 336 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
f87f9497 337 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
ad0cae4c
EW
338 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
339 "[--] [<file>]*";
cf9a113d 340
a633fca0 341int cmd_ls_files(int argc, const char **argv, const char *prefix)
8695c8bf
LT
342{
343 int i;
6d9ba67b 344 int exc_given = 0, require_work_tree = 0;
453ec4bd 345 struct dir_struct dir;
8695c8bf 346
453ec4bd 347 memset(&dir, 0, sizeof(dir));
5be4efbe 348 if (prefix)
56fc5108 349 prefix_offset = strlen(prefix);
39b4ac99 350 git_config(git_default_config);
5be4efbe 351
8695c8bf 352 for (i = 1; i < argc; i++) {
6b5ee137 353 const char *arg = argv[i];
8695c8bf 354
500b97e4
FK
355 if (!strcmp(arg, "--")) {
356 i++;
357 break;
358 }
b83c8345
JH
359 if (!strcmp(arg, "-z")) {
360 line_terminator = 0;
5be4efbe
LT
361 continue;
362 }
8bb2e03b 363 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
20d37ef6
PB
364 tag_cached = "H ";
365 tag_unmerged = "M ";
366 tag_removed = "R ";
b0391890 367 tag_modified = "C ";
20d37ef6 368 tag_other = "? ";
6ca45943 369 tag_killed = "K ";
8bb2e03b
JH
370 if (arg[1] == 'v')
371 show_valid_bit = 1;
5be4efbe
LT
372 continue;
373 }
374 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
8695c8bf 375 show_cached = 1;
5be4efbe
LT
376 continue;
377 }
378 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
8695c8bf 379 show_deleted = 1;
5be4efbe
LT
380 continue;
381 }
b0391890
JH
382 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
383 show_modified = 1;
6d9ba67b 384 require_work_tree = 1;
b0391890
JH
385 continue;
386 }
5be4efbe 387 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
8695c8bf 388 show_others = 1;
6d9ba67b 389 require_work_tree = 1;
5be4efbe
LT
390 continue;
391 }
392 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
453ec4bd 393 dir.show_ignored = 1;
6d9ba67b 394 require_work_tree = 1;
5be4efbe
LT
395 continue;
396 }
397 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
aee46198 398 show_stage = 1;
5be4efbe
LT
399 continue;
400 }
401 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
6ca45943 402 show_killed = 1;
6d9ba67b 403 require_work_tree = 1;
5be4efbe
LT
404 continue;
405 }
9518eb26 406 if (!strcmp(arg, "--directory")) {
453ec4bd 407 dir.show_other_directories = 1;
9518eb26
LT
408 continue;
409 }
b0a3de42 410 if (!strcmp(arg, "--no-empty-directory")) {
453ec4bd 411 dir.hide_empty_directories = 1;
b0a3de42
PB
412 continue;
413 }
5be4efbe 414 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
20d37ef6
PB
415 /* There's no point in showing unmerged unless
416 * you also show the stage information.
417 */
eec8c633
LT
418 show_stage = 1;
419 show_unmerged = 1;
5be4efbe
LT
420 continue;
421 }
422 if (!strcmp(arg, "-x") && i+1 < argc) {
fee88256 423 exc_given = 1;
453ec4bd 424 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
425 continue;
426 }
cc44c765 427 if (!prefixcmp(arg, "--exclude=")) {
fee88256 428 exc_given = 1;
453ec4bd 429 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
5be4efbe
LT
430 continue;
431 }
432 if (!strcmp(arg, "-X") && i+1 < argc) {
fee88256 433 exc_given = 1;
453ec4bd 434 add_excludes_from_file(&dir, argv[++i]);
5be4efbe
LT
435 continue;
436 }
cc44c765 437 if (!prefixcmp(arg, "--exclude-from=")) {
fee88256 438 exc_given = 1;
453ec4bd 439 add_excludes_from_file(&dir, arg+15);
5be4efbe
LT
440 continue;
441 }
cc44c765 442 if (!prefixcmp(arg, "--exclude-per-directory=")) {
fee88256 443 exc_given = 1;
453ec4bd 444 dir.exclude_per_dir = arg + 24;
5be4efbe
LT
445 continue;
446 }
447 if (!strcmp(arg, "--full-name")) {
448 prefix_offset = 0;
449 continue;
450 }
bba319b5
JH
451 if (!strcmp(arg, "--error-unmatch")) {
452 error_unmatch = 1;
453 continue;
454 }
cc44c765 455 if (!prefixcmp(arg, "--abbrev=")) {
ad0cae4c
EW
456 abbrev = strtoul(arg+9, NULL, 10);
457 if (abbrev && abbrev < MINIMUM_ABBREV)
458 abbrev = MINIMUM_ABBREV;
459 else if (abbrev > 40)
460 abbrev = 40;
461 continue;
462 }
463 if (!strcmp(arg, "--abbrev")) {
464 abbrev = DEFAULT_ABBREV;
465 continue;
466 }
56fc5108 467 if (*arg == '-')
17710391 468 usage(ls_files_usage);
56fc5108 469 break;
9ff768e9
NP
470 }
471
e90fdc39
JS
472 if (require_work_tree && !is_inside_work_tree()) {
473 const char *work_tree = get_git_work_tree();
474 if (!work_tree || chdir(work_tree))
475 die("This operation must be run in a work tree");
476 }
6d9ba67b 477
56fc5108
LT
478 pathspec = get_pathspec(prefix, argv + i);
479
480 /* Verify that the pathspec matches the prefix */
481 if (pathspec)
3e04228b 482 prefix = verify_pathspec(prefix);
5be4efbe 483
bba319b5
JH
484 /* Treat unmatching pathspec elements as errors */
485 if (pathspec && error_unmatch) {
486 int num;
487 for (num = 0; pathspec[num]; num++)
488 ;
489 ps_matched = xcalloc(1, num);
490 }
491
453ec4bd 492 if (dir.show_ignored && !exc_given) {
20d37ef6
PB
493 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
494 argv[0]);
9ff768e9 495 exit(1);
8695c8bf
LT
496 }
497
498 /* With no flags, we default to showing the cached files */
b0391890
JH
499 if (!(show_stage | show_deleted | show_others | show_unmerged |
500 show_killed | show_modified))
8695c8bf
LT
501 show_cached = 1;
502
503 read_cache();
5be4efbe 504 if (prefix)
3e04228b
LT
505 prune_cache(prefix);
506 show_files(&dir, prefix);
bba319b5
JH
507
508 if (ps_matched) {
509 /* We need to make sure all pathspec matched otherwise
510 * it is an error.
511 */
512 int num, errors = 0;
513 for (num = 0; pathspec[num]; num++) {
514 if (ps_matched[num])
515 continue;
ced7b828 516 error("pathspec '%s' did not match any file(s) known to git.",
6becd7da 517 pathspec[num] + prefix_offset);
c8af25ca 518 errors++;
bba319b5 519 }
ced7b828
AE
520
521 if (errors)
522 fprintf(stderr, "Did you forget to 'git add'?\n");
523
bba319b5
JH
524 return errors ? 1 : 0;
525 }
526
8695c8bf
LT
527 return 0;
528}