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
15 static int abbrev
= 0;
16 static int show_deleted
= 0;
17 static int show_cached
= 0;
18 static int show_others
= 0;
19 static int show_ignored
= 0;
20 static int show_stage
= 0;
21 static int show_unmerged
= 0;
22 static int show_modified
= 0;
23 static int show_killed
= 0;
24 static int show_other_directories
= 0;
25 static int hide_empty_directories
= 0;
26 static int show_valid_bit
= 0;
27 static int line_terminator
= '\n';
29 static int prefix_len
= 0, prefix_offset
= 0;
30 static const char *prefix
= NULL
;
31 static const char **pathspec
= NULL
;
32 static int error_unmatch
= 0;
33 static char *ps_matched
= NULL
;
35 static const char *tag_cached
= "";
36 static const char *tag_unmerged
= "";
37 static const char *tag_removed
= "";
38 static const char *tag_other
= "";
39 static const char *tag_killed
= "";
40 static const char *tag_modified
= "";
42 static const char *exclude_per_dir
= NULL
;
44 /* We maintain three exclude pattern lists:
45 * EXC_CMDL lists patterns explicitly given on the command line.
46 * EXC_DIRS lists patterns obtained from per-directory ignore files.
47 * EXC_FILE lists patterns from fallback ignore files.
52 static struct exclude_list
{
62 static void add_exclude(const char *string
, const char *base
,
63 int baselen
, struct exclude_list
*which
)
65 struct exclude
*x
= xmalloc(sizeof (*x
));
70 if (which
->nr
== which
->alloc
) {
71 which
->alloc
= alloc_nr(which
->alloc
);
72 which
->excludes
= realloc(which
->excludes
,
73 which
->alloc
* sizeof(x
));
75 which
->excludes
[which
->nr
++] = x
;
78 static int add_excludes_from_file_1(const char *fname
,
81 struct exclude_list
*which
)
87 fd
= open(fname
, O_RDONLY
);
90 size
= lseek(fd
, 0, SEEK_END
);
93 lseek(fd
, 0, SEEK_SET
);
98 buf
= xmalloc(size
+1);
99 if (read(fd
, buf
, size
) != size
)
105 for (i
= 0; i
< size
; i
++) {
106 if (buf
[i
] == '\n') {
107 if (entry
!= buf
+ i
&& entry
[0] != '#') {
108 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
109 add_exclude(entry
, base
, baselen
, which
);
122 static void add_excludes_from_file(const char *fname
)
124 if (add_excludes_from_file_1(fname
, "", 0,
125 &exclude_list
[EXC_FILE
]) < 0)
126 die("cannot use %s as an exclude file", fname
);
129 static int push_exclude_per_directory(const char *base
, int baselen
)
131 char exclude_file
[PATH_MAX
];
132 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
133 int current_nr
= el
->nr
;
135 if (exclude_per_dir
) {
136 memcpy(exclude_file
, base
, baselen
);
137 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
138 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
143 static void pop_exclude_per_directory(int stk
)
145 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
148 free(el
->excludes
[--el
->nr
]);
151 /* Scan the list and let the last match determines the fate.
152 * Return 1 for exclude, 0 for include and -1 for undecided.
154 static int excluded_1(const char *pathname
,
156 struct exclude_list
*el
)
161 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
162 struct exclude
*x
= el
->excludes
[i
];
163 const char *exclude
= x
->pattern
;
166 if (*exclude
== '!') {
171 if (!strchr(exclude
, '/')) {
173 const char *basename
= strrchr(pathname
, '/');
174 basename
= (basename
) ? basename
+1 : pathname
;
175 if (fnmatch(exclude
, basename
, 0) == 0)
179 /* match with FNM_PATHNAME:
180 * exclude has base (baselen long) implicitly
183 int baselen
= x
->baselen
;
187 if (pathlen
< baselen
||
188 (baselen
&& pathname
[baselen
-1] != '/') ||
189 strncmp(pathname
, x
->base
, baselen
))
192 if (fnmatch(exclude
, pathname
+baselen
,
198 return -1; /* undecided */
201 static int excluded(const char *pathname
)
203 int pathlen
= strlen(pathname
);
206 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
207 switch (excluded_1(pathname
, pathlen
, &exclude_list
[st
])) {
219 char name
[FLEX_ARRAY
]; /* more */
222 static struct nond_on_fs
**dir
;
224 static int dir_alloc
;
226 static void add_name(const char *pathname
, int len
)
228 struct nond_on_fs
*ent
;
230 if (cache_name_pos(pathname
, len
) >= 0)
233 if (nr_dir
== dir_alloc
) {
234 dir_alloc
= alloc_nr(dir_alloc
);
235 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
237 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
239 memcpy(ent
->name
, pathname
, len
);
244 static int dir_exists(const char *dirname
, int len
)
246 int pos
= cache_name_pos(dirname
, len
);
250 if (pos
>= active_nr
) /* can't */
252 return !strncmp(active_cache
[pos
]->name
, dirname
, len
);
256 * Read a directory tree. We currently ignore anything but
257 * directories, regular files and symlinks. That's because git
258 * doesn't handle them at all yet. Maybe that will change some
261 * Also, we ignore the name ".git" (even if it is not a directory).
262 * That likely will not change.
264 static int read_directory(const char *path
, const char *base
, int baselen
)
266 DIR *fdir
= opendir(path
);
272 char fullname
[MAXPATHLEN
+ 1];
273 memcpy(fullname
, base
, baselen
);
275 exclude_stk
= push_exclude_per_directory(base
, baselen
);
277 while ((de
= readdir(fdir
)) != NULL
) {
280 if ((de
->d_name
[0] == '.') &&
281 (de
->d_name
[1] == 0 ||
282 !strcmp(de
->d_name
+ 1, ".") ||
283 !strcmp(de
->d_name
+ 1, "git")))
285 len
= strlen(de
->d_name
);
286 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
287 if (excluded(fullname
) != show_ignored
) {
288 if (!show_ignored
|| DTYPE(de
) != DT_DIR
) {
295 int subdir
, rewind_base
;
299 if (lstat(fullname
, &st
))
301 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
303 if (!S_ISDIR(st
.st_mode
))
307 memcpy(fullname
+ baselen
+ len
, "/", 2);
309 rewind_base
= nr_dir
;
310 subdir
= read_directory(fullname
, fullname
,
312 if (show_other_directories
&&
313 (subdir
|| !hide_empty_directories
) &&
314 !dir_exists(fullname
, baselen
+ len
)) {
315 // Rewind the read subdirectory
316 while (nr_dir
> rewind_base
)
326 add_name(fullname
, baselen
+ len
);
331 pop_exclude_per_directory(exclude_stk
);
337 static int cmp_name(const void *p1
, const void *p2
)
339 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
340 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
342 return cache_name_compare(e1
->name
, e1
->len
,
347 * Match a pathspec against a filename. The first "len" characters
348 * are the common prefix
350 static int match(const char **spec
, char *ps_matched
,
351 const char *filename
, int len
)
355 while ((m
= *spec
++) != NULL
) {
356 int matchlen
= strlen(m
+ len
);
360 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
361 if (m
[len
+ matchlen
- 1] == '/')
363 switch (filename
[len
+ matchlen
]) {
368 if (!fnmatch(m
+ len
, filename
+ len
, 0))
381 static void show_dir_entry(const char *tag
, struct nond_on_fs
*ent
)
383 int len
= prefix_len
;
384 int offset
= prefix_offset
;
387 die("git-ls-files: internal error - directory entry not superset of prefix");
389 if (pathspec
&& !match(pathspec
, ps_matched
, ent
->name
, len
))
393 write_name_quoted("", 0, ent
->name
+ offset
, line_terminator
, stdout
);
394 putchar(line_terminator
);
397 static void show_other_files(void)
400 for (i
= 0; i
< nr_dir
; i
++) {
401 /* We should not have a matching entry, but we
402 * may have an unmerged entry for this path.
404 struct nond_on_fs
*ent
= dir
[i
];
405 int pos
= cache_name_pos(ent
->name
, ent
->len
);
406 struct cache_entry
*ce
;
408 die("bug in show-other-files");
410 if (pos
< active_nr
) {
411 ce
= active_cache
[pos
];
412 if (ce_namelen(ce
) == ent
->len
&&
413 !memcmp(ce
->name
, ent
->name
, ent
->len
))
414 continue; /* Yup, this one exists unmerged */
416 show_dir_entry(tag_other
, ent
);
420 static void show_killed_files(void)
423 for (i
= 0; i
< nr_dir
; i
++) {
424 struct nond_on_fs
*ent
= dir
[i
];
426 int pos
, len
, killed
= 0;
428 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
429 sp
= strchr(cp
, '/');
431 /* If ent->name is prefix of an entry in the
432 * cache, it will be killed.
434 pos
= cache_name_pos(ent
->name
, ent
->len
);
436 die("bug in show-killed-files");
438 while (pos
< active_nr
&&
439 ce_stage(active_cache
[pos
]))
440 pos
++; /* skip unmerged */
441 if (active_nr
<= pos
)
443 /* pos points at a name immediately after
444 * ent->name in the cache. Does it expect
445 * ent->name to be a directory?
447 len
= ce_namelen(active_cache
[pos
]);
448 if ((ent
->len
< len
) &&
449 !strncmp(active_cache
[pos
]->name
,
450 ent
->name
, ent
->len
) &&
451 active_cache
[pos
]->name
[ent
->len
] == '/')
455 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
456 /* If any of the leading directories in
457 * ent->name is registered in the cache,
458 * ent->name will be killed.
465 show_dir_entry(tag_killed
, dir
[i
]);
469 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
471 int len
= prefix_len
;
472 int offset
= prefix_offset
;
474 if (len
>= ce_namelen(ce
))
475 die("git-ls-files: internal error - cache entry not superset of prefix");
477 if (pathspec
&& !match(pathspec
, ps_matched
, ce
->name
, len
))
480 if (tag
&& *tag
&& show_valid_bit
&&
481 (ce
->ce_flags
& htons(CE_VALID
))) {
482 static char alttag
[4];
483 memcpy(alttag
, tag
, 3);
485 alttag
[0] = tolower(tag
[0]);
486 else if (tag
[0] == '?')
499 write_name_quoted("", 0, ce
->name
+ offset
,
500 line_terminator
, stdout
);
501 putchar(line_terminator
);
504 printf("%s%06o %s %d\t",
507 abbrev
? find_unique_abbrev(ce
->sha1
,abbrev
)
508 : sha1_to_hex(ce
->sha1
),
510 write_name_quoted("", 0, ce
->name
+ offset
,
511 line_terminator
, stdout
);
512 putchar(line_terminator
);
516 static void show_files(void)
520 /* For cached/deleted files we don't need to even do the readdir */
521 if (show_others
|| show_killed
) {
522 const char *path
= ".", *base
= "";
523 int baselen
= prefix_len
;
526 path
= base
= prefix
;
527 if (exclude_per_dir
) {
528 char *p
, *pp
= xmalloc(baselen
+1);
529 memcpy(pp
, prefix
, baselen
+1);
534 push_exclude_per_directory(pp
, p
-pp
);
547 read_directory(path
, base
, baselen
);
548 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
554 if (show_cached
| show_stage
) {
555 for (i
= 0; i
< active_nr
; i
++) {
556 struct cache_entry
*ce
= active_cache
[i
];
557 if (excluded(ce
->name
) != show_ignored
)
559 if (show_unmerged
&& !ce_stage(ce
))
561 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
564 if (show_deleted
| show_modified
) {
565 for (i
= 0; i
< active_nr
; i
++) {
566 struct cache_entry
*ce
= active_cache
[i
];
569 if (excluded(ce
->name
) != show_ignored
)
571 err
= lstat(ce
->name
, &st
);
572 if (show_deleted
&& err
)
573 show_ce_entry(tag_removed
, ce
);
574 if (show_modified
&& ce_modified(ce
, &st
, 0))
575 show_ce_entry(tag_modified
, ce
);
581 * Prune the index to only contain stuff starting with "prefix"
583 static void prune_cache(void)
585 int pos
= cache_name_pos(prefix
, prefix_len
);
586 unsigned int first
, last
;
594 while (last
> first
) {
595 int next
= (last
+ first
) >> 1;
596 struct cache_entry
*ce
= active_cache
[next
];
597 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
606 static void verify_pathspec(void)
608 const char **p
, *n
, *prev
;
614 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
616 for (i
= 0; i
< max
; i
++) {
618 if (prev
&& prev
[i
] != c
)
620 if (!c
|| c
== '*' || c
== '?')
633 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
634 die("git-ls-files: cannot generate relative filenames containing '..'");
639 real_prefix
= xmalloc(max
+ 1);
640 memcpy(real_prefix
, prev
, max
);
641 real_prefix
[max
] = 0;
643 prefix
= real_prefix
;
646 static const char ls_files_usage
[] =
647 "git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
648 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
649 "[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] "
652 int cmd_ls_files(int argc
, const char **argv
, char** envp
)
657 prefix
= setup_git_directory();
659 prefix_offset
= strlen(prefix
);
660 git_config(git_default_config
);
662 for (i
= 1; i
< argc
; i
++) {
663 const char *arg
= argv
[i
];
665 if (!strcmp(arg
, "--")) {
669 if (!strcmp(arg
, "-z")) {
673 if (!strcmp(arg
, "-t") || !strcmp(arg
, "-v")) {
684 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
688 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
692 if (!strcmp(arg
, "-m") || !strcmp(arg
, "--modified")) {
696 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
700 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
704 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
708 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
712 if (!strcmp(arg
, "--directory")) {
713 show_other_directories
= 1;
716 if (!strcmp(arg
, "--no-empty-directory")) {
717 hide_empty_directories
= 1;
720 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
721 /* There's no point in showing unmerged unless
722 * you also show the stage information.
728 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
730 add_exclude(argv
[++i
], "", 0, &exclude_list
[EXC_CMDL
]);
733 if (!strncmp(arg
, "--exclude=", 10)) {
735 add_exclude(arg
+10, "", 0, &exclude_list
[EXC_CMDL
]);
738 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
740 add_excludes_from_file(argv
[++i
]);
743 if (!strncmp(arg
, "--exclude-from=", 15)) {
745 add_excludes_from_file(arg
+15);
748 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
750 exclude_per_dir
= arg
+ 24;
753 if (!strcmp(arg
, "--full-name")) {
757 if (!strcmp(arg
, "--error-unmatch")) {
761 if (!strncmp(arg
, "--abbrev=", 9)) {
762 abbrev
= strtoul(arg
+9, NULL
, 10);
763 if (abbrev
&& abbrev
< MINIMUM_ABBREV
)
764 abbrev
= MINIMUM_ABBREV
;
765 else if (abbrev
> 40)
769 if (!strcmp(arg
, "--abbrev")) {
770 abbrev
= DEFAULT_ABBREV
;
774 usage(ls_files_usage
);
778 pathspec
= get_pathspec(prefix
, argv
+ i
);
780 /* Verify that the pathspec matches the prefix */
784 /* Treat unmatching pathspec elements as errors */
785 if (pathspec
&& error_unmatch
) {
787 for (num
= 0; pathspec
[num
]; num
++)
789 ps_matched
= xcalloc(1, num
);
792 if (show_ignored
&& !exc_given
) {
793 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
798 /* With no flags, we default to showing the cached files */
799 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
|
800 show_killed
| show_modified
))
809 /* We need to make sure all pathspec matched otherwise
813 for (num
= 0; pathspec
[num
]; num
++) {
816 error("pathspec '%s' did not match any.",
817 pathspec
[num
] + prefix_offset
);
820 return errors
? 1 : 0;