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 int nr_excludes
;
22 static const char **excludes
;
23 static int excludes_alloc
;
25 static void add_exclude(const char *string
)
27 if (nr_excludes
== excludes_alloc
) {
28 excludes_alloc
= alloc_nr(excludes_alloc
);
29 excludes
= realloc(excludes
, excludes_alloc
*sizeof(char *));
31 excludes
[nr_excludes
++] = string
;
34 static void add_excludes_from_file(const char *fname
)
40 fd
= open(fname
, O_RDONLY
);
43 size
= lseek(fd
, 0, SEEK_END
);
46 lseek(fd
, 0, SEEK_SET
);
52 if (read(fd
, buf
, size
) != size
)
57 for (i
= 0; i
< size
; i
++) {
59 if (entry
!= buf
+ i
) {
72 static int excluded(const char *pathname
)
76 const char *basename
= strrchr(pathname
, '/');
77 basename
= (basename
) ? basename
+1 : pathname
;
78 for (i
= 0; i
< nr_excludes
; i
++)
79 if (fnmatch(excludes
[i
], basename
, 0) == 0)
85 static const char **dir
;
89 static void add_name(const char *pathname
, int len
)
93 if (cache_name_pos(pathname
, len
) >= 0)
96 if (nr_dir
== dir_alloc
) {
97 dir_alloc
= alloc_nr(dir_alloc
);
98 dir
= xrealloc(dir
, dir_alloc
*sizeof(char *));
100 name
= xmalloc(len
+ 1);
101 memcpy(name
, pathname
, len
+ 1);
102 dir
[nr_dir
++] = name
;
106 * Read a directory tree. We currently ignore anything but
107 * directories and regular files. That's because git doesn't
108 * handle them at all yet. Maybe that will change some day.
110 * Also, we currently ignore all names starting with a dot.
111 * That likely will not change.
113 static void read_directory(const char *path
, const char *base
, int baselen
)
115 DIR *dir
= opendir(path
);
119 char fullname
[MAXPATHLEN
+ 1];
120 memcpy(fullname
, base
, baselen
);
122 while ((de
= readdir(dir
)) != NULL
) {
125 if (de
->d_name
[0] == '.')
127 if (excluded(de
->d_name
) != show_ignored
)
129 len
= strlen(de
->d_name
);
130 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
137 if (lstat(fullname
, &st
))
139 if (S_ISREG(st
.st_mode
))
141 if (!S_ISDIR(st
.st_mode
))
145 memcpy(fullname
+ baselen
+ len
, "/", 2);
146 read_directory(fullname
, fullname
, baselen
+ len
+ 1);
151 add_name(fullname
, baselen
+ len
);
157 static int cmp_name(const void *p1
, const void *p2
)
159 const char *n1
= *(const char **)p1
;
160 const char *n2
= *(const char **)p2
;
161 int l1
= strlen(n1
), l2
= strlen(n2
);
163 return cache_name_compare(n1
, l1
, n2
, l2
);
166 static void show_files(void)
170 /* For cached/deleted files we don't need to even do the readdir */
172 read_directory(".", "", 0);
173 qsort(dir
, nr_dir
, sizeof(char *), cmp_name
);
174 for (i
= 0; i
< nr_dir
; i
++)
175 printf("%s%c", dir
[i
], line_terminator
);
177 if (show_cached
| show_stage
) {
178 for (i
= 0; i
< active_nr
; i
++) {
179 struct cache_entry
*ce
= active_cache
[i
];
180 if (excluded(ce
->name
) != show_ignored
)
182 if (show_unmerged
&& !ce_stage(ce
))
185 printf("%s%c", ce
->name
, line_terminator
);
187 printf(/* "%06o %s %d %10d %s%c", */
190 sha1_to_hex(ce
->sha1
),
192 /* ntohl(ce->ce_size), */
193 ce
->name
, line_terminator
);
197 for (i
= 0; i
< active_nr
; i
++) {
198 struct cache_entry
*ce
= active_cache
[i
];
200 if (excluded(ce
->name
) != show_ignored
)
202 if (!stat(ce
->name
, &st
))
204 printf("%s%c", ce
->name
, line_terminator
);
209 static const char *ls_files_usage
=
210 "ls-files [-z] (--[cached|deleted|others|stage|unmerged])* "
211 "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
213 int main(int argc
, char **argv
)
217 for (i
= 1; i
< argc
; i
++) {
220 if (!strcmp(arg
, "-z")) {
222 } else if (!strcmp(arg
, "-c") || !strcmp(arg
, "--cached")) {
224 } else if (!strcmp(arg
, "-d") || !strcmp(arg
, "--deleted")) {
226 } else if (!strcmp(arg
, "-o") || !strcmp(arg
, "--others")) {
228 } else if (!strcmp(arg
, "-i") || !strcmp(arg
, "--ignored")) {
230 } else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--stage")) {
232 } else if (!strcmp(arg
, "-u") || !strcmp(arg
, "--unmerged")) {
233 // There's no point in showing unmerged unless you also show the stage information
236 } else if (!strcmp(arg
, "-x") && i
+1 < argc
) {
237 add_exclude(argv
[++i
]);
238 } else if (!strncmp(arg
, "--exclude=", 10)) {
240 } else if (!strcmp(arg
, "-X") && i
+1 < argc
) {
241 add_excludes_from_file(argv
[++i
]);
242 } else if (!strncmp(arg
, "--exclude-from=", 15)) {
243 add_excludes_from_file(arg
+15);
245 usage(ls_files_usage
);
248 if (show_ignored
&& !nr_excludes
) {
249 fprintf(stderr
, "%s: --ignored needs some exclude pattern\n", argv
[0]);
253 /* With no flags, we default to showing the cached files */
254 if (!(show_stage
| show_deleted
| show_others
| show_unmerged
))