From 73b0898d0d04d432525f9d56e29bf70fc7bf6ea9 Mon Sep 17 00:00:00 2001 From: Jens Lehmann Date: Sun, 30 Sep 2012 01:05:58 +0200 Subject: Teach "git submodule add" the --name option "git submodule add" initializes the name of a submodule to its path. This was ok as long as the .git directory lived inside the submodule's work tree, but since 1.7.8 it is stored in the .git/modules/ directory of the superproject, making the submodule name survive the removal of the submodule's work tree. This leads to problems when the user tries to add a different submodule at the same path - and thus the same name - later, as that will happily try to restore the submodule from the old repository instead of the one the user specified and will lead to a checkout of the wrong repository. Add the new "--name" option to let the user provide a name for the submodule. This enables the user to solve this conflict without having to remove .git/modules/ by hand (which is no viable solution as it makes it impossible to checkout a commit that records the old submodule and populate it, as that will still check out the new submodule for the same reason). To achieve that the submodule's name is added to the parameter list of the module_clone() helper function. This makes it possible to remove the call of module_name() there because both callers of module_clone() already know the name and can provide it as argument number two. Reported-by: Jonathan Johnson Signed-off-by: Jens Lehmann Signed-off-by: Junio C Hamano --- git-submodule.sh | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'git-submodule.sh') diff --git a/git-submodule.sh b/git-submodule.sh index 3e2045e52d..22febb104c 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -5,7 +5,7 @@ # Copyright (c) 2007 Lars Hjemli dashless=$(basename "$0" | sed -e 's/-/ /') -USAGE="[--quiet] add [-b branch] [-f|--force] [--reference ] [--] [] +USAGE="[--quiet] add [-b branch] [-f|--force] [--name ] [--reference ] [--] [] or: $dashless [--quiet] status [--cached] [--recursive] [--] [...] or: $dashless [--quiet] init [--] [...] or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference ] [--merge] [--recursive] [--] [...] @@ -29,6 +29,7 @@ files= nofetch= update= prefix= +custom_name= # The function takes at most 2 arguments. The first argument is the # URL that navigates to the submodule origin repo. When relative, this URL @@ -179,8 +180,9 @@ module_name() module_clone() { sm_path=$1 - url=$2 - reference="$3" + name=$2 + url=$3 + reference="$4" quiet= if test -n "$GIT_QUIET" then @@ -189,8 +191,6 @@ module_clone() gitdir= gitdir_base= - name=$(module_name "$sm_path" 2>/dev/null) - test -n "$name" || name="$sm_path" base_name=$(dirname "$name") gitdir=$(git rev-parse --git-dir) @@ -272,6 +272,11 @@ cmd_add() reference="$1" shift ;; + --name) + case "$2" in '') usage ;; esac + custom_name=$2 + shift + ;; --) shift break @@ -336,6 +341,13 @@ Use -f if you really want to add it." >&2 exit 1 fi + if test -n "$custom_name" + then + sm_name="$custom_name" + else + sm_name="$sm_path" + fi + # perhaps the path exists and is already a git repo, else clone it if test -e "$sm_path" then @@ -348,7 +360,7 @@ Use -f if you really want to add it." >&2 else - module_clone "$sm_path" "$realrepo" "$reference" || exit + module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit ( clear_local_git_env cd "$sm_path" && @@ -359,13 +371,13 @@ Use -f if you really want to add it." >&2 esac ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")" fi - git config submodule."$sm_path".url "$realrepo" + git config submodule."$sm_name".url "$realrepo" git add $force "$sm_path" || die "$(eval_gettext "Failed to add submodule '\$sm_path'")" - git config -f .gitmodules submodule."$sm_path".path "$sm_path" && - git config -f .gitmodules submodule."$sm_path".url "$repo" && + git config -f .gitmodules submodule."$sm_name".path "$sm_path" && + git config -f .gitmodules submodule."$sm_name".url "$repo" && git add --force .gitmodules || die "$(eval_gettext "Failed to register submodule '\$sm_path'")" } @@ -594,7 +606,7 @@ Maybe you want to use 'update --init'?")" if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git then - module_clone "$sm_path" "$url" "$reference"|| exit + module_clone "$sm_path" "$name" "$url" "$reference" || exit cloned_modules="$cloned_modules;$name" subsha1= else -- cgit v1.2.3 From 4b7c286ec74c7bb88e9e7ac2d283d86f64b6b0ea Mon Sep 17 00:00:00 2001 From: Jens Lehmann Date: Sun, 30 Sep 2012 23:01:29 +0200 Subject: submodule add: Fail when .git/modules/ already exists unless forced When adding a new submodule it can happen that .git/modules/ already contains a submodule repo, e.g. when a submodule is removed from the work tree and another submodule is added at the same path. But then the work tree of the submodule will be populated using the existing repository and not the one the user provided, which results in an incorrect work tree. On the other hand the user might reactivate a submodule removed earlier, then reusing that .git directory is the Right Thing to do. As git can't decide what is the case, error out and tell the user she should use either use a different name for the submodule with the "--name" option or can reuse the .git directory for the newly added submodule by providing the --force option (which only makes sense when the upstream matches, so the error message lists all remotes of .git/modules/). In one test in t7406 the --force option had to be added to "git submodule add", as that test re-adds a formerly removed submodule. Reported-by: Jonathan Johnson Signed-off-by: Jens Lehmann Signed-off-by: Junio C Hamano --- git-submodule.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'git-submodule.sh') diff --git a/git-submodule.sh b/git-submodule.sh index 22febb104c..e8112c8d1a 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -359,7 +359,20 @@ Use -f if you really want to add it." >&2 fi else - + if test -d ".git/modules/$sm_name" + then + if test -z "$force" + then + echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")" + GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2 + echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")" + echo >&2 " $realrepo" + echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")" + die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")" + else + echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")" + fi + fi module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit ( clear_local_git_env -- cgit v1.2.3