contrib / examples / git-fetch.shon commit traverse_trees(): handle D/F conflict case sanely (1ee2657)
   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
  29quiet=
  30while test $# != 0
  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="--upload-pack=$1"
  40                ;;
  41        --upl=*|--uplo=*|--uploa=*|--upload=*|\
  42        --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
  43                exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
  44                shift
  45                ;;
  46        -f|--f|--fo|--for|--forc|--force)
  47                force=t
  48                ;;
  49        -t|--t|--ta|--tag|--tags)
  50                tags=t
  51                ;;
  52        -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
  53                no_tags=t
  54                ;;
  55        -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
  56        --update-he|--update-hea|--update-head|--update-head-|\
  57        --update-head-o|--update-head-ok)
  58                update_head_ok=t
  59                ;;
  60        -q|--q|--qu|--qui|--quie|--quiet)
  61                quiet=--quiet
  62                ;;
  63        -v|--verbose)
  64                verbose="$verbose"Yes
  65                ;;
  66        -k|--k|--ke|--kee|--keep)
  67                keep='-k -k'
  68                ;;
  69        --depth=*)
  70                shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
  71                ;;
  72        --depth)
  73                shift
  74                shallow_depth="--depth=$1"
  75                ;;
  76        -*)
  77                usage
  78                ;;
  79        *)
  80                break
  81                ;;
  82        esac
  83        shift
  84done
  85
  86case "$#" in
  870)
  88        origin=$(get_default_remote)
  89        test -n "$(get_remote_url ${origin})" ||
  90                die "Where do you want to fetch from today?"
  91        set x $origin ; shift ;;
  92esac
  93
  94if test -z "$exec"
  95then
  96        # No command line override and we have configuration for the remote.
  97        exec="--upload-pack=$(get_uploadpack $1)"
  98fi
  99
 100remote_nick="$1"
 101remote=$(get_remote_url "$@")
 102refs=
 103rref=
 104rsync_slurped_objects=
 105
 106if test "" = "$append"
 107then
 108        : >"$GIT_DIR/FETCH_HEAD"
 109fi
 110
 111# Global that is reused later
 112ls_remote_result=$(git ls-remote $exec "$remote") ||
 113        die "Cannot get the repository state from $remote"
 114
 115append_fetch_head () {
 116        flags=
 117        test -n "$verbose" && flags="$flags$LF-v"
 118        test -n "$force$single_force" && flags="$flags$LF-f"
 119        GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
 120                git fetch--tool $flags append-fetch-head "$@"
 121}
 122
 123# updating the current HEAD with git-fetch in a bare
 124# repository is always fine.
 125if test -z "$update_head_ok" && test $(is_bare_repository) = false
 126then
 127        orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
 128fi
 129
 130# Allow --notags from remote.$1.tagopt
 131case "$tags$no_tags" in
 132'')
 133        case "$(git config --get "remote.$1.tagopt")" in
 134        --no-tags)
 135                no_tags=t ;;
 136        esac
 137esac
 138
 139# If --tags (and later --heads or --all) is specified, then we are
 140# not talking about defaults stored in Pull: line of remotes or
 141# branches file, and just fetch those and refspecs explicitly given.
 142# Otherwise we do what we always did.
 143
 144reflist=$(get_remote_refs_for_fetch "$@")
 145if test "$tags"
 146then
 147        taglist=`IFS='  ' &&
 148                  echo "$ls_remote_result" |
 149                  git show-ref --exclude-existing=refs/tags/ |
 150                  while read sha1 name
 151                  do
 152                        echo ".${name}:${name}"
 153                  done` || exit
 154        if test "$#" -gt 1
 155        then
 156                # remote URL plus explicit refspecs; we need to merge them.
 157                reflist="$reflist$LF$taglist"
 158        else
 159                # No explicit refspecs; fetch tags only.
 160                reflist=$taglist
 161        fi
 162fi
 163
 164fetch_all_at_once () {
 165
 166  eval=$(echo "$1" | git fetch--tool parse-reflist "-")
 167  eval "$eval"
 168
 169    ( : subshell because we muck with IFS
 170      IFS="     $LF"
 171      (
 172        if test "$remote" = . ; then
 173            git show-ref $rref || echo failed "$remote"
 174        elif test -f "$remote" ; then
 175            test -n "$shallow_depth" &&
 176                die "shallow clone with bundle is not supported"
 177            git bundle unbundle "$remote" $rref ||
 178            echo failed "$remote"
 179        else
 180                if      test -d "$remote" &&
 181
 182                        # The remote might be our alternate.  With
 183                        # this optimization we will bypass fetch-pack
 184                        # altogether, which means we cannot be doing
 185                        # the shallow stuff at all.
 186                        test ! -f "$GIT_DIR/shallow" &&
 187                        test -z "$shallow_depth" &&
 188
 189                        # See if all of what we are going to fetch are
 190                        # connected to our repository's tips, in which
 191                        # case we do not have to do any fetch.
 192                        theirs=$(echo "$ls_remote_result" | \
 193                                git fetch--tool -s pick-rref "$rref" "-") &&
 194
 195                        # This will barf when $theirs reach an object that
 196                        # we do not have in our repository.  Otherwise,
 197                        # we already have everything the fetch would bring in.
 198                        git rev-list --objects $theirs --not --all \
 199                                >/dev/null 2>/dev/null
 200                then
 201                        echo "$ls_remote_result" | \
 202                                git fetch--tool pick-rref "$rref" "-"
 203                else
 204                        flags=
 205                        case $verbose in
 206                        YesYes*)
 207                            flags="-v"
 208                            ;;
 209                        esac
 210                        git-fetch-pack --thin $exec $keep $shallow_depth \
 211                                $quiet $no_progress $flags "$remote" $rref ||
 212                        echo failed "$remote"
 213                fi
 214        fi
 215      ) |
 216      (
 217        flags=
 218        test -n "$verbose" && flags="$flags -v"
 219        test -n "$force" && flags="$flags -f"
 220        GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
 221                git fetch--tool $flags native-store \
 222                        "$remote" "$remote_nick" "$refs"
 223      )
 224    ) || exit
 225
 226}
 227
 228fetch_per_ref () {
 229  reflist="$1"
 230  refs=
 231  rref=
 232
 233  for ref in $reflist
 234  do
 235      refs="$refs$LF$ref"
 236
 237      # These are relative path from $GIT_DIR, typically starting at refs/
 238      # but may be HEAD
 239      if expr "z$ref" : 'z\.' >/dev/null
 240      then
 241          not_for_merge=t
 242          ref=$(expr "z$ref" : 'z\.\(.*\)')
 243      else
 244          not_for_merge=
 245      fi
 246      if expr "z$ref" : 'z+' >/dev/null
 247      then
 248          single_force=t
 249          ref=$(expr "z$ref" : 'z+\(.*\)')
 250      else
 251          single_force=
 252      fi
 253      remote_name=$(expr "z$ref" : 'z\([^:]*\):')
 254      local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
 255
 256      rref="$rref$LF$remote_name"
 257
 258      # There are transports that can fetch only one head at a time...
 259      case "$remote" in
 260      http://* | https://* | ftp://*)
 261          test -n "$shallow_depth" &&
 262                die "shallow clone with http not supported"
 263          proto=`expr "$remote" : '\([^:]*\):'`
 264          if [ -n "$GIT_SSL_NO_VERIFY" ]; then
 265              curl_extra_args="-k"
 266          fi
 267          if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
 268                "`git config --bool http.noEPSV`" = true ]; then
 269              noepsv_opt="--disable-epsv"
 270          fi
 271
 272          # Find $remote_name from ls-remote output.
 273          head=$(echo "$ls_remote_result" | \
 274                git fetch--tool -s pick-rref "$remote_name" "-")
 275          expr "z$head" : "z$_x40\$" >/dev/null ||
 276                die "No such ref $remote_name at $remote"
 277          echo >&2 "Fetching $remote_name from $remote using $proto"
 278          case "$quiet" in '') v=-v ;; *) v= ;; esac
 279          git-http-fetch $v -a "$head" "$remote" || exit
 280          ;;
 281      rsync://*)
 282          test -n "$shallow_depth" &&
 283                die "shallow clone with rsync not supported"
 284          TMP_HEAD="$GIT_DIR/TMP_HEAD"
 285          rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
 286          head=$(git rev-parse --verify TMP_HEAD)
 287          rm -f "$TMP_HEAD"
 288          case "$quiet" in '') v=-v ;; *) v= ;; esac
 289          test "$rsync_slurped_objects" || {
 290              rsync -a $v --ignore-existing --exclude info \
 291                  "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
 292
 293              # Look at objects/info/alternates for rsync -- http will
 294              # support it natively and git native ones will do it on
 295              # the remote end.  Not having that file is not a crime.
 296              rsync -q "$remote/objects/info/alternates" \
 297                  "$GIT_DIR/TMP_ALT" 2>/dev/null ||
 298                  rm -f "$GIT_DIR/TMP_ALT"
 299              if test -f "$GIT_DIR/TMP_ALT"
 300              then
 301                  resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
 302                  while read alt
 303                  do
 304                      case "$alt" in 'bad alternate: '*) die "$alt";; esac
 305                      echo >&2 "Getting alternate: $alt"
 306                      rsync -av --ignore-existing --exclude info \
 307                      "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
 308                  done
 309                  rm -f "$GIT_DIR/TMP_ALT"
 310              fi
 311              rsync_slurped_objects=t
 312          }
 313          ;;
 314      esac
 315
 316      append_fetch_head "$head" "$remote" \
 317          "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
 318
 319  done
 320
 321}
 322
 323fetch_main () {
 324        case "$remote" in
 325        http://* | https://* | ftp://* | rsync://* )
 326                fetch_per_ref "$@"
 327                ;;
 328        *)
 329                fetch_all_at_once "$@"
 330                ;;
 331        esac
 332}
 333
 334fetch_main "$reflist" || exit
 335
 336# automated tag following
 337case "$no_tags$tags" in
 338'')
 339        case "$reflist" in
 340        *:refs/*)
 341                # effective only when we are following remote branch
 342                # using local tracking branch.
 343                taglist=$(IFS=' ' &&
 344                echo "$ls_remote_result" |
 345                git show-ref --exclude-existing=refs/tags/ |
 346                while read sha1 name
 347                do
 348                        git cat-file -t "$sha1" >/dev/null 2>&1 || continue
 349                        echo >&2 "Auto-following $name"
 350                        echo ".${name}:${name}"
 351                done)
 352        esac
 353        case "$taglist" in
 354        '') ;;
 355        ?*)
 356                # do not deepen a shallow tree when following tags
 357                shallow_depth=
 358                fetch_main "$taglist" || exit ;;
 359        esac
 360esac
 361
 362# If the original head was empty (i.e. no "master" yet), or
 363# if we were told not to worry, we do not have to check.
 364case "$orig_head" in
 365'')
 366        ;;
 367?*)
 368        curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
 369        if test "$curr_head" != "$orig_head"
 370        then
 371            git update-ref \
 372                        -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
 373                        HEAD "$orig_head"
 374                die "Cannot fetch into the current branch."
 375        fi
 376        ;;
 377esac