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 show_other_directories
= 0;
23 static int show_valid_bit
= 0;
24 static int line_terminator
= '\n';
26 static int prefix_len
= 0, prefix_offset
= 0;
27 static const char *prefix
= NULL
;
28 static const char **pathspec
= NULL
;
29 static int error_unmatch
= 0;
30 static char *ps_matched
= NULL
;
32 static const char *tag_cached
= "";
33 static const char *tag_unmerged
= "";
34 static const char *tag_removed
= "";
35 static const char *tag_other
= "";
36 static const char *tag_killed
= "";
37 static const char *tag_modified
= "";
39 static const char *exclude_per_dir
= NULL
;
41 /* We maintain three exclude pattern lists:
42 * EXC_CMDL lists patterns explicitly given on the command line.
43 * EXC_DIRS lists patterns obtained from per-directory ignore files.
44 * EXC_FILE lists patterns from fallback ignore files.
49 static struct exclude_list
{
59 static void add_exclude(const char *string
, const char *base
,
60 int baselen
, struct exclude_list
*which
)
62 struct exclude
*x
= xmalloc(sizeof (*x
));
67 if (which
->nr
== which
->alloc
) {
68 which
->alloc
= alloc_nr(which
->alloc
);
69 which
->excludes
= realloc(which
->excludes
,
70 which
->alloc
* sizeof(x
));
72 which
->excludes
[which
->nr
++] = x
;
75 static int add_excludes_from_file_1(const char *fname
,
78 struct exclude_list
*which
)
84 fd
= open(fname
, O_RDONLY
);
87 size
= lseek(fd
, 0, SEEK_END
);
90 lseek(fd
, 0, SEEK_SET
);
96 if (read(fd
, buf
, size
) != size
)
101 for (i
= 0; i
< size
; i
++) {
102 if (buf
[i
] == '\n') {
103 if (entry
!= buf
+ i
&& entry
[0] != '#') {
104 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
105 add_exclude(entry
, base
, baselen
, which
);
118 static void add_excludes_from_file(const char *fname
)
120 if (add_excludes_from_file_1(fname
, "", 0,
121 &exclude_list
[EXC_FILE
]) < 0)
122 die("cannot use %s as an exclude file", fname
);
125 static int push_exclude_per_directory(const char *base
, int baselen
)
127 char exclude_file
[PATH_MAX
];
128 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
129 int current_nr
= el
->nr
;
131 if (exclude_per_dir
) {
132 memcpy(exclude_file
, base
, baselen
);
133 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
134 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
139 static void pop_exclude_per_directory(int stk
)
141 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
144 free(el
->excludes
[--el
->nr
]);
147 /* Scan the list and let the last match determines the fate.
148 * Return 1 for exclude, 0 for include and -1 for undecided.
150 static int excluded_1(const char *pathname
,
152 struct exclude_list
*el
)
157 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
158 struct exclude
*x
= el
->excludes
[i
];
159 const char *exclude
= x
->pattern
;
162 if (*exclude
== '!') {
167 if (!strchr(exclude
, '/')) {
169 const char *basename
= strrchr(pathname
, '/');
170 basename
= (basename
) ? basename
+1 : pathname
;
171 if (fnmatch(exclude
, basename
, 0) == 0)
175 /* match with FNM_PATHNAME:
176 * exclude has base (baselen long) implicitly
179 int baselen
= x
->baselen
;
183 if (pathlen
< baselen
||
184 (baselen
&& pathname
[baselen
-1] != '/') ||
185 strncmp(pathname
, x
->base
, baselen
))
188 if (fnmatch(exclude
, pathname
+baselen
,
194 return -1; /* undecided */
197 static int excluded(const char *pathname
)
199 int pathlen
= strlen(pathname
);
202 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
203 switch (excluded_1(pathname
, pathlen
, &exclude_list
[st
])) {
215 char name
[FLEX_ARRAY
]; /* more */
218 static struct nond_on_fs
**dir
;
220 static int dir_alloc
;
222 static void add_name(const char *pathname
, int len
)
224 struct nond_on_fs
*ent
;
226 if (cache_name_pos(pathname
, len
) >= 0)
229 if (nr_dir
== dir_alloc
) {
230 dir_alloc
= alloc_nr(dir_alloc
);
231 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
233 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
235 memcpy(ent
->name
, pathname
, len
);
240 static int dir_exists(const char *dirname
, int len
)
242 int pos
= cache_name_pos(dirname
, len
);
246 if (pos
>= active_nr
) /* can't */
248 return !strncmp(active_cache
[pos
]->name
, dirname
, len
);
252 * Read a directory tree. We currently ignore anything but
253 * directories, regular files and symlinks. That's because git
254 * doesn't handle them at all yet. Maybe that will change some
257 * Also, we ignore the name ".git" (even if it is not a directory).
258 * That likely will not change.
260 static void read_directory(const char *path
, const char *base
, int baselen
)
262 DIR *dir
= opendir(path
);
267 char fullname
[MAXPATHLEN
+ 1];
268 memcpy(fullname
, base
, baselen
);
270 exclude_stk
= push_exclude_per_directory(base
, baselen
);
272 while ((de
= readdir(dir
)) != NULL
) {
275 if ((de
->d_name
[0] == '.') &&
276 (de
->d_name
[1] == 0 ||
277 !strcmp(de
->d_name
+ 1, ".") ||
278 !strcmp(de
->d_name
+ 1, "git")))
280 len
= strlen(de
->d_name
);
281 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
282 if (excluded(fullname
) != show_ignored
) {
283 if (!show_ignored
|| DTYPE(de
) != DT_DIR
) {
293 if (lstat(fullname
, &st
))
295 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
297 if (!S_ISDIR(st
.st_mode
))
301 memcpy(fullname
+ baselen
+ len
, "/", 2);
303 if (show_other_directories
&&
304 !dir_exists(fullname
, baselen
+ len
))
306 read_directory(fullname
, fullname
,
313 add_name(fullname
, baselen
+ len
);
317 pop_exclude_per_directory(exclude_stk
);
321 static int cmp_name(const void *p1
, const void *p2
)
323 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
324 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
326 return cache_name_compare(e1
->name
, e1
->len
,
331 * Match a pathspec against a filename. The first "len" characters
332 * are the common prefix
334 static int match(const char **spec
, char *ps_matched
,
335 const char *filename
, int len
)
339 while ((m
= *spec
++) != NULL
) {
340 int matchlen
= strlen(m
+ len
);
344 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
345 if (m
[len
+ matchlen
- 1] == '/')
347 switch (filename
[len
+ matchlen
]) {
352 if (!fnmatch(m
+ len
, filename
+ len
, 0))
365 static void show_dir_entry(const char *tag
, struct nond_on_fs
*ent
)
367 int len
= prefix_len
;
368 int offset
= prefix_offset
;
371 die("git-ls-files: internal error - directory entry not superset of prefix");
373 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
377 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
378 putchar(line_terminator
);
381 static void show_other_files(void)
384 for (i
= 0; i
< nr_dir
; i
++) {
385 /* We should not have a matching entry, but we
386 * may have an unmerged entry for this path.
388 struct nond_on_fs
*ent
= dir
[i
];
389 int pos
= cache_name_pos(ent
->name
, ent
->len
);
390 struct cache_entry
*ce
;
392 die("bug in show-other-files");
394 if (pos
< active_nr
) {
395 ce
= active_cache
[pos
];
396 if (ce_namelen(ce
) == ent
->len
&&
397 !memcmp(ce
->name
, ent
->name
, ent
->len
))
398 continue; /* Yup, this one exists unmerged */
400 show_dir_entry(tag_other
, ent
);
404 static void show_killed_files(void)
407 for (i
= 0; i
< nr_dir
; i
++) {
408 struct nond_on_fs
*ent
= dir
[i
];
410 int pos
, len
, killed
= 0;
412 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
413 sp
= strchr(cp
, '/');
415 /* If ent->name is prefix of an entry in the
416 * cache, it will be killed.
418 pos
= cache_name_pos(ent
->name
, ent
->len
);
420 die("bug in show-killed-files");
422 while (pos
< active_nr
&&
423 ce_stage(active_cache
[pos
]))
424 pos
++; /* skip unmerged */
425 if (active_nr
<= pos
)
427 /* pos points at a name immediately after
428 * ent->name in the cache. Does it expect
429 * ent->name to be a directory?
431 len
= ce_namelen(active_cache
[pos
]);
432 if ((ent
->len
< len
) &&
433 !strncmp(active_cache
[pos
]->name
,
434 ent
->name
, ent
->len
) &&
435 active_cache
[pos
]->name
[ent
->len
] == '/')
439 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
440 /* If any of the leading directories in
441 * ent->name is registered in the cache,
442 * ent->name will be killed.
449 show_dir_entry(tag_killed
, dir
[i
]);
453 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
455 int len
= prefix_len
;
456 int offset
= prefix_offset
;
458 if (len
>= ce_namelen(ce
))
459 die("git-ls-files: internal error - cache entry not superset of prefix");
461 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
464 if (tag
&& *tag
&& show_valid_bit
&&
465 (ce
->ce_flags
& htons(CE_VALID
))) {
466 static char alttag
[4];
467 memcpy(alttag
, tag
, 3);
469 alttag
[0] = tolower(tag
[0]);
470 else if (tag
[0] == '?')
483 write_name_quoted("", 0, ce
->name
+ offset
,
484 line_terminator
, stdout
);
485 putchar(line_terminator
);
488 printf("%s%06o %s %d\t",
491 sha1_to_hex(ce
->sha1
),
493 write_name_quoted("", 0, ce
->name
+ offset
,
494 line_terminator
, stdout
);
495 putchar(line_terminator
);
499 static void show_files(void)
503 /* For cached/deleted files we don't need to even do the readdir */
504 if (show_others
|| show_killed
) {
505 const char *path
= ".", *base
= "";
506 int baselen
= prefix_len
;
509 path
= base
= prefix
;
510 if (exclude_per_dir
) {
511 char *p
, *pp
= xmalloc(baselen
+1);
512 memcpy(pp
, prefix
, baselen
+1);
517 push_exclude_per_directory(pp
, p
-pp
);
530 read_directory(path
, base
, baselen
);
531 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
537 if (show_cached
| show_stage
) {
538 for (i
= 0; i
< active_nr
; i
++) {
539 struct cache_entry
*ce
= active_cache
[i
];
540 if (excluded(ce
->name
) != show_ignored
)
542 if (show_unmerged
&& !ce_stage(ce
))
544 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
547 if (show_deleted
| show_modified
) {
548 for (i
= 0; i
< active_nr
; i
++) {
549 struct cache_entry
*ce
= active_cache
[i
];
552 if (excluded(ce
->name
) != show_ignored
)
554 err
= lstat(ce
->name
, &st
);
555 if (show_deleted
&& err
)
556 show_ce_entry(tag_removed
, ce
);
557 if (show_modified
&& ce_modified(ce
, &st
, 0))
558 show_ce_entry(tag_modified
, ce
);
564 * Prune the index to only contain stuff starting with "prefix"
566 static void prune_cache(void)
568 int pos
= cache_name_pos(prefix
, prefix_len
);
569 unsigned int first
, last
;
577 while (last
> first
) {
578 int next
= (last
+ first
) >> 1;
579 struct cache_entry
*ce
= active_cache
[next
];
580 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
589 static void verify_pathspec(void)
591 const char **p
, *n
, *prev
;
597 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
599 for (i
= 0; i
< max
; i
++) {
601 if (prev
&& prev
[i
] != c
)
603 if (!c
|| c
== '*' || c
== '?')
616 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
617 die("git-ls-files: cannot generate relative filenames containing '..'");
622 real_prefix
= xmalloc(max
+ 1);
623 memcpy(real_prefix
, prev
, max
);
624 real_prefix
[max
] = 0;
626 prefix
= real_prefix
;
629 static const char ls_files_usage
[] =
630 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
631 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
632 "[ --exclude-per-directory=<filename> ] [--full-name] [--] [<file>]*";
634 int main(int argc
, const char **argv
)
639 prefix
= setup_git_directory();
641 prefix_offset
= strlen(prefix
);
642 git_config(git_default_config
);
644 for (i
= 1; i
< argc
; i
++) {
645 const char *arg
= argv
[i
];
647 if (!strcmp(arg
, "--")) {
651 if (!strcmp(arg
, "-z")) {
655 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
666 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
670 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
674 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
678 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
682 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
686 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
690 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
694 if (!strcmp(arg
, "--directory")) {
695 show_other_directories
= 1;
698 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
699 /* There's no point in showing unmerged unless
700 * you also show the stage information.
706 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
708 add_exclude(argv
[++i
], "", 0, &exclude_list
[EXC_CMDL
]);
711 if (!strncmp(arg
, "--exclude=", 10)) {
713 add_exclude(arg
+10, "", 0, &exclude_list
[EXC_CMDL
]);
716 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
718 add_excludes_from_file(argv
[++i
]);
721 if (!strncmp(arg
, "--exclude-from=", 15)) {
723 add_excludes_from_file(arg
+15);
726 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
728 exclude_per_dir
= arg
+ 24;
731 if (!strcmp(arg
, "--full-name")) {
735 if (!strcmp(arg
, "--error-unmatch")) {
740 usage(ls_files_usage
);
744 pathspec
= get_pathspec(prefix
, argv
+ i
);
746 /* Verify that the pathspec matches the prefix */
750 /* Treat unmatching pathspec elements as errors */
751 if (pathspec
&& error_unmatch
) {
753 for (num
= 0; pathspec
[num
]; num
++)
755 ps_matched
= xcalloc(1, num
);
758 if (show_ignored
&& !exc_given
) {
759 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
764 /* With no flags, we default to showing the cached files */
765 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
766 show_killed
| show_modified
))
775 /* We need to make sure all pathspec matched otherwise
779 for (num
= 0; pathspec
[num
]; num
++) {
782 error("pathspec '%s' did not match any.",
783 pathspec
[num
] + prefix_offset
);
786 return errors
? 1 : 0;