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
14 static int show_deleted
= 0;
15 static int show_cached
= 0;
16 static int show_others
= 0;
17 static int show_ignored
= 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 line_terminator
= '\n';
24 static int prefix_len
= 0, prefix_offset
= 0;
25 static const char *prefix
= NULL
;
26 static const char **pathspec
= NULL
;
28 static const char *tag_cached
= "";
29 static const char *tag_unmerged
= "";
30 static const char *tag_removed
= "";
31 static const char *tag_other
= "";
32 static const char *tag_killed
= "";
33 static const char *tag_modified
= "";
35 static const char *exclude_per_dir
= NULL
;
37 /* We maintain three exclude pattern lists:
38 * EXC_CMDL lists patterns explicitly given on the command line.
39 * EXC_DIRS lists patterns obtained from per-directory ignore files.
40 * EXC_FILE lists patterns from fallback ignore files.
45 static struct exclude_list
{
55 static void add_exclude(const char *string
, const char *base
,
56 int baselen
, struct exclude_list
*which
)
58 struct exclude
*x
= xmalloc(sizeof (*x
));
63 if (which
->nr
== which
->alloc
) {
64 which
->alloc
= alloc_nr(which
->alloc
);
65 which
->excludes
= realloc(which
->excludes
,
66 which
->alloc
* sizeof(x
));
68 which
->excludes
[which
->nr
++] = x
;
71 static int add_excludes_from_file_1(const char *fname
,
74 struct exclude_list
*which
)
80 fd
= open(fname
, O_RDONLY
);
83 size
= lseek(fd
, 0, SEEK_END
);
86 lseek(fd
, 0, SEEK_SET
);
92 if (read(fd
, buf
, size
) != size
)
97 for (i
= 0; i
< size
; i
++) {
99 if (entry
!= buf
+ i
&& entry
[0] != '#') {
100 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
101 add_exclude(entry
, base
, baselen
, which
);
114 static void add_excludes_from_file(const char *fname
)
116 if (add_excludes_from_file_1(fname
, "", 0,
117 &exclude_list
[EXC_FILE
]) < 0)
118 die("cannot use %s as an exclude file", fname
);
121 static int push_exclude_per_directory(const char *base
, int baselen
)
123 char exclude_file
[PATH_MAX
];
124 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
125 int current_nr
= el
->nr
;
127 if (exclude_per_dir
) {
128 memcpy(exclude_file
, base
, baselen
);
129 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
130 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
135 static void pop_exclude_per_directory(int stk
)
137 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
140 free(el
->excludes
[--el
->nr
]);
143 /* Scan the list and let the last match determines the fate.
144 * Return 1 for exclude, 0 for include and -1 for undecided.
146 static int excluded_1(const char *pathname
,
148 struct exclude_list
*el
)
153 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
154 struct exclude
*x
= el
->excludes
[i
];
155 const char *exclude
= x
->pattern
;
158 if (*exclude
== '!') {
163 if (!strchr(exclude
, '/')) {
165 const char *basename
= strrchr(pathname
, '/');
166 basename
= (basename
) ? basename
+1 : pathname
;
167 if (fnmatch(exclude
, basename
, 0) == 0)
171 /* match with FNM_PATHNAME:
172 * exclude has base (baselen long) inplicitly
175 int baselen
= x
->baselen
;
179 if (pathlen
< baselen
||
180 (baselen
&& pathname
[baselen
-1] != '/') ||
181 strncmp(pathname
, x
->base
, baselen
))
184 if (fnmatch(exclude
, pathname
+baselen
,
190 return -1; /* undecided */
193 static int excluded(const char *pathname
)
195 int pathlen
= strlen(pathname
);
198 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
199 switch (excluded_1(pathname
, pathlen
, &exclude_list
[st
])) {
214 static struct nond_on_fs
**dir
;
216 static int dir_alloc
;
218 static void add_name(const char *pathname
, int len
)
220 struct nond_on_fs
*ent
;
222 if (cache_name_pos(pathname
, len
) >= 0)
225 if (nr_dir
== dir_alloc
) {
226 dir_alloc
= alloc_nr(dir_alloc
);
227 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
229 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
231 memcpy(ent
->name
, pathname
, len
);
237 * Read a directory tree. We currently ignore anything but
238 * directories, regular files and symlinks. That's because git
239 * doesn't handle them at all yet. Maybe that will change some
242 * Also, we ignore the name ".git" (even if it is not a directory).
243 * That likely will not change.
245 static void read_directory(const char *path
, const char *base
, int baselen
)
247 DIR *dir
= opendir(path
);
252 char fullname
[MAXPATHLEN
+ 1];
253 memcpy(fullname
, base
, baselen
);
255 exclude_stk
= push_exclude_per_directory(base
, baselen
);
257 while ((de
= readdir(dir
)) != NULL
) {
260 if ((de
->d_name
[0] == '.') &&
261 (de
->d_name
[1] == 0 ||
262 !strcmp(de
->d_name
+ 1, ".") ||
263 !strcmp(de
->d_name
+ 1, "git")))
265 len
= strlen(de
->d_name
);
266 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
267 if (excluded(fullname
) != show_ignored
)
275 if (lstat(fullname
, &st
))
277 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
279 if (!S_ISDIR(st
.st_mode
))
283 memcpy(fullname
+ baselen
+ len
, "/", 2);
284 read_directory(fullname
, fullname
,
291 add_name(fullname
, baselen
+ len
);
295 pop_exclude_per_directory(exclude_stk
);
299 static int cmp_name(const void *p1
, const void *p2
)
301 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
302 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
304 return cache_name_compare(e1
->name
, e1
->len
,
309 * Match a pathspec against a filename. The first "len" characters
310 * are the common prefix
312 static int match(const char **spec
, const char *filename
, int len
)
316 while ((m
= *spec
++) != NULL
) {
317 int matchlen
= strlen(m
+ len
);
321 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
322 if (m
[len
+ matchlen
- 1] == '/')
324 switch (filename
[len
+ matchlen
]) {
329 if (!fnmatch(m
+ len
, filename
+ len
, 0))
335 static void show_dir_entry(const char *tag
, struct nond_on_fs
*ent
)
337 int len
= prefix_len
;
338 int offset
= prefix_offset
;
341 die("git-ls-files: internal error - directory entry not superset of prefix");
343 if (pathspec
&& !match(pathspec
, ent
->name
, len
))
347 write_name_quoted("", ent
->name
+ offset
, line_terminator
, stdout
);
348 putchar(line_terminator
);
351 static void show_killed_files(void)
354 for (i
= 0; i
< nr_dir
; i
++) {
355 struct nond_on_fs
*ent
= dir
[i
];
357 int pos
, len
, killed
= 0;
359 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
360 sp
= strchr(cp
, '/');
362 /* If ent->name is prefix of an entry in the
363 * cache, it will be killed.
365 pos
= cache_name_pos(ent
->name
, ent
->len
);
367 die("bug in show-killed-files");
369 while (pos
< active_nr
&&
370 ce_stage(active_cache
[pos
]))
371 pos
++; /* skip unmerged */
372 if (active_nr
<= pos
)
374 /* pos points at a name immediately after
375 * ent->name in the cache. Does it expect
376 * ent->name to be a directory?
378 len
= ce_namelen(active_cache
[pos
]);
379 if ((ent
->len
< len
) &&
380 !strncmp(active_cache
[pos
]->name
,
381 ent
->name
, ent
->len
) &&
382 active_cache
[pos
]->name
[ent
->len
] == '/')
386 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
387 /* If any of the leading directories in
388 * ent->name is registered in the cache,
389 * ent->name will be killed.
396 show_dir_entry(tag_killed
, dir
[i
]);
400 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
402 int len
= prefix_len
;
403 int offset
= prefix_offset
;
405 if (len
>= ce_namelen(ce
))
406 die("git-ls-files: internal error - cache entry not superset of prefix");
408 if (pathspec
&& !match(pathspec
, ce
->name
, len
))
413 write_name_quoted("", ce
->name
+ offset
, line_terminator
, stdout
);
414 putchar(line_terminator
);
417 printf("%s%06o %s %d\t",
420 sha1_to_hex(ce
->sha1
),
422 write_name_quoted("", ce
->name
+ offset
, line_terminator
, stdout
);
423 putchar(line_terminator
);
427 static void show_files(void)
431 /* For cached/deleted files we don't need to even do the readdir */
432 if (show_others
|| show_killed
) {
433 const char *path
= ".", *base
= "";
434 int baselen
= prefix_len
;
437 path
= base
= prefix
;
438 read_directory(path
, base
, baselen
);
439 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
441 for (i
= 0; i
< nr_dir
; i
++)
442 show_dir_entry(tag_other
, dir
[i
]);
446 if (show_cached
| show_stage
) {
447 for (i
= 0; i
< active_nr
; i
++) {
448 struct cache_entry
*ce
= active_cache
[i
];
449 if (excluded(ce
->name
) != show_ignored
)
451 if (show_unmerged
&& !ce_stage(ce
))
453 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
456 if (show_deleted
| show_modified
) {
457 for (i
= 0; i
< active_nr
; i
++) {
458 struct cache_entry
*ce
= active_cache
[i
];
461 if (excluded(ce
->name
) != show_ignored
)
463 err
= lstat(ce
->name
, &st
);
464 if (show_deleted
&& err
)
465 show_ce_entry(tag_removed
, ce
);
466 if (show_modified
&& ce_modified(ce
, &st
))
467 show_ce_entry(tag_modified
, ce
);
473 * Prune the index to only contain stuff starting with "prefix"
475 static void prune_cache(void)
477 int pos
= cache_name_pos(prefix
, prefix_len
);
478 unsigned int first
, last
;
486 while (last
> first
) {
487 int next
= (last
+ first
) >> 1;
488 struct cache_entry
*ce
= active_cache
[next
];
489 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
498 static void verify_pathspec(void)
500 const char **p
, *n
, *prev
;
506 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
508 for (i
= 0; i
< max
; i
++) {
510 if (prev
&& prev
[i
] != c
)
512 if (!c
|| c
== '*' || c
== '?')
525 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
526 die("git-ls-files: cannot generate relative filenames containing '..'");
531 real_prefix
= xmalloc(max
+ 1);
532 memcpy(real_prefix
, prev
, max
);
533 real_prefix
[max
] = 0;
535 prefix
= real_prefix
;
538 static const char ls_files_usage
[] =
539 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
540 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
541 "[ --exclude-per-directory=<filename> ] [--] [<file>]*";
543 int main(int argc
, const char **argv
)
548 prefix
= setup_git_directory();
550 prefix_offset
= strlen(prefix
);
552 for (i
= 1; i
< argc
; i
++) {
553 const char *arg
= argv
[i
];
555 if (!strcmp(arg
, "--")) {
559 if (!strcmp(arg
, "-z")) {
563 if (!strcmp(arg
, "-t")) {
572 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
576 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
580 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
584 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
588 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
592 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
596 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
600 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
601 /* There's no point in showing unmerged unless
602 * you also show the stage information.
608 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
610 add_exclude(argv
[++i
], "", 0, &exclude_list
[EXC_CMDL
]);
613 if (!strncmp(arg
, "--exclude=", 10)) {
615 add_exclude(arg
+10, "", 0, &exclude_list
[EXC_CMDL
]);
618 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
620 add_excludes_from_file(argv
[++i
]);
623 if (!strncmp(arg
, "--exclude-from=", 15)) {
625 add_excludes_from_file(arg
+15);
628 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
630 exclude_per_dir
= arg
+ 24;
633 if (!strcmp(arg
, "--full-name")) {
638 usage(ls_files_usage
);
642 pathspec
= get_pathspec(prefix
, argv
+ i
);
644 /* Verify that the pathspec matches the prefix */
648 if (show_ignored
&& !exc_given
) {
649 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
654 /* With no flags, we default to showing the cached files */
655 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
656 show_killed
| show_modified
))