git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Guillaume Galeazzi via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Guillaume G." <guillaume.galeazzi@gmail.com>,
	Guillaume Galeazzi <guillaume.galeazzi@gmail.com>
Subject: [PATCH v2 3/3] submodule--helper.c: add branch to foreach
Date: Sun, 17 May 2020 06:30:23 +0000	[thread overview]
Message-ID: <65504cb780a3a596163d8c8a3b39cb208380de72.1589697023.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.631.v2.git.1589697023.gitgitgadget@gmail.com>

From: Guillaume Galeazzi <guillaume.galeazzi@gmail.com>

On a repository with some submodules tracking different branch, one
may need to run a command only for submodule tracking one of specified
branch. This change, add flags `-b <branch>` and --branch <branch>` to
subcommand `foreach` of `git submodule--helper`.

Signed-off-by: Guillaume Galeazzi <guillaume.galeazzi@gmail.com>
---
 builtin/submodule--helper.c  | 21 +++++++++++++++++++--
 t/t7407-submodule-foreach.sh | 15 +++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index eb17e8c5293..066d1974605 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -452,11 +452,13 @@ struct foreach_cb {
 	int recursive;
 	int active_only;
 	int populated_only;
+	struct string_list remote_branch_filter;
 };
 
 #define FOREACH_BOOL_FILTER_NOT_SET -1
 
-#define FOREACH_CB_INIT { .active_only = FOREACH_BOOL_FILTER_NOT_SET, .populated_only = 1 }
+#define FOREACH_CB_INIT { .active_only = FOREACH_BOOL_FILTER_NOT_SET, \
+	.populated_only = 1, .remote_branch_filter = STRING_LIST_INIT_NODUP }
 
 static void runcommand_in_submodule(const struct cache_entry *list_item,
 				    struct foreach_cb *info)
@@ -555,12 +557,15 @@ static void runcommand_in_submodule(const struct cache_entry *list_item,
 	free(displaypath);
 }
 
+static const char *remote_submodule_branch(const char *path);
+
 static void runcommand_in_submodule_filtered_cb(const struct cache_entry *list_item,
 						void *cb_data)
 {
 	const char *path = list_item->name;
 	struct foreach_cb *info = cb_data;
 	int is_active;
+	const char *branch;
 
 	if (info->active_only != FOREACH_BOOL_FILTER_NOT_SET) {
 		is_active = is_submodule_active(the_repository, path);
@@ -571,6 +576,14 @@ static void runcommand_in_submodule_filtered_cb(const struct cache_entry *list_i
 	if (info->populated_only != is_submodule_populated_gently(path, NULL))
 		return;
 
+	if (info->remote_branch_filter.nr) {
+		branch = remote_submodule_branch(path);
+		if (!branch)
+			return;
+		if (!unsorted_string_list_has_string(&info->remote_branch_filter, branch))
+			return;
+	}
+
 	runcommand_in_submodule(list_item, info);
 }
 
@@ -588,11 +601,13 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
 			 N_("Call command depending on submodule active state")),
 		OPT_BOOL(0, "populated", &info.populated_only,
 			 N_("Call command depending on submodule populated state")),
+		OPT_STRING_LIST('b', "branch", &info.remote_branch_filter,
+			 N_("branch"), N_("Call command only if submodule remote branch is one of <branch> given")),
 		OPT_END()
 	};
 
 	const char *const git_submodule_helper_usage[] = {
-		N_("git submodule--helper foreach [--quiet] [--recursive] [--[no-]active] [--[no-]populated] [--] <command>"),
+		N_("git submodule--helper foreach [--quiet] [--recursive] [--[no-]active] [--[no-]populated] [-b|--branch <branch>] [--] <command>"),
 		NULL
 	};
 
@@ -608,6 +623,8 @@ static int module_foreach(int argc, const char **argv, const char *prefix)
 
 	for_each_listed_submodule(&list, runcommand_in_submodule_filtered_cb, &info);
 
+	string_list_clear(&info.remote_branch_filter, 0);
+
 	return 0;
 }
 
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index be6cd80d464..2da19ac54ce 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -125,6 +125,21 @@ test_expect_success 'test "submodule--helper foreach --no-active" usage' '
 	test_i18ncmp expect actual
 '
 
+cat > expect <<EOF
+Entering 'sub1'
+$pwd/clone-foo1-sub1-$sub1sha1
+EOF
+
+test_expect_success 'test "submodule--helper foreach --branch" usage' '
+	test_when_finished "git -C clone config  -f .gitmodules --unset submodule.foo1.branch" &&
+	(
+		cd clone &&
+		git config -f .gitmodules --add submodule.foo1.branch test &&
+		git submodule--helper foreach --branch test "echo \$toplevel-\$name-\$path-\$sha1" > ../actual
+	) &&
+	test_i18ncmp expect actual
+'
+
 cat >expect <<EOF
 Entering '../sub1'
 $pwd/clone-foo1-sub1-../sub1-$sub1sha1
-- 
gitgitgadget

  parent reply	other threads:[~2020-05-17  6:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-10  8:26 [PATCH] submodule--helper.c: add only-active to foreach Guillaume G. via GitGitGadget
2020-05-10 16:44 ` Shourya Shukla
2020-05-10 21:51   ` Guillaume Galeazzi
2020-05-10 22:42     ` Eric Sunshine
2020-05-15 16:29       ` Guillaume Galeazzi
2020-05-12 14:15     ` Shourya Shukla
2020-05-15 16:51       ` Guillaume Galeazzi
2020-05-15 17:03         ` Junio C Hamano
2020-05-15 18:53           ` Guillaume Galeazzi
2020-05-12 18:53 ` Junio C Hamano
2020-05-13  5:17   ` Guillaume Galeazzi
2020-05-13 15:35     ` Junio C Hamano
2020-05-13 20:07       ` Guillaume Galeazzi
2020-05-13 20:35         ` Junio C Hamano
2020-05-15 11:04           ` Guillaume Galeazzi
2020-05-17  6:30 ` [PATCH v2 0/3] " Guillaume G. via GitGitGadget
2020-05-17  6:30   ` [PATCH v2 1/3] submodule--helper.c: add active " Guillaume Galeazzi via GitGitGadget
2020-05-17  6:30   ` [PATCH v2 2/3] submodule--helper.c: add populated " Guillaume Galeazzi via GitGitGadget
2020-05-17  6:30   ` Guillaume Galeazzi via GitGitGadget [this message]
2020-05-17 15:46   ` [PATCH v2 0/3] submodule--helper.c: add only-active " Junio C Hamano
2020-05-17 19:47     ` Guillaume Galeazzi
2020-08-18 15:57     ` Guillaume Galeazzi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65504cb780a3a596163d8c8a3b39cb208380de72.1589697023.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=guillaume.galeazzi@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).