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