Skip to content

Commit f4a8fde

Browse files
committed
dir: match "attr" pathspec magic with correct paths
The match_pathspec_item() function takes "prefix" value, allowing a caller to chop off the common leading prefix of pathspec pattern strings from the path and only use the remainder of the path to match the pathspec patterns (after chopping the same leading prefix of them, of course). This "common leading prefix" optimization has two main features: * discard the entries in the in-core index that are outside of the common leading prefix; if you are doing "ls-files one/a one/b", we know all matches must be from "one/", so first the code discards all entries outside the "one/" directory from the in-core index. This allows us to work on a smaller dataset. * allow skipping the comparison of the leading bytes when matching pathspec with path. When "ls-files" finds the path "one/a/1" in the in-core index given "one/a" and "one/b" as the pathspec, knowing that common leading prefix "one/" was found lets the pathspec matchinery not to bother comparing "one/" part, and allows it to feed "a/1" down, as long as the pathspec element "one/a" gets corresponding adjustment to "a". When the "attr" pathspec magic is in effect, however, the current code breaks down. The attributes, other than the ones that are built-in and the ones that come from the $GIT_DIR/info/attributes file and the top-level .gitattributes file, are lazily read from the filesystem on-demand, as we encounter each path and ask if it matches the pathspec. For example, if you say "git ls-files "(attr:label)sub/" in a repository with a file "sub/file" that is given the 'label' attribute in "sub/.gitattributes": * The common prefix optimization finds that "sub/" is the common prefix and prunes the in-core index so that it has only entries inside that directory. This is desirable. * The code then walks the in-core index, finds "sub/file", and eventually asks do_match_pathspec() if it matches the given pathspec. * do_match_pathspec() calls match_pathspec_item() _after_ stripping the common prefix "sub/" from the path, giving it "file", plus the length of the common prefix (4-bytes), so that the pathspec element "(attr:label)sub/" can be treated as if it were "(attr:label)". The last one is what breaks the match in the current code, as the pathspec subsystem ends up asking the attribute subsystem to find the attribute attached to the path "file". We need to ask about the attributes on "sub/file" when calling match_pathspec_attrs(); this can be done by looking at "prefix" bytes before the beginning of "name", which is the same trick already used by another piece of the code in the same match_pathspec_item() function. Unfortunately this was not discovered so far because the code works with slightly different arguments, e.g. $ git ls-files "(attr:label)sub" $ git ls-files "(attr:label)sub/" "no/such/dir/" would have reported "sub/file" as a path with the 'label' attribute just fine, because neither would trigger the common prefix optimization. Reported-by: Matthew Hughes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e360bc commit f4a8fde

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static int match_pathspec_item(struct index_state *istate,
374374
return 0;
375375

376376
if (item->attr_match_nr &&
377-
!match_pathspec_attrs(istate, name, namelen, item))
377+
!match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
378378
return 0;
379379

380380
/* If the match was just the prefix, we matched */

t/t6135-pathspec-with-attrs.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ test_expect_success 'setup .gitattributes' '
6565
fileValue label=foo
6666
fileWrongLabel label☺
6767
EOF
68-
git add .gitattributes &&
68+
echo fileSetLabel label1 >sub/.gitattributes &&
69+
git add .gitattributes sub/.gitattributes &&
6970
git commit -m "add attributes"
7071
'
7172

@@ -157,6 +158,7 @@ test_expect_success 'check unspecified attr' '
157158
fileC
158159
fileNoLabel
159160
fileWrongLabel
161+
sub/.gitattributes
160162
sub/fileA
161163
sub/fileAB
162164
sub/fileAC
@@ -181,6 +183,7 @@ test_expect_success 'check unspecified attr (2)' '
181183
HEAD:fileC
182184
HEAD:fileNoLabel
183185
HEAD:fileWrongLabel
186+
HEAD:sub/.gitattributes
184187
HEAD:sub/fileA
185188
HEAD:sub/fileAB
186189
HEAD:sub/fileAC
@@ -200,6 +203,7 @@ test_expect_success 'check multiple unspecified attr' '
200203
fileC
201204
fileNoLabel
202205
fileWrongLabel
206+
sub/.gitattributes
203207
sub/fileC
204208
sub/fileNoLabel
205209
sub/fileWrongLabel
@@ -273,4 +277,22 @@ test_expect_success 'backslash cannot be used as a value' '
273277
test_i18ngrep "for value matching" actual
274278
'
275279

280+
test_expect_success 'reading from .gitattributes in a subdirectory (1)' '
281+
git ls-files ":(attr:label1)" >actual &&
282+
test_write_lines "sub/fileSetLabel" >expect &&
283+
test_cmp expect actual
284+
'
285+
286+
test_expect_success 'reading from .gitattributes in a subdirectory (2)' '
287+
git ls-files ":(attr:label1)sub" >actual &&
288+
test_write_lines "sub/fileSetLabel" >expect &&
289+
test_cmp expect actual
290+
'
291+
292+
test_expect_success 'reading from .gitattributes in a subdirectory (3)' '
293+
git ls-files ":(attr:label1)sub/" >actual &&
294+
test_write_lines "sub/fileSetLabel" >expect &&
295+
test_cmp expect actual
296+
'
297+
276298
test_done

0 commit comments

Comments
 (0)