Skip to content

Commit 42b4917

Browse files
jherlandgitster
authored andcommitted
git-submodule: add support for --merge.
'git submodule update --merge' merges the commit referenced by the superproject into your local branch, instead of checking it out on a detached HEAD. As evidenced by the addition of "git submodule update --rebase", it is useful to provide alternatives to the default 'checkout' behaviour of "git submodule update". One such alternative is, when updating a submodule to a new commit, to merge that commit into the current local branch in that submodule. This is useful in workflows where you want to update your submodule from its upstream, but you cannot use --rebase, because you have downstream people working on top of your submodule branch, and you don't want to disrupt their work. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3294842 commit 42b4917

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

Documentation/git-submodule.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ init::
113113
update::
114114
Update the registered submodules, i.e. clone missing submodules and
115115
checkout the commit specified in the index of the containing repository.
116-
This will make the submodules HEAD be detached unless '--rebase' is
117-
specified or the key `submodule.$name.update` is set to `rebase`.
116+
This will make the submodules HEAD be detached unless '--rebase' or
117+
'--merge' is specified or the key `submodule.$name.update` is set to
118+
`rebase` or `merge`.
118119
+
119120
If the submodule is not yet initialized, and you just want to use the
120121
setting as stored in .gitmodules, you can automatically initialize the
@@ -187,6 +188,16 @@ OPTIONS
187188
If the key `submodule.$name.update` is set to `rebase`, this option is
188189
implicit.
189190

191+
--merge::
192+
This option is only valid for the update command.
193+
Merge the commit recorded in the superproject into the current branch
194+
of the submodule. If this option is given, the submodule's HEAD will
195+
not be detached. If a merge failure prevents this process, you will
196+
have to resolve the resulting conflicts within the submodule with the
197+
usual conflict resolution tools.
198+
If the key `submodule.$name.update` is set to `merge`, this option is
199+
implicit.
200+
190201
<path>...::
191202
Paths to submodule(s). When specified this will restrict the command
192203
to only operate on the submodules found at the specified paths.

Documentation/gitmodules.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ submodule.<name>.update::
3535
If 'checkout' (the default), the new commit specified in the
3636
superproject will be checked out in the submodule on a detached HEAD.
3737
If 'rebase', the current branch of the submodule will be rebased onto
38-
the commit specified in the superproject.
38+
the commit specified in the superproject. If 'merge', the commit
39+
specified in the superproject will be merged into the current branch
40+
in the submodule.
3941
This config option is overridden if 'git submodule update' is given
40-
the '--rebase' option.
42+
the '--merge' or '--rebase' options.
4143

4244

4345
EXAMPLES

git-submodule.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Copyright (c) 2007 Lars Hjemli
66

77
USAGE="[--quiet] [--cached] \
8-
[add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
8+
[add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch] [--rebase|--merge]|summary [-n|--summary-limit <n>] [<commit>]] \
99
[--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
1010
OPTIONS_SPEC=
1111
. git-sh-setup
@@ -331,6 +331,10 @@ cmd_update()
331331
shift
332332
update="rebase"
333333
;;
334+
-m|--merge)
335+
shift
336+
update="merge"
337+
;;
334338
--)
335339
shift
336340
break
@@ -396,6 +400,11 @@ cmd_update()
396400
action="rebase"
397401
msg="rebased onto"
398402
;;
403+
merge)
404+
command="git merge"
405+
action="merge"
406+
msg="merged in"
407+
;;
399408
*)
400409
command="git checkout $force -q"
401410
action="checkout"

t/t7406-submodule-update.sh

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
test_description='Test updating submodules
77
88
This test verifies that "git submodule update" detaches the HEAD of the
9-
submodule and "git submodule update --rebase" does not detach the HEAD.
9+
submodule and "git submodule update --rebase/--merge" does not detach the HEAD.
1010
'
1111

1212
. ./test-lib.sh
@@ -76,6 +76,20 @@ test_expect_success 'submodule update --rebase staying on master' '
7676
)
7777
'
7878

79+
test_expect_success 'submodule update --merge staying on master' '
80+
(cd super/submodule &&
81+
git reset --hard HEAD~1
82+
) &&
83+
(cd super &&
84+
(cd submodule &&
85+
compare_head
86+
) &&
87+
git submodule update --merge submodule &&
88+
cd submodule &&
89+
compare_head
90+
)
91+
'
92+
7993
test_expect_success 'submodule update - rebase in .git/config' '
8094
(cd super &&
8195
git config submodule.submodule.update rebase
@@ -110,6 +124,40 @@ test_expect_success 'submodule update - checkout in .git/config but --rebase giv
110124
)
111125
'
112126

127+
test_expect_success 'submodule update - merge in .git/config' '
128+
(cd super &&
129+
git config submodule.submodule.update merge
130+
) &&
131+
(cd super/submodule &&
132+
git reset --hard HEAD~1
133+
) &&
134+
(cd super &&
135+
(cd submodule &&
136+
compare_head
137+
) &&
138+
git submodule update submodule &&
139+
cd submodule &&
140+
compare_head
141+
)
142+
'
143+
144+
test_expect_success 'submodule update - checkout in .git/config but --merge given' '
145+
(cd super &&
146+
git config submodule.submodule.update checkout
147+
) &&
148+
(cd super/submodule &&
149+
git reset --hard HEAD~1
150+
) &&
151+
(cd super &&
152+
(cd submodule &&
153+
compare_head
154+
) &&
155+
git submodule update --merge submodule &&
156+
cd submodule &&
157+
compare_head
158+
)
159+
'
160+
113161
test_expect_success 'submodule update - checkout in .git/config' '
114162
(cd super &&
115163
git config submodule.submodule.update checkout
@@ -137,4 +185,14 @@ test_expect_success 'submodule init picks up rebase' '
137185
)
138186
'
139187

188+
test_expect_success 'submodule init picks up merge' '
189+
(cd super &&
190+
git config submodule.merging.url git://non-existing/git &&
191+
git config submodule.merging.path does-not-matter &&
192+
git config submodule.merging.update merge &&
193+
git submodule init merging &&
194+
test "merge" = $(git config submodule.merging.update)
195+
)
196+
'
197+
140198
test_done

0 commit comments

Comments
 (0)