Skip to content

Commit 2964d6e

Browse files
periperidipgitster
authored andcommitted
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 <[email protected]> Mentored-by: Kaartic Sivaraam <[email protected]> Helped-by: Denton Liu <[email protected]> Helped-by: Eric Sunshine <[email protected]> Helped-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Shourya Shukla <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae92ac8 commit 2964d6e

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

builtin/submodule--helper.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,6 +2277,49 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
22772277
return 0;
22782278
}
22792279

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+
22802323
#define SUPPORT_SUPER_PREFIX (1<<0)
22812324

22822325
struct cmd_struct {
@@ -2308,6 +2351,7 @@ static struct cmd_struct commands[] = {
23082351
{"check-name", check_name, 0},
23092352
{"config", module_config, 0},
23102353
{"set-url", module_set_url, 0},
2354+
{"set-branch", module_set_branch, 0},
23112355
};
23122356

23132357
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)

git-submodule.sh

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ cmd_update()
719719
# $@ = requested path
720720
#
721721
cmd_set_branch() {
722-
unset_branch=false
722+
default=
723723
branch=
724724

725725
while test $# -ne 0
@@ -729,7 +729,7 @@ cmd_set_branch() {
729729
# we don't do anything with this but we need to accept it
730730
;;
731731
-d|--default)
732-
unset_branch=true
732+
default=1
733733
;;
734734
-b|--branch)
735735
case "$2" in '') usage ;; esac
@@ -750,33 +750,7 @@ cmd_set_branch() {
750750
shift
751751
done
752752

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} -- "$@"
780754
}
781755

782756
#

0 commit comments

Comments
 (0)