submodule helper list: respect correct path prefix

This is a regression introduced by 74703a1e4d (submodule: rewrite `module_list` shell function in C, 2015-09-02). Add a test to ensure we list the right submodule when giving a specific pathspec. Reported-By: Caleb Jorden <cjorden@gmail.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Feb 24, 2016 at 13:15 UTC 2b56bb7a87ab429c225f6325f2870cbeac0d19f9
2 files changed +27 -8
builtin/submodule--helper.c
+2 -8
@@ -22,17 +22,12 @@ static int module_list_compute(int argc, const char **argv,
22 struct module_list *list)
23 {
24 int i, result = 0;
25 - char *max_prefix, *ps_matched = NULL;
26 - int max_prefix_len;
25 + char *ps_matched = NULL;
26 parse_pathspec(pathspec, 0,
27 PATHSPEC_PREFER_FULL |
28 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
29 prefix, argv);
30
32 - /* Find common prefix for all pathspec's */
33 - max_prefix = common_prefix(pathspec);
34 - max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
35 -
31 if (pathspec->nr)
32 ps_matched = xcalloc(pathspec->nr, 1);
33
@@ -44,7 +39,7 @@ static int module_list_compute(int argc, const char **argv,
39
40 if (!S_ISGITLINK(ce->ce_mode) ||
41 !match_pathspec(pathspec, ce->name, ce_namelen(ce),
47 - max_prefix_len, ps_matched, 1))
42 + 0, ps_matched, 1))
43 continue;
44
45 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
@@ -57,7 +52,6 @@ static int module_list_compute(int argc, const char **argv,
52 */
53 i++;
54 }
60 - free(max_prefix);
55
56 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
57 result = -1;
t/t7400-submodule-basic.sh
+25
@@ -999,5 +999,30 @@ test_expect_success 'submodule add clone shallow submodule' '
999 )
1000 '
1001
1002 +test_expect_success 'submodule helper list is not confused by common prefixes' '
1003 + mkdir -p dir1/b &&
1004 + (
1005 + cd dir1/b &&
1006 + git init &&
1007 + echo hi >testfile2 &&
1008 + git add . &&
1009 + git commit -m "test1"
1010 + ) &&
1011 + mkdir -p dir2/b &&
1012 + (
1013 + cd dir2/b &&
1014 + git init &&
1015 + echo hello >testfile1 &&
1016 + git add . &&
1017 + git commit -m "test2"
1018 + ) &&
1019 + git submodule add /dir1/b dir1/b &&
1020 + git submodule add /dir2/b dir2/b &&
1021 + git commit -m "first submodule commit" &&
1022 + git submodule--helper list dir1/b |cut -c51- >actual &&
1023 + echo "dir1/b" >expect &&
1024 + test_cmp expect actual
1025 +'
1026 +
1027
1028 test_done