git-fetch.shon commit make git a bit less cryptic on fetch errors (b3d9899)
   1#!/bin/sh
   2#
   3
   4USAGE='<fetch-options> <repository> <refspec>...'
   5SUBDIRECTORY_OK=Yes
   6. git-sh-setup
   7TOP=$(git-rev-parse --show-cdup)
   8if test ! -z "$TOP"
   9then
  10        cd "$TOP"
  11fi
  12. git-parse-remote
  13_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
  14_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
  15
  16LF='
  17'
  18IFS="$LF"
  19
  20rloga=fetch
  21no_tags=
  22tags=
  23append=
  24force=
  25verbose=
  26update_head_ok=
  27exec=
  28upload_pack=
  29keep=
  30while case "$#" in 0) break ;; esac
  31do
  32        case "$1" in
  33        -a|--a|--ap|--app|--appe|--appen|--append)
  34                append=t
  35                ;;
  36        --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
  37        --upload-pa|--upload-pac|--upload-pack)
  38                shift
  39                exec="--exec=$1" 
  40                upload_pack="-u $1"
  41                ;;
  42        -f|--f|--fo|--for|--forc|--force)
  43                force=t
  44                ;;
  45        -t|--t|--ta|--tag|--tags)
  46                tags=t
  47                ;;
  48        -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
  49                no_tags=t
  50                ;;
  51        -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
  52        --update-he|--update-hea|--update-head|--update-head-|\
  53        --update-head-o|--update-head-ok)
  54                update_head_ok=t
  55                ;;
  56        -v|--verbose)
  57                verbose=Yes
  58                ;;
  59        -k|--k|--ke|--kee|--keep)
  60                keep='-k -k'
  61                ;;
  62        --reflog-action=*)
  63                rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
  64                ;;
  65        -*)
  66                usage
  67                ;;
  68        *)
  69                break
  70                ;;
  71        esac
  72        shift
  73done
  74
  75case "$#" in
  760)
  77        origin=$(get_default_remote)
  78        test -n "$(get_remote_url ${origin})" ||
  79                die "Where do you want to fetch from today?"
  80        set x $origin ; shift ;;
  81esac
  82
  83remote_nick="$1"
  84remote=$(get_remote_url "$@")
  85refs=
  86rref=
  87rsync_slurped_objects=
  88
  89rloga="$rloga $remote_nick"
  90test "$remote_nick" = "$remote" || rloga="$rloga $remote"
  91
  92if test "" = "$append"
  93then
  94        : >"$GIT_DIR/FETCH_HEAD"
  95fi
  96
  97# Global that is reused later
  98ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
  99        die "Cannot get the repository state from $remote"
 100
 101append_fetch_head () {
 102    head_="$1"
 103    remote_="$2"
 104    remote_name_="$3"
 105    remote_nick_="$4"
 106    local_name_="$5"
 107    case "$6" in
 108    t) not_for_merge_='not-for-merge' ;;
 109    '') not_for_merge_= ;;
 110    esac
 111
 112    # remote-nick is the URL given on the command line (or a shorthand)
 113    # remote-name is the $GIT_DIR relative refs/ path we computed
 114    # for this refspec.
 115
 116    # the $note_ variable will be fed to git-fmt-merge-msg for further
 117    # processing.
 118    case "$remote_name_" in
 119    HEAD)
 120        note_= ;;
 121    refs/heads/*)
 122        note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
 123        note_="branch '$note_' of " ;;
 124    refs/tags/*)
 125        note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
 126        note_="tag '$note_' of " ;;
 127    refs/remotes/*)
 128        note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
 129        note_="remote branch '$note_' of " ;;
 130    *)
 131        note_="$remote_name of " ;;
 132    esac
 133    remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
 134        remote_="$remote_1_"
 135    note_="$note_$remote_"
 136
 137    # 2.6.11-tree tag would not be happy to be fed to resolve.
 138    if git-cat-file commit "$head_" >/dev/null 2>&1
 139    then
 140        headc_=$(git-rev-parse --verify "$head_^0") || exit
 141        echo "$headc_   $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
 142    else
 143        echo "$head_    not-for-merge   $note_" >>"$GIT_DIR/FETCH_HEAD"
 144    fi
 145
 146    update_local_ref "$local_name_" "$head_" "$note_"
 147}
 148
 149update_local_ref () {
 150    # If we are storing the head locally make sure that it is
 151    # a fast forward (aka "reverse push").
 152
 153    label_=$(git-cat-file -t $2)
 154    newshort_=$(git-rev-parse --short $2)
 155    if test -z "$1" ; then
 156        [ "$verbose" ] && echo >&2 "* fetched $3"
 157        [ "$verbose" ] && echo >&2 "  $label_: $newshort_"
 158        return 0
 159    fi
 160    oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
 161
 162    case "$1" in
 163    refs/tags/*)
 164        # Tags need not be pointing at commits so there
 165        # is no way to guarantee "fast-forward" anyway.
 166        if test -n "$oldshort_"
 167        then
 168                if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
 169                then
 170                        [ "$verbose" ] && echo >&2 "* $1: same as $3"
 171                        [ "$verbose" ] && echo >&2 "  $label_: $newshort_" ||:
 172                else
 173                        echo >&2 "* $1: updating with $3"
 174                        echo >&2 "  $label_: $newshort_"
 175                        git-update-ref -m "$rloga: updating tag" "$1" "$2"
 176                fi
 177        else
 178                echo >&2 "* $1: storing $3"
 179                echo >&2 "  $label_: $newshort_"
 180                git-update-ref -m "$rloga: storing tag" "$1" "$2"
 181        fi
 182        ;;
 183
 184    refs/heads/* | refs/remotes/*)
 185        # $1 is the ref being updated.
 186        # $2 is the new value for the ref.
 187        local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
 188        if test "$local"
 189        then
 190            # Require fast-forward.
 191            mb=$(git-merge-base "$local" "$2") &&
 192            case "$2,$mb" in
 193            $local,*)
 194                if test -n "$verbose"
 195                then
 196                        echo >&2 "* $1: same as $3"
 197                        echo >&2 "  $label_: $newshort_"
 198                fi
 199                ;;
 200            *,$local)
 201                echo >&2 "* $1: fast forward to $3"
 202                echo >&2 "  old..new: $oldshort_..$newshort_"
 203                git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
 204                ;;
 205            *)
 206                false
 207                ;;
 208            esac || {
 209                case ",$force,$single_force," in
 210                *,t,*)
 211                        echo >&2 "* $1: forcing update to non-fast forward $3"
 212                        echo >&2 "  old...new: $oldshort_...$newshort_"
 213                        git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
 214                        ;;
 215                *)
 216                        echo >&2 "* $1: not updating to non-fast forward $3"
 217                        echo >&2 "  old...new: $oldshort_...$newshort_"
 218                        exit 1
 219                        ;;
 220                esac
 221            }
 222        else
 223            echo >&2 "* $1: storing $3"
 224            echo >&2 "  $label_: $newshort_"
 225            git-update-ref -m "$rloga: storing head" "$1" "$2"
 226        fi
 227        ;;
 228    esac
 229}
 230
 231case "$update_head_ok" in
 232'')
 233        orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
 234        ;;
 235esac
 236
 237# If --tags (and later --heads or --all) is specified, then we are
 238# not talking about defaults stored in Pull: line of remotes or
 239# branches file, and just fetch those and refspecs explicitly given.
 240# Otherwise we do what we always did.
 241
 242reflist=$(get_remote_refs_for_fetch "$@")
 243if test "$tags"
 244then
 245        taglist=`IFS='  ' &&
 246                  echo "$ls_remote_result" |
 247                  while read sha1 name
 248                  do
 249                        case "$sha1" in
 250                        fail)
 251                                exit 1
 252                        esac
 253                        case "$name" in
 254                        *^*) continue ;;
 255                        refs/tags/*) ;;
 256                        *) continue ;;
 257                        esac
 258                        if git-check-ref-format "$name"
 259                        then
 260                            echo ".${name}:${name}"
 261                        else
 262                            echo >&2 "warning: tag ${name} ignored"
 263                        fi
 264                  done` || exit
 265        if test "$#" -gt 1
 266        then
 267                # remote URL plus explicit refspecs; we need to merge them.
 268                reflist="$reflist$LF$taglist"
 269        else
 270                # No explicit refspecs; fetch tags only.
 271                reflist=$taglist
 272        fi
 273fi
 274
 275fetch_main () {
 276  reflist="$1"
 277  refs=
 278  rref=
 279
 280  for ref in $reflist
 281  do
 282      refs="$refs$LF$ref"
 283
 284      # These are relative path from $GIT_DIR, typically starting at refs/
 285      # but may be HEAD
 286      if expr "z$ref" : 'z\.' >/dev/null
 287      then
 288          not_for_merge=t
 289          ref=$(expr "z$ref" : 'z\.\(.*\)')
 290      else
 291          not_for_merge=
 292      fi
 293      if expr "z$ref" : 'z+' >/dev/null
 294      then
 295          single_force=t
 296          ref=$(expr "z$ref" : 'z+\(.*\)')
 297      else
 298          single_force=
 299      fi
 300      remote_name=$(expr "z$ref" : 'z\([^:]*\):')
 301      local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
 302
 303      rref="$rref$LF$remote_name"
 304
 305      # There are transports that can fetch only one head at a time...
 306      case "$remote" in
 307      http://* | https://* | ftp://*)
 308          proto=`expr "$remote" : '\([^:]*\):'`
 309          if [ -n "$GIT_SSL_NO_VERIFY" ]; then
 310              curl_extra_args="-k"
 311          fi
 312          if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
 313                "`git-repo-config --bool http.noEPSV`" = true ]; then
 314              noepsv_opt="--disable-epsv"
 315          fi
 316
 317          # Find $remote_name from ls-remote output.
 318          head=$(
 319                IFS='   '
 320                echo "$ls_remote_result" |
 321                while read sha1 name
 322                do
 323                        test "z$name" = "z$remote_name" || continue
 324                        echo "$sha1"
 325                        break
 326                done
 327          )
 328          expr "z$head" : "z$_x40\$" >/dev/null ||
 329                die "No such ref $remote_name at $remote"
 330          echo >&2 "Fetching $remote_name from $remote using $proto"
 331          git-http-fetch -v -a "$head" "$remote/" || exit
 332          ;;
 333      rsync://*)
 334          TMP_HEAD="$GIT_DIR/TMP_HEAD"
 335          rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
 336          head=$(git-rev-parse --verify TMP_HEAD)
 337          rm -f "$TMP_HEAD"
 338          test "$rsync_slurped_objects" || {
 339              rsync -av --ignore-existing --exclude info \
 340                  "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
 341
 342              # Look at objects/info/alternates for rsync -- http will
 343              # support it natively and git native ones will do it on
 344              # the remote end.  Not having that file is not a crime.
 345              rsync -q "$remote/objects/info/alternates" \
 346                  "$GIT_DIR/TMP_ALT" 2>/dev/null ||
 347                  rm -f "$GIT_DIR/TMP_ALT"
 348              if test -f "$GIT_DIR/TMP_ALT"
 349              then
 350                  resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
 351                  while read alt
 352                  do
 353                      case "$alt" in 'bad alternate: '*) die "$alt";; esac
 354                      echo >&2 "Getting alternate: $alt"
 355                      rsync -av --ignore-existing --exclude info \
 356                      "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
 357                  done
 358                  rm -f "$GIT_DIR/TMP_ALT"
 359              fi
 360              rsync_slurped_objects=t
 361          }
 362          ;;
 363      *)
 364          # We will do git native transport with just one call later.
 365          continue ;;
 366      esac
 367
 368      append_fetch_head "$head" "$remote" \
 369          "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
 370
 371  done
 372
 373  case "$remote" in
 374  http://* | https://* | ftp://* | rsync://* )
 375      ;; # we are already done.
 376  *)
 377    ( : subshell because we muck with IFS
 378      pack_lockfile=
 379      IFS="     $LF"
 380      (
 381          git-fetch-pack --thin $exec $keep "$remote" $rref || echo failed "$remote"
 382      ) |
 383      while read sha1 remote_name
 384      do
 385          case "$sha1" in
 386          failed)
 387                  echo >&2 "Fetch failure: $remote"
 388                  exit 1 ;;
 389          # special line coming from index-pack with the pack name
 390          pack)
 391                  continue ;;
 392          keep)
 393                  pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
 394                  continue ;;
 395          esac
 396          found=
 397          single_force=
 398          for ref in $refs
 399          do
 400              case "$ref" in
 401              +$remote_name:*)
 402                  single_force=t
 403                  not_for_merge=
 404                  found="$ref"
 405                  break ;;
 406              .+$remote_name:*)
 407                  single_force=t
 408                  not_for_merge=t
 409                  found="$ref"
 410                  break ;;
 411              .$remote_name:*)
 412                  not_for_merge=t
 413                  found="$ref"
 414                  break ;;
 415              $remote_name:*)
 416                  not_for_merge=
 417                  found="$ref"
 418                  break ;;
 419              esac
 420          done
 421          local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
 422          append_fetch_head "$sha1" "$remote" \
 423                  "$remote_name" "$remote_nick" "$local_name" \
 424                  "$not_for_merge" || exit
 425      done &&
 426      if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
 427    ) || exit ;;
 428  esac
 429
 430}
 431
 432fetch_main "$reflist" || exit
 433
 434# automated tag following
 435case "$no_tags$tags" in
 436'')
 437        case "$reflist" in
 438        *:refs/*)
 439                # effective only when we are following remote branch
 440                # using local tracking branch.
 441                taglist=$(IFS=' ' &&
 442                echo "$ls_remote_result" |
 443                git-show-ref --exclude-existing=refs/tags/ |
 444                while read sha1 name
 445                do
 446                        git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
 447                        echo >&2 "Auto-following $name"
 448                        echo ".${name}:${name}"
 449                done)
 450        esac
 451        case "$taglist" in
 452        '') ;;
 453        ?*)
 454                fetch_main "$taglist" || exit ;;
 455        esac
 456esac
 457
 458# If the original head was empty (i.e. no "master" yet), or
 459# if we were told not to worry, we do not have to check.
 460case "$orig_head" in
 461'')
 462        ;;
 463?*)
 464        curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
 465        if test "$curr_head" != "$orig_head"
 466        then
 467            git-update-ref \
 468                        -m "$rloga: Undoing incorrectly fetched HEAD." \
 469                        HEAD "$orig_head"
 470                die "Cannot fetch into the current branch."
 471        fi
 472        ;;
 473esac