From: "W. Trevor King" <wk...@tremily.us>

This patch teaches 'git submodule add' to look for a preferred
local-branch, and to checkout that branch after the initial clone.
The local branch will always point at the commit checked out by the
internal 'git clone' operation.  For example:

  $ git submodule add git://example.com/subproject.git submod

will checkout the branch pointed to by the cloned repository's HEAD,
and call the local branch 'master'.

  $ git submodule add -b my-feature git://example.com/subproject.git submod

will checkout the branch pointed to by the cloned repository's
my-feature, and *still* call the local branch 'master'.

'git submodule add' does not always make an initial clone (e.g. if a
git repository already exists at the target path).  In cases where
'git submodule add' does not clone a repository, we just leave the
local branch alone.

This commit also shifts the post-clone branch checkout logic from
cmd_add to module_clone, so it can be shared with cmd_update.  The
previous code only checked out the requested branch in cmd_add but not
in cmd_update; this left the user on a detached HEAD after an update
initially cloned, and subsequent updates kept the HEAD detached,
unless the user moved to the desired branch himself.  Now, unless the
user explicitly asks to work on a detached HEAD, subsequent updates
all happen on the specified branch, which matches the end-user
expectation much better.
---
 git-submodule.sh | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/git-submodule.sh b/git-submodule.sh
index c5ea7bd..7cee0bf 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -339,7 +339,19 @@ module_clone()
        echo "gitdir: $rel/$a" >"$sm_path/.git"
 
        rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
-       (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config 
core.worktree "$rel/$b")
+       superproject_branch=$(get_current_branch)
+       default_local_branch=$(get_submodule_config "$sm_name" local-branch)
+       (
+               clear_local_git_env
+               cd "$sm_path" &&
+               GIT_WORK_TREE=. git config core.worktree "$rel/$b" &&
+               local_branch=$(get_local_branch "${superproject_branch}" 
"${default_local_branch}") &&
+               # ash fails to wordsplit ${branch:+-b "$branch"...}
+               case "$branch" in
+               '') git checkout -f -q -B "$local_branch" ;;
+               ?*) git checkout -f -q -B "$local_branch" "origin/$branch" ;;
+               esac
+       ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 }
 
 isnumber()
@@ -503,15 +515,6 @@ Use -f if you really want to add it." >&2
                        fi
                fi
                module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" 
"$depth" || exit
-               (
-                       clear_local_git_env
-                       cd "$sm_path" &&
-                       # ash fails to wordsplit ${branch:+-b "$branch"...}
-                       case "$branch" in
-                       '') git checkout -f -q ;;
-                       ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
-                       esac
-               ) || die "$(eval_gettext "Unable to checkout submodule 
'\$sm_path'")"
        fi
        git config submodule."$sm_name".url "$realrepo"
 
-- 
1.8.5.2.237.g01c62c6

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to