Skip to content

Commit 2cfe66a

Browse files
jacob-kellergitster
authored andcommitted
ls-files: fix path used when recursing into submodules
Don't assume that the current working directory is the root of the repository. Correctly generate the path for the recursing child processes by building it from the work_tree() root instead. Otherwise if we run ls-files using --git-dir or --work-tree it will not work correctly as it attempts to change directory into a potentially invalid location. Best case, it doesn't exist and we produce an error. Worst case we cd into the wrong location and unknown behavior occurs. Add a new test which highlights this possibility. Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e5d650 commit 2cfe66a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

builtin/ls-files.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static void show_gitlink(const struct cache_entry *ce)
203203
{
204204
struct child_process cp = CHILD_PROCESS_INIT;
205205
int status;
206+
char *dir;
206207

207208
prepare_submodule_repo_env(&cp.env_array);
208209
argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
@@ -221,8 +222,10 @@ static void show_gitlink(const struct cache_entry *ce)
221222
argv_array_pushv(&cp.args, submodule_options.argv);
222223

223224
cp.git_cmd = 1;
224-
cp.dir = ce->name;
225+
dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
226+
cp.dir = dir;
225227
status = run_command(&cp);
228+
free(dir);
226229
if (status)
227230
exit(status);
228231
}

t/t3007-ls-files-recurse-submodules.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ test_expect_success 'ls-files recurses more than 1 level' '
8282
test_cmp expect actual
8383
'
8484

85+
test_expect_success 'ls-files works with GIT_DIR' '
86+
cat >expect <<-\EOF &&
87+
.gitmodules
88+
c
89+
subsub/d
90+
EOF
91+
92+
git --git-dir=submodule/.git ls-files --recurse-submodules >actual &&
93+
test_cmp expect actual
94+
'
95+
8596
test_expect_success '--recurse-submodules and pathspecs setup' '
8697
echo e >submodule/subsub/e.txt &&
8798
git -C submodule/subsub add e.txt &&

0 commit comments

Comments
 (0)