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