Optionally do not list empty directories in git-ls-files --others
Without the --directory flag, git-ls-files wouldn't ever list directories, producing no output for empty directories, which is good since they cannot be added and they bear no content, even untracked one (if Git ever starts tracking directories on their own, this should obviously change since the content notion will change). With the --directory flag however, git-ls-files would list even empty directories. This may be good in some situations but sometimes you want to prevent that. This patch adds a --no-empty-directory option which makes git-ls-files omit empty directories. Signed-off-by: Petr Baudis <pasky@suse.cz>
Petr Baudis committed
Mar 26, 2006 at 16:59 UTC
b0a3de42316a4e8f1d561cbe12b7bb282631a0d6
2 files changed
+28
-8
Documentation/git-ls-files.txt
+3
@@ -52,6 +52,9 @@ OPTIONS
52
If a whole directory is classified as "other", show just its
53
name (with a trailing slash) and not its whole contents.
54
55
+--no-empty-directory::
56
+ Do not list empty directories. Has no effect without --directory.
57
+
58
-u|--unmerged::
59
Show unmerged files in the output (forces --stage)
60
ls-files.c
+25
-8
@@ -20,6 +20,7 @@ static int show_unmerged = 0;
20
static int show_modified = 0;
21
static int show_killed = 0;
22
static int show_other_directories = 0;
23
+static int hide_empty_directories = 0;
24
static int show_valid_bit = 0;
25
static int line_terminator = '\n';
26
@@ -258,11 +259,12 @@ static int dir_exists(const char *dirname, int len)
259
* Also, we ignore the name ".git" (even if it is not a directory).
260
* That likely will not change.
261
*/
261
-static void read_directory(const char *path, const char *base, int baselen)
262
+static int read_directory(const char *path, const char *base, int baselen)
263
{
263
- DIR *dir = opendir(path);
264
+ DIR *fdir = opendir(path);
265
+ int contents = 0;
266
265
- if (dir) {
267
+ if (fdir) {
268
int exclude_stk;
269
struct dirent *de;
270
char fullname[MAXPATHLEN + 1];
@@ -270,7 +272,7 @@ static void read_directory(const char *path, const char *base, int baselen)
272
273
exclude_stk = push_exclude_per_directory(base, baselen);
274
273
- while ((de = readdir(dir)) != NULL) {
275
+ while ((de = readdir(fdir)) != NULL) {
276
int len;
277
278
if ((de->d_name[0] == '.') &&
@@ -288,6 +290,7 @@ static void read_directory(const char *path, const char *base, int baselen)
290
291
switch (DTYPE(de)) {
292
struct stat st;
293
+ int subdir, rewind_base;
294
default:
295
continue;
296
case DT_UNKNOWN:
@@ -301,22 +304,32 @@ static void read_directory(const char *path, const char *base, int baselen)
304
case DT_DIR:
305
memcpy(fullname + baselen + len, "/", 2);
306
len++;
307
+ rewind_base = nr_dir;
308
+ subdir = read_directory(fullname, fullname,
309
+ baselen + len);
310
if (show_other_directories &&
305
- !dir_exists(fullname, baselen + len))
311
+ (subdir || !hide_empty_directories) &&
312
+ !dir_exists(fullname, baselen + len)) {
313
+ // Rewind the read subdirectory
314
+ while (nr_dir > rewind_base)
315
+ free(dir[--nr_dir]);
316
break;
307
- read_directory(fullname, fullname,
308
- baselen + len);
317
+ }
318
+ contents += subdir;
319
continue;
320
case DT_REG:
321
case DT_LNK:
322
break;
323
}
324
add_name(fullname, baselen + len);
325
+ contents++;
326
}
316
- closedir(dir);
327
+ closedir(fdir);
328
329
pop_exclude_per_directory(exclude_stk);
330
}
331
+
332
+ return contents;
333
}
334
335
static int cmp_name(const void *p1, const void *p2)
@@ -696,6 +709,10 @@ int main(int argc, const char **argv)
709
show_other_directories = 1;
710
continue;
711
}
712
+ if (!strcmp(arg, "--no-empty-directory")) {
713
+ hide_empty_directories = 1;
714
+ continue;
715
+ }
716
if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
717
/* There's no point in showing unmerged unless
718
* you also show the stage information.