@matheusbsbs / matheus-bagliano / commits / 1d8842d921
Fork 0

Add 'fill_directory()' helper function for directory traversal

Most of the users of "read_directory()" actually want a much simpler interface than the whole complex (but rather powerful) one. In fact 'git add' had already largely abstracted out the core interface issues into a private "fill_directory()" function that was largely applicable almost as-is to a number of callers. Yes, 'git add' wants to do some extra work of its own, specific to the add semantics, but we can easily split that out, and use the core as a generic function. This function does exactly that, and now that much simplified 'fill_directory()' function can be shared with a number of callers, while also ensuring that the rather more complex calling conventions of read_directory() are used by fewer call-sites. This also makes the 'common_prefix()' helper function private to dir.c, since all callers are now in that file. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed May 14, 2009 at 13:22 UTC 1d8842d921cc2695f155f4a10904eeffad085c77
6 files changed +40 -52
builtin-add.c
+14 -31
@@ -97,35 +97,6 @@ static void treat_gitlinks(const char **pathspec)
97 }
98 }
99
100 -static void fill_directory(struct dir_struct *dir, const char **pathspec,
101 - int ignored_too)
102 -{
103 - const char *path, *base;
104 - int baselen;
105 -
106 - /* Set up the default git porcelain excludes */
107 - memset(dir, 0, sizeof(*dir));
108 - if (!ignored_too) {
109 - dir->flags |= DIR_COLLECT_IGNORED;
110 - setup_standard_excludes(dir);
111 - }
112 -
113 - /*
114 - * Calculate common prefix for the pathspec, and
115 - * use that to optimize the directory walk
116 - */
117 - baselen = common_prefix(pathspec);
118 - path = ".";
119 - base = "";
120 - if (baselen)
121 - path = base = xmemdupz(*pathspec, baselen);
122 -
123 - /* Read the directory and prune it */
124 - read_directory(dir, path, base, baselen, pathspec);
125 - if (pathspec)
126 - prune_directory(dir, pathspec, baselen);
127 -}
128 -
100 static void refresh(int verbose, const char **pathspec)
101 {
102 char *seen;
@@ -343,9 +314,21 @@ int cmd_add(int argc, const char **argv, const char *prefix)
314 die("index file corrupt");
315 treat_gitlinks(pathspec);
316
346 - if (add_new_files)
317 + if (add_new_files) {
318 + int baselen;
319 +
320 + /* Set up the default git porcelain excludes */
321 + memset(&dir, 0, sizeof(dir));
322 + if (!ignored_too) {
323 + dir.flags |= DIR_COLLECT_IGNORED;
324 + setup_standard_excludes(&dir);
325 + }
326 +
327 /* This picks up the paths that are not tracked */
348 - fill_directory(&dir, pathspec, ignored_too);
328 + baselen = fill_directory(&dir, pathspec);
329 + if (pathspec)
330 + prune_directory(&dir, pathspec, baselen);
331 + }
332
333 if (refresh_only) {
334 refresh(verbose, pathspec);
builtin-clean.c
+1 -11
@@ -33,7 +33,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
33 int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
34 struct strbuf directory = STRBUF_INIT;
35 struct dir_struct dir;
36 - const char *path, *base;
36 static const char **pathspec;
37 struct strbuf buf = STRBUF_INIT;
38 const char *qname;
@@ -78,16 +77,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
77 pathspec = get_pathspec(prefix, argv);
78 read_cache();
79
81 - /*
82 - * Calculate common prefix for the pathspec, and
83 - * use that to optimize the directory walk
84 - */
85 - baselen = common_prefix(pathspec);
86 - path = ".";
87 - base = "";
88 - if (baselen)
89 - path = base = xmemdupz(*pathspec, baselen);
90 - read_directory(&dir, path, base, baselen, pathspec);
80 + fill_directory(&dir, pathspec);
81
82 if (pathspec)
83 seen = xmalloc(argc > 0 ? argc : 1);
builtin-ls-files.c
+1 -6
@@ -161,12 +161,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
161
162 /* For cached/deleted files we don't need to even do the readdir */
163 if (show_others || show_killed) {
164 - const char *path = ".", *base = "";
165 - int baselen = prefix_len;
166 -
167 - if (baselen)
168 - path = base = prefix;
169 - read_directory(dir, path, base, baselen, pathspec);
164 + fill_directory(dir, pathspec);
165 if (show_others)
166 show_other_files(dir);
167 if (show_killed)
dir.c
+22 -1
@@ -19,7 +19,7 @@ static int read_directory_recursive(struct dir_struct *dir,
19 int check_only, const struct path_simplify *simplify);
20 static int get_dtype(struct dirent *de, const char *path);
21
22 -int common_prefix(const char **pathspec)
22 +static int common_prefix(const char **pathspec)
23 {
24 const char *path, *slash, *next;
25 int prefix;
@@ -52,6 +52,27 @@ int common_prefix(const char **pathspec)
52 return prefix;
53 }
54
55 +int fill_directory(struct dir_struct *dir, const char **pathspec)
56 +{
57 + const char *path, *base;
58 + int baselen;
59 +
60 + /*
61 + * Calculate common prefix for the pathspec, and
62 + * use that to optimize the directory walk
63 + */
64 + baselen = common_prefix(pathspec);
65 + path = "";
66 + base = "";
67 +
68 + if (baselen)
69 + path = base = xmemdupz(*pathspec, baselen);
70 +
71 + /* Read the directory and prune it */
72 + read_directory(dir, path, base, baselen, pathspec);
73 + return baselen;
74 +}
75 +
76 /*
77 * Does 'match' match the given name?
78 * A match is found if
dir.h
+1 -2
@@ -61,13 +61,12 @@ struct dir_struct {
61 char basebuf[PATH_MAX];
62 };
63
64 -extern int common_prefix(const char **pathspec);
65 -
64 #define MATCHED_RECURSIVELY 1
65 #define MATCHED_FNMATCH 2
66 #define MATCHED_EXACTLY 3
67 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
68
69 +extern int fill_directory(struct dir_struct *dir, const char **pathspec);
70 extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
71
72 extern int excluded(struct dir_struct *, const char *, int *);
wt-status.c
+1 -1
@@ -255,7 +255,7 @@ static void wt_status_print_untracked(struct wt_status *s)
255 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
256 setup_standard_excludes(&dir);
257
258 - read_directory(&dir, ".", "", 0, NULL);
258 + fill_directory(&dir, NULL);
259 for(i = 0; i < dir.nr; i++) {
260 struct dir_entry *ent = dir.entries[i];
261 if (!cache_name_is_other(ent->name, ent->len))