libify git-ls-files directory traversal
[git.git] / ls-files.c
blob89941a3ff8baa023840c092f2b6964cb2b33cce4
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.
6 * Copyright (C) Linus Torvalds, 2005
7 */
8 #include <fnmatch.h>
10 #include "cache.h"
11 #include "quote.h"
12 #include "dir.h"
14 static int abbrev = 0;
15 static int show_deleted = 0;
16 static int show_cached = 0;
17 static int show_others = 0;
18 static int show_stage = 0;
19 static int show_unmerged = 0;
20 static int show_modified = 0;
21 static int show_killed = 0;
22 static int show_valid_bit = 0;
23 static int line_terminator = '\n';
25 static int prefix_len = 0, prefix_offset = 0;
26 static const char *prefix = NULL;
27 static const char **pathspec = NULL;
28 static int error_unmatch = 0;
29 static char *ps_matched = NULL;
31 static const char *tag_cached = "";
32 static const char *tag_unmerged = "";
33 static const char *tag_removed = "";
34 static const char *tag_other = "";
35 static const char *tag_killed = "";
36 static const char *tag_modified = "";
40 * Match a pathspec against a filename. The first "len" characters
41 * are the common prefix
43 static int match(const char **spec, char *ps_matched,
44 const char *filename, int len)
46 const char *m;
48 while ((m = *spec++) != NULL) {
49 int matchlen = strlen(m + len);
51 if (!matchlen)
52 goto matched;
53 if (!strncmp(m + len, filename + len, matchlen)) {
54 if (m[len + matchlen - 1] == '/')
55 goto matched;
56 switch (filename[len + matchlen]) {
57 case '/': case '\0':
58 goto matched;
61 if (!fnmatch(m + len, filename + len, 0))
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;
71 return 0;
74 static void show_dir_entry(const char *tag, struct dir_entry *ent)
76 int len = prefix_len;
77 int offset = prefix_offset;
79 if (len >= ent->len)
80 die("git-ls-files: internal error - directory entry not superset of prefix");
82 if (pathspec && !match(pathspec, ps_matched, ent->name, len))
83 return;
85 fputs(tag, stdout);
86 write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
87 putchar(line_terminator);
90 static void show_other_files(struct dir_struct *dir)
92 int i;
93 for (i = 0; i < dir->nr; i++) {
94 /* We should not have a matching entry, but we
95 * may have an unmerged entry for this path.
97 struct dir_entry *ent = dir->entries[i];
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 */
109 show_dir_entry(tag_other, ent);
113 static void show_killed_files(struct dir_struct *dir)
115 int i;
116 for (i = 0; i < dir->nr; i++) {
117 struct dir_entry *ent = dir->entries[i];
118 char *cp, *sp;
119 int pos, len, killed = 0;
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.
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?
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;
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.
153 killed = 1;
154 break;
157 if (killed)
158 show_dir_entry(tag_killed, dir->entries[i]);
162 static void show_ce_entry(const char *tag, struct cache_entry *ce)
164 int len = prefix_len;
165 int offset = prefix_offset;
167 if (len >= ce_namelen(ce))
168 die("git-ls-files: internal error - cache entry not superset of prefix");
170 if (pathspec && !match(pathspec, ps_matched, ce->name, len))
171 return;
173 if (tag && *tag && show_valid_bit &&
174 (ce->ce_flags & htons(CE_VALID))) {
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;
187 tag = alttag;
190 if (!show_stage) {
191 fputs(tag, stdout);
192 write_name_quoted("", 0, ce->name + offset,
193 line_terminator, stdout);
194 putchar(line_terminator);
196 else {
197 printf("%s%06o %s %d\t",
198 tag,
199 ntohl(ce->ce_mode),
200 abbrev ? find_unique_abbrev(ce->sha1,abbrev)
201 : sha1_to_hex(ce->sha1),
202 ce_stage(ce));
203 write_name_quoted("", 0, ce->name + offset,
204 line_terminator, stdout);
205 putchar(line_terminator);
209 static void show_files(struct dir_struct *dir)
211 int i;
213 /* For cached/deleted files we don't need to even do the readdir */
214 if (show_others || show_killed) {
215 const char *path = ".", *base = "";
216 int baselen = prefix_len;
218 if (baselen) {
219 path = base = prefix;
220 if (dir->exclude_per_dir) {
221 char *p, *pp = xmalloc(baselen+1);
222 memcpy(pp, prefix, baselen+1);
223 p = pp;
224 while (1) {
225 char save = *p;
226 *p = 0;
227 push_exclude_per_directory(dir, pp, p-pp);
228 *p++ = save;
229 if (!save)
230 break;
231 p = strchr(p, '/');
232 if (p)
233 p++;
234 else
235 p = pp + baselen;
237 free(pp);
240 read_directory(dir, path, base, baselen);
241 if (show_others)
242 show_other_files(dir);
243 if (show_killed)
244 show_killed_files(dir);
246 if (show_cached | show_stage) {
247 for (i = 0; i < active_nr; i++) {
248 struct cache_entry *ce = active_cache[i];
249 if (excluded(dir, ce->name) != dir->show_ignored)
250 continue;
251 if (show_unmerged && !ce_stage(ce))
252 continue;
253 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
256 if (show_deleted | show_modified) {
257 for (i = 0; i < active_nr; i++) {
258 struct cache_entry *ce = active_cache[i];
259 struct stat st;
260 int err;
261 if (excluded(dir, ce->name) != dir->show_ignored)
262 continue;
263 err = lstat(ce->name, &st);
264 if (show_deleted && err)
265 show_ce_entry(tag_removed, ce);
266 if (show_modified && ce_modified(ce, &st, 0))
267 show_ce_entry(tag_modified, ce);
273 * Prune the index to only contain stuff starting with "prefix"
275 static void prune_cache(void)
277 int pos = cache_name_pos(prefix, prefix_len);
278 unsigned int first, last;
280 if (pos < 0)
281 pos = -pos-1;
282 active_cache += pos;
283 active_nr -= pos;
284 first = 0;
285 last = active_nr;
286 while (last > first) {
287 int next = (last + first) >> 1;
288 struct cache_entry *ce = active_cache[next];
289 if (!strncmp(ce->name, prefix, prefix_len)) {
290 first = next+1;
291 continue;
293 last = next;
295 active_nr = last;
298 static void verify_pathspec(void)
300 const char **p, *n, *prev;
301 char *real_prefix;
302 unsigned long max;
304 prev = NULL;
305 max = PATH_MAX;
306 for (p = pathspec; (n = *p) != NULL; p++) {
307 int i, len = 0;
308 for (i = 0; i < max; i++) {
309 char c = n[i];
310 if (prev && prev[i] != c)
311 break;
312 if (!c || c == '*' || c == '?')
313 break;
314 if (c == '/')
315 len = i+1;
317 prev = n;
318 if (len < max) {
319 max = len;
320 if (!max)
321 break;
325 if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
326 die("git-ls-files: cannot generate relative filenames containing '..'");
328 real_prefix = NULL;
329 prefix_len = max;
330 if (max) {
331 real_prefix = xmalloc(max + 1);
332 memcpy(real_prefix, prev, max);
333 real_prefix[max] = 0;
335 prefix = real_prefix;
338 static const char ls_files_usage[] =
339 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
340 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
341 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
342 "[--] [<file>]*";
344 int main(int argc, const char **argv)
346 int i;
347 int exc_given = 0;
348 struct dir_struct dir;
350 memset(&dir, 0, sizeof(dir));
351 prefix = setup_git_directory();
352 if (prefix)
353 prefix_offset = strlen(prefix);
354 git_config(git_default_config);
356 for (i = 1; i < argc; i++) {
357 const char *arg = argv[i];
359 if (!strcmp(arg, "--")) {
360 i++;
361 break;
363 if (!strcmp(arg, "-z")) {
364 line_terminator = 0;
365 continue;
367 if (!strcmp(arg, "-t") || !strcmp(arg, "-v")) {
368 tag_cached = "H ";
369 tag_unmerged = "M ";
370 tag_removed = "R ";
371 tag_modified = "C ";
372 tag_other = "? ";
373 tag_killed = "K ";
374 if (arg[1] == 'v')
375 show_valid_bit = 1;
376 continue;
378 if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
379 show_cached = 1;
380 continue;
382 if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
383 show_deleted = 1;
384 continue;
386 if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
387 show_modified = 1;
388 continue;
390 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
391 show_others = 1;
392 continue;
394 if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
395 dir.show_ignored = 1;
396 continue;
398 if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
399 show_stage = 1;
400 continue;
402 if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
403 show_killed = 1;
404 continue;
406 if (!strcmp(arg, "--directory")) {
407 dir.show_other_directories = 1;
408 continue;
410 if (!strcmp(arg, "--no-empty-directory")) {
411 dir.hide_empty_directories = 1;
412 continue;
414 if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
415 /* There's no point in showing unmerged unless
416 * you also show the stage information.
418 show_stage = 1;
419 show_unmerged = 1;
420 continue;
422 if (!strcmp(arg, "-x") && i+1 < argc) {
423 exc_given = 1;
424 add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
425 continue;
427 if (!strncmp(arg, "--exclude=", 10)) {
428 exc_given = 1;
429 add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
430 continue;
432 if (!strcmp(arg, "-X") && i+1 < argc) {
433 exc_given = 1;
434 add_excludes_from_file(&dir, argv[++i]);
435 continue;
437 if (!strncmp(arg, "--exclude-from=", 15)) {
438 exc_given = 1;
439 add_excludes_from_file(&dir, arg+15);
440 continue;
442 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
443 exc_given = 1;
444 dir.exclude_per_dir = arg + 24;
445 continue;
447 if (!strcmp(arg, "--full-name")) {
448 prefix_offset = 0;
449 continue;
451 if (!strcmp(arg, "--error-unmatch")) {
452 error_unmatch = 1;
453 continue;
455 if (!strncmp(arg, "--abbrev=", 9)) {
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;
463 if (!strcmp(arg, "--abbrev")) {
464 abbrev = DEFAULT_ABBREV;
465 continue;
467 if (*arg == '-')
468 usage(ls_files_usage);
469 break;
472 pathspec = get_pathspec(prefix, argv + i);
474 /* Verify that the pathspec matches the prefix */
475 if (pathspec)
476 verify_pathspec();
478 /* Treat unmatching pathspec elements as errors */
479 if (pathspec && error_unmatch) {
480 int num;
481 for (num = 0; pathspec[num]; num++)
483 ps_matched = xcalloc(1, num);
486 if (dir.show_ignored && !exc_given) {
487 fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
488 argv[0]);
489 exit(1);
492 /* With no flags, we default to showing the cached files */
493 if (!(show_stage | show_deleted | show_others | show_unmerged |
494 show_killed | show_modified))
495 show_cached = 1;
497 read_cache();
498 if (prefix)
499 prune_cache();
500 show_files(&dir);
502 if (ps_matched) {
503 /* We need to make sure all pathspec matched otherwise
504 * it is an error.
506 int num, errors = 0;
507 for (num = 0; pathspec[num]; num++) {
508 if (ps_matched[num])
509 continue;
510 error("pathspec '%s' did not match any.",
511 pathspec[num] + prefix_offset);
512 errors++;
514 return errors ? 1 : 0;
517 return 0;