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 const char *tag_cached
= "";
23 static const char *tag_unmerged
= "";
24 static const char *tag_removed
= "";
25 static const char *tag_other
= "";
26 static const char *tag_killed
= "";
28 static char *exclude_per_dir
= NULL
;
29 static int nr_excludes
;
30 static int excludes_alloc
;
31 static struct exclude
{
37 static void add_exclude(const char *string
, const char *base
, int baselen
)
39 struct exclude
*x
= xmalloc(sizeof (*x
));
44 if (nr_excludes
== excludes_alloc
) {
45 excludes_alloc
= alloc_nr(excludes_alloc
);
46 excludes
= realloc(excludes
, excludes_alloc
*sizeof(char *));
48 excludes
[nr_excludes
++] = x
;
51 static int add_excludes_from_file_1(const char *fname
,
52 const char *base
, int baselen
)
58 fd
= open(fname
, O_RDONLY
);
61 size
= lseek(fd
, 0, SEEK_END
);
64 lseek(fd
, 0, SEEK_SET
);
70 if (read(fd
, buf
, size
) != size
)
75 for (i
= 0; i
< size
; i
++) {
77 if (entry
!= buf
+ i
&& entry
[0] != '#') {
79 add_exclude(entry
, base
, baselen
);
92 static void add_excludes_from_file(const char *fname
)
94 if (add_excludes_from_file_1(fname
, "", 0) < 0)
95 die("cannot use %s as an exclude file", fname
);
98 static int push_exclude_per_directory(const char *base
, int baselen
)
100 char exclude_file
[PATH_MAX
];
101 int current_nr
= nr_excludes
;
103 if (exclude_per_dir
) {
104 memcpy(exclude_file
, base
, baselen
);
105 strcpy(exclude_file
+ baselen
, exclude_per_dir
);
106 add_excludes_from_file_1(exclude_file
, base
, baselen
);
111 static void pop_exclude_per_directory(int stk
)
113 while (stk
< nr_excludes
)
114 free(excludes
[--nr_excludes
]);
117 static int excluded(const char *pathname
)
122 int pathlen
= strlen(pathname
);
124 for (i
= 0; i
< nr_excludes
; i
++) {
125 struct exclude
*x
= excludes
[i
];
126 const char *exclude
= x
->pattern
;
129 if (*exclude
== '!') {
134 if (!strchr(exclude
, '/')) {
136 const char *basename
= strrchr(pathname
, '/');
137 basename
= (basename
) ? basename
+1 : pathname
;
138 if (fnmatch(exclude
, basename
, 0) == 0)
142 /* match with FNM_PATHNAME:
143 * exclude has base (baselen long) inplicitly
146 int baselen
= x
->baselen
;
150 if (pathlen
< baselen
||
151 (baselen
&& pathname
[baselen
-1] != '/') ||
152 strncmp(pathname
, x
->base
, baselen
))
155 if (fnmatch(exclude
, pathname
+baselen
,
169 static struct nond_on_fs
**dir
;
171 static int dir_alloc
;
173 static void add_name(const char *pathname
, int len
)
175 struct nond_on_fs
*ent
;
177 if (cache_name_pos(pathname
, len
) >= 0)
180 if (nr_dir
== dir_alloc
) {
181 dir_alloc
= alloc_nr(dir_alloc
);
182 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
184 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
186 memcpy(ent
->name
, pathname
, len
);
191 * Read a directory tree. We currently ignore anything but
192 * directories, regular files and symlinks. That's because git
193 * doesn't handle them at all yet. Maybe that will change some
196 * Also, we ignore the name ".git" (even if it is not a directory).
197 * That likely will not change.
199 static void read_directory(const char *path
, const char *base
, int baselen
)
201 DIR *dir
= opendir(path
);
206 char fullname
[MAXPATHLEN
+ 1];
207 memcpy(fullname
, base
, baselen
);
209 exclude_stk
= push_exclude_per_directory(base
, baselen
);
211 while ((de
= readdir(dir
)) != NULL
) {
214 if ((de
->d_name
[0] == '.') &&
215 (de
->d_name
[1] == 0 ||
216 !strcmp(de
->d_name
+ 1, ".") ||
217 !strcmp(de
->d_name
+ 1, "git")))
219 len
= strlen(de
->d_name
);
220 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
221 if (excluded(fullname
) != show_ignored
)
229 if (lstat(fullname
, &st
))
231 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
233 if (!S_ISDIR(st
.st_mode
))
237 memcpy(fullname
+ baselen
+ len
, "/", 2);
238 read_directory(fullname
, fullname
,
245 add_name(fullname
, baselen
+ len
);
249 pop_exclude_per_directory(exclude_stk
);
253 static int cmp_name(const void *p1
, const void *p2
)
255 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
256 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
258 return cache_name_compare(e1
->name
, e1
->len
,
262 static void show_killed_files(void)
265 for (i
= 0; i
< nr_dir
; i
++) {
266 struct nond_on_fs
*ent
= dir
[i
];
268 int pos
, len
, killed
= 0;
270 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
271 sp
= strchr(cp
, '/');
273 /* If ent->name is prefix of an entry in the
274 * cache, it will be killed.
276 pos
= cache_name_pos(ent
->name
, ent
->len
);
278 die("bug in show-killed-files");
280 while (pos
< active_nr
&&
281 ce_stage(active_cache
[pos
]))
282 pos
++; /* skip unmerged */
283 if (active_nr
<= pos
)
285 /* pos points at a name immediately after
286 * ent->name in the cache. Does it expect
287 * ent->name to be a directory?
289 len
= ce_namelen(active_cache
[pos
]);
290 if ((ent
->len
< len
) &&
291 !strncmp(active_cache
[pos
]->name
,
292 ent
->name
, ent
->len
) &&
293 active_cache
[pos
]->name
[ent
->len
] == '/')
297 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
298 /* If any of the leading directories in
299 * ent->name is registered in the cache,
300 * ent->name will be killed.
307 printf("%s%.*s%c", tag_killed
,
308 dir
[i
]->len
, dir
[i
]->name
,
313 static void show_files(void)
317 /* For cached/deleted files we don't need to even do the readdir */
318 if (show_others
|| show_killed
) {
319 read_directory(".", "", 0);
320 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
322 for (i
= 0; i
< nr_dir
; i
++)
323 printf("%s%.*s%c", tag_other
,
324 dir
[i
]->len
, dir
[i
]->name
,
329 if (show_cached
| show_stage
) {
330 for (i
= 0; i
< active_nr
; i
++) {
331 struct cache_entry
*ce
= active_cache
[i
];
332 if (excluded(ce
->name
) != show_ignored
)
334 if (show_unmerged
&& !ce_stage(ce
))
338 ce_stage(ce
) ? tag_unmerged
:
340 ce
->name
, line_terminator
);
342 printf("%s%06o %s %d\t%s%c",
343 ce_stage(ce
) ? tag_unmerged
:
346 sha1_to_hex(ce
->sha1
),
348 ce
->name
, line_terminator
);
352 for (i
= 0; i
< active_nr
; i
++) {
353 struct cache_entry
*ce
= active_cache
[i
];
355 if (excluded(ce
->name
) != show_ignored
)
357 if (!lstat(ce
->name
, &st
))
359 printf("%s%s%c", tag_removed
, ce
->name
,
365 static const char *ls_files_usage
=
366 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
367 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
368 "[ --exclude-per-directory=<filename> ]";
371 int main(int argc
, char **argv
)
375 for (i
= 1; i
< argc
; i
++) {
378 if (!strcmp(arg
, "-z")) {
380 } else if (!strcmp(arg
, "-t")) {
386 } else if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
388 } else if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
390 } else if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
392 } else if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
394 } else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
396 } else if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
398 } else if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
399 /* There's no point in showing unmerged unless
400 * you also show the stage information.
404 } else if (!strcmp(arg
, "-x") && i
+1 < argc
) {
405 add_exclude(argv
[++i
], "", 0);
406 } else if (!strncmp(arg
, "--exclude=", 10)) {
407 add_exclude(arg
+10, "", 0);
408 } else if (!strcmp(arg
, "-X") && i
+1 < argc
) {
409 add_excludes_from_file(argv
[++i
]);
410 } else if (!strncmp(arg
, "--exclude-from=", 15)) {
411 add_excludes_from_file(arg
+15);
412 } else if (!strncmp(arg
, "--exclude-per-directory=", 24)) {
413 exclude_per_dir
= arg
+ 24;
415 usage(ls_files_usage
);
418 if (show_ignored
&& !nr_excludes
) {
419 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
424 /* With no flags, we default to showing the cached files */
425 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
| show_killed
))