From: Junio C Hamano Date: Tue, 16 Aug 2011 19:41:13 +0000 (-0700) Subject: Merge branch 'jl/submodule-add-relurl-wo-upstream' into maint X-Git-Tag: v1.7.6.1~15 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/f4af7f19639457d71c4e9b53892b937468824cac?ds=inline;hp=-c Merge branch 'jl/submodule-add-relurl-wo-upstream' into maint * jl/submodule-add-relurl-wo-upstream: submodule add: clean up duplicated code submodule add: allow relative repository path even when no url is set submodule add: test failure when url is not configured in superproject Conflicts: git-submodule.sh --- f4af7f19639457d71c4e9b53892b937468824cac diff --combined Documentation/git-submodule.txt index 2b31d5f1f2,f72e900d23..585f03681b --- a/Documentation/git-submodule.txt +++ b/Documentation/git-submodule.txt @@@ -78,7 -78,9 +78,9 @@@ to exist in the superproject. If is the URL of the new submodule's origin repository. This may be either an absolute URL, or (if it begins with ./ or ../), the location relative to the superproject's origin - repository. + repository. If the superproject doesn't have an origin configured + the superproject is its own authoritative upstream and the current + working directory is used instead. + is the relative location for the cloned submodule to exist in the superproject. If does not exist, then the @@@ -167,9 -169,7 +169,9 @@@ commit for each submodule sync:: Synchronizes submodules' remote URL configuration setting - to the value specified in .gitmodules. This is useful when + to the value specified in .gitmodules. It will only affect those + submodules which already have an url entry in .git/config (that is the + case when they are initialized or freshly added). This is useful when submodule URLs change upstream and you need to update your local repositories accordingly. + @@@ -188,10 -188,8 +190,10 @@@ OPTION -f:: --force:: - This option is only valid for the add command. - Allow adding an otherwise ignored submodule path. + This option is only valid for add and update commands. + When running add, allow adding an otherwise ignored submodule path. + When running update, throw away local changes in submodules when + switching to a different commit. --cached:: This option is only valid for status and summary commands. These diff --combined git-submodule.sh index 20c9bec970,32cb05443a..f8ea3bf6f2 --- a/git-submodule.sh +++ b/git-submodule.sh @@@ -8,7 -8,7 +8,7 @@@ dashless=$(basename "$0" | sed -e 's/- USAGE="[--quiet] add [-b branch] [-f|--force] [--reference ] [--] [] or: $dashless [--quiet] status [--cached] [--recursive] [--] [...] or: $dashless [--quiet] init [--] [...] - or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference ] [--merge] [--recursive] [--] [...] + or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference ] [--merge] [--recursive] [--] [...] or: $dashless [--quiet] summary [--cached|--files] [--summary-limit ] [commit] [--] [...] or: $dashless [--quiet] foreach [--recursive] or: $dashless [--quiet] sync [--] [...]" @@@ -34,7 -34,7 +34,7 @@@ resolve_relative_url ( { remote=$(get_default_remote) remoteurl=$(git config "remote.$remote.url") || - die "remote ($remote) does not have a url defined in .git/config" + remoteurl=$(pwd) # the repository is its own authoritative upstream url="$1" remoteurl=${remoteurl%/} sep=/ @@@ -238,14 -238,7 +238,6 @@@ cmd_add( die "'$path' already exists and is not a valid git repo" fi - case "$repo" in - ./*|../*) - url=$(resolve_relative_url "$repo") || exit - ;; - *) - url="$repo" - ;; - esac - git config submodule."$path".url "$realrepo" else module_clone "$path" "$realrepo" "$reference" || exit @@@ -259,7 -252,6 +251,7 @@@ esac ) || die "Unable to checkout submodule '$path'" fi - git config submodule."$path".url "$url" ++ git config submodule."$path".url "$realrepo" git add $force "$path" || die "Failed to add submodule '$path'" @@@ -300,10 -292,6 +292,10 @@@ cmd_foreach( toplevel=$(pwd) + # dup stdin so that it can be restored when running the external + # command in the subshell (and a recursive call to this function) + exec 3<&0 + module_list | while read mode sha1 stage path do @@@ -320,7 -308,7 +312,7 @@@ then cmd_foreach "--recursive" "$@" fi - ) || + ) <&3 3<&- || die "Stopping at '$path'; script returned non-zero status." fi done @@@ -359,26 -347,25 +351,26 @@@ cmd_init( do # Skip already registered paths name=$(module_name "$path") || exit - url=$(git config submodule."$name".url) - test -z "$url" || continue - - url=$(git config -f .gitmodules submodule."$name".url) - test -z "$url" && - die "No url found for submodule path '$path' in .gitmodules" - - # Possibly a url relative to parent - case "$url" in - ./*|../*) - url=$(resolve_relative_url "$url") || exit - ;; - esac - - git config submodule."$name".url "$url" || - die "Failed to register url for submodule path '$path'" + if test -z "$(git config "submodule.$name.url")" + then + url=$(git config -f .gitmodules submodule."$name".url) + test -z "$url" && + die "No url found for submodule path '$path' in .gitmodules" + + # Possibly a url relative to parent + case "$url" in + ./*|../*) + url=$(resolve_relative_url "$url") || exit + ;; + esac + git config submodule."$name".url "$url" || + die "Failed to register url for submodule path '$path'" + fi + # Copy "update" setting when it is not set yet upd="$(git config -f .gitmodules submodule."$name".update)" test -z "$upd" || + test -n "$(git config submodule."$name".update)" || git config submodule."$name".update "$upd" || die "Failed to register update mode for submodule path '$path'" @@@ -407,9 -394,6 +399,9 @@@ cmd_update( -N|--no-fetch) nofetch=1 ;; + -f|--force) + force=$1 + ;; -r|--rebase) update="rebase" ;; @@@ -488,11 -472,10 +480,11 @@@ if test "$subsha1" != "$sha1" then - force= - if test -z "$subsha1" + subforce=$force + # If we don't already have a -f flag and the submodule has never been checked out + if test -z "$subsha1" -a -z "$force" then - force="-f" + subforce="-f" fi if test -z "$nofetch" @@@ -524,7 -507,7 +516,7 @@@ msg="merged in" ;; *) - command="git checkout $force -q" + command="git checkout $subforce -q" action="checkout" msg="checked out" ;; @@@ -879,20 -862,17 +871,20 @@@ cmd_sync( ;; esac - say "Synchronizing submodule url for '$name'" - git config submodule."$name".url "$url" - - if test -e "$path"/.git + if git config "submodule.$name.url" >/dev/null 2>/dev/null then - ( - clear_local_git_env - cd "$path" - remote=$(get_default_remote) - git config remote."$remote".url "$url" - ) + say "Synchronizing submodule url for '$name'" + git config submodule."$name".url "$url" + + if test -e "$path"/.git + then + ( + clear_local_git_env + cd "$path" + remote=$(get_default_remote) + git config remote."$remote".url "$url" + ) + fi fi done } diff --combined t/t7400-submodule-basic.sh index 3f115d9ca9,9099e80e9e..662fe91295 --- a/t/t7400-submodule-basic.sh +++ b/t/t7400-submodule-basic.sh @@@ -47,10 -47,8 +47,10 @@@ test_expect_success 'setup - repositor ' # The 'submodule add' tests need some repository to add as a submodule. -# The trash directory is a good one as any. -submodurl=$TRASH_DIRECTORY +# The trash directory is a good one as any. We need to canonicalize +# the name, though, as some tests compare it to the absolute path git +# generates, which will expand symbolic links. +submodurl=$(pwd -P) listbranches() { git for-each-ref --format='%(refname)' 'refs/heads/*' @@@ -448,6 -446,16 +448,16 @@@ test_expect_success 'add should fail wh ) ' + test_expect_success 'use superproject as upstream when path is relative and no url is set there' ' + ( + cd addtest && + git submodule add ../repo relative && + test "$(git config -f .gitmodules submodule.relative.url)" = ../repo && + git submodule sync relative && + test "$(git config submodule.relative.url)" = "$submodurl/repo" + ) + ' + test_expect_success 'set up for relative path tests' ' mkdir reltest && (