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 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
= "";
38 static const char *exclude_per_dir
= NULL
;
40 /* We maintain three exclude pattern lists:
41 * EXC_CMDL lists patterns explicitly given on the command line.
42 * EXC_DIRS lists patterns obtained from per-directory ignore files.
43 * EXC_FILE lists patterns from fallback ignore files.
48 static struct exclude_list
{
58 static void add_exclude(const char *string
, const char *base
,
59 int baselen
, struct exclude_list
*which
)
61 struct exclude
*x
= xmalloc(sizeof (*x
));
66 if (which
->nr
== which
->alloc
) {
67 which
->alloc
= alloc_nr(which
->alloc
);
68 which
->excludes
= realloc(which
->excludes
,
69 which
->alloc
* sizeof(x
));
71 which
->excludes
[which
->nr
++] = x
;
74 static int add_excludes_from_file_1(const char *fname
,
77 struct exclude_list
*which
)
83 fd
= open(fname
, O_RDONLY
);
86 size
= lseek(fd
, 0, SEEK_END
);
89 lseek(fd
, 0, SEEK_SET
);
95 if (read(fd
, buf
, size
) != size
)
100 for (i
= 0; i
< size
; i
++) {
101 if (buf
[i
] == '\n') {
102 if (entry
!= buf
+ i
&& entry
[0] != '#') {
103 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
104 add_exclude(entry
, base
, baselen
, which
);
117 static void add_excludes_from_file(const char *fname
)
119 if (add_excludes_from_file_1(fname
, "", 0,
120 &exclude_list
[EXC_FILE
]) < 0)
121 die("cannot use %s as an exclude file", fname
);
124 static int push_exclude_per_directory(const char *base
, int baselen
)
126 char exclude_file
[PATH_MAX
];
127 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
128 int current_nr
= el
->nr
;
130 if (exclude_per_dir
) {
131 memcpy(exclude_file
, base
, baselen
);
132 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
133 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
138 static void pop_exclude_per_directory(int stk
)
140 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
143 free(el
->excludes
[--el
->nr
]);
146 /* Scan the list and let the last match determines the fate.
147 * Return 1 for exclude, 0 for include and -1 for undecided.
149 static int excluded_1(const char *pathname
,
151 struct exclude_list
*el
)
156 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
157 struct exclude
*x
= el
->excludes
[i
];
158 const char *exclude
= x
->pattern
;
161 if (*exclude
== '!') {
166 if (!strchr(exclude
, '/')) {
168 const char *basename
= strrchr(pathname
, '/');
169 basename
= (basename
) ? basename
+1 : pathname
;
170 if (fnmatch(exclude
, basename
, 0) == 0)
174 /* match with FNM_PATHNAME:
175 * exclude has base (baselen long) implicitly
178 int baselen
= x
->baselen
;
182 if (pathlen
< baselen
||
183 (baselen
&& pathname
[baselen
-1] != '/') ||
184 strncmp(pathname
, x
->base
, baselen
))
187 if (fnmatch(exclude
, pathname
+baselen
,
193 return -1; /* undecided */
196 static int excluded(const char *pathname
)
198 int pathlen
= strlen(pathname
);
201 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
202 switch (excluded_1(pathname
, pathlen
, &exclude_list
[st
])) {
214 char name
[FLEX_ARRAY
]; /* more */
217 static struct nond_on_fs
**dir
;
219 static int dir_alloc
;
221 static void add_name(const char *pathname
, int len
)
223 struct nond_on_fs
*ent
;
225 if (cache_name_pos(pathname
, len
) >= 0)
228 if (nr_dir
== dir_alloc
) {
229 dir_alloc
= alloc_nr(dir_alloc
);
230 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
232 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
234 memcpy(ent
->name
, pathname
, len
);
239 static int dir_exists(const char *dirname
, int len
)
241 int pos
= cache_name_pos(dirname
, len
);
245 if (pos
>= active_nr
) /* can't */
247 return !strncmp(active_cache
[pos
]->name
, dirname
, len
);
251 * Read a directory tree. We currently ignore anything but
252 * directories, regular files and symlinks. That's because git
253 * doesn't handle them at all yet. Maybe that will change some
256 * Also, we ignore the name ".git" (even if it is not a directory).
257 * That likely will not change.
259 static void read_directory(const char *path
, const char *base
, int baselen
)
261 DIR *dir
= opendir(path
);
266 char fullname
[MAXPATHLEN
+ 1];
267 memcpy(fullname
, base
, baselen
);
269 exclude_stk
= push_exclude_per_directory(base
, baselen
);
271 while ((de
= readdir(dir
)) != NULL
) {
274 if ((de
->d_name
[0] == '.') &&
275 (de
->d_name
[1] == 0 ||
276 !strcmp(de
->d_name
+ 1, ".") ||
277 !strcmp(de
->d_name
+ 1, "git")))
279 len
= strlen(de
->d_name
);
280 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
281 if (excluded(fullname
) != show_ignored
)
289 if (lstat(fullname
, &st
))
291 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
293 if (!S_ISDIR(st
.st_mode
))
297 memcpy(fullname
+ baselen
+ len
, "/", 2);
299 if (show_other_directories
&&
300 !dir_exists(fullname
, baselen
+ len
))
302 read_directory(fullname
, fullname
,
309 add_name(fullname
, baselen
+ len
);
313 pop_exclude_per_directory(exclude_stk
);
317 static int cmp_name(const void *p1
, const void *p2
)
319 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
320 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
322 return cache_name_compare(e1
->name
, e1
->len
,
327 * Match a pathspec against a filename. The first "len" characters
328 * are the common prefix
330 static int match(const char **spec
, char *ps_matched
,
331 const char *filename
, int len
)
335 while ((m
= *spec
++) != NULL
) {
336 int matchlen
= strlen(m
+ len
);
340 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
341 if (m
[len
+ matchlen
- 1] == '/')
343 switch (filename
[len
+ matchlen
]) {
348 if (!fnmatch(m
+ len
, filename
+ len
, 0))
361 static void show_dir_entry(const char *tag
, struct nond_on_fs
*ent
)
363 int len
= prefix_len
;
364 int offset
= prefix_offset
;
367 die("git-ls-files: internal error - directory entry not superset of prefix");
369 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
373 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
374 putchar(line_terminator
);
377 static void show_other_files(void)
380 for (i
= 0; i
< nr_dir
; i
++) {
381 /* We should not have a matching entry, but we
382 * may have an unmerged entry for this path.
384 struct nond_on_fs
*ent
= dir
[i
];
385 int pos
= cache_name_pos(ent
->name
, ent
->len
);
386 struct cache_entry
*ce
;
388 die("bug in show-other-files");
390 if (pos
< active_nr
) {
391 ce
= active_cache
[pos
];
392 if (ce_namelen(ce
) == ent
->len
&&
393 !memcmp(ce
->name
, ent
->name
, ent
->len
))
394 continue; /* Yup, this one exists unmerged */
396 show_dir_entry(tag_other
, ent
);
400 static void show_killed_files(void)
403 for (i
= 0; i
< nr_dir
; i
++) {
404 struct nond_on_fs
*ent
= dir
[i
];
406 int pos
, len
, killed
= 0;
408 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
409 sp
= strchr(cp
, '/');
411 /* If ent->name is prefix of an entry in the
412 * cache, it will be killed.
414 pos
= cache_name_pos(ent
->name
, ent
->len
);
416 die("bug in show-killed-files");
418 while (pos
< active_nr
&&
419 ce_stage(active_cache
[pos
]))
420 pos
++; /* skip unmerged */
421 if (active_nr
<= pos
)
423 /* pos points at a name immediately after
424 * ent->name in the cache. Does it expect
425 * ent->name to be a directory?
427 len
= ce_namelen(active_cache
[pos
]);
428 if ((ent
->len
< len
) &&
429 !strncmp(active_cache
[pos
]->name
,
430 ent
->name
, ent
->len
) &&
431 active_cache
[pos
]->name
[ent
->len
] == '/')
435 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
436 /* If any of the leading directories in
437 * ent->name is registered in the cache,
438 * ent->name will be killed.
445 show_dir_entry(tag_killed
, dir
[i
]);
449 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
451 int len
= prefix_len
;
452 int offset
= prefix_offset
;
454 if (len
>= ce_namelen(ce
))
455 die("git-ls-files: internal error - cache entry not superset of prefix");
457 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
462 write_name_quoted("", 0, ce
->name
+ offset
,
463 line_terminator
, stdout
);
464 putchar(line_terminator
);
467 printf("%s%06o %s %d\t",
470 sha1_to_hex(ce
->sha1
),
472 write_name_quoted("", 0, ce
->name
+ offset
,
473 line_terminator
, stdout
);
474 putchar(line_terminator
);
478 static void show_files(void)
482 /* For cached/deleted files we don't need to even do the readdir */
483 if (show_others
|| show_killed
) {
484 const char *path
= ".", *base
= "";
485 int baselen
= prefix_len
;
488 path
= base
= prefix
;
489 if (exclude_per_dir
) {
490 char *p
, *pp
= xmalloc(baselen
+1);
491 memcpy(pp
, prefix
, baselen
+1);
496 push_exclude_per_directory(pp
, p
-pp
);
509 read_directory(path
, base
, baselen
);
510 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
516 if (show_cached
| show_stage
) {
517 for (i
= 0; i
< active_nr
; i
++) {
518 struct cache_entry
*ce
= active_cache
[i
];
519 if (excluded(ce
->name
) != show_ignored
)
521 if (show_unmerged
&& !ce_stage(ce
))
523 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
526 if (show_deleted
| show_modified
) {
527 for (i
= 0; i
< active_nr
; i
++) {
528 struct cache_entry
*ce
= active_cache
[i
];
531 if (excluded(ce
->name
) != show_ignored
)
533 err
= lstat(ce
->name
, &st
);
534 if (show_deleted
&& err
)
535 show_ce_entry(tag_removed
, ce
);
536 if (show_modified
&& ce_modified(ce
, &st
))
537 show_ce_entry(tag_modified
, ce
);
543 * Prune the index to only contain stuff starting with "prefix"
545 static void prune_cache(void)
547 int pos
= cache_name_pos(prefix
, prefix_len
);
548 unsigned int first
, last
;
556 while (last
> first
) {
557 int next
= (last
+ first
) >> 1;
558 struct cache_entry
*ce
= active_cache
[next
];
559 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
568 static void verify_pathspec(void)
570 const char **p
, *n
, *prev
;
576 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
578 for (i
= 0; i
< max
; i
++) {
580 if (prev
&& prev
[i
] != c
)
582 if (!c
|| c
== '*' || c
== '?')
595 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
596 die("git-ls-files: cannot generate relative filenames containing '..'");
601 real_prefix
= xmalloc(max
+ 1);
602 memcpy(real_prefix
, prev
, max
);
603 real_prefix
[max
] = 0;
605 prefix
= real_prefix
;
608 static const char ls_files_usage
[] =
609 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
610 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
611 "[ --exclude-per-directory=<filename> ] [--full-name] [--] [<file>]*";
613 int main(int argc
, const char **argv
)
618 prefix
= setup_git_directory();
620 prefix_offset
= strlen(prefix
);
621 git_config(git_default_config
);
623 for (i
= 1; i
< argc
; i
++) {
624 const char *arg
= argv
[i
];
626 if (!strcmp(arg
, "--")) {
630 if (!strcmp(arg
, "-z")) {
634 if (!strcmp(arg
, "-t")) {
643 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
647 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
651 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
655 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
659 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
663 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
667 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
671 if (!strcmp(arg
, "--directory")) {
672 show_other_directories
= 1;
675 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
676 /* There's no point in showing unmerged unless
677 * you also show the stage information.
683 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
685 add_exclude(argv
[++i
], "", 0, &exclude_list
[EXC_CMDL
]);
688 if (!strncmp(arg
, "--exclude=", 10)) {
690 add_exclude(arg
+10, "", 0, &exclude_list
[EXC_CMDL
]);
693 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
695 add_excludes_from_file(argv
[++i
]);
698 if (!strncmp(arg
, "--exclude-from=", 15)) {
700 add_excludes_from_file(arg
+15);
703 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
705 exclude_per_dir
= arg
+ 24;
708 if (!strcmp(arg
, "--full-name")) {
712 if (!strcmp(arg
, "--error-unmatch")) {
717 usage(ls_files_usage
);
721 pathspec
= get_pathspec(prefix
, argv
+ i
);
723 /* Verify that the pathspec matches the prefix */
727 /* Treat unmatching pathspec elements as errors */
728 if (pathspec
&& error_unmatch
) {
730 for (num
= 0; pathspec
[num
]; num
++)
732 ps_matched
= xcalloc(1, num
);
735 if (show_ignored
&& !exc_given
) {
736 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
741 /* With no flags, we default to showing the cached files */
742 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
743 show_killed
| show_modified
))
752 /* We need to make sure all pathspec matched otherwise
756 for (num
= 0; pathspec
[num
]; num
++) {
759 error("pathspec '%s' did not match any.",
760 pathspec
[num
] + prefix_offset
);
762 return errors
? 1 : 0;