Skip to content

Commit 2a43e0e

Browse files
To1negitster
authored andcommitted
within_depth: fix return for empty path
The within_depth() function is used to check whether pathspecs limited by a max-depth parameter are acceptable. It takes a path to check, a maximum depth, and a "base" depth. It counts the components in the path (by counting slashes), adds them to the base, and compares them to the maximum. However, if the base does not have any slashes at all, we always return `true`. If the base depth is 0, then this is correct; no matter what the maximum is, we are always within it. However, if the base depth is greater than 0, then we might return an erroneous result. This ends up not causing any user-visible bugs in the current code. The call sites in dir.c always pass a base depth of 0, so are unaffected. But tree_entry_interesting() uses this function differently: it will pass the prefix of the current entry, along with a `1` if the entry is a directory, in essence checking whether items inside the entry would be of interest. It turns out not to make a difference in behavior, but the reasoning is complex. Given a tree like: file a/file a/b/file walking the tree and calling tree_entry_interesting() will yield the following results: (with max_depth=0): file: yes a: yes a/file: no a/b: no (with max_depth=1): file: yes a: yes a/file: yes a/b: no So we have inconsistent behavior in considering directories interesting. If they are at the edge of our depth but at the root, we will recurse into them, but then find all of their entries uninteresting (e.g., in the first case, we will look at "a" but find "a/*" uninteresting). But if they are at the edge of our depth and not at the root, then we will not recurse (in the second example, we do not even bother entering "a/b"). This turns out not to matter because the only caller which uses max-depth pathspecs is cmd_grep(), which only cares about blob entries. From its perspective, it is exactly the same to not recurse into a subtree, or to recurse and find that it contains no matching entries. Not recursing is merely an optimization. It is debatable whether tree_entry_interesting() should consider such an entry interesting. The only caller does not care if it sees the tree itself, and can benefit from the optimization. But if we add a "max-depth" limiter to regular diffs, then a diff with DIFF_OPT_TREE_IN_RECURSIVE would probably want to show the tree itself, but not what it contains. This patch just fixes within_depth(), which means we consider such entries uninteresting (and makes the current caller happy). If we want to change that in the future, then this fix is still the correct first step, as the current behavior is simply inconsistent. This has the effect the function tree_entry_interesting() now behaves like following on the first example: (with max_depth=0): file: yes a: no a/file: no a/b: no Meaning we won't step in "a/" no more to realize all "a/*" entries are uninterested, but we stop at the tree entry itself. Based-on-patch-by: Jeff King <[email protected]> Signed-off-by: Toon Claes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bb4abe commit 2a43e0e

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
13561356
THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
13571357

13581358
CLAR_TEST_SUITES += u-ctype
1359+
CLAR_TEST_SUITES += u-dir
13591360
CLAR_TEST_SUITES += u-example-decorate
13601361
CLAR_TEST_SUITES += u-hash
13611362
CLAR_TEST_SUITES += u-hashmap

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ int within_depth(const char *name, int namelen,
277277
if (depth > max_depth)
278278
return 0;
279279
}
280-
return 1;
280+
return depth <= max_depth;
281281
}
282282

283283
/*

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
clar_test_suites = [
22
'unit-tests/u-ctype.c',
3+
'unit-tests/u-dir.c',
34
'unit-tests/u-example-decorate.c',
45
'unit-tests/u-hash.c',
56
'unit-tests/u-hashmap.c',

t/unit-tests/u-dir.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "unit-test.h"
2+
#include "dir.h"
3+
4+
#define TEST_WITHIN_DEPTH(path, depth, max_depth, expect) do { \
5+
int actual = within_depth(path, strlen(path), \
6+
depth, max_depth); \
7+
if (actual != expect) \
8+
cl_failf("path '%s' with depth '%d' and max-depth '%d': expected %d, got %d", \
9+
path, depth, max_depth, expect, actual); \
10+
} while (0)
11+
12+
void test_dir__within_depth(void)
13+
{
14+
/* depth = 0; max_depth = 0 */
15+
TEST_WITHIN_DEPTH("", 0, 0, 1);
16+
TEST_WITHIN_DEPTH("file", 0, 0, 1);
17+
TEST_WITHIN_DEPTH("a", 0, 0, 1);
18+
TEST_WITHIN_DEPTH("a/file", 0, 0, 0);
19+
TEST_WITHIN_DEPTH("a/b", 0, 0, 0);
20+
TEST_WITHIN_DEPTH("a/b/file", 0, 0, 0);
21+
22+
/* depth = 0; max_depth = 1 */
23+
TEST_WITHIN_DEPTH("", 0, 1, 1);
24+
TEST_WITHIN_DEPTH("file", 0, 1, 1);
25+
TEST_WITHIN_DEPTH("a", 0, 1, 1);
26+
TEST_WITHIN_DEPTH("a/file", 0, 1, 1);
27+
TEST_WITHIN_DEPTH("a/b", 0, 1, 1);
28+
TEST_WITHIN_DEPTH("a/b/file", 0, 1, 0);
29+
30+
/* depth = 1; max_depth = 1 */
31+
TEST_WITHIN_DEPTH("", 1, 1, 1);
32+
TEST_WITHIN_DEPTH("file", 1, 1, 1);
33+
TEST_WITHIN_DEPTH("a", 1, 1, 1);
34+
TEST_WITHIN_DEPTH("a/file", 1, 1, 0);
35+
TEST_WITHIN_DEPTH("a/b", 1, 1, 0);
36+
TEST_WITHIN_DEPTH("a/b/file", 1, 1, 0);
37+
38+
/* depth = 1; max_depth = 0 */
39+
TEST_WITHIN_DEPTH("", 1, 0, 0);
40+
TEST_WITHIN_DEPTH("file", 1, 0, 0);
41+
TEST_WITHIN_DEPTH("a", 1, 0, 0);
42+
TEST_WITHIN_DEPTH("a/file", 1, 0, 0);
43+
TEST_WITHIN_DEPTH("a/b", 1, 0, 0);
44+
TEST_WITHIN_DEPTH("a/b/file", 1, 0, 0);
45+
46+
47+
}

0 commit comments

Comments
 (0)