git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3 v3] ls-files: optionally recurse into submodules

From
BWBrandon Williams <bmwill@google.com>
Date
Sep 24, 2016, 00:13 UTC
Message-ID
<1474676014-134568-3-git-send-email-bmwill@google.com>
In-Reply-To
<1474676014-134568-1-git-send-email-bmwill@google.com>

Allow ls-files to recognize submodules in order to retrieve a list of files from a repository's submodules. This is done by forking off a process to recursively call ls-files on all submodules. Use environment variable `GIT_INTERNAL_SUBMODULE_PREFIX` to pass a path to the submodule which it can use to prepend to output or pathspec matching logic.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 Documentation/git-ls-files.txt         |  7 ++-
 builtin/ls-files.c                     | 63 ++++++++++++++++++++++
 t/t3007-ls-files-recurse-submodules.sh | 99 ++++++++++++++++++++++++++++++++++
 3 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100755 t/t3007-ls-files-recurse-submodules.sh
diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index 0d933ac..446209e 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -18,7 +18,8 @@ SYNOPSIS
 		[--exclude-per-directory=<file>]
 		[--exclude-standard]
 		[--error-unmatch] [--with-tree=<tree-ish>]
-		[--full-name] [--abbrev] [--] [<file>...]
+		[--full-name] [--recurse-submodules]
+		[--abbrev] [--] [<file>...]
 
 DESCRIPTION
 -----------
@@ -137,6 +138,10 @@ a space) at the start of each line:
 	option forces paths to be output relative to the project
 	top directory.
 
+--recurse-submodules::
+	Recursively calls ls-files on each submodule in the repository.
+	Currently there is only support for the --cached mode.
+
 --abbrev[=<n>]::
 	Instead of showing the full 40-byte hexadecimal object
 	lines, show only a partial prefix.
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 00ea91a..54ab765 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -14,6 +14,7 @@
 #include "resolve-undo.h"
 #include "string-list.h"
 #include "pathspec.h"
+#include "run-command.h"
 
 static int abbrev;
 static int show_deleted;
@@ -28,6 +29,8 @@ static int show_valid_bit;
 static int line_terminator = '\n';
 static int debug_mode;
 static int show_eol;
+static int recurse_submodules;
+static const char *submodule_prefix;
 
 static const char *prefix;
 static int max_prefix_len;
@@ -68,6 +71,21 @@ static void write_eolinfo(const struct cache_entry *ce, const char *path)
 static void write_name(const char *name)
 {
 	/*
+	 * NEEDSWORK: To make this thread-safe, full_name would have to be owned
+	 * by the caller.
+	 *
+	 * full_name get reused across output lines to minimize the allocation
+	 * churn.
+	 */
+	static struct strbuf full_name = STRBUF_INIT;
+	if (submodule_prefix && *submodule_prefix) {
+		strbuf_reset(&full_name);
+		strbuf_addstr(&full_name, submodule_prefix);
+		strbuf_addstr(&full_name, name);
+		name = full_name.buf;
+	}
+
+	/*
 	 * With "--full-name", prefix_len=0; this caller needs to pass
 	 * an empty string in that case (a NULL is good for "").
 	 */
@@ -152,6 +170,27 @@ static void show_killed_files(struct dir_struct *dir)
 	}
 }
 
+/**
+ * Recursively call ls-files on a submodule
+ */
+static void show_gitlink(const struct cache_entry *ce)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	int status;
+
+	argv_array_push(&cp.args, "ls-files");
+	argv_array_push(&cp.args, "--recurse-submodules");
+	argv_array_pushf(&cp.env_array, "%s=%s%s/",
+			 GIT_SUBMODULE_PREFIX_ENVIRONMENT,
+			 submodule_prefix ? submodule_prefix : "",
+			 ce->name);
+	cp.git_cmd = 1;
+	cp.dir = ce->name;
+	status = run_command(&cp);
+	if (status)
+		exit(status);
+}
+
 static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 {
 	int len = max_prefix_len;
@@ -163,6 +202,10 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 			    len, ps_matched,
 			    S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode)))
 		return;
+	if (recurse_submodules && S_ISGITLINK(ce->ce_mode)) {
+		show_gitlink(ce);
+		return;
+	}
 
 	if (tag && *tag && show_valid_bit &&
 	    (ce->ce_flags & CE_VALID)) {
@@ -468,6 +511,8 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		{ OPTION_SET_INT, 0, "full-name", &prefix_len, NULL,
 			N_("make the output relative to the project top directory"),
 			PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL },
+		OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
+			N_("recurse through submodules")),
 		OPT_BOOL(0, "error-unmatch", &error_unmatch,
 			N_("if any <file> is not in the index, treat this as an error")),
 		OPT_STRING(0, "with-tree", &with_tree, N_("tree-ish"),
@@ -519,6 +564,24 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	if (require_work_tree && !is_inside_work_tree())
 		setup_work_tree();
 
+	if (recurse_submodules)
+		submodule_prefix = getenv(GIT_SUBMODULE_PREFIX_ENVIRONMENT);
+
+	if (recurse_submodules &&
+	    (show_stage || show_deleted || show_others || show_unmerged ||
+	     show_killed || show_modified || show_resolve_undo ||
+	     show_valid_bit || show_tag || show_eol))
+		die("ls-files --recurse-submodules can only be used in "
+		    "--cached mode");
+
+	if (recurse_submodules && error_unmatch)
+		die("ls-files --recurse-submodules does not support "
+		    "--error-unmatch");
+
+	if (recurse_submodules && argc)
+		die("ls-files --recurse-submodules does not support path "
+		    "arguments");
+
 	parse_pathspec(&pathspec, 0,
 		       PATHSPEC_PREFER_CWD |
 		       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
diff --git a/t/t3007-ls-files-recurse-submodules.sh b/t/t3007-ls-files-recurse-submodules.sh
new file mode 100755
index 0000000..caf3815
--- /dev/null
+++ b/t/t3007-ls-files-recurse-submodules.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='Test ls-files recurse-submodules feature
+
+This test verifies the recurse-submodules feature correctly lists files from
+submodules.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup directory structure and submodules' '
+	echo a >a &&
+	mkdir b &&
+	echo b >b/b &&
+	git add a b &&
+	git commit -m "add a and b" &&
+	git init submodule &&
+	echo c >submodule/c &&
+	git -C submodule add c &&
+	git -C submodule commit -m "add c" &&
+	git submodule add ./submodule &&
+	git commit -m "added submodule"
+'
+
+test_expect_success 'ls-files correctly outputs files in submodule' '
+	cat >expect <<-\EOF &&
+	.gitmodules
+	a
+	b/b
+	submodule/c
+	EOF
+
+	git ls-files --recurse-submodules >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-files does not output files not added to a repo' '
+	cat >expect <<-\EOF &&
+	.gitmodules
+	a
+	b/b
+	submodule/c
+	EOF
+
+	echo a >not_added &&
+	echo b >b/not_added &&
+	echo c >submodule/not_added &&
+	git ls-files --recurse-submodules >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'ls-files recurses more than 1 level' '
+	cat >expect <<-\EOF &&
+	.gitmodules
+	a
+	b/b
+	submodule/.gitmodules
+	submodule/c
+	submodule/subsub/d
+	EOF
+
+	git init submodule/subsub &&
+	echo d >submodule/subsub/d &&
+	git -C submodule/subsub add d &&
+	git -C submodule/subsub commit -m "add d" &&
+	git -C submodule submodule add ./subsub &&
+	git -C submodule commit -m "added subsub" &&
+	git ls-files --recurse-submodules >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules does not support using path arguments' '
+	test_must_fail git ls-files --recurse-submodules b 2>actual &&
+	test_i18ngrep "does not support path arguments" actual
+'
+
+test_expect_success '--recurse-submodules does not support --error-unmatch' '
+	test_must_fail git ls-files --recurse-submodules --error-unmatch 2>actual &&
+	test_i18ngrep "does not support --error-unmatch" actual
+'
+
+test_incompatible_with_recurse_submodules () {
+	test_expect_success "--recurse-submodules and $1 are incompatible" "
+		test_must_fail git ls-files --recurse-submodules $1 2>actual &&
+		test_i18ngrep 'can only be used in --cached mode' actual
+	"
+}
+
+test_incompatible_with_recurse_submodules -v
+test_incompatible_with_recurse_submodules -t
+test_incompatible_with_recurse_submodules --deleted
+test_incompatible_with_recurse_submodules --modified
+test_incompatible_with_recurse_submodules --others
+test_incompatible_with_recurse_submodules --stage
+test_incompatible_with_recurse_submodules --killed
+test_incompatible_with_recurse_submodules --unmerged
+test_incompatible_with_recurse_submodules --eol
+
+test_done
-- 
2.8.0.rc3.226.g39d4020
Previous: Junio C HamanoNext: Brandon Williams
Message 4 of 67 in “recursive support for ls-files”
  1. 0/3 recursive support for ls-filesBrandon Williams, Sep 24, 2016
  2. 1/3 submodules: make submodule-prefix option an envvarBrandon Williams, Sep 24, 2016
  3. Junio C HamanoSep 25, 2016
  4. 2/3 ls-files: optionally recurse into submodulesBrandon Williams, Sep 24, 2016
  5. 3/3 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 24, 2016
  6. Jeff KingSep 25, 2016
  7. Brandon WilliamsSep 25, 2016
  8. Junio C HamanoSep 25, 2016
  9. Brandon WilliamsSep 26, 2016
  10. Junio C HamanoSep 26, 2016
  11. Brandon WilliamsSep 26, 2016
  12. Junio C HamanoSep 26, 2016
  13. 0/4 recursive support for ls-filesBrandon Williams, Sep 26, 2016
  14. 1/4 submodules: make submodule-prefix optionBrandon Williams, Sep 26, 2016
  15. Junio C HamanoSep 27, 2016
  16. Brandon WilliamsSep 27, 2016
  17. Junio C HamanoSep 27, 2016
  18. Brandon WilliamsSep 27, 2016
  19. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 26, 2016
  20. Junio C HamanoSep 27, 2016
  21. Brandon WilliamsSep 27, 2016
  22. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 26, 2016
  23. Junio C HamanoSep 27, 2016
  24. Brandon WilliamsSep 27, 2016
  25. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 26, 2016
  26. Junio C HamanoSep 27, 2016
  27. Junio C HamanoSep 27, 2016
  28. Brandon WilliamsSep 27, 2016
  29. Junio C HamanoSep 27, 2016
  30. Stefan BellerSep 27, 2016
  31. Brandon WilliamsSep 28, 2016
  32. Junio C HamanoSep 28, 2016
  33. Brandon WilliamsSep 27, 2016
  34. Junio C HamanoSep 27, 2016
  35. Brandon WilliamsSep 27, 2016
  36. Junio C HamanoSep 27, 2016
  37. 0/4 recursive support for ls-filesBrandon Williams, Sep 28, 2016
  38. 1/4 git: make super-prefix optionBrandon Williams, Sep 28, 2016
  39. Stefan BellerSep 28, 2016
  40. Junio C HamanoSep 28, 2016
  41. Jeff KingSep 29, 2016
  42. Brandon WilliamsSep 29, 2016
  43. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 28, 2016
  44. Stefan BellerSep 28, 2016
  45. Junio C HamanoSep 28, 2016
  46. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 28, 2016
  47. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 28, 2016
  48. 0/4 recursive support for ls-filesBrandon Williams, Sep 29, 2016
  49. 1/4 git: make super-prefix optionBrandon Williams, Sep 29, 2016
  50. Stefan BellerOct 4, 2016
  51. Junio C HamanoOct 4, 2016
  52. Jeff KingOct 4, 2016
  53. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Sep 29, 2016
  54. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Sep 29, 2016
  55. Junio C HamanoSep 30, 2016
  56. Brandon WilliamsSep 30, 2016
  57. Brandon WilliamsSep 30, 2016
  58. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Sep 29, 2016
  59. Stefan BellerOct 4, 2016
  60. 0/4 recursive support for ls-filesBrandon Williams, Oct 7, 2016
  61. 1/4 git: make super-prefix optionBrandon Williams, Oct 7, 2016
  62. 2/4 ls-files: optionally recurse into submodulesBrandon Williams, Oct 7, 2016
  63. 3/4 ls-files: pass through safe options for --recurse-submodulesBrandon Williams, Oct 7, 2016
  64. 4/4 ls-files: add pathspec matching for submodulesBrandon Williams, Oct 7, 2016
  65. Stefan BellerOct 7, 2016
  66. Stefan BellerOct 7, 2016
  67. Brandon WilliamsOct 7, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.