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
13 static int show_deleted
= 0;
14 static int show_cached
= 0;
15 static int show_others
= 0;
16 static int show_ignored
= 0;
17 static int show_stage
= 0;
18 static int show_unmerged
= 0;
19 static int show_killed
= 0;
20 static int line_terminator
= '\n';
22 static int prefix_len
= 0, prefix_offset
= 0;
23 static const char *prefix
= NULL
;
24 static const char **pathspec
= NULL
;
26 static const char *tag_cached
= "";
27 static const char *tag_unmerged
= "";
28 static const char *tag_removed
= "";
29 static const char *tag_other
= "";
30 static const char *tag_killed
= "";
32 static char *exclude_per_dir
= NULL
;
34 /* We maintain three exclude pattern lists:
35 * EXC_CMDL lists patterns explicitly given on the command line.
36 * EXC_DIRS lists patterns obtained from per-directory ignore files.
37 * EXC_FILE lists patterns from fallback ignore files.
42 static struct exclude_list
{
52 static void add_exclude(const char *string
, const char *base
,
53 int baselen
, struct exclude_list
*which
)
55 struct exclude
*x
= xmalloc(sizeof (*x
));
60 if (which
->nr
== which
->alloc
) {
61 which
->alloc
= alloc_nr(which
->alloc
);
62 which
->excludes
= realloc(which
->excludes
,
63 which
->alloc
* sizeof(x
));
65 which
->excludes
[which
->nr
++] = x
;
68 static int add_excludes_from_file_1(const char *fname
,
71 struct exclude_list
*which
)
77 fd
= open(fname
, O_RDONLY
);
80 size
= lseek(fd
, 0, SEEK_END
);
83 lseek(fd
, 0, SEEK_SET
);
89 if (read(fd
, buf
, size
) != size
)
94 for (i
= 0; i
< size
; i
++) {
96 if (entry
!= buf
+ i
&& entry
[0] != '#') {
98 add_exclude(entry
, base
, baselen
, which
);
111 static void add_excludes_from_file(const char *fname
)
113 if (add_excludes_from_file_1(fname
, "", 0,
114 &exclude_list
[EXC_FILE
]) < 0)
115 die("cannot use %s as an exclude file", fname
);
118 static int push_exclude_per_directory(const char *base
, int baselen
)
120 char exclude_file
[PATH_MAX
];
121 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
122 int current_nr
= el
->nr
;
124 if (exclude_per_dir
) {
125 memcpy(exclude_file
, base
, baselen
);
126 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
127 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
132 static void pop_exclude_per_directory(int stk
)
134 struct exclude_list
*el
= &exclude_list
[EXC_DIRS
];
137 free(el
->excludes
[--el
->nr
]);
140 /* Scan the list and let the last match determines the fate.
141 * Return 1 for exclude, 0 for include and -1 for undecided.
143 static int excluded_1(const char *pathname
,
145 struct exclude_list
*el
)
150 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
151 struct exclude
*x
= el
->excludes
[i
];
152 const char *exclude
= x
->pattern
;
155 if (*exclude
== '!') {
160 if (!strchr(exclude
, '/')) {
162 const char *basename
= strrchr(pathname
, '/');
163 basename
= (basename
) ? basename
+1 : pathname
;
164 if (fnmatch(exclude
, basename
, 0) == 0)
168 /* match with FNM_PATHNAME:
169 * exclude has base (baselen long) inplicitly
172 int baselen
= x
->baselen
;
176 if (pathlen
< baselen
||
177 (baselen
&& pathname
[baselen
-1] != '/') ||
178 strncmp(pathname
, x
->base
, baselen
))
181 if (fnmatch(exclude
, pathname
+baselen
,
187 return -1; /* undecided */
190 static int excluded(const char *pathname
)
192 int pathlen
= strlen(pathname
);
195 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
196 switch (excluded_1(pathname
, pathlen
, &exclude_list
[st
])) {
211 static struct nond_on_fs
**dir
;
213 static int dir_alloc
;
215 static void add_name(const char *pathname
, int len
)
217 struct nond_on_fs
*ent
;
219 if (cache_name_pos(pathname
, len
) >= 0)
222 if (nr_dir
== dir_alloc
) {
223 dir_alloc
= alloc_nr(dir_alloc
);
224 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
226 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
228 memcpy(ent
->name
, pathname
, len
);
234 * Read a directory tree. We currently ignore anything but
235 * directories, regular files and symlinks. That's because git
236 * doesn't handle them at all yet. Maybe that will change some
239 * Also, we ignore the name ".git" (even if it is not a directory).
240 * That likely will not change.
242 static void read_directory(const char *path
, const char *base
, int baselen
)
244 DIR *dir
= opendir(path
);
249 char fullname
[MAXPATHLEN
+ 1];
250 memcpy(fullname
, base
, baselen
);
252 exclude_stk
= push_exclude_per_directory(base
, baselen
);
254 while ((de
= readdir(dir
)) != NULL
) {
257 if ((de
->d_name
[0] == '.') &&
258 (de
->d_name
[1] == 0 ||
259 !strcmp(de
->d_name
+ 1, ".") ||
260 !strcmp(de
->d_name
+ 1, "git")))
262 len
= strlen(de
->d_name
);
263 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
264 if (excluded(fullname
) != show_ignored
)
272 if (lstat(fullname
, &st
))
274 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
276 if (!S_ISDIR(st
.st_mode
))
280 memcpy(fullname
+ baselen
+ len
, "/", 2);
281 read_directory(fullname
, fullname
,
288 add_name(fullname
, baselen
+ len
);
292 pop_exclude_per_directory(exclude_stk
);
296 static int cmp_name(const void *p1
, const void *p2
)
298 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
299 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
301 return cache_name_compare(e1
->name
, e1
->len
,
306 * Match a pathspec against a filename. The first "len" characters
307 * are the common prefix
309 static int match(const char **spec
, const char *filename
, int len
)
313 while ((m
= *spec
++) != NULL
) {
314 int matchlen
= strlen(m
+ len
);
318 if (!strncmp(m
+ len
, filename
+ len
, matchlen
)) {
319 if (m
[len
+ matchlen
- 1] == '/')
321 switch (filename
[len
+ matchlen
]) {
326 if (!fnmatch(m
+ len
, filename
+ len
, 0))
332 static void show_dir_entry(const char *tag
, struct nond_on_fs
*ent
)
334 int len
= prefix_len
;
335 int offset
= prefix_offset
;
338 die("git-ls-files: internal error - directory entry not superset of prefix");
340 if (pathspec
&& !match(pathspec
, ent
->name
, len
))
343 printf("%s%s%c", tag
, ent
->name
+ offset
, line_terminator
);
346 static void show_killed_files(void)
349 for (i
= 0; i
< nr_dir
; i
++) {
350 struct nond_on_fs
*ent
= dir
[i
];
352 int pos
, len
, killed
= 0;
354 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
355 sp
= strchr(cp
, '/');
357 /* If ent->name is prefix of an entry in the
358 * cache, it will be killed.
360 pos
= cache_name_pos(ent
->name
, ent
->len
);
362 die("bug in show-killed-files");
364 while (pos
< active_nr
&&
365 ce_stage(active_cache
[pos
]))
366 pos
++; /* skip unmerged */
367 if (active_nr
<= pos
)
369 /* pos points at a name immediately after
370 * ent->name in the cache. Does it expect
371 * ent->name to be a directory?
373 len
= ce_namelen(active_cache
[pos
]);
374 if ((ent
->len
< len
) &&
375 !strncmp(active_cache
[pos
]->name
,
376 ent
->name
, ent
->len
) &&
377 active_cache
[pos
]->name
[ent
->len
] == '/')
381 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
382 /* If any of the leading directories in
383 * ent->name is registered in the cache,
384 * ent->name will be killed.
391 show_dir_entry(tag_killed
, dir
[i
]);
395 static void show_ce_entry(const char *tag
, struct cache_entry
*ce
)
397 int len
= prefix_len
;
398 int offset
= prefix_offset
;
400 if (len
>= ce_namelen(ce
))
401 die("git-ls-files: internal error - cache entry not superset of prefix");
403 if (pathspec
&& !match(pathspec
, ce
->name
, len
))
407 printf("%s%s%c", tag
, ce
->name
+ offset
, line_terminator
);
409 printf("%s%06o %s %d\t%s%c",
412 sha1_to_hex(ce
->sha1
),
414 ce
->name
+ offset
, line_terminator
);
417 static void show_files(void)
421 /* For cached/deleted files we don't need to even do the readdir */
422 if (show_others
|| show_killed
) {
423 const char *path
= ".", *base
= "";
424 int baselen
= prefix_len
;
427 path
= base
= prefix
;
428 read_directory(path
, base
, baselen
);
429 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
431 for (i
= 0; i
< nr_dir
; i
++)
432 show_dir_entry(tag_other
, dir
[i
]);
436 if (show_cached
| show_stage
) {
437 for (i
= 0; i
< active_nr
; i
++) {
438 struct cache_entry
*ce
= active_cache
[i
];
439 if (excluded(ce
->name
) != show_ignored
)
441 if (show_unmerged
&& !ce_stage(ce
))
443 show_ce_entry(ce_stage(ce
) ? tag_unmerged
: tag_cached
, ce
);
447 for (i
= 0; i
< active_nr
; i
++) {
448 struct cache_entry
*ce
= active_cache
[i
];
450 if (excluded(ce
->name
) != show_ignored
)
452 if (!lstat(ce
->name
, &st
))
454 show_ce_entry(tag_removed
, ce
);
460 * Prune the index to only contain stuff starting with "prefix"
462 static void prune_cache(void)
464 int pos
= cache_name_pos(prefix
, prefix_len
);
465 unsigned int first
, last
;
473 while (last
> first
) {
474 int next
= (last
+ first
) >> 1;
475 struct cache_entry
*ce
= active_cache
[next
];
476 if (!strncmp(ce
->name
, prefix
, prefix_len
)) {
485 static void verify_pathspec(void)
487 const char **p
, *n
, *prev
;
493 for (p
= pathspec
; (n
= *p
) != NULL
; p
++) {
495 for (i
= 0; i
< max
; i
++) {
497 if (prev
&& prev
[i
] != c
)
499 if (!c
|| c
== '*' || c
== '?')
512 if (prefix_offset
> max
|| memcmp(prev
, prefix
, prefix_offset
))
513 die("git-ls-files: cannot generate relative filenames containing '..'");
518 real_prefix
= xmalloc(max
+ 1);
519 memcpy(real_prefix
, prev
, max
);
520 real_prefix
[max
] = 0;
522 prefix
= real_prefix
;
525 static const char ls_files_usage
[] =
526 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
527 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
528 "[ --exclude-per-directory=<filename> ]";
530 int main(int argc
, char **argv
)
535 prefix
= setup_git_directory();
537 prefix_offset
= strlen(prefix
);
539 for (i
= 1; i
< argc
; i
++) {
542 if (!strcmp(arg
, "-z")) {
546 if (!strcmp(arg
, "-t")) {
554 if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
558 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
562 if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
566 if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
570 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
574 if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
578 if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
579 /* There's no point in showing unmerged unless
580 * you also show the stage information.
586 if (!strcmp(arg
, "-x") && i
+1 < argc
) {
588 add_exclude(argv
[++i
], "", 0, &exclude_list
[EXC_CMDL
]);
591 if (!strncmp(arg
, "--exclude=", 10)) {
593 add_exclude(arg
+10, "", 0, &exclude_list
[EXC_CMDL
]);
596 if (!strcmp(arg
, "-X") && i
+1 < argc
) {
598 add_excludes_from_file(argv
[++i
]);
601 if (!strncmp(arg
, "--exclude-from=", 15)) {
603 add_excludes_from_file(arg
+15);
606 if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
608 exclude_per_dir
= arg
+ 24;
611 if (!strcmp(arg
, "--full-name")) {
616 usage(ls_files_usage
);
620 pathspec
= get_pathspec(prefix
, argv
+ i
);
622 /* Verify that the pathspec matches the prefix */
626 if (show_ignored
&& !exc_given
) {
627 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
632 /* With no flags, we default to showing the cached files */
633 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
| show_killed
))