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 int nr_excludes
;
29 static const char **excludes
;
30 static int excludes_alloc
;
32 static void add_exclude(const char *string
)
34 if (nr_excludes
== excludes_alloc
) {
35 excludes_alloc
= alloc_nr(excludes_alloc
);
36 excludes
= realloc(excludes
, excludes_alloc
*sizeof(char *));
38 excludes
[nr_excludes
++] = string
;
41 static void add_excludes_from_file(const char *fname
)
47 fd
= open(fname
, O_RDONLY
);
50 size
= lseek(fd
, 0, SEEK_END
);
53 lseek(fd
, 0, SEEK_SET
);
59 if (read(fd
, buf
, size
) != size
)
64 for (i
= 0; i
< size
; i
++) {
66 if (entry
!= buf
+ i
) {
79 static int excluded(const char *pathname
)
83 const char *basename
= strrchr(pathname
, '/');
84 basename
= (basename
) ? basename
+1 : pathname
;
85 for (i
= 0; i
< nr_excludes
; i
++)
86 if (fnmatch(excludes
[i
], basename
, 0) == 0)
97 static struct nond_on_fs
**dir
;
101 static void add_name(const char *pathname
, int len
)
103 struct nond_on_fs
*ent
;
105 if (cache_name_pos(pathname
, len
) >= 0)
108 if (nr_dir
== dir_alloc
) {
109 dir_alloc
= alloc_nr(dir_alloc
);
110 dir
= xrealloc(dir
, dir_alloc
*sizeof(ent
));
112 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
114 memcpy(ent
->name
, pathname
, len
);
119 * Read a directory tree. We currently ignore anything but
120 * directories, regular files and symlinks. That's because git
121 * doesn't handle them at all yet. Maybe that will change some
124 * Also, we currently ignore all names starting with a dot.
125 * That likely will not change.
127 static void read_directory(const char *path
, const char *base
, int baselen
)
129 DIR *dir
= opendir(path
);
133 char fullname
[MAXPATHLEN
+ 1];
134 memcpy(fullname
, base
, baselen
);
136 while ((de
= readdir(dir
)) != NULL
) {
139 if ((de
->d_name
[0] == '.') &&
140 (de
->d_name
[1] == 0 ||
141 !strcmp(de
->d_name
+ 1, ".") ||
142 !strcmp(de
->d_name
+ 1, "git")))
144 if (excluded(de
->d_name
) != show_ignored
)
146 len
= strlen(de
->d_name
);
147 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
154 if (lstat(fullname
, &st
))
156 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
158 if (!S_ISDIR(st
.st_mode
))
162 memcpy(fullname
+ baselen
+ len
, "/", 2);
163 read_directory(fullname
, fullname
,
170 add_name(fullname
, baselen
+ len
);
176 static int cmp_name(const void *p1
, const void *p2
)
178 const struct nond_on_fs
*e1
= *(const struct nond_on_fs
**)p1
;
179 const struct nond_on_fs
*e2
= *(const struct nond_on_fs
**)p2
;
181 return cache_name_compare(e1
->name
, e1
->len
,
185 static void show_killed_files(void)
188 for (i
= 0; i
< nr_dir
; i
++) {
189 struct nond_on_fs
*ent
= dir
[i
];
191 int pos
, len
, killed
= 0;
193 for (cp
= ent
->name
; cp
- ent
->name
< ent
->len
; cp
= sp
+ 1) {
194 sp
= strchr(cp
, '/');
196 /* If ent->name is prefix of an entry in the
197 * cache, it will be killed.
199 pos
= cache_name_pos(ent
->name
, ent
->len
);
201 die("bug in show-killed-files");
203 while (pos
< active_nr
&&
204 ce_stage(active_cache
[pos
]))
205 pos
++; /* skip unmerged */
206 if (active_nr
<= pos
)
208 /* pos points at a name immediately after
209 * ent->name in the cache. Does it expect
210 * ent->name to be a directory?
212 len
= ce_namelen(active_cache
[pos
]);
213 if ((ent
->len
< len
) &&
214 !strncmp(active_cache
[pos
]->name
,
215 ent
->name
, ent
->len
) &&
216 active_cache
[pos
]->name
[ent
->len
] == '/')
220 if (0 <= cache_name_pos(ent
->name
, sp
- ent
->name
)) {
221 /* If any of the leading directories in
222 * ent->name is registered in the cache,
223 * ent->name will be killed.
230 printf("%s%.*s%c", tag_killed
,
231 dir
[i
]->len
, dir
[i
]->name
,
236 static void show_files(void)
240 /* For cached/deleted files we don't need to even do the readdir */
241 if (show_others
|| show_killed
) {
242 read_directory(".", "", 0);
243 qsort(dir
, nr_dir
, sizeof(struct nond_on_fs
*), cmp_name
);
245 for (i
= 0; i
< nr_dir
; i
++)
246 printf("%s%.*s%c", tag_other
,
247 dir
[i
]->len
, dir
[i
]->name
,
252 if (show_cached
| show_stage
) {
253 for (i
= 0; i
< active_nr
; i
++) {
254 struct cache_entry
*ce
= active_cache
[i
];
255 if (excluded(ce
->name
) != show_ignored
)
257 if (show_unmerged
&& !ce_stage(ce
))
261 ce_stage(ce
) ? tag_unmerged
:
263 ce
->name
, line_terminator
);
265 printf("%s%06o %s %d\t%s%c",
266 ce_stage(ce
) ? tag_unmerged
:
269 sha1_to_hex(ce
->sha1
),
271 ce
->name
, line_terminator
);
275 for (i
= 0; i
< active_nr
; i
++) {
276 struct cache_entry
*ce
= active_cache
[i
];
278 if (excluded(ce
->name
) != show_ignored
)
280 if (!lstat(ce
->name
, &st
))
282 printf("%s%s%c", tag_removed
, ce
->name
,
288 static const char *ls_files_usage
=
289 "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
290 "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
292 int main(int argc
, char **argv
)
296 for (i
= 1; i
< argc
; i
++) {
299 if (!strcmp(arg
, "-z")) {
301 } else if (!strcmp(arg
, "-t")) {
307 } else if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
309 } else if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
311 } else if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
313 } else if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
315 } else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
317 } else if (!strcmp(arg
, "-k") || !strcmp(arg
, "--killed")) {
319 } else if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
320 /* There's no point in showing unmerged unless
321 * you also show the stage information.
325 } else if (!strcmp(arg
, "-x") && i
+1 < argc
) {
326 add_exclude(argv
[++i
]);
327 } else if (!strncmp(arg
, "--exclude=", 10)) {
329 } else if (!strcmp(arg
, "-X") && i
+1 < argc
) {
330 add_excludes_from_file(argv
[++i
]);
331 } else if (!strncmp(arg
, "--exclude-from=", 15)) {
332 add_excludes_from_file(arg
+15);
334 usage(ls_files_usage
);
337 if (show_ignored
&& !nr_excludes
) {
338 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
343 /* With no flags, we default to showing the cached files */
344 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
| show_killed
))