Code

Merge branch 'jl/submodule-add-relurl-wo-upstream' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 16 Aug 2011 19:41:13 +0000 (12:41 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 16 Aug 2011 19:41:13 +0000 (12:41 -0700)
* 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

1  2 
Documentation/git-submodule.txt
git-submodule.sh
t/t7400-submodule-basic.sh

index 2b31d5f1f2c63cdd9286eed8090ce27724e5d0bf,f72e900d238311a68152d10003081a1963e1c2dc..585f03681b7aca236679a3f3d507f02c6736fdf9
@@@ -78,7 -78,9 +78,9 @@@ to exist in the superproject. If <path
  <repository> 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.
  +
  <path> is the relative location for the cloned submodule to
  exist in the superproject. If <path> 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 20c9bec9709749239849e5e91df2a9ac40c77eae,32cb05443a851e7bd77d86b3bb3b2f7962c4d53b..f8ea3bf6f2bc4310fd0cfcf212f89c1e06ee8f33
@@@ -8,7 -8,7 +8,7 @@@ dashless=$(basename "$0" | sed -e 's/-
  USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
     or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
     or: $dashless [--quiet] init [--] [<path>...]
 -   or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
 +   or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
     or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
     or: $dashless [--quiet] foreach [--recursive] <command>
     or: $dashless [--quiet] sync [--] [<path>...]"
@@@ -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
                        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
                                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"
                        ;;
  
                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"
                                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
  }
index 3f115d9ca966879ccd7d587911d7d54bc88c08c2,9099e80e9e2bde606ca481dd4a0a9cc977c8d38e..662fe91295262976c2a541d296194d3d2e9dcc5f
@@@ -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 &&
        (