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