Skip to content

Commit 86b0185

Browse files
committed
Backport branch 'show-ignored-directory'
This branch introduces an experimental option allowing `git status` to list all untracked files individually, but show ignored directories' names only instead of all ignored files. This topic branch was originally accepted into Git for Windows v2.14.0 and is hereby backported to v2.13.1. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents a9b0b6d + 0fdc42e commit 86b0185

File tree

8 files changed

+216
-8
lines changed

8 files changed

+216
-8
lines changed

Documentation/git-status.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ configuration variable documented in linkgit:git-config[1].
9797
--ignored::
9898
Show ignored files as well.
9999

100+
--show-ignored-directory::
101+
(EXPERIMENTAL) Show directories that are ignored, instead of individual
102+
files. Does not recurse into excluded directories when listing all
103+
untracked files.
104+
100105
-z::
101106
Terminate entries with NUL, instead of LF. This implies
102107
the `--porcelain=v1` output format if no other format is given.

Documentation/technical/api-directory-listing.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ The notable options are:
3333
Similar to `DIR_SHOW_IGNORED`, but return ignored files in `ignored[]`
3434
in addition to untracked files in `entries[]`.
3535

36+
`DIR_SHOW_IGNORED_DIRECTORY`:::
37+
38+
(EXPERIMENTAL) If this is set, non-empty directories that match an
39+
ignore pattern are returned. The individual files contained in
40+
ignored directories are not included.
41+
3642
`DIR_COLLECT_IGNORED`:::
3743

3844
Special mode for git-add. Return ignored files in `ignored[]` and

builtin/commit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
13311331
int cmd_status(int argc, const char **argv, const char *prefix)
13321332
{
13331333
static int no_lock_index = 0;
1334+
static int show_ignored_directory = 0;
13341335
static struct wt_status s;
13351336
int fd;
13361337
unsigned char sha1[20];
@@ -1360,6 +1361,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13601361
OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
13611362
OPT_BOOL(0, "no-lock-index", &no_lock_index,
13621363
N_("do not lock the index")),
1364+
OPT_BOOL(0, "show-ignored-directory", &show_ignored_directory,
1365+
N_("(EXPERIMENTAL) Only show directories that match an ignore pattern name.")),
13631366
OPT_END(),
13641367
};
13651368

@@ -1398,6 +1401,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13981401
s.ignore_submodule_arg = ignore_submodule_arg;
13991402
s.status_format = status_format;
14001403
s.verbose = verbose;
1404+
s.show_ignored_directory = show_ignored_directory;
14011405

14021406
wt_status_collect(&s);
14031407

dir.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct cached_dir {
4545

4646
static enum path_treatment read_directory_recursive(struct dir_struct *dir,
4747
const char *path, int len, struct untracked_cache_dir *untracked,
48-
int check_only, const struct pathspec *pathspec);
48+
int check_only, int stop_at_first_file,
49+
const struct pathspec *pathspec);
4950
static int get_dtype(struct dirent *de, const char *path, int len);
5051

5152
int fspathcmp(const char *a, const char *b)
@@ -1323,6 +1324,21 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
13231324
case index_nonexistent:
13241325
if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
13251326
break;
1327+
if (exclude &&
1328+
(dir->flags & DIR_SHOW_IGNORED_TOO) &&
1329+
(dir->flags & DIR_SHOW_IGNORED_DIRECTORY)) {
1330+
1331+
/*
1332+
* This is an excluded directory, and we are only
1333+
* showing the name of a excluded directory.
1334+
* Check to see if there are any contained files
1335+
* to determine if the directory is empty or not.
1336+
*/
1337+
if (read_directory_recursive(dir, dirname, len,
1338+
untracked, 1, 1, pathspec) == path_excluded)
1339+
return path_excluded;
1340+
return path_none;
1341+
}
13261342
if (!(dir->flags & DIR_NO_GITLINKS)) {
13271343
unsigned char sha1[20];
13281344
if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
@@ -1332,16 +1348,16 @@ static enum path_treatment treat_directory(struct dir_struct *dir,
13321348
}
13331349

13341350
/* This is the "show_other_directories" case */
1335-
13361351
if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
13371352
return exclude ? path_excluded : path_untracked;
13381353

13391354
untracked = lookup_untracked(dir->untracked, untracked,
13401355
dirname + baselen, len - baselen);
13411356
return read_directory_recursive(dir, dirname, len,
1342-
untracked, 1, pathspec);
1357+
untracked, 1, 0, pathspec);
13431358
}
13441359

1360+
13451361
/*
13461362
* This is an inexact early pruning of any recursive directory
13471363
* reading - if the path cannot possibly be in the pathspec,
@@ -1562,7 +1578,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,
15621578
* with check_only set.
15631579
*/
15641580
return read_directory_recursive(dir, path->buf, path->len,
1565-
cdir->ucd, 1, pathspec);
1581+
cdir->ucd, 1, 0, pathspec);
15661582
/*
15671583
* We get path_recurse in the first run when
15681584
* directory_exists_in_index() returns index_nonexistent. We
@@ -1723,7 +1739,8 @@ static void close_cached_dir(struct cached_dir *cdir)
17231739
*/
17241740
static enum path_treatment read_directory_recursive(struct dir_struct *dir,
17251741
const char *base, int baselen,
1726-
struct untracked_cache_dir *untracked, int check_only,
1742+
struct untracked_cache_dir *untracked,
1743+
int check_only, int stop_at_first_file,
17271744
const struct pathspec *pathspec)
17281745
{
17291746
struct cached_dir cdir;
@@ -1755,12 +1772,32 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
17551772
subdir_state =
17561773
read_directory_recursive(dir, path.buf,
17571774
path.len, ud,
1758-
check_only, pathspec);
1775+
check_only, stop_at_first_file, pathspec);
17591776
if (subdir_state > dir_state)
17601777
dir_state = subdir_state;
17611778
}
17621779

17631780
if (check_only) {
1781+
if (stop_at_first_file) {
1782+
/*
1783+
* In general, if we are stopping at the first found file,
1784+
* We can only signal that a path of at least "excluded" was
1785+
* found. If the first file we find is "excluded" - there might
1786+
* be other untracked files later on that will not be searched.
1787+
*
1788+
* In current usage of this function, stop_at_first_file will
1789+
* only be set when called from a directory that matches the
1790+
* exclude pattern - there should be no untracked files -
1791+
* all contents should be marked as excluded.
1792+
*/
1793+
if (dir_state == path_excluded)
1794+
break;
1795+
else if (dir_state > path_excluded) {
1796+
dir_state = path_excluded;
1797+
break;
1798+
}
1799+
}
1800+
17641801
/* abort early if maximum state has been reached */
17651802
if (dir_state == path_untracked) {
17661803
if (cdir.fdir)
@@ -2022,9 +2059,10 @@ int read_directory(struct dir_struct *dir, const char *path,
20222059
*/
20232060
dir->untracked = NULL;
20242061
if (!len || treat_leading_path(dir, path, len, pathspec))
2025-
read_directory_recursive(dir, path, len, untracked, 0, pathspec);
2062+
read_directory_recursive(dir, path, len, untracked, 0, 0, pathspec);
20262063
QSORT(dir->entries, dir->nr, cmp_name);
20272064
QSORT(dir->ignored, dir->ignored_nr, cmp_name);
2065+
20282066
if (dir->untracked) {
20292067
static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
20302068
trace_printf_key(&trace_untracked_stats,

dir.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ struct dir_struct {
151151
DIR_NO_GITLINKS = 1<<3,
152152
DIR_COLLECT_IGNORED = 1<<4,
153153
DIR_SHOW_IGNORED_TOO = 1<<5,
154-
DIR_COLLECT_KILLED_ONLY = 1<<6
154+
DIR_COLLECT_KILLED_ONLY = 1<<6,
155+
DIR_SHOW_IGNORED_DIRECTORY = 1<<8
155156
} flags;
156157
struct dir_entry **entries;
157158
struct dir_entry **ignored;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/bin/sh
2+
#
3+
#
4+
5+
test_description='git status collapse ignored'
6+
7+
. ./test-lib.sh
8+
9+
10+
cat >.gitignore <<\EOF
11+
*.ign
12+
ignored_dir/
13+
!*.unignore
14+
EOF
15+
16+
# commit initial ignore file
17+
test_expect_success 'setup initial commit and ignore file' '
18+
git add . &&
19+
test_tick &&
20+
git commit -m "Initial commit"
21+
'
22+
23+
cat >expect <<\EOF
24+
? expect
25+
? output
26+
! dir/ignored/ignored_1.ign
27+
! dir/ignored/ignored_2.ign
28+
! ignored/ignored_1.ign
29+
! ignored/ignored_2.ign
30+
EOF
31+
32+
# Test status behavior on folder with ignored files
33+
test_expect_success 'setup folder with ignored files' '
34+
mkdir -p ignored dir/ignored &&
35+
touch ignored/ignored_1.ign ignored/ignored_2.ign \
36+
dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign
37+
'
38+
39+
test_expect_success 'Verify behavior of status on folders with ignored files' '
40+
test_when_finished "git clean -fdx" &&
41+
git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output &&
42+
test_i18ncmp expect output
43+
'
44+
45+
# Test status bahavior on folder with tracked and ignored files
46+
cat >expect <<\EOF
47+
? expect
48+
? output
49+
! dir/tracked_ignored/ignored_1.ign
50+
! dir/tracked_ignored/ignored_2.ign
51+
! tracked_ignored/ignored_1.ign
52+
! tracked_ignored/ignored_2.ign
53+
EOF
54+
55+
test_expect_success 'setup folder with tracked & ignored files' '
56+
mkdir -p tracked_ignored dir/tracked_ignored &&
57+
touch tracked_ignored/tracked_1 tracked_ignored/tracked_2 \
58+
tracked_ignored/ignored_1.ign tracked_ignored/ignored_2.ign \
59+
dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 \
60+
dir/tracked_ignored/ignored_1.ign dir/tracked_ignored/ignored_2.ign &&
61+
62+
git add tracked_ignored/tracked_1 tracked_ignored/tracked_2 \
63+
dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 &&
64+
test_tick &&
65+
git commit -m "commit tracked files"
66+
'
67+
68+
test_expect_success 'Verify status on folder with tracked & ignored files' '
69+
test_when_finished "git clean -fdx && git reset HEAD~1 --hard" &&
70+
git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output &&
71+
test_i18ncmp expect output
72+
'
73+
74+
75+
# Test status behavior on folder with untracked and ignored files
76+
cat >expect <<\EOF
77+
? dir/untracked_ignored/untracked_1
78+
? dir/untracked_ignored/untracked_2
79+
? expect
80+
? output
81+
? untracked_ignored/untracked_1
82+
? untracked_ignored/untracked_2
83+
! dir/untracked_ignored/ignored_1.ign
84+
! dir/untracked_ignored/ignored_2.ign
85+
! untracked_ignored/ignored_1.ign
86+
! untracked_ignored/ignored_2.ign
87+
EOF
88+
89+
test_expect_success 'setup folder with tracked & ignored files' '
90+
mkdir -p untracked_ignored dir/untracked_ignored &&
91+
touch untracked_ignored/untracked_1 untracked_ignored/untracked_2 \
92+
untracked_ignored/ignored_1.ign untracked_ignored/ignored_2.ign \
93+
dir/untracked_ignored/untracked_1 dir/untracked_ignored/untracked_2 \
94+
dir/untracked_ignored/ignored_1.ign dir/untracked_ignored/ignored_2.ign
95+
'
96+
97+
test_expect_success 'Verify status on folder with tracked & ignored files' '
98+
test_when_finished "git clean -fdx" &&
99+
git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output &&
100+
test_i18ncmp expect output
101+
'
102+
103+
# Test status behavior on ignored folder
104+
cat >expect <<\EOF
105+
? expect
106+
? output
107+
! ignored_dir/
108+
EOF
109+
110+
test_expect_success 'setup folder with tracked & ignored files' '
111+
mkdir ignored_dir &&
112+
touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
113+
ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign
114+
'
115+
116+
test_expect_success 'Verify status on folder with tracked & ignored files' '
117+
test_when_finished "git clean -fdx" &&
118+
git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output &&
119+
test_i18ncmp expect output
120+
'
121+
122+
# Test status behavior on ignored folder with tracked file
123+
cat >expect <<\EOF
124+
? expect
125+
? output
126+
! ignored_dir/ignored_1
127+
! ignored_dir/ignored_1.ign
128+
! ignored_dir/ignored_2
129+
! ignored_dir/ignored_2.ign
130+
EOF
131+
132+
test_expect_success 'setup folder with tracked & ignored files' '
133+
mkdir ignored_dir &&
134+
touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
135+
ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign \
136+
ignored_dir/tracked &&
137+
git add -f ignored_dir/tracked &&
138+
test_tick &&
139+
git commit -m "Force add file in ignored directory"
140+
'
141+
142+
test_expect_success 'Verify status on folder with tracked & ignored files' '
143+
test_when_finished "git clean -fdx && git reset HEAD~1 --hard" &&
144+
git status --porcelain=v2 --ignored --untracked-files=all --show-ignored-directory >output &&
145+
test_i18ncmp expect output
146+
'
147+
148+
test_done
149+

wt-status.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ static void wt_status_collect_untracked(struct wt_status *s)
650650
dir.flags |= DIR_SHOW_IGNORED_TOO;
651651
else
652652
dir.untracked = the_index.untracked;
653+
654+
if (s->show_ignored_directory)
655+
dir.flags |= DIR_SHOW_IGNORED_DIRECTORY;
656+
653657
setup_standard_excludes(&dir);
654658

655659
fill_directory(&dir, &s->pathspec);

wt-status.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct wt_status {
7272
int submodule_summary;
7373
int show_ignored_files;
7474
enum untracked_status_type show_untracked_files;
75+
int show_ignored_directory;
7576
const char *ignore_submodule_arg;
7677
char color_palette[WT_STATUS_MAXSLOT][COLOR_MAXLEN];
7778
unsigned colopts;

0 commit comments

Comments
 (0)