Merge branch 'sb/pathspec-errors'

Running "git add a/b" when "a" is a submodule correctly errored
out, but without a meaningful error message.

* sb/pathspec-errors:
  pathspec: give better message for submodule related pathspec error
This commit is contained in:
Junio C Hamano 2017-01-18 15:12:15 -08:00
commit 00880a17dd
2 changed files with 69 additions and 2 deletions

View File

@ -296,6 +296,27 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item)
}
}
static void die_inside_submodule_path(struct pathspec_item *item)
{
int i;
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
int ce_len = ce_namelen(ce);
if (!S_ISGITLINK(ce->ce_mode))
continue;
if (item->len < ce_len ||
!(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
memcmp(ce->name, item->match, ce_len))
continue;
die(_("Pathspec '%s' is in submodule '%.*s'"),
item->original, ce_len, ce->name);
}
}
/*
* Perform the initialization of a pathspec_item based on a pathspec element.
*/
@ -391,8 +412,18 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
}
/* sanity checks, pathspec matchers assume these are sane */
assert(item->nowildcard_len <= item->len &&
item->prefix <= item->len);
if (item->nowildcard_len > item->len ||
item->prefix > item->len) {
/*
* This case can be triggered by the user pointing us to a
* pathspec inside a submodule, which is an input error.
* Detect that here and complain, but fallback in the
* non-submodule case to a BUG, as we have no idea what
* would trigger that.
*/
die_inside_submodule_path(item);
die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
}
}
static int pathspec_item_cmp(const void *a_, const void *b_)

View File

@ -0,0 +1,36 @@
#!/bin/sh
test_description='test case exclude pathspec'
. ./test-lib.sh
test_expect_success 'setup a submodule' '
test_create_repo pretzel &&
: >pretzel/a &&
git -C pretzel add a &&
git -C pretzel commit -m "add a file" -- a &&
git submodule add ./pretzel sub &&
git commit -a -m "add submodule" &&
git submodule deinit --all
'
cat <<EOF >expect
fatal: Pathspec 'sub/a' is in submodule 'sub'
EOF
test_expect_success 'error message for path inside submodule' '
echo a >sub/a &&
test_must_fail git add sub/a 2>actual &&
test_cmp expect actual
'
cat <<EOF >expect
fatal: Pathspec '.' is in submodule 'sub'
EOF
test_expect_success 'error message for path inside submodule from within submodule' '
test_must_fail git -C sub add . 2>actual &&
test_cmp expect actual
'
test_done