Skip to content

Commit 1d8842d

Browse files
torvaldsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f581de1 commit 1d8842d

File tree

6 files changed

+40
-52
lines changed

6 files changed

+40
-52
lines changed

builtin-add.c

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,35 +97,6 @@ static void treat_gitlinks(const char **pathspec)
9797
}
9898
}
9999

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-
129100
static void refresh(int verbose, const char **pathspec)
130101
{
131102
char *seen;
@@ -343,9 +314,21 @@ int cmd_add(int argc, const char **argv, const char *prefix)
343314
die("index file corrupt");
344315
treat_gitlinks(pathspec);
345316

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+
347327
/* 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+
}
349332

350333
if (refresh_only) {
351334
refresh(verbose, pathspec);

builtin-clean.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
3333
int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
3434
struct strbuf directory = STRBUF_INIT;
3535
struct dir_struct dir;
36-
const char *path, *base;
3736
static const char **pathspec;
3837
struct strbuf buf = STRBUF_INIT;
3938
const char *qname;
@@ -78,16 +77,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
7877
pathspec = get_pathspec(prefix, argv);
7978
read_cache();
8079

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);
9181

9282
if (pathspec)
9383
seen = xmalloc(argc > 0 ? argc : 1);

builtin-ls-files.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
161161

162162
/* For cached/deleted files we don't need to even do the readdir */
163163
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);
170165
if (show_others)
171166
show_other_files(dir);
172167
if (show_killed)

dir.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static int read_directory_recursive(struct dir_struct *dir,
1919
int check_only, const struct path_simplify *simplify);
2020
static int get_dtype(struct dirent *de, const char *path);
2121

22-
int common_prefix(const char **pathspec)
22+
static int common_prefix(const char **pathspec)
2323
{
2424
const char *path, *slash, *next;
2525
int prefix;
@@ -52,6 +52,27 @@ int common_prefix(const char **pathspec)
5252
return prefix;
5353
}
5454

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+
5576
/*
5677
* Does 'match' match the given name?
5778
* A match is found if

dir.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ struct dir_struct {
6161
char basebuf[PATH_MAX];
6262
};
6363

64-
extern int common_prefix(const char **pathspec);
65-
6664
#define MATCHED_RECURSIVELY 1
6765
#define MATCHED_FNMATCH 2
6866
#define MATCHED_EXACTLY 3
6967
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
7068

69+
extern int fill_directory(struct dir_struct *dir, const char **pathspec);
7170
extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
7271

7372
extern int excluded(struct dir_struct *, const char *, int *);

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static void wt_status_print_untracked(struct wt_status *s)
255255
DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
256256
setup_standard_excludes(&dir);
257257

258-
read_directory(&dir, ".", "", 0, NULL);
258+
fill_directory(&dir, NULL);
259259
for(i = 0; i < dir.nr; i++) {
260260
struct dir_entry *ent = dir.entries[i];
261261
if (!cache_name_is_other(ent->name, ent->len))

0 commit comments

Comments
 (0)