ls-files: support --recurse-submodules --stage

e77aa336f1 ("ls-files: optionally recurse into submodules", 2016-10-10) taught ls-files the --recurse-submodules argument, but only in a limited set of circumstances. In particular, --stage was unsupported, perhaps because there was no repo_find_unique_abbrev(), which was only introduced in 8bb95572b0 ("sha1-name.c: add repo_find_unique_abbrev_r()", 2019-04-16). This function is needed for using --recurse-submodules with --stage. Now that we have repo_find_unique_abbrev(), teach support for this combination of arguments. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Feb 23, 2022 at 16:23 UTC 290eada0ac9d374501fa24268c2e1ec8b8924358
3 files changed +20 -4
Documentation/git-ls-files.txt
+1 -1
@@ -156,7 +156,7 @@ a space) at the start of each line:
156
157 --recurse-submodules::
158 Recursively calls ls-files on each active submodule in the repository.
159 - Currently there is only support for the --cached mode.
159 + Currently there is only support for the --cached and --stage modes.
160
161 --abbrev[=<n>]::
162 Instead of showing the full 40-byte hexadecimal object
builtin/ls-files.c
+2 -2
@@ -244,7 +244,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
244 printf("%s%06o %s %d\t",
245 tag,
246 ce->ce_mode,
247 - find_unique_abbrev(&ce->oid, abbrev),
247 + repo_find_unique_abbrev(repo, &ce->oid, abbrev),
248 ce_stage(ce));
249 }
250 write_eolinfo(repo->index, ce, fullname);
@@ -726,7 +726,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
726 setup_work_tree();
727
728 if (recurse_submodules &&
729 - (show_stage || show_deleted || show_others || show_unmerged ||
729 + (show_deleted || show_others || show_unmerged ||
730 show_killed || show_modified || show_resolve_undo || with_tree))
731 die("ls-files --recurse-submodules unsupported mode");
732
t/t3007-ls-files-recurse-submodules.sh
+17 -1
@@ -34,6 +34,23 @@ test_expect_success 'ls-files correctly outputs files in submodule' '
34 test_cmp expect actual
35 '
36
37 +test_expect_success '--stage' '
38 + GITMODULES_HASH=$(git rev-parse HEAD:.gitmodules) &&
39 + A_HASH=$(git rev-parse HEAD:a) &&
40 + B_HASH=$(git rev-parse HEAD:b/b) &&
41 + C_HASH=$(git -C submodule rev-parse HEAD:c) &&
42 +
43 + cat >expect <<-EOF &&
44 + 100644 $GITMODULES_HASH 0 .gitmodules
45 + 100644 $A_HASH 0 a
46 + 100644 $B_HASH 0 b/b
47 + 100644 $C_HASH 0 submodule/c
48 + EOF
49 +
50 + git ls-files --stage --recurse-submodules >actual &&
51 + test_cmp expect actual
52 +'
53 +
54 test_expect_success 'ls-files correctly outputs files in submodule with -z' '
55 lf_to_nul >expect <<-\EOF &&
56 .gitmodules
@@ -292,7 +309,6 @@ test_incompatible_with_recurse_submodules () {
309 test_incompatible_with_recurse_submodules --deleted
310 test_incompatible_with_recurse_submodules --modified
311 test_incompatible_with_recurse_submodules --others
295 -test_incompatible_with_recurse_submodules --stage
312 test_incompatible_with_recurse_submodules --killed
313 test_incompatible_with_recurse_submodules --unmerged
314