In later patches we need to tell if a submodule is in a group,
which is defined by name, path or labels.

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 builtin/submodule--helper.c  | 43 ++++++++++++++++++++++-
 submodule-config.c           | 50 +++++++++++++++++++++++++++
 submodule-config.h           |  3 ++
 t/t7413-submodule--helper.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 176 insertions(+), 1 deletion(-)
 create mode 100755 t/t7413-submodule--helper.sh

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index b6d4f27..23d7224 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -814,6 +814,46 @@ static int update_clone(int argc, const char **argv, const 
char *prefix)
        return 0;
 }
 
+int in_group(int argc, const char **argv, const char *prefix)
+{
+       const struct string_list *list;
+       struct string_list actual_list = STRING_LIST_INIT_DUP;
+       const struct submodule *sub;
+       const char *group = NULL;
+
+       struct option default_group_options[] = {
+               OPT_STRING('g', "group", &group, N_("group"),
+                               N_("group specifier for submodules")),
+               OPT_END()
+       };
+
+       const char *const git_submodule_helper_usage[] = {
+               N_("git submodule--helper in-group <path>"),
+               NULL
+       };
+
+       argc = parse_options(argc, argv, prefix, default_group_options,
+                            git_submodule_helper_usage, 0);
+
+       /* Overlay the parsed .gitmodules file with .git/config */
+       gitmodules_config();
+       git_config(submodule_config, NULL);
+
+       if (argc != 1)
+               usage(git_submodule_helper_usage[0]);
+
+       sub = submodule_from_path(null_sha1, argv[0]);
+
+       if (!group)
+               list = git_config_get_value_multi("submodule.defaultGroup");
+       else {
+               string_list_split(&actual_list, group, ',', -1);
+               list = &actual_list;
+       }
+
+       return !submodule_in_group(list, sub);
+}
+
 struct cmd_struct {
        const char *cmd;
        int (*fn)(int, const char **, const char *);
@@ -826,7 +866,8 @@ static struct cmd_struct commands[] = {
        {"update-clone", update_clone},
        {"resolve-relative-url", resolve_relative_url},
        {"resolve-relative-url-test", resolve_relative_url_test},
-       {"init", module_init}
+       {"init", module_init},
+       {"in-group", in_group}
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/submodule-config.c b/submodule-config.c
index 0cdb47e..ebed0f2 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -522,3 +522,53 @@ void submodule_free(void)
        cache_free(&cache);
        is_cache_init = 0;
 }
+
+int submodule_in_group(const struct string_list *group,
+                      const struct submodule *sub)
+{
+       int matched = 0;
+       struct strbuf sb = STRBUF_INIT;
+
+       if (!group)
+               /*
+                * If no group is specified all, all submodules match to
+                * keep traditional behavior
+                */
+               return 1;
+
+       if (sub->labels) {
+               struct string_list_item *item;
+               for_each_string_list_item(item, sub->labels) {
+                       strbuf_reset(&sb);
+                       strbuf_addf(&sb, "*%s", item->string);
+                       if (string_list_has_string(group, sb.buf)) {
+                               matched = 1;
+                               break;
+                       }
+               }
+       }
+       if (sub->path) {
+               /*
+                * NEEDSWORK: This currently works only for
+                * exact paths, but we want to enable
+                * inexact matches such wildcards.
+                */
+               strbuf_reset(&sb);
+               strbuf_addf(&sb, "./%s", sub->path);
+               if (string_list_has_string(group, sb.buf))
+                       matched = 1;
+       }
+       if (sub->name) {
+               /*
+                * NEEDSWORK: Same as with path. Do we want to
+                * support wildcards or such?
+                */
+               strbuf_reset(&sb);
+               strbuf_addf(&sb, ":%s", sub->name);
+               if (string_list_has_string(group, sb.buf))
+                       matched = 1;
+       }
+       strbuf_release(&sb);
+
+       return matched;
+}
diff --git a/submodule-config.h b/submodule-config.h
index d57da59..4c696cc 100644
--- a/submodule-config.h
+++ b/submodule-config.h
@@ -31,4 +31,7 @@ const struct submodule *submodule_from_path(const unsigned 
char *commit_sha1,
                const char *path);
 void submodule_free(void);
 
+int submodule_in_group(const struct string_list *group,
+                      const struct submodule *sub);
+
 #endif /* SUBMODULE_CONFIG_H */
diff --git a/t/t7413-submodule--helper.sh b/t/t7413-submodule--helper.sh
new file mode 100755
index 0000000..c6939ab
--- /dev/null
+++ b/t/t7413-submodule--helper.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+# This should be merged with t7412 eventually.
+# (currently in flight as jk/submodule-c-credential)
+
+
+test_description='Basic plumbing support of submodule--helper
+
+This test verifies the submodule--helper plumbing command used to implement
+git-submodule.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup superproject with submodules' '
+
+       mkdir sub &&
+       (
+               cd sub &&
+               git init &&
+               test_commit test
+               test_commit test2
+       ) &&
+       mkdir super &&
+       (
+               cd super &&
+               git init &&
+               git submodule add ../sub sub0 &&
+               git submodule add -l bit1 ../sub sub1 &&
+               git submodule add -l bit2 ../sub sub2 &&
+               git submodule add -l bit2 -l bit1 ../sub sub3 &&
+               git commit -m "add labeled submodules"
+       )
+'
+
+test_expect_success 'in-group' '
+       (
+               cd super &&
+               # we do not specify a group nor have set a default group,
+               # any submodule should be in the default group:
+               git submodule--helper in-group sub0 &&
+               git submodule--helper in-group sub1 &&
+               git submodule--helper in-group sub2 &&
+               git submodule--helper in-group sub3 &&
+
+               # test bit1:
+               test_must_fail git submodule--helper in-group --group=*bit1 
sub0 &&
+                              git submodule--helper in-group --group=*bit1 
sub1 &&
+               test_must_fail git submodule--helper in-group --group=*bit1 
sub2 &&
+                              git submodule--helper in-group --group=*bit1 
sub3 &&
+
+               # test by path:
+                              git submodule--helper in-group --group=./sub0 
sub0 &&
+               test_must_fail git submodule--helper in-group --group=./sub0 
sub1 &&
+               test_must_fail git submodule--helper in-group --group=./sub0 
sub2 &&
+               test_must_fail git submodule--helper in-group --group=./sub0 
sub3 &&
+
+               # test by name:
+                              git submodule--helper in-group --group=:sub0 
sub0 &&
+               test_must_fail git submodule--helper in-group --group=:sub0 
sub1 &&
+               test_must_fail git submodule--helper in-group --group=:sub0 
sub2 &&
+               test_must_fail git submodule--helper in-group --group=:sub0 
sub3 &&
+
+               # logical OR of path and labels
+                              git submodule--helper in-group 
--group=*bit1,./sub0 sub0 &&
+                              git submodule--helper in-group 
--group=*bit1,./sub0 sub1 &&
+               test_must_fail git submodule--helper in-group 
--group=*bit1,./sub0 sub2 &&
+                              git submodule--helper in-group 
--group=*bit1,./sub0 sub3 &&
+
+               # test if the config option is picked up
+               git config --add submodule.defaultGroup *bit1 &&
+               git config --add submodule.defaultGroup ./sub0 &&
+
+                              git submodule--helper in-group sub0 &&
+                              git submodule--helper in-group sub1 &&
+               test_must_fail git submodule--helper in-group sub2 &&
+                              git submodule--helper in-group sub3
+       )
+'
+
+test_done
-- 
2.8.0.41.g8d9aeb3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to