summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2012-05-25 12:07:52 -0700
committerLibravatar Junio C Hamano <gitster@pobox.com>2012-05-25 12:07:52 -0700
commit3501b89fb55d9314922675fd2861cf3792db42bf (patch)
treeb8c73797536cf8d5ec0a095fe7a96ac1a4a6c7bf /dir.c
parentMerge branch 'fc/git-complete-helper-fix' (diff)
parentdir: simplify fill_directory() (diff)
downloadtgif-3501b89fb55d9314922675fd2861cf3792db42bf.tar.xz
Merge branch 'rs/dir-strbuf-read-recursive-fix'
Simplification for the codepath to read directories recursively. By René Scharfe * rs/dir-strbuf-read-recursive-fix: dir: simplify fill_directory() dir: respect string length argument of read_directory_recursive()
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c25
1 files changed, 9 insertions, 16 deletions
diff --git a/dir.c b/dir.c
index c6a98cc8dd..ed1510fbc8 100644
--- a/dir.c
+++ b/dir.c
@@ -74,7 +74,6 @@ char *common_prefix(const char **pathspec)
int fill_directory(struct dir_struct *dir, const char **pathspec)
{
- const char *path;
size_t len;
/*
@@ -82,15 +81,9 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)
* use that to optimize the directory walk
*/
len = common_prefix_len(pathspec);
- path = "";
-
- if (len)
- path = xmemdupz(*pathspec, len);
/* Read the directory and prune it */
- read_directory(dir, path, len, pathspec);
- if (*path)
- free((char *)path);
+ read_directory(dir, pathspec ? *pathspec : "", len, pathspec);
return len;
}
@@ -960,16 +953,17 @@ static int read_directory_recursive(struct dir_struct *dir,
int check_only,
const struct path_simplify *simplify)
{
- DIR *fdir = opendir(*base ? base : ".");
+ DIR *fdir;
int contents = 0;
struct dirent *de;
struct strbuf path = STRBUF_INIT;
- if (!fdir)
- return 0;
-
strbuf_add(&path, base, baselen);
+ fdir = opendir(path.len ? path.buf : ".");
+ if (!fdir)
+ goto out;
+
while ((de = readdir(fdir)) != NULL) {
switch (treat_path(dir, de, &path, baselen, simplify)) {
case path_recurse:
@@ -984,12 +978,11 @@ static int read_directory_recursive(struct dir_struct *dir,
}
contents++;
if (check_only)
- goto exit_early;
- else
- dir_add_name(dir, path.buf, path.len);
+ break;
+ dir_add_name(dir, path.buf, path.len);
}
-exit_early:
closedir(fdir);
+ out:
strbuf_release(&path);
return contents;