submodule: port subcommand 'set-branch' from shell to C
Convert submodule subcommand 'set-branch' to a builtin and call it via 'git-submodule.sh'. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> Helped-by: Denton Liu <liu.denton@gmail.com> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Đoàn Trần Công Danh <congdanhqx@gmail.com> Signed-off-by: Shourya Shukla <shouryashukla.oo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Shourya Shukla committed
Jun 2, 2020 at 22:05 UTC
2964d6e5e1e5e3286cfdc8c68f347e0aba97caf0
2 files changed
+47
-29
builtin/submodule--helper.c
+44
@@ -2277,6 +2277,49 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
2277
return 0;
2278
}
2279
2280
+static int module_set_branch(int argc, const char **argv, const char *prefix)
2281
+{
2282
+ int opt_default = 0, ret;
2283
+ const char *opt_branch = NULL;
2284
+ const char *path;
2285
+ char *config_name;
2286
+
2287
+ /*
2288
+ * We accept the `quiet` option for uniformity across subcommands,
2289
+ * though there is nothing to make less verbose in this subcommand.
2290
+ */
2291
+ struct option options[] = {
2292
+ OPT_NOOP_NOARG('q', "quiet"),
2293
+ OPT_BOOL('d', "default", &opt_default,
2294
+ N_("set the default tracking branch to master")),
2295
+ OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2296
+ N_("set the default tracking branch")),
2297
+ OPT_END()
2298
+ };
2299
+ const char *const usage[] = {
2300
+ N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2301
+ N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2302
+ NULL
2303
+ };
2304
+
2305
+ argc = parse_options(argc, argv, prefix, options, usage, 0);
2306
+
2307
+ if (!opt_branch && !opt_default)
2308
+ die(_("--branch or --default required"));
2309
+
2310
+ if (opt_branch && opt_default)
2311
+ die(_("--branch and --default are mutually exclusive"));
2312
+
2313
+ if (argc != 1 || !(path = argv[0]))
2314
+ usage_with_options(usage, options);
2315
+
2316
+ config_name = xstrfmt("submodule.%s.branch", path);
2317
+ ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2318
+
2319
+ free(config_name);
2320
+ return !!ret;
2321
+}
2322
+
2323
#define SUPPORT_SUPER_PREFIX (1<<0)
2324
2325
struct cmd_struct {
@@ -2308,6 +2351,7 @@ static struct cmd_struct commands[] = {
2351
{"check-name", check_name, 0},
2352
{"config", module_config, 0},
2353
{"set-url", module_set_url, 0},
2354
+ {"set-branch", module_set_branch, 0},
2355
};
2356
2357
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
git-submodule.sh
+3
-29
@@ -719,7 +719,7 @@ cmd_update()
719
# $@ = requested path
720
#
721
cmd_set_branch() {
722
- unset_branch=false
722
+ default=
723
branch=
724
725
while test $# -ne 0
@@ -729,7 +729,7 @@ cmd_set_branch() {
729
# we don't do anything with this but we need to accept it
730
;;
731
-d|--default)
732
- unset_branch=true
732
+ default=1
733
;;
734
-b|--branch)
735
case "$2" in '') usage ;; esac
@@ -750,33 +750,7 @@ cmd_set_branch() {
750
shift
751
done
752
753
- if test $# -ne 1
754
- then
755
- usage
756
- fi
757
-
758
- # we can't use `git submodule--helper name` here because internally, it
759
- # hashes the path so a trailing slash could lead to an unintentional no match
760
- name="$(git submodule--helper list "$1" | cut -f2)"
761
- if test -z "$name"
762
- then
763
- exit 1
764
- fi
765
-
766
- test -n "$branch"; has_branch=$?
767
- test "$unset_branch" = true; has_unset_branch=$?
768
-
769
- if test $((!$has_branch != !$has_unset_branch)) -eq 0
770
- then
771
- usage
772
- fi
773
-
774
- if test $has_branch -eq 0
775
- then
776
- git submodule--helper config submodule."$name".branch "$branch"
777
- else
778
- git submodule--helper config --unset submodule."$name".branch
779
- fi
753
+ git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
754
}
755
756
#