libify git-ls-files directory traversal
This moves the core directory traversal and filename exclusion logic into the general git library, making it available for other users directly. If we ever want to do "git commit" or "git add" as a built-in (and we do), we want to be able to handle most of git-ls-files as a library. NOTE! Not all of git-ls-files is libified by this. The index matching and pathspec prefix calculation is still in ls-files.c, but this is a big part of it. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
May 16, 2006 at 19:02 UTC
453ec4bdf403c2e89892266a0a660c21680d3f9d
4 files changed
+376
-336
Makefile
+2
-2
@@ -199,7 +199,7 @@ LIB_H = \
199
blob.h cache.h commit.h csum-file.h delta.h \
200
diff.h object.h pack.h pkt-line.h quote.h refs.h \
201
run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
202
- tree-walk.h log-tree.h
202
+ tree-walk.h log-tree.h dir.h
203
204
DIFF_OBJS = \
205
diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -210,7 +210,7 @@ LIB_OBJS = \
210
blob.o commit.o connect.o csum-file.o base85.o \
211
date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
212
object.o pack-check.o patch-delta.o path.o pkt-line.o \
213
- quote.o read-cache.o refs.o run-command.o \
213
+ quote.o read-cache.o refs.o run-command.o dir.o \
214
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
215
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
216
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
dir.c
new
+295
@@ -0,0 +1,295 @@
1
+/*
2
+ * This handles recursive filename detection with exclude
3
+ * files, index knowledge etc..
4
+ *
5
+ * Copyright (C) Linus Torvalds, 2005-2006
6
+ * Junio Hamano, 2005-2006
7
+ */
8
+#include <dirent.h>
9
+#include <fnmatch.h>
10
+
11
+#include "cache.h"
12
+#include "dir.h"
13
+
14
+void add_exclude(const char *string, const char *base,
15
+ int baselen, struct exclude_list *which)
16
+{
17
+ struct exclude *x = xmalloc(sizeof (*x));
18
+
19
+ x->pattern = string;
20
+ x->base = base;
21
+ x->baselen = baselen;
22
+ if (which->nr == which->alloc) {
23
+ which->alloc = alloc_nr(which->alloc);
24
+ which->excludes = realloc(which->excludes,
25
+ which->alloc * sizeof(x));
26
+ }
27
+ which->excludes[which->nr++] = x;
28
+}
29
+
30
+static int add_excludes_from_file_1(const char *fname,
31
+ const char *base,
32
+ int baselen,
33
+ struct exclude_list *which)
34
+{
35
+ int fd, i;
36
+ long size;
37
+ char *buf, *entry;
38
+
39
+ fd = open(fname, O_RDONLY);
40
+ if (fd < 0)
41
+ goto err;
42
+ size = lseek(fd, 0, SEEK_END);
43
+ if (size < 0)
44
+ goto err;
45
+ lseek(fd, 0, SEEK_SET);
46
+ if (size == 0) {
47
+ close(fd);
48
+ return 0;
49
+ }
50
+ buf = xmalloc(size+1);
51
+ if (read(fd, buf, size) != size)
52
+ goto err;
53
+ close(fd);
54
+
55
+ buf[size++] = '\n';
56
+ entry = buf;
57
+ for (i = 0; i < size; i++) {
58
+ if (buf[i] == '\n') {
59
+ if (entry != buf + i && entry[0] != '#') {
60
+ buf[i - (i && buf[i-1] == '\r')] = 0;
61
+ add_exclude(entry, base, baselen, which);
62
+ }
63
+ entry = buf + i + 1;
64
+ }
65
+ }
66
+ return 0;
67
+
68
+ err:
69
+ if (0 <= fd)
70
+ close(fd);
71
+ return -1;
72
+}
73
+
74
+void add_excludes_from_file(struct dir_struct *dir, const char *fname)
75
+{
76
+ if (add_excludes_from_file_1(fname, "", 0,
77
+ &dir->exclude_list[EXC_FILE]) < 0)
78
+ die("cannot use %s as an exclude file", fname);
79
+}
80
+
81
+int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
82
+{
83
+ char exclude_file[PATH_MAX];
84
+ struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
85
+ int current_nr = el->nr;
86
+
87
+ if (dir->exclude_per_dir) {
88
+ memcpy(exclude_file, base, baselen);
89
+ strcpy(exclude_file + baselen, dir->exclude_per_dir);
90
+ add_excludes_from_file_1(exclude_file, base, baselen, el);
91
+ }
92
+ return current_nr;
93
+}
94
+
95
+static void pop_exclude_per_directory(struct dir_struct *dir, int stk)
96
+{
97
+ struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
98
+
99
+ while (stk < el->nr)
100
+ free(el->excludes[--el->nr]);
101
+}
102
+
103
+/* Scan the list and let the last match determines the fate.
104
+ * Return 1 for exclude, 0 for include and -1 for undecided.
105
+ */
106
+static int excluded_1(const char *pathname,
107
+ int pathlen,
108
+ struct exclude_list *el)
109
+{
110
+ int i;
111
+
112
+ if (el->nr) {
113
+ for (i = el->nr - 1; 0 <= i; i--) {
114
+ struct exclude *x = el->excludes[i];
115
+ const char *exclude = x->pattern;
116
+ int to_exclude = 1;
117
+
118
+ if (*exclude == '!') {
119
+ to_exclude = 0;
120
+ exclude++;
121
+ }
122
+
123
+ if (!strchr(exclude, '/')) {
124
+ /* match basename */
125
+ const char *basename = strrchr(pathname, '/');
126
+ basename = (basename) ? basename+1 : pathname;
127
+ if (fnmatch(exclude, basename, 0) == 0)
128
+ return to_exclude;
129
+ }
130
+ else {
131
+ /* match with FNM_PATHNAME:
132
+ * exclude has base (baselen long) implicitly
133
+ * in front of it.
134
+ */
135
+ int baselen = x->baselen;
136
+ if (*exclude == '/')
137
+ exclude++;
138
+
139
+ if (pathlen < baselen ||
140
+ (baselen && pathname[baselen-1] != '/') ||
141
+ strncmp(pathname, x->base, baselen))
142
+ continue;
143
+
144
+ if (fnmatch(exclude, pathname+baselen,
145
+ FNM_PATHNAME) == 0)
146
+ return to_exclude;
147
+ }
148
+ }
149
+ }
150
+ return -1; /* undecided */
151
+}
152
+
153
+int excluded(struct dir_struct *dir, const char *pathname)
154
+{
155
+ int pathlen = strlen(pathname);
156
+ int st;
157
+
158
+ for (st = EXC_CMDL; st <= EXC_FILE; st++) {
159
+ switch (excluded_1(pathname, pathlen, &dir->exclude_list[st])) {
160
+ case 0:
161
+ return 0;
162
+ case 1:
163
+ return 1;
164
+ }
165
+ }
166
+ return 0;
167
+}
168
+
169
+static void add_name(struct dir_struct *dir, const char *pathname, int len)
170
+{
171
+ struct dir_entry *ent;
172
+
173
+ if (cache_name_pos(pathname, len) >= 0)
174
+ return;
175
+
176
+ if (dir->nr == dir->alloc) {
177
+ int alloc = alloc_nr(dir->alloc);
178
+ dir->alloc = alloc;
179
+ dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
180
+ }
181
+ ent = xmalloc(sizeof(*ent) + len + 1);
182
+ ent->len = len;
183
+ memcpy(ent->name, pathname, len);
184
+ ent->name[len] = 0;
185
+ dir->entries[dir->nr++] = ent;
186
+}
187
+
188
+static int dir_exists(const char *dirname, int len)
189
+{
190
+ int pos = cache_name_pos(dirname, len);
191
+ if (pos >= 0)
192
+ return 1;
193
+ pos = -pos-1;
194
+ if (pos >= active_nr) /* can't */
195
+ return 0;
196
+ return !strncmp(active_cache[pos]->name, dirname, len);
197
+}
198
+
199
+/*
200
+ * Read a directory tree. We currently ignore anything but
201
+ * directories, regular files and symlinks. That's because git
202
+ * doesn't handle them at all yet. Maybe that will change some
203
+ * day.
204
+ *
205
+ * Also, we ignore the name ".git" (even if it is not a directory).
206
+ * That likely will not change.
207
+ */
208
+static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen)
209
+{
210
+ DIR *fdir = opendir(path);
211
+ int contents = 0;
212
+
213
+ if (fdir) {
214
+ int exclude_stk;
215
+ struct dirent *de;
216
+ char fullname[MAXPATHLEN + 1];
217
+ memcpy(fullname, base, baselen);
218
+
219
+ exclude_stk = push_exclude_per_directory(dir, base, baselen);
220
+
221
+ while ((de = readdir(fdir)) != NULL) {
222
+ int len;
223
+
224
+ if ((de->d_name[0] == '.') &&
225
+ (de->d_name[1] == 0 ||
226
+ !strcmp(de->d_name + 1, ".") ||
227
+ !strcmp(de->d_name + 1, "git")))
228
+ continue;
229
+ len = strlen(de->d_name);
230
+ memcpy(fullname + baselen, de->d_name, len+1);
231
+ if (excluded(dir, fullname) != dir->show_ignored) {
232
+ if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
233
+ continue;
234
+ }
235
+ }
236
+
237
+ switch (DTYPE(de)) {
238
+ struct stat st;
239
+ int subdir, rewind_base;
240
+ default:
241
+ continue;
242
+ case DT_UNKNOWN:
243
+ if (lstat(fullname, &st))
244
+ continue;
245
+ if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
246
+ break;
247
+ if (!S_ISDIR(st.st_mode))
248
+ continue;
249
+ /* fallthrough */
250
+ case DT_DIR:
251
+ memcpy(fullname + baselen + len, "/", 2);
252
+ len++;
253
+ rewind_base = dir->nr;
254
+ subdir = read_directory_recursive(dir, fullname, fullname,
255
+ baselen + len);
256
+ if (dir->show_other_directories &&
257
+ (subdir || !dir->hide_empty_directories) &&
258
+ !dir_exists(fullname, baselen + len)) {
259
+ // Rewind the read subdirectory
260
+ while (dir->nr > rewind_base)
261
+ free(dir->entries[--dir->nr]);
262
+ break;
263
+ }
264
+ contents += subdir;
265
+ continue;
266
+ case DT_REG:
267
+ case DT_LNK:
268
+ break;
269
+ }
270
+ add_name(dir, fullname, baselen + len);
271
+ contents++;
272
+ }
273
+ closedir(fdir);
274
+
275
+ pop_exclude_per_directory(dir, exclude_stk);
276
+ }
277
+
278
+ return contents;
279
+}
280
+
281
+static int cmp_name(const void *p1, const void *p2)
282
+{
283
+ const struct dir_entry *e1 = *(const struct dir_entry **)p1;
284
+ const struct dir_entry *e2 = *(const struct dir_entry **)p2;
285
+
286
+ return cache_name_compare(e1->name, e1->len,
287
+ e2->name, e2->len);
288
+}
289
+
290
+int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen)
291
+{
292
+ read_directory_recursive(dir, path, base, baselen);
293
+ qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
294
+ return dir->nr;
295
+}
dir.h
new
+50
@@ -0,0 +1,50 @@
1
+#ifndef DIR_H
2
+#define DIR_H
3
+
4
+/*
5
+ * We maintain three exclude pattern lists:
6
+ * EXC_CMDL lists patterns explicitly given on the command line.
7
+ * EXC_DIRS lists patterns obtained from per-directory ignore files.
8
+ * EXC_FILE lists patterns from fallback ignore files.
9
+ */
10
+#define EXC_CMDL 0
11
+#define EXC_DIRS 1
12
+#define EXC_FILE 2
13
+
14
+
15
+struct dir_entry {
16
+ int len;
17
+ char name[FLEX_ARRAY]; /* more */
18
+};
19
+
20
+struct exclude_list {
21
+ int nr;
22
+ int alloc;
23
+ struct exclude {
24
+ const char *pattern;
25
+ const char *base;
26
+ int baselen;
27
+ } **excludes;
28
+};
29
+
30
+struct dir_struct {
31
+ int nr, alloc;
32
+ unsigned int show_ignored:1,
33
+ show_other_directories:1,
34
+ hide_empty_directories:1;
35
+ struct dir_entry **entries;
36
+
37
+ /* Exclude info */
38
+ const char *exclude_per_dir;
39
+ struct exclude_list exclude_list[3];
40
+};
41
+
42
+extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
43
+extern int excluded(struct dir_struct *, const char *);
44
+extern void add_excludes_from_file(struct dir_struct *, const char *fname);
45
+extern void add_exclude(const char *string, const char *base,
46
+ int baselen, struct exclude_list *which);
47
+extern int push_exclude_per_directory(struct dir_struct *,
48
+ const char *base, int baselen);
49
+
50
+#endif
ls-files.c
+29
-334
@@ -5,23 +5,20 @@
5
*
6
* Copyright (C) Linus Torvalds, 2005
7
*/
8
-#include <dirent.h>
8
#include <fnmatch.h>
9
10
#include "cache.h"
11
#include "quote.h"
12
+#include "dir.h"
13
14
static int abbrev = 0;
15
static int show_deleted = 0;
16
static int show_cached = 0;
17
static int show_others = 0;
18
-static int show_ignored = 0;
18
static int show_stage = 0;
19
static int show_unmerged = 0;
20
static int show_modified = 0;
21
static int show_killed = 0;
23
-static int show_other_directories = 0;
24
-static int hide_empty_directories = 0;
22
static int show_valid_bit = 0;
23
static int line_terminator = '\n';
24
@@ -38,309 +35,6 @@ static const char *tag_other = "";
35
static const char *tag_killed = "";
36
static const char *tag_modified = "";
37
41
-static const char *exclude_per_dir = NULL;
42
-
43
-/* We maintain three exclude pattern lists:
44
- * EXC_CMDL lists patterns explicitly given on the command line.
45
- * EXC_DIRS lists patterns obtained from per-directory ignore files.
46
- * EXC_FILE lists patterns from fallback ignore files.
47
- */
48
-#define EXC_CMDL 0
49
-#define EXC_DIRS 1
50
-#define EXC_FILE 2
51
-static struct exclude_list {
52
- int nr;
53
- int alloc;
54
- struct exclude {
55
- const char *pattern;
56
- const char *base;
57
- int baselen;
58
- } **excludes;
59
-} exclude_list[3];
60
-
61
-static void add_exclude(const char *string, const char *base,
62
- int baselen, struct exclude_list *which)
63
-{
64
- struct exclude *x = xmalloc(sizeof (*x));
65
-
66
- x->pattern = string;
67
- x->base = base;
68
- x->baselen = baselen;
69
- if (which->nr == which->alloc) {
70
- which->alloc = alloc_nr(which->alloc);
71
- which->excludes = realloc(which->excludes,
72
- which->alloc * sizeof(x));
73
- }
74
- which->excludes[which->nr++] = x;
75
-}
76
-
77
-static int add_excludes_from_file_1(const char *fname,
78
- const char *base,
79
- int baselen,
80
- struct exclude_list *which)
81
-{
82
- int fd, i;
83
- long size;
84
- char *buf, *entry;
85
-
86
- fd = open(fname, O_RDONLY);
87
- if (fd < 0)
88
- goto err;
89
- size = lseek(fd, 0, SEEK_END);
90
- if (size < 0)
91
- goto err;
92
- lseek(fd, 0, SEEK_SET);
93
- if (size == 0) {
94
- close(fd);
95
- return 0;
96
- }
97
- buf = xmalloc(size+1);
98
- if (read(fd, buf, size) != size)
99
- goto err;
100
- close(fd);
101
-
102
- buf[size++] = '\n';
103
- entry = buf;
104
- for (i = 0; i < size; i++) {
105
- if (buf[i] == '\n') {
106
- if (entry != buf + i && entry[0] != '#') {
107
- buf[i - (i && buf[i-1] == '\r')] = 0;
108
- add_exclude(entry, base, baselen, which);
109
- }
110
- entry = buf + i + 1;
111
- }
112
- }
113
- return 0;
114
-
115
- err:
116
- if (0 <= fd)
117
- close(fd);
118
- return -1;
119
-}
120
-
121
-static void add_excludes_from_file(const char *fname)
122
-{
123
- if (add_excludes_from_file_1(fname, "", 0,
124
- &exclude_list[EXC_FILE]) < 0)
125
- die("cannot use %s as an exclude file", fname);
126
-}
127
-
128
-static int push_exclude_per_directory(const char *base, int baselen)
129
-{
130
- char exclude_file[PATH_MAX];
131
- struct exclude_list *el = &exclude_list[EXC_DIRS];
132
- int current_nr = el->nr;
133
-
134
- if (exclude_per_dir) {
135
- memcpy(exclude_file, base, baselen);
136
- strcpy(exclude_file + baselen, exclude_per_dir);
137
- add_excludes_from_file_1(exclude_file, base, baselen, el);
138
- }
139
- return current_nr;
140
-}
141
-
142
-static void pop_exclude_per_directory(int stk)
143
-{
144
- struct exclude_list *el = &exclude_list[EXC_DIRS];
145
-
146
- while (stk < el->nr)
147
- free(el->excludes[--el->nr]);
148
-}
149
-
150
-/* Scan the list and let the last match determines the fate.
151
- * Return 1 for exclude, 0 for include and -1 for undecided.
152
- */
153
-static int excluded_1(const char *pathname,
154
- int pathlen,
155
- struct exclude_list *el)
156
-{
157
- int i;
158
-
159
- if (el->nr) {
160
- for (i = el->nr - 1; 0 <= i; i--) {
161
- struct exclude *x = el->excludes[i];
162
- const char *exclude = x->pattern;
163
- int to_exclude = 1;
164
-
165
- if (*exclude == '!') {
166
- to_exclude = 0;
167
- exclude++;
168
- }
169
-
170
- if (!strchr(exclude, '/')) {
171
- /* match basename */
172
- const char *basename = strrchr(pathname, '/');
173
- basename = (basename) ? basename+1 : pathname;
174
- if (fnmatch(exclude, basename, 0) == 0)
175
- return to_exclude;
176
- }
177
- else {
178
- /* match with FNM_PATHNAME:
179
- * exclude has base (baselen long) implicitly
180
- * in front of it.
181
- */
182
- int baselen = x->baselen;
183
- if (*exclude == '/')
184
- exclude++;
185
-
186
- if (pathlen < baselen ||
187
- (baselen && pathname[baselen-1] != '/') ||
188
- strncmp(pathname, x->base, baselen))
189
- continue;
190
-
191
- if (fnmatch(exclude, pathname+baselen,
192
- FNM_PATHNAME) == 0)
193
- return to_exclude;
194
- }
195
- }
196
- }
197
- return -1; /* undecided */
198
-}
199
-
200
-static int excluded(const char *pathname)
201
-{
202
- int pathlen = strlen(pathname);
203
- int st;
204
-
205
- for (st = EXC_CMDL; st <= EXC_FILE; st++) {
206
- switch (excluded_1(pathname, pathlen, &exclude_list[st])) {
207
- case 0:
208
- return 0;
209
- case 1:
210
- return 1;
211
- }
212
- }
213
- return 0;
214
-}
215
-
216
-struct nond_on_fs {
217
- int len;
218
- char name[FLEX_ARRAY]; /* more */
219
-};
220
-
221
-static struct nond_on_fs **dir;
222
-static int nr_dir;
223
-static int dir_alloc;
224
-
225
-static void add_name(const char *pathname, int len)
226
-{
227
- struct nond_on_fs *ent;
228
-
229
- if (cache_name_pos(pathname, len) >= 0)
230
- return;
231
-
232
- if (nr_dir == dir_alloc) {
233
- dir_alloc = alloc_nr(dir_alloc);
234
- dir = xrealloc(dir, dir_alloc*sizeof(ent));
235
- }
236
- ent = xmalloc(sizeof(*ent) + len + 1);
237
- ent->len = len;
238
- memcpy(ent->name, pathname, len);
239
- ent->name[len] = 0;
240
- dir[nr_dir++] = ent;
241
-}
242
-
243
-static int dir_exists(const char *dirname, int len)
244
-{
245
- int pos = cache_name_pos(dirname, len);
246
- if (pos >= 0)
247
- return 1;
248
- pos = -pos-1;
249
- if (pos >= active_nr) /* can't */
250
- return 0;
251
- return !strncmp(active_cache[pos]->name, dirname, len);
252
-}
253
-
254
-/*
255
- * Read a directory tree. We currently ignore anything but
256
- * directories, regular files and symlinks. That's because git
257
- * doesn't handle them at all yet. Maybe that will change some
258
- * day.
259
- *
260
- * Also, we ignore the name ".git" (even if it is not a directory).
261
- * That likely will not change.
262
- */
263
-static int read_directory(const char *path, const char *base, int baselen)
264
-{
265
- DIR *fdir = opendir(path);
266
- int contents = 0;
267
-
268
- if (fdir) {
269
- int exclude_stk;
270
- struct dirent *de;
271
- char fullname[MAXPATHLEN + 1];
272
- memcpy(fullname, base, baselen);
273
-
274
- exclude_stk = push_exclude_per_directory(base, baselen);
275
-
276
- while ((de = readdir(fdir)) != NULL) {
277
- int len;
278
-
279
- if ((de->d_name[0] == '.') &&
280
- (de->d_name[1] == 0 ||
281
- !strcmp(de->d_name + 1, ".") ||
282
- !strcmp(de->d_name + 1, "git")))
283
- continue;
284
- len = strlen(de->d_name);
285
- memcpy(fullname + baselen, de->d_name, len+1);
286
- if (excluded(fullname) != show_ignored) {
287
- if (!show_ignored || DTYPE(de) != DT_DIR) {
288
- continue;
289
- }
290
- }
291
-
292
- switch (DTYPE(de)) {
293
- struct stat st;
294
- int subdir, rewind_base;
295
- default:
296
- continue;
297
- case DT_UNKNOWN:
298
- if (lstat(fullname, &st))
299
- continue;
300
- if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
301
- break;
302
- if (!S_ISDIR(st.st_mode))
303
- continue;
304
- /* fallthrough */
305
- case DT_DIR:
306
- memcpy(fullname + baselen + len, "/", 2);
307
- len++;
308
- rewind_base = nr_dir;
309
- subdir = read_directory(fullname, fullname,
310
- baselen + len);
311
- if (show_other_directories &&
312
- (subdir || !hide_empty_directories) &&
313
- !dir_exists(fullname, baselen + len)) {
314
- // Rewind the read subdirectory
315
- while (nr_dir > rewind_base)
316
- free(dir[--nr_dir]);
317
- break;
318
- }
319
- contents += subdir;
320
- continue;
321
- case DT_REG:
322
- case DT_LNK:
323
- break;
324
- }
325
- add_name(fullname, baselen + len);
326
- contents++;
327
- }
328
- closedir(fdir);
329
-
330
- pop_exclude_per_directory(exclude_stk);
331
- }
332
-
333
- return contents;
334
-}
335
-
336
-static int cmp_name(const void *p1, const void *p2)
337
-{
338
- const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
339
- const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;
340
-
341
- return cache_name_compare(e1->name, e1->len,
342
- e2->name, e2->len);
343
-}
38
39
/*
40
* Match a pathspec against a filename. The first "len" characters
@@ -377,7 +71,7 @@ static int match(const char **spec, char *ps_matched,
71
return 0;
72
}
73
380
-static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
74
+static void show_dir_entry(const char *tag, struct dir_entry *ent)
75
{
76
int len = prefix_len;
77
int offset = prefix_offset;
@@ -393,14 +87,14 @@ static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
87
putchar(line_terminator);
88
}
89
396
-static void show_other_files(void)
90
+static void show_other_files(struct dir_struct *dir)
91
{
92
int i;
399
- for (i = 0; i < nr_dir; i++) {
93
+ for (i = 0; i < dir->nr; i++) {
94
/* We should not have a matching entry, but we
95
* may have an unmerged entry for this path.
96
*/
403
- struct nond_on_fs *ent = dir[i];
97
+ struct dir_entry *ent = dir->entries[i];
98
int pos = cache_name_pos(ent->name, ent->len);
99
struct cache_entry *ce;
100
if (0 <= pos)
@@ -416,11 +110,11 @@ static void show_other_files(void)
110
}
111
}
112
419
-static void show_killed_files(void)
113
+static void show_killed_files(struct dir_struct *dir)
114
{
115
int i;
422
- for (i = 0; i < nr_dir; i++) {
423
- struct nond_on_fs *ent = dir[i];
116
+ for (i = 0; i < dir->nr; i++) {
117
+ struct dir_entry *ent = dir->entries[i];
118
char *cp, *sp;
119
int pos, len, killed = 0;
120
@@ -461,7 +155,7 @@ static void show_killed_files(void)
155
}
156
}
157
if (killed)
464
- show_dir_entry(tag_killed, dir[i]);
158
+ show_dir_entry(tag_killed, dir->entries[i]);
159
}
160
}
161
@@ -512,7 +206,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
206
}
207
}
208
515
-static void show_files(void)
209
+static void show_files(struct dir_struct *dir)
210
{
211
int i;
212
@@ -523,14 +217,14 @@ static void show_files(void)
217
218
if (baselen) {
219
path = base = prefix;
526
- if (exclude_per_dir) {
220
+ if (dir->exclude_per_dir) {
221
char *p, *pp = xmalloc(baselen+1);
222
memcpy(pp, prefix, baselen+1);
223
p = pp;
224
while (1) {
225
char save = *p;
226
*p = 0;
533
- push_exclude_per_directory(pp, p-pp);
227
+ push_exclude_per_directory(dir, pp, p-pp);
228
*p++ = save;
229
if (!save)
230
break;
@@ -543,17 +237,16 @@ static void show_files(void)
237
free(pp);
238
}
239
}
546
- read_directory(path, base, baselen);
547
- qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
240
+ read_directory(dir, path, base, baselen);
241
if (show_others)
549
- show_other_files();
242
+ show_other_files(dir);
243
if (show_killed)
551
- show_killed_files();
244
+ show_killed_files(dir);
245
}
246
if (show_cached | show_stage) {
247
for (i = 0; i < active_nr; i++) {
248
struct cache_entry *ce = active_cache[i];
556
- if (excluded(ce->name) != show_ignored)
249
+ if (excluded(dir, ce->name) != dir->show_ignored)
250
continue;
251
if (show_unmerged && !ce_stage(ce))
252
continue;
@@ -565,7 +258,7 @@ static void show_files(void)
258
struct cache_entry *ce = active_cache[i];
259
struct stat st;
260
int err;
568
- if (excluded(ce->name) != show_ignored)
261
+ if (excluded(dir, ce->name) != dir->show_ignored)
262
continue;
263
err = lstat(ce->name, &st);
264
if (show_deleted && err)
@@ -652,7 +345,9 @@ int main(int argc, const char **argv)
345
{
346
int i;
347
int exc_given = 0;
348
+ struct dir_struct dir;
349
350
+ memset(&dir, 0, sizeof(dir));
351
prefix = setup_git_directory();
352
if (prefix)
353
prefix_offset = strlen(prefix);
@@ -697,7 +392,7 @@ int main(int argc, const char **argv)
392
continue;
393
}
394
if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
700
- show_ignored = 1;
395
+ dir.show_ignored = 1;
396
continue;
397
}
398
if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
@@ -709,11 +404,11 @@ int main(int argc, const char **argv)
404
continue;
405
}
406
if (!strcmp(arg, "--directory")) {
712
- show_other_directories = 1;
407
+ dir.show_other_directories = 1;
408
continue;
409
}
410
if (!strcmp(arg, "--no-empty-directory")) {
716
- hide_empty_directories = 1;
411
+ dir.hide_empty_directories = 1;
412
continue;
413
}
414
if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
@@ -726,27 +421,27 @@ int main(int argc, const char **argv)
421
}
422
if (!strcmp(arg, "-x") && i+1 < argc) {
423
exc_given = 1;
729
- add_exclude(argv[++i], "", 0, &exclude_list[EXC_CMDL]);
424
+ add_exclude(argv[++i], "", 0, &dir.exclude_list[EXC_CMDL]);
425
continue;
426
}
427
if (!strncmp(arg, "--exclude=", 10)) {
428
exc_given = 1;
734
- add_exclude(arg+10, "", 0, &exclude_list[EXC_CMDL]);
429
+ add_exclude(arg+10, "", 0, &dir.exclude_list[EXC_CMDL]);
430
continue;
431
}
432
if (!strcmp(arg, "-X") && i+1 < argc) {
433
exc_given = 1;
739
- add_excludes_from_file(argv[++i]);
434
+ add_excludes_from_file(&dir, argv[++i]);
435
continue;
436
}
437
if (!strncmp(arg, "--exclude-from=", 15)) {
438
exc_given = 1;
744
- add_excludes_from_file(arg+15);
439
+ add_excludes_from_file(&dir, arg+15);
440
continue;
441
}
442
if (!strncmp(arg, "--exclude-per-directory=", 24)) {
443
exc_given = 1;
749
- exclude_per_dir = arg + 24;
444
+ dir.exclude_per_dir = arg + 24;
445
continue;
446
}
447
if (!strcmp(arg, "--full-name")) {
@@ -788,7 +483,7 @@ int main(int argc, const char **argv)
483
ps_matched = xcalloc(1, num);
484
}
485
791
- if (show_ignored && !exc_given) {
486
+ if (dir.show_ignored && !exc_given) {
487
fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
488
argv[0]);
489
exit(1);
@@ -802,7 +497,7 @@ int main(int argc, const char **argv)
497
read_cache();
498
if (prefix)
499
prune_cache();
805
- show_files();
500
+ show_files(&dir);
501
502
if (ps_matched) {
503
/* We need to make sure all pathspec matched otherwise