]>
Commit | Line | Data |
---|---|---|
8695c8bf LT |
1 | /* |
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. | |
5 | * | |
6 | * Copyright (C) Linus Torvalds, 2005 | |
7 | */ | |
8 | #include <dirent.h> | |
9ff768e9 | 9 | #include <fnmatch.h> |
8695c8bf LT |
10 | |
11 | #include "cache.h" | |
12 | ||
13 | static int show_deleted = 0; | |
14 | static int show_cached = 0; | |
15 | static int show_others = 0; | |
16 | static int show_ignored = 0; | |
aee46198 | 17 | static int show_stage = 0; |
eec8c633 | 18 | static int show_unmerged = 0; |
b83c8345 | 19 | static int line_terminator = '\n'; |
8695c8bf | 20 | |
9ff768e9 NP |
21 | static int nr_excludes; |
22 | static const char **excludes; | |
23 | static int excludes_alloc; | |
24 | ||
25 | static void add_exclude(const char *string) | |
26 | { | |
27 | if (nr_excludes == excludes_alloc) { | |
28 | excludes_alloc = alloc_nr(excludes_alloc); | |
29 | excludes = realloc(excludes, excludes_alloc*sizeof(char *)); | |
30 | } | |
31 | excludes[nr_excludes++] = string; | |
32 | } | |
33 | ||
34 | static void add_excludes_from_file(const char *fname) | |
35 | { | |
36 | int fd, i; | |
37 | long size; | |
38 | char *buf, *entry; | |
39 | ||
40 | fd = open(fname, O_RDONLY); | |
41 | if (fd < 0) | |
42 | goto err; | |
43 | size = lseek(fd, 0, SEEK_END); | |
44 | if (size < 0) | |
45 | goto err; | |
46 | lseek(fd, 0, SEEK_SET); | |
47 | if (size == 0) { | |
48 | close(fd); | |
49 | return; | |
50 | } | |
51 | buf = xmalloc(size); | |
52 | if (read(fd, buf, size) != size) | |
53 | goto err; | |
54 | close(fd); | |
55 | ||
56 | entry = buf; | |
57 | for (i = 0; i < size; i++) { | |
58 | if (buf[i] == '\n') { | |
59 | if (entry != buf + i) { | |
60 | buf[i] = 0; | |
61 | add_exclude(entry); | |
62 | } | |
63 | entry = buf + i + 1; | |
64 | } | |
65 | } | |
66 | return; | |
67 | ||
68 | err: perror(fname); | |
69 | exit(1); | |
70 | } | |
71 | ||
72 | static int excluded(const char *pathname) | |
73 | { | |
74 | int i; | |
75 | if (nr_excludes) { | |
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) | |
80 | return 1; | |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
8695c8bf LT |
85 | static const char **dir; |
86 | static int nr_dir; | |
87 | static int dir_alloc; | |
88 | ||
89 | static void add_name(const char *pathname, int len) | |
90 | { | |
91 | char *name; | |
92 | ||
93 | if (cache_name_pos(pathname, len) >= 0) | |
94 | return; | |
95 | ||
96 | if (nr_dir == dir_alloc) { | |
97 | dir_alloc = alloc_nr(dir_alloc); | |
812666c8 | 98 | dir = xrealloc(dir, dir_alloc*sizeof(char *)); |
8695c8bf | 99 | } |
812666c8 | 100 | name = xmalloc(len + 1); |
8695c8bf LT |
101 | memcpy(name, pathname, len + 1); |
102 | dir[nr_dir++] = name; | |
103 | } | |
104 | ||
105 | /* | |
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. | |
109 | * | |
110 | * Also, we currently ignore all names starting with a dot. | |
aebb2679 | 111 | * That likely will not change. |
8695c8bf LT |
112 | */ |
113 | static void read_directory(const char *path, const char *base, int baselen) | |
114 | { | |
115 | DIR *dir = opendir(path); | |
116 | ||
117 | if (dir) { | |
118 | struct dirent *de; | |
119 | char fullname[MAXPATHLEN + 1]; | |
120 | memcpy(fullname, base, baselen); | |
121 | ||
122 | while ((de = readdir(dir)) != NULL) { | |
123 | int len; | |
124 | ||
125 | if (de->d_name[0] == '.') | |
126 | continue; | |
9ff768e9 NP |
127 | if (excluded(de->d_name) != show_ignored) |
128 | continue; | |
8695c8bf LT |
129 | len = strlen(de->d_name); |
130 | memcpy(fullname + baselen, de->d_name, len+1); | |
131 | ||
132 | switch (de->d_type) { | |
133 | struct stat st; | |
134 | default: | |
135 | continue; | |
136 | case DT_UNKNOWN: | |
137 | if (lstat(fullname, &st)) | |
138 | continue; | |
139 | if (S_ISREG(st.st_mode)) | |
140 | break; | |
141 | if (!S_ISDIR(st.st_mode)) | |
142 | continue; | |
143 | /* fallthrough */ | |
144 | case DT_DIR: | |
145 | memcpy(fullname + baselen + len, "/", 2); | |
146 | read_directory(fullname, fullname, baselen + len + 1); | |
147 | continue; | |
148 | case DT_REG: | |
149 | break; | |
150 | } | |
151 | add_name(fullname, baselen + len); | |
152 | } | |
153 | closedir(dir); | |
154 | } | |
155 | } | |
156 | ||
157 | static int cmp_name(const void *p1, const void *p2) | |
158 | { | |
159 | const char *n1 = *(const char **)p1; | |
160 | const char *n2 = *(const char **)p2; | |
161 | int l1 = strlen(n1), l2 = strlen(n2); | |
162 | ||
163 | return cache_name_compare(n1, l1, n2, l2); | |
164 | } | |
165 | ||
166 | static void show_files(void) | |
167 | { | |
168 | int i; | |
169 | ||
170 | /* For cached/deleted files we don't need to even do the readdir */ | |
9ff768e9 | 171 | if (show_others) { |
8695c8bf LT |
172 | read_directory(".", "", 0); |
173 | qsort(dir, nr_dir, sizeof(char *), cmp_name); | |
8695c8bf | 174 | for (i = 0; i < nr_dir; i++) |
b83c8345 | 175 | printf("%s%c", dir[i], line_terminator); |
8695c8bf | 176 | } |
aee46198 | 177 | if (show_cached | show_stage) { |
8695c8bf LT |
178 | for (i = 0; i < active_nr; i++) { |
179 | struct cache_entry *ce = active_cache[i]; | |
9ff768e9 NP |
180 | if (excluded(ce->name) != show_ignored) |
181 | continue; | |
eec8c633 LT |
182 | if (show_unmerged && !ce_stage(ce)) |
183 | continue; | |
aee46198 JH |
184 | if (!show_stage) |
185 | printf("%s%c", ce->name, line_terminator); | |
186 | else | |
187 | printf(/* "%06o %s %d %10d %s%c", */ | |
188 | "%06o %s %d %s%c", | |
189 | ntohl(ce->ce_mode), | |
190 | sha1_to_hex(ce->sha1), | |
191 | ce_stage(ce), | |
192 | /* ntohl(ce->ce_size), */ | |
193 | ce->name, line_terminator); | |
8695c8bf LT |
194 | } |
195 | } | |
196 | if (show_deleted) { | |
197 | for (i = 0; i < active_nr; i++) { | |
198 | struct cache_entry *ce = active_cache[i]; | |
199 | struct stat st; | |
9ff768e9 NP |
200 | if (excluded(ce->name) != show_ignored) |
201 | continue; | |
8695c8bf LT |
202 | if (!stat(ce->name, &st)) |
203 | continue; | |
b83c8345 | 204 | printf("%s%c", ce->name, line_terminator); |
8695c8bf LT |
205 | } |
206 | } | |
8695c8bf LT |
207 | } |
208 | ||
209 | int main(int argc, char **argv) | |
210 | { | |
211 | int i; | |
212 | ||
213 | for (i = 1; i < argc; i++) { | |
214 | char *arg = argv[i]; | |
215 | ||
b83c8345 JH |
216 | if (!strcmp(arg, "-z")) { |
217 | line_terminator = 0; | |
218 | continue; | |
219 | } | |
220 | ||
8695c8bf LT |
221 | if (!strcmp(arg, "--cached")) { |
222 | show_cached = 1; | |
223 | continue; | |
224 | } | |
225 | if (!strcmp(arg, "--deleted")) { | |
226 | show_deleted = 1; | |
227 | continue; | |
228 | } | |
229 | if (!strcmp(arg, "--others")) { | |
230 | show_others = 1; | |
231 | continue; | |
232 | } | |
233 | if (!strcmp(arg, "--ignored")) { | |
234 | show_ignored = 1; | |
235 | continue; | |
236 | } | |
aee46198 JH |
237 | if (!strcmp(arg, "--stage")) { |
238 | show_stage = 1; | |
239 | continue; | |
240 | } | |
eec8c633 LT |
241 | if (!strcmp(arg, "--unmerged")) { |
242 | // There's no point in showing unmerged unless you also show the stage information | |
243 | show_stage = 1; | |
244 | show_unmerged = 1; | |
245 | continue; | |
246 | } | |
8695c8bf | 247 | |
9ff768e9 NP |
248 | if (!strcmp(arg, "-x") && i+1 < argc) { |
249 | add_exclude(argv[++i]); | |
250 | continue; | |
251 | } | |
252 | if (!strncmp(arg, "--exclude=", 10)) { | |
253 | add_exclude(arg+10); | |
254 | continue; | |
255 | } | |
256 | if (!strcmp(arg, "-X") && i+1 < argc) { | |
257 | add_excludes_from_file(argv[++i]); | |
258 | continue; | |
259 | } | |
260 | if (!strncmp(arg, "--exclude-from=", 15)) { | |
261 | add_excludes_from_file(arg+15); | |
262 | continue; | |
263 | } | |
264 | ||
265 | usage("show-files [-z] (--[cached|deleted|others|stage])* " | |
266 | "[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]"); | |
267 | } | |
268 | ||
269 | if (show_ignored && !nr_excludes) { | |
270 | fprintf(stderr, "%s: --ignored needs some exclude pattern\n", argv[0]); | |
271 | exit(1); | |
8695c8bf LT |
272 | } |
273 | ||
274 | /* With no flags, we default to showing the cached files */ | |
9ff768e9 | 275 | if (!(show_stage | show_deleted | show_others | show_unmerged)) |
8695c8bf LT |
276 | show_cached = 1; |
277 | ||
278 | read_cache(); | |
279 | show_files(); | |
280 | return 0; | |
281 | } |