git-pull.shon commit transport: simplify duplicating a substring in transport_get() using xmemdupz() (6b33894)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5# Fetch one or more remote refs and merge it/them into the current HEAD.
   6
   7USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff|--ff-only] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
   8LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
   9SUBDIRECTORY_OK=Yes
  10OPTIONS_SPEC=
  11. git-sh-setup
  12. git-sh-i18n
  13set_reflog_action "pull${1+ $*}"
  14require_work_tree_exists
  15cd_to_toplevel
  16
  17
  18die_conflict () {
  19    git diff-index --cached --name-status -r --ignore-submodules HEAD --
  20    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
  21        die "$(gettext "Pull is not possible because you have unmerged files.
  22Please, fix them up in the work tree, and then use 'git add/rm <file>'
  23as appropriate to mark resolution and make a commit.")"
  24    else
  25        die "$(gettext "Pull is not possible because you have unmerged files.")"
  26    fi
  27}
  28
  29die_merge () {
  30    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
  31        die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
  32Please, commit your changes before you can merge.")"
  33    else
  34        die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
  35    fi
  36}
  37
  38test -z "$(git ls-files -u)" || die_conflict
  39test -f "$GIT_DIR/MERGE_HEAD" && die_merge
  40
  41bool_or_string_config () {
  42        git config --bool "$1" 2>/dev/null || git config "$1"
  43}
  44
  45strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
  46log_arg= verbosity= progress= recurse_submodules= verify_signatures=
  47merge_args= edit= rebase_args=
  48curr_branch=$(git symbolic-ref -q HEAD)
  49curr_branch_short="${curr_branch#refs/heads/}"
  50rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
  51if test -z "$rebase"
  52then
  53        rebase=$(bool_or_string_config pull.rebase)
  54fi
  55
  56# Setup default fast-forward options via `pull.ff`
  57pull_ff=$(git config pull.ff)
  58case "$pull_ff" in
  59false)
  60        no_ff=--no-ff
  61        ;;
  62only)
  63        ff_only=--ff-only
  64        ;;
  65esac
  66
  67
  68dry_run=
  69while :
  70do
  71        case "$1" in
  72        -q|--quiet)
  73                verbosity="$verbosity -q" ;;
  74        -v|--verbose)
  75                verbosity="$verbosity -v" ;;
  76        --progress)
  77                progress=--progress ;;
  78        --no-progress)
  79                progress=--no-progress ;;
  80        -n|--no-stat|--no-summary)
  81                diffstat=--no-stat ;;
  82        --stat|--summary)
  83                diffstat=--stat ;;
  84        --log|--no-log)
  85                log_arg=$1 ;;
  86        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
  87                no_commit=--no-commit ;;
  88        --c|--co|--com|--comm|--commi|--commit)
  89                no_commit=--commit ;;
  90        -e|--edit)
  91                edit=--edit ;;
  92        --no-edit)
  93                edit=--no-edit ;;
  94        --sq|--squ|--squa|--squas|--squash)
  95                squash=--squash ;;
  96        --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
  97                squash=--no-squash ;;
  98        --ff)
  99                no_ff=--ff ;;
 100        --no-ff)
 101                no_ff=--no-ff ;;
 102        --ff-only)
 103                ff_only=--ff-only ;;
 104        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
 105                --strateg=*|--strategy=*|\
 106        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
 107                case "$#,$1" in
 108                *,*=*)
 109                        strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
 110                1,*)
 111                        usage ;;
 112                *)
 113                        strategy="$2"
 114                        shift ;;
 115                esac
 116                strategy_args="${strategy_args}-s $strategy "
 117                ;;
 118        -X*)
 119                case "$#,$1" in
 120                1,-X)
 121                        usage ;;
 122                *,-X)
 123                        xx="-X $(git rev-parse --sq-quote "$2")"
 124                        shift ;;
 125                *,*)
 126                        xx=$(git rev-parse --sq-quote "$1") ;;
 127                esac
 128                merge_args="$merge_args$xx "
 129                ;;
 130        -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
 131                rebase="${1#*=}"
 132                ;;
 133        -r|--r|--re|--reb|--reba|--rebas|--rebase)
 134                rebase=true
 135                ;;
 136        --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
 137                rebase=false
 138                ;;
 139        --recurse-submodules)
 140                recurse_submodules=--recurse-submodules
 141                ;;
 142        --recurse-submodules=*)
 143                recurse_submodules="$1"
 144                ;;
 145        --no-recurse-submodules)
 146                recurse_submodules=--no-recurse-submodules
 147                ;;
 148        --verify-signatures)
 149                verify_signatures=--verify-signatures
 150                ;;
 151        --no-verify-signatures)
 152                verify_signatures=--no-verify-signatures
 153                ;;
 154        --gpg-sign|-S)
 155                gpg_sign_args=-S
 156                ;;
 157        --gpg-sign=*)
 158                gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
 159                ;;
 160        -S*)
 161                gpg_sign_args=$(git rev-parse --sq-quote "$1")
 162                ;;
 163        --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
 164                dry_run=--dry-run
 165                ;;
 166        -h|--help-all)
 167                usage
 168                ;;
 169        *)
 170                # Pass thru anything that may be meant for fetch.
 171                break
 172                ;;
 173        esac
 174        shift
 175done
 176
 177case "$rebase" in
 178preserve)
 179        rebase=true
 180        rebase_args=--preserve-merges
 181        ;;
 182true|false|'')
 183        ;;
 184*)
 185        echo "Invalid value for --rebase, should be true, false, or preserve"
 186        usage
 187        exit 1
 188        ;;
 189esac
 190
 191error_on_no_merge_candidates () {
 192        exec >&2
 193        for opt
 194        do
 195                case "$opt" in
 196                -t|--t|--ta|--tag|--tags)
 197                        echo "It doesn't make sense to pull all tags; you probably meant:"
 198                        echo "  git fetch --tags"
 199                        exit 1
 200                esac
 201        done
 202
 203        if test true = "$rebase"
 204        then
 205                op_type=rebase
 206                op_prep=against
 207        else
 208                op_type=merge
 209                op_prep=with
 210        fi
 211
 212        upstream=$(git config "branch.$curr_branch_short.merge")
 213        remote=$(git config "branch.$curr_branch_short.remote")
 214
 215        if [ $# -gt 1 ]; then
 216                if [ "$rebase" = true ]; then
 217                        printf "There is no candidate for rebasing against "
 218                else
 219                        printf "There are no candidates for merging "
 220                fi
 221                echo "among the refs that you just fetched."
 222                echo "Generally this means that you provided a wildcard refspec which had no"
 223                echo "matches on the remote end."
 224        elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
 225                echo "You asked to pull from the remote '$1', but did not specify"
 226                echo "a branch. Because this is not the default configured remote"
 227                echo "for your current branch, you must specify a branch on the command line."
 228        elif [ -z "$curr_branch" -o -z "$upstream" ]; then
 229                . git-parse-remote
 230                error_on_missing_default_upstream "pull" $op_type $op_prep \
 231                        "git pull <remote> <branch>"
 232        else
 233                echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
 234                echo "from the remote, but no such ref was fetched."
 235        fi
 236        exit 1
 237}
 238
 239test true = "$rebase" && {
 240        if ! git rev-parse -q --verify HEAD >/dev/null
 241        then
 242                # On an unborn branch
 243                if test -f "$GIT_DIR/index"
 244                then
 245                        die "$(gettext "updating an unborn branch with changes added to the index")"
 246                fi
 247        else
 248                require_clean_work_tree "pull with rebase" "Please commit or stash them."
 249        fi
 250        oldremoteref= &&
 251        test -n "$curr_branch" &&
 252        . git-parse-remote &&
 253        remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
 254        oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
 255}
 256orig_head=$(git rev-parse -q --verify HEAD)
 257git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
 258test -z "$dry_run" || exit 0
 259
 260curr_head=$(git rev-parse -q --verify HEAD)
 261if test -n "$orig_head" && test "$curr_head" != "$orig_head"
 262then
 263        # The fetch involved updating the current branch.
 264
 265        # The working tree and the index file is still based on the
 266        # $orig_head commit, but we are merging into $curr_head.
 267        # First update the working tree to match $curr_head.
 268
 269        eval_gettextln "Warning: fetch updated the current branch head.
 270Warning: fast-forwarding your working tree from
 271Warning: commit \$orig_head." >&2
 272        git update-index -q --refresh
 273        git read-tree -u -m "$orig_head" "$curr_head" ||
 274                die "$(eval_gettext "Cannot fast-forward your working tree.
 275After making sure that you saved anything precious from
 276$ git diff \$orig_head
 277output, run
 278$ git reset --hard
 279to recover.")"
 280
 281fi
 282
 283merge_head=$(sed -e '/  not-for-merge   /d' \
 284        -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
 285        tr '\012' ' ')
 286
 287case "$merge_head" in
 288'')
 289        error_on_no_merge_candidates "$@"
 290        ;;
 291?*' '?*)
 292        if test -z "$orig_head"
 293        then
 294                die "$(gettext "Cannot merge multiple branches into empty head")"
 295        fi
 296        if test true = "$rebase"
 297        then
 298                die "$(gettext "Cannot rebase onto multiple branches")"
 299        fi
 300        ;;
 301esac
 302
 303# Pulling into unborn branch: a shorthand for branching off
 304# FETCH_HEAD, for lazy typers.
 305if test -z "$orig_head"
 306then
 307        # Two-way merge: we claim the index is based on an empty tree,
 308        # and try to fast-forward to HEAD.  This ensures we will not
 309        # lose index/worktree changes that the user already made on
 310        # the unborn branch.
 311        empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 312        git read-tree -m -u $empty_tree $merge_head &&
 313        git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
 314        exit
 315fi
 316
 317if test true = "$rebase"
 318then
 319        o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
 320        if test "$oldremoteref" = "$o"
 321        then
 322                unset oldremoteref
 323        fi
 324fi
 325
 326merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
 327case "$rebase" in
 328true)
 329        eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
 330        eval="$eval $gpg_sign_args"
 331        eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
 332        ;;
 333*)
 334        eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
 335        eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
 336        eval="$eval $gpg_sign_args"
 337        eval="$eval \"\$merge_name\" HEAD $merge_head"
 338        ;;
 339esac
 340eval "exec $eval"