@@ -241,6 +241,15 @@ module_name()
241241#
242242# Clone a submodule
243243#
244+ # $1 = submodule path
245+ # $2 = submodule name
246+ # $3 = URL to clone
247+ # $4 = reference repository to reuse (empty for independent)
248+ # $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)
252+ #
244253# Prior to calling, cmd_update checks that a possibly existing
245254# path is not a git repository.
246255# Likewise, cmd_add checks that path does not exist at all,
@@ -253,6 +262,8 @@ module_clone()
253262 url=$3
254263 reference=" $4 "
255264 depth=" $5 "
265+ start_point=" $6 "
266+ local_branch=" $7 "
256267 quiet=
257268 if test -n " $GIT_QUIET "
258269 then
@@ -306,7 +317,16 @@ module_clone()
306317 echo " gitdir: $rel /$a " > " $sm_path /.git"
307318
308319 rel=$( echo $a | sed -e ' s|[^/][^/]*|..|g' )
309- (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'" ) "
310330}
311331
312332isnumber ()
@@ -469,16 +489,15 @@ Use -f if you really want to add it." >&2
469489 echo " $( eval_gettext " Reactivating local git directory for submodule '\$ sm_name'." ) "
470490 fi
471491 fi
472- module_clone " $sm_path " " $sm_name " " $realrepo " " $reference " " $depth " || exit
473- (
474- clear_local_git_env
475- cd " $sm_path " &&
476- # ash fails to wordsplit ${branch:+-b "$branch"...}
477- case " $branch " in
478- ' ' ) git checkout -f -q ;;
479- ?* ) git checkout -f -q -B " $branch " " origin/$branch " ;;
480- esac
481- ) || 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
482501 fi
483502 git config submodule." $sm_name " .url " $realrepo "
484503
@@ -799,32 +818,35 @@ cmd_update()
799818 fi
800819 name=$( module_name " $sm_path " ) || exit
801820 url=$( git config submodule." $name " .url)
802- branch=$( get_submodule_config " $name " branch master)
821+ config_branch=$( get_submodule_config " $name " branch)
822+ branch=" ${config_branch:- master} "
823+ local_branch=" $branch "
803824 if ! test -z " $update "
804825 then
805826 update_module=$update
806827 else
807828 update_module=$( git config submodule." $name " .update)
808- case " $update_module " in
809- ' ' )
810- ;; # Unset update mode
811- checkout | rebase | merge | none)
812- ;; # Known update modes
813- !* )
814- ;; # Custom update command
815- * )
816- die " $( eval_gettext " Invalid update mode '$update_module ' for submodule '$name '" ) "
817- ;;
818- esac
829+ if test -z " $update_module "
830+ then
831+ update_module=" checkout"
832+ fi
819833 fi
820834
821835 displaypath=$( relative_path " $prefix$sm_path " )
822836
823- if test " $update_module " = " none "
824- then
837+ case " $update_module " in
838+ none)
825839 echo " Skipping submodule '$displaypath '"
826840 continue
827- fi
841+ ;;
842+ checkout)
843+ local_branch=" "
844+ ;;
845+ rebase | merge | !* )
846+ ;;
847+ * )
848+ die " $( eval_gettext " Invalid update mode '$update_module ' for submodule '$name '" ) "
849+ esac
828850
829851 if test -z " $url "
830852 then
@@ -838,7 +860,8 @@ Maybe you want to use 'update --init'?")"
838860
839861 if ! test -d " $sm_path " /.git -o -f " $sm_path " /.git
840862 then
841- module_clone " $sm_path " " $name " " $url " " $reference " " $depth " || exit
863+ start_point=" origin/${branch} "
864+ module_clone " $sm_path " " $name " " $url " " $reference " " $depth " " $start_point " " $local_branch " || exit
842865 cloned_modules=" $cloned_modules ;$name "
843866 subsha1=
844867 else
@@ -884,11 +907,16 @@ Maybe you want to use 'update --init'?")"
884907 case " ;$cloned_modules ;" in
885908 * " ;$name ;" * )
886909 # then there is no local change to integrate
887- update_module= ;;
910+ update_module=' !git reset --hard -q '
888911 esac
889912
890913 must_die_on_failure=
891914 case " $update_module " in
915+ checkout)
916+ command=" git checkout $subforce -q"
917+ die_msg=" $( eval_gettext " Unable to checkout '\$ sha1' in submodule path '\$ displaypath'" ) "
918+ say_msg=" $( eval_gettext " Submodule path '\$ displaypath': checked out '\$ sha1'" ) "
919+ ;;
892920 rebase)
893921 command=" git rebase"
894922 die_msg=" $( eval_gettext " Unable to rebase '\$ sha1' in submodule path '\$ displaypath'" ) "
@@ -908,10 +936,7 @@ Maybe you want to use 'update --init'?")"
908936 must_die_on_failure=yes
909937 ;;
910938 * )
911- command=" git checkout $subforce -q"
912- die_msg=" $( eval_gettext " Unable to checkout '\$ sha1' in submodule path '\$ displaypath'" ) "
913- say_msg=" $( eval_gettext " Submodule path '\$ displaypath': checked out '\$ sha1'" ) "
914- ;;
939+ die " $( eval_gettext " Invalid update mode '$update_module ' for submodule '$name '" ) "
915940 esac
916941
917942 if (clear_local_git_env; cd " $sm_path " && $command " $sha1 " )
0 commit comments