Merge branch 'jl/submodule-add-relurl-wo-upstream'
authorJunio C Hamano <gitster@pobox.com>
Fri, 22 Jul 2011 21:24:35 +0000 (14:24 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 22 Jul 2011 21:24:35 +0000 (14:24 -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 acb9002fe432c6849bd894567d2272bfbe8efdbc,f72e900d238311a68152d10003081a1963e1c2dc..0ec85742ddc1c360d066659fb562898a259e48f7
@@@ -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,14 -169,12 +169,14 @@@ 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.
  +
  "git submodule sync" synchronizes all submodules while
 -"git submodule sync -- A" synchronizes submodule "A" only.
 +"git submodule sync \-- A" synchronizes submodule "A" only.
  
  OPTIONS
  -------
  
  -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 3daa0c834cad1ed402376adf535c73e1c06172e4,32cb05443a851e7bd77d86b3bb3b2f7962c4d53b..bc1d3fa663a5eb39dd96f6ad2ca17437073ceb0a
@@@ -8,13 -8,12 +8,13 @@@ 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>...]"
  OPTIONS_SPEC=
  . git-sh-setup
 +. git-sh-i18n
  . git-parse-remote
  require_work_tree
  
@@@ -35,7 -34,7 +35,7 @@@ resolve_relative_url (
  {
        remote=$(get_default_remote)
        remoteurl=$(git config "remote.$remote.url") ||
-               die "$(eval_gettext "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=/
@@@ -53,7 -52,7 +53,7 @@@
                                sep=:
                                ;;
                        *)
 -                              die "cannot strip one component off url '$remoteurl'"
 +                              die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
                                ;;
                        esac
                        ;;
@@@ -105,7 -104,7 +105,7 @@@ module_name(
        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
         test -z "$name" &&
 -       die "No submodule mapping found in .gitmodules for path '$path'"
 +       die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
         echo "$name"
  }
  
@@@ -129,7 -128,7 +129,7 @@@ module_clone(
        else
                git-clone -n "$url" "$path"
        fi ||
 -      die "Clone of '$url' into submodule path '$path' failed"
 +      die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
  }
  
  #
@@@ -202,7 -201,7 +202,7 @@@ cmd_add(
                realrepo=$repo
                ;;
        *)
 -              die "repo URL: '$repo' must be absolute or begin with ./|../"
 +              die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
        ;;
        esac
  
                        s|/*$||
                ')
        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 -      die "'$path' already exists in the index"
 +      die "$(eval_gettext "'\$path' already exists in the index")"
  
        if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
        then
 -              echo >&2 "The following path is ignored by one of your .gitignore files:" &&
 -              echo >&2 $path &&
 -              echo >&2 "Use -f if you really want to add it."
 +              (
 +                      eval_gettext "The following path is ignored by one of your .gitignore files:
 +\$path
 +Use -f if you really want to add it." &&
 +                      echo
 +              ) >&2
                exit 1
        fi
  
        then
                if test -d "$path"/.git -o -f "$path"/.git
                then
 -                      echo "Adding existing repo at '$path' to the index"
 +                      eval_gettext "Adding existing repo at '\$path' to the index"; echo
                else
 -                      die "'$path' already exists and is not a valid git repo"
 +                      die "$(eval_gettext "'\$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
                        '') git checkout -f -q ;;
                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
                        esac
 -              ) || die "Unable to checkout submodule '$path'"
 +              ) || die "$(eval_gettext "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'"
 +      die "$(eval_gettext "Failed to add submodule '\$path'")"
  
        git config -f .gitmodules submodule."$path".path "$path" &&
        git config -f .gitmodules submodule."$path".url "$repo" &&
        git add --force .gitmodules ||
 -      die "Failed to register submodule '$path'"
 +      die "$(eval_gettext "Failed to register submodule '\$path'")"
  }
  
  #
@@@ -304,16 -292,12 +296,16 @@@ 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
                if test -e "$path"/.git
                then
 -                      say "Entering '$prefix$path'"
 +                      say "$(eval_gettext "Entering '\$prefix\$path'")"
                        name=$(module_name "$path")
                        (
                                prefix="$prefix$path/"
                                then
                                        cmd_foreach "--recursive" "$@"
                                fi
 -                      ) ||
 -                      die "Stopping at '$path'; script returned non-zero status."
 +                      ) <&3 3<&- ||
 +                      die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
                fi
        done
  }
@@@ -363,30 -347,29 +355,30 @@@ 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 "$(eval_gettext "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 "$(eval_gettext "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'"
 +              die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
  
 -              say "Submodule '$name' ($url) registered for path '$path'"
 +              say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
        done
  }
  
@@@ -411,9 -394,6 +403,9 @@@ cmd_update(
                -N|--no-fetch)
                        nofetch=1
                        ;;
 +              -f|--force)
 +                      force=$1
 +                      ;;
                -r|--rebase)
                        update="rebase"
                        ;;
        fi
  
        cloned_modules=
 -      module_list "$@" |
 +      module_list "$@" | {
 +      err=
        while read mode sha1 stage path
        do
                if test "$stage" = U
                        # Only mention uninitialized submodules when its
                        # path have been specified
                        test "$#" != "0" &&
 -                      say "Submodule path '$path' not initialized" &&
 -                      say "Maybe you want to use 'update --init'?"
 +                      say "$(eval_gettext "Submodule path '\$path' not initialized
 +Maybe you want to use 'update --init'?")"
                        continue
                fi
  
                else
                        subsha1=$(clear_local_git_env; cd "$path" &&
                                git rev-parse --verify HEAD) ||
 -                      die "Unable to find current revision in submodule path '$path'"
 +                      die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
                fi
  
                if ! test -z "$update"
  
                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"
                                (clear_local_git_env; cd "$path" &&
                                        ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
                                         test -z "$rev") || git-fetch)) ||
 -                              die "Unable to fetch in submodule path '$path'"
 +                              die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
                        fi
  
                        # Is this something we just cloned?
                                update_module= ;;
                        esac
  
 +                      must_die_on_failure=
                        case "$update_module" in
                        rebase)
                                command="git rebase"
 -                              action="rebase"
 -                              msg="rebased onto"
 +                              die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
 +                              say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
 +                              must_die_on_failure=yes
                                ;;
                        merge)
                                command="git merge"
 -                              action="merge"
 -                              msg="merged in"
 +                              die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
 +                              say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
 +                              must_die_on_failure=yes
                                ;;
                        *)
 -                              command="git checkout $force -q"
 -                              action="checkout"
 -                              msg="checked out"
 +                              command="git checkout $subforce -q"
 +                              die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
 +                              say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
                                ;;
                        esac
  
 -                      (clear_local_git_env; cd "$path" && $command "$sha1") ||
 -                      die "Unable to $action '$sha1' in submodule path '$path'"
 -                      say "Submodule path '$path': $msg '$sha1'"
 +                      if (clear_local_git_env; cd "$path" && $command "$sha1")
 +                      then
 +                              say "$say_msg"
 +                      elif test -n "$must_die_on_failure"
 +                      then
 +                              die_with_status 2 "$die_msg"
 +                      else
 +                              err="${err};$die_msg"
 +                              continue
 +                      fi
                fi
  
                if test -n "$recursive"
                then
 -                      (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
 -                      die "Failed to recurse into submodule path '$path'"
 +                      (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
 +                      res=$?
 +                      if test $res -gt 0
 +                      then
 +                              die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
 +                              if test $res -eq 1
 +                              then
 +                                      err="${err};$die_msg"
 +                                      continue
 +                              else
 +                                      die_with_status $res "$die_msg"
 +                              fi
 +                      fi
                fi
        done
 +
 +      if test -n "$err"
 +      then
 +              OIFS=$IFS
 +              IFS=';'
 +              for e in $err
 +              do
 +                      if test -n "$e"
 +                      then
 +                              echo >&2 "$e"
 +                      fi
 +              done
 +              IFS=$OIFS
 +              exit 1
 +      fi
 +      }
  }
  
  set_name_rev () {
@@@ -664,7 -605,7 +656,7 @@@ cmd_summary() 
        if [ -n "$files" ]
        then
                test -n "$cached" &&
 -              die "--cached cannot be used with --files"
 +              die "$(gettext -- "--cached cannot be used with --files")"
                diff_cmd=diff-files
                head=
        fi
                                ;; # removed
                        *)
                                # unexpected type
 -                              echo >&2 "unexpected mode $mod_dst"
 +                              (
 +                                      eval_gettext "unexpected mode \$mod_dst" &&
 +                                      echo
 +                              ) >&2
                                continue ;;
                        esac
                fi
                total_commits=
                case "$missing_src,$missing_dst" in
                t,)
 -                      errmsg="  Warn: $name doesn't contain commit $sha1_src"
 +                      errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
                        ;;
                ,t)
 -                      errmsg="  Warn: $name doesn't contain commit $sha1_dst"
 +                      errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
                        ;;
                t,t)
 -                      errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
 +                      errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
                        ;;
                *)
                        errmsg=
                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
                if test $status = T
                then
 +                      blob="$(gettext "blob")"
 +                      submodule="$(gettext "submodule")"
                        if test $mod_dst = 160000
                        then
 -                              echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
 +                              echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
                        else
 -                              echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
 +                              echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
                        fi
                else
                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
        done |
        if test -n "$for_status"; then
                if [ -n "$files" ]; then
 -                      echo "# Submodules changed but not updated:"
 +                      gettext "# Submodules changed but not updated:"; echo
                else
 -                      echo "# Submodule changes to be committed:"
 +                      gettext "# Submodule changes to be committed:"; echo
                fi
                echo "#"
                sed -e 's|^|# |' -e 's|^# $|#|'
@@@ -882,7 -818,7 +874,7 @@@ cmd_status(
                                cd "$path" &&
                                eval cmd_status "$orig_args"
                        ) ||
 -                      die "Failed to recurse into submodule path '$path'"
 +                      die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
                fi
        done
  }
@@@ -926,20 -862,17 +918,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 "$(eval_gettext "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 b2b26b72d05a392d8adb83be50c1bb9e73c4eb2d,9099e80e9e2bde606ca481dd4a0a9cc977c8d38e..5afe6ccd78f7171c6bdbb5be35d9663a618c1c73
@@@ -99,7 -99,7 +99,7 @@@ test_expect_success 'submodule add to .
                git add --force .gitignore &&
                git commit -m"Ignore everything" &&
                ! git submodule add "$submodurl" submod >actual 2>&1 &&
 -              test_cmp expect actual
 +              test_i18ncmp expect actual
        )
  '
  
@@@ -357,7 -357,7 +357,7 @@@ test_expect_success 'update --init' 
  
        git submodule update init > update.out &&
        cat update.out &&
 -      grep "not initialized" update.out &&
 +      test_i18ngrep "not initialized" update.out &&
        ! test -d init/.git &&
  
        git submodule update --init init &&
@@@ -446,6 -446,16 +446,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 &&
        (