@@ -241,6 +241,15 @@ module_name()
241
241
#
242
242
# Clone a submodule
243
243
#
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
+ #
244
253
# Prior to calling, cmd_update checks that a possibly existing
245
254
# path is not a git repository.
246
255
# Likewise, cmd_add checks that path does not exist at all,
@@ -253,6 +262,8 @@ module_clone()
253
262
url=$3
254
263
reference=" $4 "
255
264
depth=" $5 "
265
+ start_point=" $6 "
266
+ local_branch=" $7 "
256
267
quiet=
257
268
if test -n " $GIT_QUIET "
258
269
then
@@ -306,7 +317,16 @@ module_clone()
306
317
echo " gitdir: $rel /$a " > " $sm_path /.git"
307
318
308
319
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'" ) "
310
330
}
311
331
312
332
isnumber ()
@@ -469,16 +489,15 @@ Use -f if you really want to add it." >&2
469
489
echo " $( eval_gettext " Reactivating local git directory for submodule '\$ sm_name'." ) "
470
490
fi
471
491
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
482
501
fi
483
502
git config submodule." $sm_name " .url " $realrepo "
484
503
@@ -799,32 +818,35 @@ cmd_update()
799
818
fi
800
819
name=$( module_name " $sm_path " ) || exit
801
820
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 "
803
824
if ! test -z " $update "
804
825
then
805
826
update_module=$update
806
827
else
807
828
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
819
833
fi
820
834
821
835
displaypath=$( relative_path " $prefix$sm_path " )
822
836
823
- if test " $update_module " = " none "
824
- then
837
+ case " $update_module " in
838
+ none)
825
839
echo " Skipping submodule '$displaypath '"
826
840
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
828
850
829
851
if test -z " $url "
830
852
then
@@ -838,7 +860,8 @@ Maybe you want to use 'update --init'?")"
838
860
839
861
if ! test -d " $sm_path " /.git -o -f " $sm_path " /.git
840
862
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
842
865
cloned_modules=" $cloned_modules ;$name "
843
866
subsha1=
844
867
else
@@ -884,11 +907,16 @@ Maybe you want to use 'update --init'?")"
884
907
case " ;$cloned_modules ;" in
885
908
* " ;$name ;" * )
886
909
# then there is no local change to integrate
887
- update_module= ;;
910
+ update_module=' !git reset --hard -q '
888
911
esac
889
912
890
913
must_die_on_failure=
891
914
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
+ ;;
892
920
rebase)
893
921
command=" git rebase"
894
922
die_msg=" $( eval_gettext " Unable to rebase '\$ sha1' in submodule path '\$ displaypath'" ) "
@@ -908,10 +936,7 @@ Maybe you want to use 'update --init'?")"
908
936
must_die_on_failure=yes
909
937
;;
910
938
* )
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 '" ) "
915
940
esac
916
941
917
942
if (clear_local_git_env; cd " $sm_path " && $command " $sha1 " )
0 commit comments