pathspec: give better message for submodule related pathspec error

Every once in a while someone complains to the mailing list to have run into this weird assertion[1]. The usual response from the mailing list is link to old discussions[2], and acknowledging the problem stating it is known. This patch accomplishes two things: 1. Switch assert() to die("BUG") to give a more readable message. 2. Take one of the cases where we hit a BUG and turn it into a normal "there was something wrong with the input" message. This assertion triggered for cases where there wasn't a programming bug, but just bogus input. In particular, if the user asks for a pathspec that is inside a submodule, we shouldn't assert() or die("BUG"); we should tell the user their request is bogus. The only reason we did not check for it, is the expensive nature of such a check, so callers avoid setting the flag PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE. However when we die due to bogus input, the expense of CPU cycles spent outweighs the user wondering what went wrong, so run that check unconditionally before dying with a more generic error message. Note: There is a case (e.g. "git -C submodule add .") in which we call strip_submodule_slash_expensive, as git-add requests it via the flag PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE, but the assert used to trigger nevertheless, because the flag PATHSPEC_LITERAL was not set, such that we executed if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; and prefixlen was not adapted (e.g. it was computed from "submodule/") So in the die_inside_submodule_path function we also need handle paths, that were stripped before, i.e. are the exact submodule path. This is why the conditions in die_inside_submodule_path are slightly different than in strip_submodule_slash_expensive. [1] https://www.google.com/search?q=item-%3Enowildcard_len [2] http://git.661346.n2.nabble.com/assert-failed-in-submodule-edge-case-td7628687.html https://www.spinics.net/lists/git/msg249473.html Helped-by: Jeff King <peff@peff.net> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jan 9, 2017 at 15:16 UTC 2d81c48fa7f7679a92c9fe674b53656166ade4f8
2 files changed +69 -2
pathspec.c
+33 -2
@@ -296,6 +296,27 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item)
296 }
297 }
298
299 +static void die_inside_submodule_path(struct pathspec_item *item)
300 +{
301 + int i;
302 +
303 + for (i = 0; i < active_nr; i++) {
304 + struct cache_entry *ce = active_cache[i];
305 + int ce_len = ce_namelen(ce);
306 +
307 + if (!S_ISGITLINK(ce->ce_mode))
308 + continue;
309 +
310 + if (item->len < ce_len ||
311 + !(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
312 + memcmp(ce->name, item->match, ce_len))
313 + continue;
314 +
315 + die(_("Pathspec '%s' is in submodule '%.*s'"),
316 + item->original, ce_len, ce->name);
317 + }
318 +}
319 +
320 /*
321 * Perform the initialization of a pathspec_item based on a pathspec element.
322 */
@@ -391,8 +412,18 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
412 }
413
414 /* sanity checks, pathspec matchers assume these are sane */
394 - assert(item->nowildcard_len <= item->len &&
395 - item->prefix <= item->len);
415 + if (item->nowildcard_len > item->len ||
416 + item->prefix > item->len) {
417 + /*
418 + * This case can be triggered by the user pointing us to a
419 + * pathspec inside a submodule, which is an input error.
420 + * Detect that here and complain, but fallback in the
421 + * non-submodule case to a BUG, as we have no idea what
422 + * would trigger that.
423 + */
424 + die_inside_submodule_path(item);
425 + die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
426 + }
427 }
428
429 static int pathspec_item_cmp(const void *a_, const void *b_)
t/t6134-pathspec-in-submodule.sh new
+36
@@ -0,0 +1,36 @@
1 +#!/bin/sh
2 +
3 +test_description='test case exclude pathspec'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'setup a submodule' '
8 + test_create_repo pretzel &&
9 + : >pretzel/a &&
10 + git -C pretzel add a &&
11 + git -C pretzel commit -m "add a file" -- a &&
12 + git submodule add ./pretzel sub &&
13 + git commit -a -m "add submodule" &&
14 + git submodule deinit --all
15 +'
16 +
17 +cat <<EOF >expect
18 +fatal: Pathspec 'sub/a' is in submodule 'sub'
19 +EOF
20 +
21 +test_expect_success 'error message for path inside submodule' '
22 + echo a >sub/a &&
23 + test_must_fail git add sub/a 2>actual &&
24 + test_cmp expect actual
25 +'
26 +
27 +cat <<EOF >expect
28 +fatal: Pathspec '.' is in submodule 'sub'
29 +EOF
30 +
31 +test_expect_success 'error message for path inside submodule from within submodule' '
32 + test_must_fail git -C sub add . 2>actual &&
33 + test_cmp expect actual
34 +'
35 +
36 +test_done