Skip to content

Commit 01f8d78

Browse files
committed
Merge branch 'dl/submodule-set-branch'
"git submodule" learns "set-branch" subcommand that allows the submodule.*.branch settings to be modified. * dl/submodule-set-branch: submodule: teach set-branch subcommand submodule--helper: teach config subcommand --unset git-submodule.txt: "--branch <branch>" option defaults to 'master'
2 parents d9d65e9 + b57e811 commit 01f8d78

File tree

6 files changed

+200
-13
lines changed

6 files changed

+200
-13
lines changed

Documentation/git-submodule.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SYNOPSIS
1515
'git submodule' [--quiet] init [--] [<path>...]
1616
'git submodule' [--quiet] deinit [-f|--force] (--all|[--] <path>...)
1717
'git submodule' [--quiet] update [<options>] [--] [<path>...]
18+
'git submodule' [--quiet] set-branch [<options>] [--] <path>
1819
'git submodule' [--quiet] summary [<options>] [--] [<path>...]
1920
'git submodule' [--quiet] foreach [--recursive] <command>
2021
'git submodule' [--quiet] sync [--recursive] [--] [<path>...]
@@ -172,6 +173,12 @@ submodule with the `--init` option.
172173
If `--recursive` is specified, this command will recurse into the
173174
registered submodules, and update any nested submodules within.
174175
--
176+
set-branch ((-d|--default)|(-b|--branch <branch>)) [--] <path>::
177+
Sets the default remote tracking branch for the submodule. The
178+
`--branch` option allows the remote branch to be specified. The
179+
`--default` option removes the submodule.<name>.branch configuration
180+
key, which causes the tracking branch to default to 'master'.
181+
175182
summary [--cached|--files] [(-n|--summary-limit) <n>] [commit] [--] [<path>...]::
176183
Show commit summary between the given commit (defaults to HEAD) and
177184
working tree/index. For a submodule in question, a series of commits
@@ -259,13 +266,14 @@ OPTIONS
259266
This option is only valid for the deinit command. Unregister all
260267
submodules in the working tree.
261268

262-
-b::
263-
--branch::
269+
-b <branch>::
270+
--branch <branch>::
264271
Branch of repository to add as submodule.
265272
The name of the branch is recorded as `submodule.<name>.branch` in
266273
`.gitmodules` for `update --remote`. A special value of `.` is used to
267274
indicate that the name of the branch in the submodule should be the
268-
same name as the current branch in the current repository.
275+
same name as the current branch in the current repository. If the
276+
option is not specified, it defaults to 'master'.
269277

270278
-f::
271279
--force::

builtin/submodule--helper.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,17 +2147,22 @@ static int check_name(int argc, const char **argv, const char *prefix)
21472147
static int module_config(int argc, const char **argv, const char *prefix)
21482148
{
21492149
enum {
2150-
CHECK_WRITEABLE = 1
2150+
CHECK_WRITEABLE = 1,
2151+
DO_UNSET = 2
21512152
} command = 0;
21522153

21532154
struct option module_config_options[] = {
21542155
OPT_CMDMODE(0, "check-writeable", &command,
21552156
N_("check if it is safe to write to the .gitmodules file"),
21562157
CHECK_WRITEABLE),
2158+
OPT_CMDMODE(0, "unset", &command,
2159+
N_("unset the config in the .gitmodules file"),
2160+
DO_UNSET),
21572161
OPT_END()
21582162
};
21592163
const char *const git_submodule_helper_usage[] = {
2160-
N_("git submodule--helper config name [value]"),
2164+
N_("git submodule--helper config <name> [<value>]"),
2165+
N_("git submodule--helper config --unset <name>"),
21612166
N_("git submodule--helper config --check-writeable"),
21622167
NULL
21632168
};
@@ -2169,15 +2174,17 @@ static int module_config(int argc, const char **argv, const char *prefix)
21692174
return is_writing_gitmodules_ok() ? 0 : -1;
21702175

21712176
/* Equivalent to ACTION_GET in builtin/config.c */
2172-
if (argc == 2)
2177+
if (argc == 2 && command != DO_UNSET)
21732178
return print_config_from_gitmodules(the_repository, argv[1]);
21742179

21752180
/* Equivalent to ACTION_SET in builtin/config.c */
2176-
if (argc == 3) {
2181+
if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2182+
const char *value = (argc == 3) ? argv[2] : NULL;
2183+
21772184
if (!is_writing_gitmodules_ok())
21782185
die(_("please make sure that the .gitmodules file is in the working tree"));
21792186

2180-
return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
2187+
return config_set_in_gitmodules_file_gently(argv[1], value);
21812188
}
21822189

21832190
usage_with_options(git_submodule_helper_usage, module_config_options);

contrib/completion/git-completion.bash

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,7 @@ _git_submodule ()
26112611
{
26122612
__git_has_doubledash && return
26132613

2614-
local subcommands="add status init deinit update summary foreach sync absorbgitdirs"
2614+
local subcommands="add status init deinit update set-branch summary foreach sync absorbgitdirs"
26152615
local subcommand="$(__git_find_on_cmdline "$subcommands")"
26162616
if [ -z "$subcommand" ]; then
26172617
case "$cur" in
@@ -2642,6 +2642,9 @@ _git_submodule ()
26422642
--force --rebase --merge --reference --depth --recursive --jobs
26432643
"
26442644
;;
2645+
set-branch,--*)
2646+
__gitcomp "--default --branch"
2647+
;;
26452648
summary,--*)
26462649
__gitcomp "--cached --files --summary-limit"
26472650
;;

git-submodule.sh

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ USAGE="[--quiet] [--cached]
1111
or: $dashless [--quiet] init [--] [<path>...]
1212
or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
1313
or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
14+
or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
1415
or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
1516
or: $dashless [--quiet] foreach [--recursive] <command>
1617
or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
@@ -685,6 +686,72 @@ cmd_update()
685686
}
686687
}
687688

689+
#
690+
# Configures a submodule's default branch
691+
#
692+
# $@ = requested path
693+
#
694+
cmd_set_branch() {
695+
unset_branch=false
696+
branch=
697+
698+
while test $# -ne 0
699+
do
700+
case "$1" in
701+
-q|--quiet)
702+
# we don't do anything with this but we need to accept it
703+
;;
704+
-d|--default)
705+
unset_branch=true
706+
;;
707+
-b|--branch)
708+
case "$2" in '') usage ;; esac
709+
branch=$2
710+
shift
711+
;;
712+
--)
713+
shift
714+
break
715+
;;
716+
-*)
717+
usage
718+
;;
719+
*)
720+
break
721+
;;
722+
esac
723+
shift
724+
done
725+
726+
if test $# -ne 1
727+
then
728+
usage
729+
fi
730+
731+
# we can't use `git submodule--helper name` here because internally, it
732+
# hashes the path so a trailing slash could lead to an unintentional no match
733+
name="$(git submodule--helper list "$1" | cut -f2)"
734+
if test -z "$name"
735+
then
736+
exit 1
737+
fi
738+
739+
test -n "$branch"; has_branch=$?
740+
test "$unset_branch" = true; has_unset_branch=$?
741+
742+
if test $((!$has_branch != !$has_unset_branch)) -eq 0
743+
then
744+
usage
745+
fi
746+
747+
if test $has_branch -eq 0
748+
then
749+
git submodule--helper config submodule."$name".branch "$branch"
750+
else
751+
git submodule--helper config --unset submodule."$name".branch
752+
fi
753+
}
754+
688755
#
689756
# Show commit summary for submodules in index or working tree
690757
#
@@ -984,7 +1051,7 @@ cmd_absorbgitdirs()
9841051
while test $# != 0 && test -z "$command"
9851052
do
9861053
case "$1" in
987-
add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
1054+
add | foreach | init | deinit | update | set-branch | status | summary | sync | absorbgitdirs)
9881055
command=$1
9891056
;;
9901057
-q|--quiet)
@@ -1025,8 +1092,8 @@ then
10251092
fi
10261093
fi
10271094

1028-
# "-b branch" is accepted only by "add"
1029-
if test -n "$branch" && test "$command" != add
1095+
# "-b branch" is accepted only by "add" and "set-branch"
1096+
if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
10301097
then
10311098
usage
10321099
fi
@@ -1037,4 +1104,4 @@ then
10371104
usage
10381105
fi
10391106

1040-
"cmd_$command" "$@"
1107+
"cmd_$(echo $command | sed -e s/-/_/g)" "$@"

t/t7411-submodule-config.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ test_expect_success 'reading submodules config from the working tree with "submo
142142
)
143143
'
144144

145+
test_expect_success 'unsetting submodules config from the working tree with "submodule--helper config --unset"' '
146+
(cd super &&
147+
git submodule--helper config --unset submodule.submodule.url &&
148+
git submodule--helper config submodule.submodule.url >actual &&
149+
test_must_be_empty actual
150+
)
151+
'
152+
153+
145154
test_expect_success 'writing submodules config with "submodule--helper config"' '
146155
(cd super &&
147156
echo "new_url" >expect &&

t/t7419-submodule-set-branch.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2019 Denton Liu
4+
#
5+
6+
test_description='Test submodules set-branch subcommand
7+
8+
This test verifies that the set-branch subcommand of git-submodule is working
9+
as expected.
10+
'
11+
12+
TEST_NO_CREATE_REPO=1
13+
. ./test-lib.sh
14+
15+
test_expect_success 'submodule config cache setup' '
16+
mkdir submodule &&
17+
(cd submodule &&
18+
git init &&
19+
echo a >a &&
20+
git add . &&
21+
git commit -ma &&
22+
git checkout -b topic &&
23+
echo b >a &&
24+
git add . &&
25+
git commit -mb
26+
) &&
27+
mkdir super &&
28+
(cd super &&
29+
git init &&
30+
git submodule add ../submodule &&
31+
git commit -m "add submodule"
32+
)
33+
'
34+
35+
test_expect_success 'ensure submodule branch is unset' '
36+
(cd super &&
37+
test_must_fail grep branch .gitmodules
38+
)
39+
'
40+
41+
test_expect_success 'test submodule set-branch --branch' '
42+
(cd super &&
43+
git submodule set-branch --branch topic submodule &&
44+
grep "branch = topic" .gitmodules &&
45+
git submodule update --remote &&
46+
cat <<-\EOF >expect &&
47+
b
48+
EOF
49+
git -C submodule show -s --pretty=%s >actual &&
50+
test_cmp expect actual
51+
)
52+
'
53+
54+
test_expect_success 'test submodule set-branch --default' '
55+
(cd super &&
56+
git submodule set-branch --default submodule &&
57+
test_must_fail grep branch .gitmodules &&
58+
git submodule update --remote &&
59+
cat <<-\EOF >expect &&
60+
a
61+
EOF
62+
git -C submodule show -s --pretty=%s >actual &&
63+
test_cmp expect actual
64+
)
65+
'
66+
67+
test_expect_success 'test submodule set-branch -b' '
68+
(cd super &&
69+
git submodule set-branch -b topic submodule &&
70+
grep "branch = topic" .gitmodules &&
71+
git submodule update --remote &&
72+
cat <<-\EOF >expect &&
73+
b
74+
EOF
75+
git -C submodule show -s --pretty=%s >actual &&
76+
test_cmp expect actual
77+
)
78+
'
79+
80+
test_expect_success 'test submodule set-branch -d' '
81+
(cd super &&
82+
git submodule set-branch -d submodule &&
83+
test_must_fail grep branch .gitmodules &&
84+
git submodule update --remote &&
85+
cat <<-\EOF >expect &&
86+
a
87+
EOF
88+
git -C submodule show -s --pretty=%s >actual &&
89+
test_cmp expect actual
90+
)
91+
'
92+
93+
test_done

0 commit comments

Comments
 (0)