submodule--helper: add is-active subcommand
authorBrandon Williams <bmwill@google.com>
Thu, 16 Mar 2017 22:29:43 +0000 (15:29 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Mar 2017 18:06:09 +0000 (11:06 -0700)
The definition of which submodules are of interest by the user
is tied to the configuration submodule.<name>.url; when it is
set to a non-empty string, it is of interest. We'd want to be
able to later change this definition, but there are many places
that explicitly check this condition in the scripted Porcelain.

Introduce the "is-active" subcommand to "submodule--helper", so
that the exact definition of what submodule is of interest can
be centrally defined (and changed in later steps). In a few
patches that follow, this helper is used to replace the explicit
checks of the configuration variable in scripts.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
t/t7413-submodule-is-active.sh [new file with mode: 0755]
index 15a5430c0028f45ff6265a821ff85aa9d8445230..5fe7e23b17cf1b62cec5aed6556730bf10eccc1b 100644 (file)
@@ -1127,6 +1127,16 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static int is_active(int argc, const char **argv, const char *prefix)
+{
+       if (argc != 2)
+               die("submodule--helper is-active takes exactly 1 arguments");
+
+       gitmodules_config();
+
+       return !is_submodule_initialized(argv[1]);
+}
+
 #define SUPPORT_SUPER_PREFIX (1<<0)
 
 struct cmd_struct {
@@ -1146,6 +1156,7 @@ static struct cmd_struct commands[] = {
        {"init", module_init, SUPPORT_SUPER_PREFIX},
        {"remote-branch", resolve_remote_submodule_branch, 0},
        {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
+       {"is-active", is_active, 0},
 };
 
 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
diff --git a/t/t7413-submodule-is-active.sh b/t/t7413-submodule-is-active.sh
new file mode 100755 (executable)
index 0000000..f18e0c9
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+test_description='Test submodule--helper is-active
+
+This test verifies that `git submodue--helper is-active` correclty identifies
+submodules which are "active" and interesting to the user.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       git init sub &&
+       test_commit -C sub initial &&
+       git init super &&
+       test_commit -C super initial &&
+       git -C super submodule add ../sub sub1 &&
+       git -C super submodule add ../sub sub2 &&
+       git -C super commit -a -m "add 2 submodules at sub{1,2}"
+'
+
+test_expect_success 'is-active works with urls' '
+       git -C super submodule--helper is-active sub1 &&
+       git -C super submodule--helper is-active sub2 &&
+
+       git -C super config --unset submodule.sub1.URL &&
+       test_must_fail git -C super submodule--helper is-active sub1 &&
+       git -C super config submodule.sub1.URL ../sub &&
+       git -C super submodule--helper is-active sub1
+'
+
+test_done