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 line_terminator
= '\n';
21 static const char *tag_cached
= "";
22 static const char *tag_unmerged
= "";
23 static const char *tag_removed
= "";
24 static const char *tag_other
= "";
26 static int nr_excludes
;
27 static const char **excludes
;
28 static int excludes_alloc
;
30 static void add_exclude(const char *string
)
32 if (nr_excludes
== excludes_alloc
) {
33 excludes_alloc
= alloc_nr(excludes_alloc
);
34 excludes
= realloc(excludes
, excludes_alloc
*sizeof(char *));
36 excludes
[nr_excludes
++] = string
;
39 static void add_excludes_from_file(const char *fname
)
45 fd
= open(fname
, O_RDONLY
);
48 size
= lseek(fd
, 0, SEEK_END
);
51 lseek(fd
, 0, SEEK_SET
);
57 if (read(fd
, buf
, size
) != size
)
62 for (i
= 0; i
< size
; i
++) {
64 if (entry
!= buf
+ i
) {
77 static int excluded(const char *pathname
)
81 const char *basename
= strrchr(pathname
, '/');
82 basename
= (basename
) ? basename
+1 : pathname
;
83 for (i
= 0; i
< nr_excludes
; i
++)
84 if (fnmatch(excludes
[i
], basename
, 0) == 0)
90 static const char **dir
;
94 static void add_name(const char *pathname
, int len
)
98 if (cache_name_pos(pathname
, len
) >= 0)
101 if (nr_dir
== dir_alloc
) {
102 dir_alloc
= alloc_nr(dir_alloc
);
103 dir
= xrealloc(dir
, dir_alloc
*sizeof(char *));
105 name
= xmalloc(len
+ 1);
106 memcpy(name
, pathname
, len
+ 1);
107 dir
[nr_dir
++] = name
;
111 * Read a directory tree. We currently ignore anything but
112 * directories and regular files. That's because git doesn't
113 * handle them at all yet. Maybe that will change some day.
115 * Also, we currently ignore all names starting with a dot.
116 * That likely will not change.
118 static void read_directory(const char *path
, const char *base
, int baselen
)
120 DIR *dir
= opendir(path
);
124 char fullname
[MAXPATHLEN
+ 1];
125 memcpy(fullname
, base
, baselen
);
127 while ((de
= readdir(dir
)) != NULL
) {
130 if (de
->d_name
[0] == '.')
132 if (excluded(de
->d_name
) != show_ignored
)
134 len
= strlen(de
->d_name
);
135 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
142 if (lstat(fullname
, &st
))
144 if (S_ISREG(st
.st_mode
))
146 if (!S_ISDIR(st
.st_mode
))
150 memcpy(fullname
+ baselen
+ len
, "/", 2);
151 read_directory(fullname
, fullname
,
157 add_name(fullname
, baselen
+ len
);
163 static int cmp_name(const void *p1
, const void *p2
)
165 const char *n1
= *(const char **)p1
;
166 const char *n2
= *(const char **)p2
;
167 int l1
= strlen(n1
), l2
= strlen(n2
);
169 return cache_name_compare(n1
, l1
, n2
, l2
);
172 static void show_files(void)
176 /* For cached/deleted files we don't need to even do the readdir */
178 read_directory(".", "", 0);
179 qsort(dir
, nr_dir
, sizeof(char *), cmp_name
);
180 for (i
= 0; i
< nr_dir
; i
++)
181 printf("%s%s%c", tag_other
, dir
[i
], line_terminator
);
183 if (show_cached
| show_stage
) {
184 for (i
= 0; i
< active_nr
; i
++) {
185 struct cache_entry
*ce
= active_cache
[i
];
186 if (excluded(ce
->name
) != show_ignored
)
188 if (show_unmerged
&& !ce_stage(ce
))
192 ce_stage(ce
) ? tag_unmerged
:
194 ce
->name
, line_terminator
);
196 printf("%s%06o %s %d %s%c",
197 ce_stage(ce
) ? tag_unmerged
:
200 sha1_to_hex(ce
->sha1
),
202 ce
->name
, line_terminator
);
206 for (i
= 0; i
< active_nr
; i
++) {
207 struct cache_entry
*ce
= active_cache
[i
];
209 if (excluded(ce
->name
) != show_ignored
)
211 if (!lstat(ce
->name
, &st
))
213 printf("%s%s%c", tag_removed
, ce
->name
,
219 static const char *ls_files_usage
=
220 "ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged])* "
221 "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
223 int main(int argc
, char **argv
)
227 for (i
= 1; i
< argc
; i
++) {
230 if (!strcmp(arg
, "-z")) {
232 } else if (!strcmp(arg
, "-t")) {
237 } else if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
239 } else if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
241 } else if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
243 } else if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
245 } else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
247 } else if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
248 /* There's no point in showing unmerged unless
249 * you also show the stage information.
253 } else if (!strcmp(arg
, "-x") && i
+1 < argc
) {
254 add_exclude(argv
[++i
]);
255 } else if (!strncmp(arg
, "--exclude=", 10)) {
257 } else if (!strcmp(arg
, "-X") && i
+1 < argc
) {
258 add_excludes_from_file(argv
[++i
]);
259 } else if (!strncmp(arg
, "--exclude-from=", 15)) {
260 add_excludes_from_file(arg
+15);
262 usage(ls_files_usage
);
265 if (show_ignored
&& !nr_excludes
) {
266 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n",
271 /* With no flags, we default to showing the cached files */
272 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
))