Skip to content

Commit 23d25e4

Browse files
wkinggitster
authored andcommitted
submodule: explicit local branch creation in module_clone
The previous code only checked out branches in cmd_add. This commit moves the branch-checkout logic into module_clone, where it can be shared by cmd_add and cmd_update. I also update the initial checkout command to use 'reset' to preserve branches setup during module_clone. With this change, folks cloning submodules for the first time via: $ git submodule update ... will get a local branch instead of a detached HEAD, unless they are using the default checkout-mode updates. This is a change from the previous situation where cmd_update always used checkout-mode logic (regardless of the requested update mode) for updates that triggered an initial clone, which always resulted in a detached HEAD. This commit does not change the logic for updates after the initial clone, which will continue to create detached HEADs for checkout-mode updates, and integrate remote work with the local HEAD (detached or not) in other modes. The motivation for the change is that developers doing local work inside the submodule are likely to select a non-checkout-mode for updates so their local work is integrated with upstream work. Developers who are not doing local submodule work stick with checkout-mode updates so any apparently local work is blown away during updates. For example, if upstream rolls back the remote branch or gitlinked commit to an earlier version, the checkout-mode developer wants their old submodule checkout to be rolled back as well, instead of getting a no-op merge/rebase with the rolled-back reference. By using the update mode to distinguish submodule developers from black-box submodule consumers, we can setup local branches for the developers who will want local branches, and stick with detached HEADs for the developers that don't care. Testing ======= In t7406, just-cloned checkouts now update to the gitlinked hash with 'reset', to preserve the local branch for situations where we're not on a detached HEAD. I also added explicit tests to t7406 for HEAD attachement after cloning updates, showing that it depends on their update mode: * Checkout-mode updates get detached HEADs * Everyone else gets a local branch, matching the configured submodule.<name>.branch and defaulting to master. The 'initial-setup' tag makes it easy to reset the superproject to a known state, as several earlier tests commit to submodules and commit the changed gitlinks to the superproject, but don't push the new submodule commits to the upstream subprojects. This makes it impossible to checkout the current super master, because it references submodule commits that don't exist in the upstream subprojects. For a specific example, see the tests that currently generate the 'two_new_submodule_commits' commits. Documentation ============= I updated the docs to describe the 'submodule update' modes in detail. The old documentation did not distinguish between cloning and non-cloning updates and lacked clarity on which operations would lead to detached HEADs, and which would not. The new documentation addresses these issues while updating the docs to reflect the changes introduced by this commit's explicit local branch creation in module_clone. I also add '--checkout' to the usage summary and group the update-mode options into a single set. Signed-off-by: W. Trevor King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9adfc1c commit 23d25e4

File tree

4 files changed

+110
-27
lines changed

4 files changed

+110
-27
lines changed

Documentation/git-submodule.txt

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ SYNOPSIS
1515
'git submodule' [--quiet] init [--] [<path>...]
1616
'git submodule' [--quiet] deinit [-f|--force] [--] <path>...
1717
'git submodule' [--quiet] update [--init] [--remote] [-N|--no-fetch]
18-
[-f|--force] [--rebase] [--reference <repository>] [--depth <depth>]
19-
[--merge] [--recursive] [--] [<path>...]
18+
[-f|--force] [--rebase|--merge|--checkout] [--reference <repository>]
19+
[--depth <depth>] [--recursive] [--] [<path>...]
2020
'git submodule' [--quiet] summary [--cached|--files] [(-n|--summary-limit) <n>]
2121
[commit] [--] [<path>...]
2222
'git submodule' [--quiet] foreach [--recursive] <command>
@@ -155,13 +155,31 @@ it contains local modifications.
155155

156156
update::
157157
Update the registered submodules, i.e. clone missing submodules and
158-
checkout the commit specified in the index of the containing repository.
159-
This will make the submodules HEAD be detached unless `--rebase` or
160-
`--merge` is specified or the key `submodule.$name.update` is set to
161-
`rebase`, `merge` or `none`. `none` can be overridden by specifying
162-
`--checkout`. Setting the key `submodule.$name.update` to `!command`
163-
will cause `command` to be run. `command` can be any arbitrary shell
164-
command that takes a single argument, namely the sha1 to update to.
158+
checkout the commit specified in the index of the containing
159+
repository. The update mode defaults to `checkout`, but can be
160+
configured with the `submodule.<name>.update` setting or the
161+
`--rebase`, `--merge`, or `--checkout` options.
162+
+
163+
For updates that clone missing submodules, checkout-mode updates will
164+
create submodules with detached HEADs; all other modes will create
165+
submodules with a local branch named after `submodule.<path>.branch`.
166+
+
167+
For updates that do not clone missing submodules, the submodule's HEAD
168+
is only touched when the remote reference does not match the
169+
submodule's HEAD (for none-mode updates, the submodule is never
170+
touched). The remote reference is usually the gitlinked commit from
171+
the superproject's tree, but with `--remote` it is the upstream
172+
subproject's `submodule.<name>.branch`. This remote reference is
173+
integrated with the submodule's HEAD using the specified update mode.
174+
For checkout-mode updates, that will result in a detached HEAD. For
175+
rebase- and merge-mode updates, the commit referenced by the
176+
submodule's HEAD may change, but the symbolic reference will remain
177+
unchanged (i.e. checked-out branches will still be checked-out
178+
branches, and detached HEADs will still be detached HEADs). If none
179+
of the builtin modes fit your needs, set `submodule.<name>.update` to
180+
`!command` to configure a custom integration command. `command` can
181+
be any arbitrary shell command that takes a single argument, namely
182+
the sha1 to update to.
165183
+
166184
If the submodule is not yet initialized, and you just want to use the
167185
setting as stored in .gitmodules, you can automatically initialize the

Documentation/gitmodules.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ submodule.<name>.branch::
5353
A remote branch name for tracking updates in the upstream submodule.
5454
If the option is not specified, it defaults to 'master'. See the
5555
`--remote` documentation in linkgit:git-submodule[1] for details.
56+
+
57+
This branch name is also used for the local branch created by
58+
non-checkout cloning updates. See the `update` documentation in
59+
linkgit:git-submodule[1] for details.
5660

5761
submodule.<name>.fetchRecurseSubmodules::
5862
This option can be used to control recursive fetching of this

git-submodule.sh

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ module_name()
246246
# $3 = URL to clone
247247
# $4 = reference repository to reuse (empty for independent)
248248
# $5 = depth argument for shallow clones (empty for deep)
249+
# $6 = (remote-tracking) starting point for the local branch (empty for HEAD)
250+
# $7 = local branch to create (empty for a detached HEAD, unless $6 is
251+
# also empty, in which case the local branch is left unchanged)
249252
#
250253
# Prior to calling, cmd_update checks that a possibly existing
251254
# path is not a git repository.
@@ -259,6 +262,8 @@ module_clone()
259262
url=$3
260263
reference="$4"
261264
depth="$5"
265+
start_point="$6"
266+
local_branch="$7"
262267
quiet=
263268
if test -n "$GIT_QUIET"
264269
then
@@ -312,7 +317,16 @@ module_clone()
312317
echo "gitdir: $rel/$a" >"$sm_path/.git"
313318

314319
rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
315-
(clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
320+
(
321+
clear_local_git_env
322+
cd "$sm_path" &&
323+
GIT_WORK_TREE=. git config core.worktree "$rel/$b" &&
324+
# ash fails to wordsplit ${local_branch:+-B "$local_branch"...}
325+
case "$local_branch" in
326+
'') git checkout -f -q ${start_point:+"$start_point"} ;;
327+
?*) git checkout -f -q -B "$local_branch" ${start_point:+"$start_point"} ;;
328+
esac
329+
) || die "$(eval_gettext "Unable to setup cloned submodule '\$sm_path'")"
316330
}
317331

318332
isnumber()
@@ -475,16 +489,15 @@ Use -f if you really want to add it." >&2
475489
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
476490
fi
477491
fi
478-
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" || exit
479-
(
480-
clear_local_git_env
481-
cd "$sm_path" &&
482-
# ash fails to wordsplit ${branch:+-b "$branch"...}
483-
case "$branch" in
484-
'') git checkout -f -q ;;
485-
?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
486-
esac
487-
) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
492+
if test -n "$branch"
493+
then
494+
start_point="origin/$branch"
495+
local_branch="$branch"
496+
else
497+
start_point=""
498+
local_branch=""
499+
fi
500+
module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" "$depth" "$start_point" "$local_branch" || exit
488501
fi
489502
git config submodule."$sm_name".url "$realrepo"
490503

@@ -803,7 +816,9 @@ cmd_update()
803816
fi
804817
name=$(module_name "$sm_path") || exit
805818
url=$(git config submodule."$name".url)
806-
branch=$(get_submodule_config "$name" branch master)
819+
config_branch=$(get_submodule_config "$name" branch)
820+
branch="${config_branch:-master}"
821+
local_branch="$branch"
807822
if ! test -z "$update"
808823
then
809824
update_module=$update
@@ -817,11 +832,19 @@ cmd_update()
817832

818833
displaypath=$(relative_path "$prefix$sm_path")
819834

820-
if test "$update_module" = "none"
821-
then
835+
case "$update_module" in
836+
none)
822837
echo "Skipping submodule '$displaypath'"
823838
continue
824-
fi
839+
;;
840+
checkout)
841+
local_branch=""
842+
;;
843+
rebase | merge | !*)
844+
;;
845+
*)
846+
die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
847+
esac
825848

826849
if test -z "$url"
827850
then
@@ -835,7 +858,8 @@ Maybe you want to use 'update --init'?")"
835858

836859
if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
837860
then
838-
module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
861+
start_point="origin/${branch}"
862+
module_clone "$sm_path" "$name" "$url" "$reference" "$depth" "$start_point" "$local_branch" || exit
839863
cloned_modules="$cloned_modules;$name"
840864
subsha1=
841865
else
@@ -881,7 +905,7 @@ Maybe you want to use 'update --init'?")"
881905
case ";$cloned_modules;" in
882906
*";$name;"*)
883907
# then there is no local change to integrate
884-
update_module=checkout ;;
908+
update_module='!git reset --hard -q'
885909
esac
886910

887911
must_die_on_failure=

t/t7406-submodule-update.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ test_expect_success 'setup a submodule tree' '
6363
git submodule add ../none none &&
6464
test_tick &&
6565
git commit -m "none"
66+
) &&
67+
(cd super &&
68+
git tag initial-setup
6669
)
6770
'
6871

@@ -703,7 +706,7 @@ test_expect_success 'submodule update places git-dir in superprojects git-dir re
703706
git clone super_update_r super_update_r2 &&
704707
(cd super_update_r2 &&
705708
git submodule update --init --recursive >actual &&
706-
test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
709+
test_i18ngrep "Submodule path .submodule/subsubmodule.: .git reset --hard -q" actual &&
707710
(cd submodule/subsubmodule &&
708711
git log > ../../expected
709712
) &&
@@ -764,4 +767,38 @@ test_expect_success 'submodule update clone shallow submodule' '
764767
)
765768
)
766769
'
770+
771+
test_expect_success 'submodule update --checkout clones detached HEAD' '
772+
git clone super super4 &&
773+
echo "detached HEAD" >expected &&
774+
(cd super4 &&
775+
git reset --hard initial-setup &&
776+
git submodule init submodule &&
777+
git submodule update >> /tmp/log 2>&1 &&
778+
(cd submodule &&
779+
git symbolic-ref HEAD > ../../actual ||
780+
echo "detached HEAD" > ../../actual
781+
)
782+
) &&
783+
test_cmp actual expected &&
784+
rm -rf super4
785+
'
786+
787+
test_expect_success 'submodule update --merge clones attached HEAD' '
788+
git clone super super4 &&
789+
echo "refs/heads/master" >expected &&
790+
(cd super4 &&
791+
git reset --hard initial-setup &&
792+
git submodule init submodule &&
793+
git config submodule.submodule.update merge &&
794+
git submodule update --merge &&
795+
(cd submodule &&
796+
git symbolic-ref HEAD > ../../actual ||
797+
echo "detached HEAD" > ../../actual
798+
)
799+
) &&
800+
test_cmp actual expected &&
801+
rm -rf super4
802+
'
803+
767804
test_done

0 commit comments

Comments
 (0)