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

[PATCH v2 2/3] submodule--helper.c: add populated to foreach

From
GGGuillaume Galeazzi via GitGitGadget <gitgitgadget@gmail.com>
Date
May 17, 2020, 06:30 UTC
Message-ID
<f9cbdcdeacff3c86a4bce6b43ef105c7aab2fcd3.1589697023.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.631.v2.git.1589697023.gitgitgadget@gmail.com>
From: Guillaume Galeazzi <guillaume.galeazzi@gmail.com>

On a repository with some submodules not initialised, one may need to run a command only for not initilaised submodule or vice-versa. This change, add the flag `--[no-]populated` to subcommand `foreach` of `git submodule--helper`.

Signed-off-by: Guillaume Galeazzi <guillaume.galeazzi@gmail.com>
---
 builtin/submodule--helper.c  | 14 ++++++++------
 t/t7407-submodule-foreach.sh | 15 +++++++++++++++
 2 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 4b17e3b3b11..eb17e8c5293 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -451,11 +451,12 @@ struct foreach_cb {
 	int quiet;
 	int recursive;
 	int active_only;
+	int populated_only;
 };
 
 #define FOREACH_BOOL_FILTER_NOT_SET -1
 
-#define FOREACH_CB_INIT { .active_only=FOREACH_BOOL_FILTER_NOT_SET }
+#define FOREACH_CB_INIT { .active_only = FOREACH_BOOL_FILTER_NOT_SET, .populated_only = 1 }
 
 static void runcommand_in_submodule(const struct cache_entry *list_item,
 				    struct foreach_cb *info)
@@ -475,9 +476,6 @@ static void runcommand_in_submodule(const struct cache_entry *list_item,
 		die(_("No url found for submodule path '%s' in .gitmodules"),
 			displaypath);
 
-	if (!is_submodule_populated_gently(path, NULL))
-		goto cleanup;
-
 	prepare_submodule_repo_env(&cp.env_array);
 
 	/*
@@ -554,7 +552,6 @@ static void runcommand_in_submodule(const struct cache_entry *list_item,
 				displaypath);
 	}
 
-cleanup:
 	free(displaypath);
 }
 
@@ -571,6 +568,9 @@ static void runcommand_in_submodule_filtered_cb(const struct cache_entry *list_i
 			return;
 	}
 
+	if (info->populated_only != is_submodule_populated_gently(path, NULL))
+		return;
+
 	runcommand_in_submodule(list_item, info);
 }
 
@@ -586,11 +586,13 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
 			 N_("Recurse into nested submodules")),
 		OPT_BOOL(0, "active", &info.active_only,
 			 N_("Call command depending on submodule active state")),
+		OPT_BOOL(0, "populated", &info.populated_only,
+			 N_("Call command depending on submodule populated state")),
 		OPT_END()
 	};
 
 	const char *const git_submodule_helper_usage[] = {
-		N_("git submodule--helper foreach [--quiet] [--recursive] [--[no-]active] [--] <command>"),
+		N_("git submodule--helper foreach [--quiet] [--recursive] [--[no-]active] [--[no-]populated] [--] <command>"),
 		NULL
 	};
 
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 57c5ae70c8d..be6cd80d464 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -80,6 +80,21 @@ test_expect_success 'test basic "submodule foreach" usage' '
 	test_i18ncmp expect actual
 '
 
+sub2sha1=$(cd super/sub2 && git rev-parse HEAD)
+
+cat > expect <<EOF
+Entering 'sub2'
+$pwd/clone-foo2-sub2-$sub2sha1
+EOF
+
+test_expect_success 'test "submodule--helper foreach --no-populated" usage' '
+	(
+		cd clone &&
+		git submodule--helper foreach --no-populated "echo \$toplevel-\$name-\$path-\$sha1" > ../actual
+	) &&
+	test_i18ncmp expect actual
+'
+
 cat > expect <<EOF
 Entering 'sub3'
 $pwd/clone-foo3-sub3-$sub3sha1
-- 
gitgitgadget
Previous: Guillaume Galeazzi via GitGitGadgetNext: Junio C Hamano
Message 19 of 22 in “submodule--helper.c: add only-active to foreach”
  1. submodule--helper.c: add only-active to foreachGuillaume G. via GitGitGadget, May 10, 2020
  2. Shourya ShuklaMay 10, 2020
  3. Guillaume GaleazziMay 10, 2020
  4. Eric SunshineMay 10, 2020
  5. Guillaume GaleazziMay 15, 2020
  6. Shourya ShuklaMay 12, 2020
  7. Guillaume GaleazziMay 15, 2020
  8. Junio C HamanoMay 15, 2020
  9. Guillaume GaleazziMay 15, 2020
  10. Junio C HamanoMay 12, 2020
  11. Guillaume GaleazziMay 13, 2020
  12. Junio C HamanoMay 13, 2020
  13. Guillaume GaleazziMay 13, 2020
  14. Junio C HamanoMay 13, 2020
  15. Guillaume GaleazziMay 15, 2020
  16. 0/3 submodule--helper.c: add only-active to foreachGuillaume G. via GitGitGadget, May 17, 2020
  17. 1/3 submodule--helper.c: add active to foreachGuillaume Galeazzi via GitGitGadget, May 17, 2020
  18. 3/3 submodule--helper.c: add branch to foreachGuillaume Galeazzi via GitGitGadget, May 17, 2020
  19. 2/3 submodule--helper.c: add populated to foreachGuillaume Galeazzi via GitGitGadget, May 17, 2020
  20. Junio C HamanoMay 17, 2020
  21. Guillaume GaleazziMay 17, 2020
  22. Guillaume GaleazziAug 18, 2020

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.