git-pull.shon commit send-email: minor code refactoring (c46e27a)
   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=$(bool_or_string_config pull.ff)
  58case "$pull_ff" in
  59true)
  60        no_ff=--ff
  61        ;;
  62false)
  63        no_ff=--no-ff
  64        ;;
  65only)
  66        ff_only=--ff-only
  67        ;;
  68esac
  69
  70
  71dry_run=
  72while :
  73do
  74        case "$1" in
  75        -q|--quiet)
  76                verbosity="$verbosity -q" ;;
  77        -v|--verbose)
  78                verbosity="$verbosity -v" ;;
  79        --progress)
  80                progress=--progress ;;
  81        --no-progress)
  82                progress=--no-progress ;;
  83        -n|--no-stat|--no-summary)
  84                diffstat=--no-stat ;;
  85        --stat|--summary)
  86                diffstat=--stat ;;
  87        --log|--log=*|--no-log)
  88                log_arg="$1" ;;
  89        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
  90                no_commit=--no-commit ;;
  91        --c|--co|--com|--comm|--commi|--commit)
  92                no_commit=--commit ;;
  93        -e|--edit)
  94                edit=--edit ;;
  95        --no-edit)
  96                edit=--no-edit ;;
  97        --sq|--squ|--squa|--squas|--squash)
  98                squash=--squash ;;
  99        --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
 100                squash=--no-squash ;;
 101        --ff)
 102                no_ff=--ff ;;
 103        --no-ff)
 104                no_ff=--no-ff ;;
 105        --ff-only)
 106                ff_only=--ff-only ;;
 107        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
 108                --strateg=*|--strategy=*|\
 109        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
 110                case "$#,$1" in
 111                *,*=*)
 112                        strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
 113                1,*)
 114                        usage ;;
 115                *)
 116                        strategy="$2"
 117                        shift ;;
 118                esac
 119                strategy_args="${strategy_args}-s $strategy "
 120                ;;
 121        -X*)
 122                case "$#,$1" in
 123                1,-X)
 124                        usage ;;
 125                *,-X)
 126                        xx="-X $(git rev-parse --sq-quote "$2")"
 127                        shift ;;
 128                *,*)
 129                        xx=$(git rev-parse --sq-quote "$1") ;;
 130                esac
 131                merge_args="$merge_args$xx "
 132                ;;
 133        -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
 134                rebase="${1#*=}"
 135                ;;
 136        -r|--r|--re|--reb|--reba|--rebas|--rebase)
 137                rebase=true
 138                ;;
 139        --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
 140                rebase=false
 141                ;;
 142        --recurse-submodules)
 143                recurse_submodules=--recurse-submodules
 144                ;;
 145        --recurse-submodules=*)
 146                recurse_submodules="$1"
 147                ;;
 148        --no-recurse-submodules)
 149                recurse_submodules=--no-recurse-submodules
 150                ;;
 151        --verify-signatures)
 152                verify_signatures=--verify-signatures
 153                ;;
 154        --no-verify-signatures)
 155                verify_signatures=--no-verify-signatures
 156                ;;
 157        --gpg-sign|-S)
 158                gpg_sign_args=-S
 159                ;;
 160        --gpg-sign=*)
 161                gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
 162                ;;
 163        -S*)
 164                gpg_sign_args=$(git rev-parse --sq-quote "$1")
 165                ;;
 166        --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
 167                dry_run=--dry-run
 168                ;;
 169        -h|--help-all)
 170                usage
 171                ;;
 172        *)
 173                # Pass thru anything that may be meant for fetch.
 174                break
 175                ;;
 176        esac
 177        shift
 178done
 179
 180case "$rebase" in
 181preserve)
 182        rebase=true
 183        rebase_args=--preserve-merges
 184        ;;
 185true|false|'')
 186        ;;
 187*)
 188        echo "Invalid value for --rebase, should be true, false, or preserve"
 189        usage
 190        exit 1
 191        ;;
 192esac
 193
 194error_on_no_merge_candidates () {
 195        exec >&2
 196
 197        if test true = "$rebase"
 198        then
 199                op_type=rebase
 200                op_prep=against
 201        else
 202                op_type=merge
 203                op_prep=with
 204        fi
 205
 206        upstream=$(git config "branch.$curr_branch_short.merge")
 207        remote=$(git config "branch.$curr_branch_short.remote")
 208
 209        if [ $# -gt 1 ]; then
 210                if [ "$rebase" = true ]; then
 211                        printf "There is no candidate for rebasing against "
 212                else
 213                        printf "There are no candidates for merging "
 214                fi
 215                echo "among the refs that you just fetched."
 216                echo "Generally this means that you provided a wildcard refspec which had no"
 217                echo "matches on the remote end."
 218        elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
 219                echo "You asked to pull from the remote '$1', but did not specify"
 220                echo "a branch. Because this is not the default configured remote"
 221                echo "for your current branch, you must specify a branch on the command line."
 222        elif [ -z "$curr_branch" -o -z "$upstream" ]; then
 223                . git-parse-remote
 224                error_on_missing_default_upstream "pull" $op_type $op_prep \
 225                        "git pull <remote> <branch>"
 226        else
 227                echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
 228                echo "from the remote, but no such ref was fetched."
 229        fi
 230        exit 1
 231}
 232
 233test true = "$rebase" && {
 234        if ! git rev-parse -q --verify HEAD >/dev/null
 235        then
 236                # On an unborn branch
 237                if test -f "$(git rev-parse --git-path index)"
 238                then
 239                        die "$(gettext "updating an unborn branch with changes added to the index")"
 240                fi
 241        else
 242                require_clean_work_tree "pull with rebase" "Please commit or stash them."
 243        fi
 244        oldremoteref= &&
 245        test -n "$curr_branch" &&
 246        . git-parse-remote &&
 247        remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
 248        oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
 249}
 250orig_head=$(git rev-parse -q --verify HEAD)
 251git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
 252test -z "$dry_run" || exit 0
 253
 254curr_head=$(git rev-parse -q --verify HEAD)
 255if test -n "$orig_head" && test "$curr_head" != "$orig_head"
 256then
 257        # The fetch involved updating the current branch.
 258
 259        # The working tree and the index file is still based on the
 260        # $orig_head commit, but we are merging into $curr_head.
 261        # First update the working tree to match $curr_head.
 262
 263        eval_gettextln "Warning: fetch updated the current branch head.
 264Warning: fast-forwarding your working tree from
 265Warning: commit \$orig_head." >&2
 266        git update-index -q --refresh
 267        git read-tree -u -m "$orig_head" "$curr_head" ||
 268                die "$(eval_gettext "Cannot fast-forward your working tree.
 269After making sure that you saved anything precious from
 270$ git diff \$orig_head
 271output, run
 272$ git reset --hard
 273to recover.")"
 274
 275fi
 276
 277merge_head=$(sed -e '/  not-for-merge   /d' \
 278        -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
 279        tr '\012' ' ')
 280
 281case "$merge_head" in
 282'')
 283        error_on_no_merge_candidates "$@"
 284        ;;
 285?*' '?*)
 286        if test -z "$orig_head"
 287        then
 288                die "$(gettext "Cannot merge multiple branches into empty head")"
 289        fi
 290        if test true = "$rebase"
 291        then
 292                die "$(gettext "Cannot rebase onto multiple branches")"
 293        fi
 294        ;;
 295esac
 296
 297# Pulling into unborn branch: a shorthand for branching off
 298# FETCH_HEAD, for lazy typers.
 299if test -z "$orig_head"
 300then
 301        # Two-way merge: we claim the index is based on an empty tree,
 302        # and try to fast-forward to HEAD.  This ensures we will not
 303        # lose index/worktree changes that the user already made on
 304        # the unborn branch.
 305        empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 306        git read-tree -m -u $empty_tree $merge_head &&
 307        git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
 308        exit
 309fi
 310
 311if test true = "$rebase"
 312then
 313        o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
 314        if test "$oldremoteref" = "$o"
 315        then
 316                unset oldremoteref
 317        fi
 318fi
 319
 320case "$rebase" in
 321true)
 322        eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
 323        eval="$eval $gpg_sign_args"
 324        eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
 325        ;;
 326*)
 327        eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
 328        eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
 329        eval="$eval $gpg_sign_args"
 330        eval="$eval FETCH_HEAD"
 331        ;;
 332esac
 333eval "exec $eval"