git-pull.shon commit Merge branch 'jc/grep-lookahead' (34349be)
   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] [-s strategy]... [<fetch-options>] <repo> <head>...'
   8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
   9SUBDIRECTORY_OK=Yes
  10OPTIONS_SPEC=
  11. git-sh-setup
  12set_reflog_action "pull $*"
  13require_work_tree
  14cd_to_toplevel
  15
  16
  17die_conflict () {
  18    git diff-index --cached --name-status -r --ignore-submodules HEAD --
  19    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
  20        die "Pull is not possible because you have unmerged files.
  21Please, fix them up in the work tree, and then use 'git add/rm <file>'
  22as appropriate to mark resolution, or use 'git commit -a'."
  23    else
  24        die "Pull is not possible because you have unmerged files."
  25    fi
  26}
  27
  28die_merge () {
  29    if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
  30        die "You have not concluded your merge (MERGE_HEAD exists).
  31Please, commit your changes before you can merge."
  32    else
  33        die "You have not concluded your merge (MERGE_HEAD exists)."
  34    fi
  35}
  36
  37test -z "$(git ls-files -u)" || die_conflict
  38test -f "$GIT_DIR/MERGE_HEAD" && die_merge
  39
  40strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
  41log_arg= verbosity=
  42curr_branch=$(git symbolic-ref -q HEAD)
  43curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
  44rebase=$(git config --bool branch.$curr_branch_short.rebase)
  45while :
  46do
  47        case "$1" in
  48        -q|--quiet)
  49                verbosity="$verbosity -q" ;;
  50        -v|--verbose)
  51                verbosity="$verbosity -v" ;;
  52        -n|--no-stat|--no-summary)
  53                diffstat=--no-stat ;;
  54        --stat|--summary)
  55                diffstat=--stat ;;
  56        --log|--no-log)
  57                log_arg=$1 ;;
  58        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
  59                no_commit=--no-commit ;;
  60        --c|--co|--com|--comm|--commi|--commit)
  61                no_commit=--commit ;;
  62        --sq|--squ|--squa|--squas|--squash)
  63                squash=--squash ;;
  64        --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
  65                squash=--no-squash ;;
  66        --ff)
  67                no_ff=--ff ;;
  68        --no-ff)
  69                no_ff=--no-ff ;;
  70        --ff-only)
  71                ff_only=--ff-only ;;
  72        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
  73                --strateg=*|--strategy=*|\
  74        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
  75                case "$#,$1" in
  76                *,*=*)
  77                        strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
  78                1,*)
  79                        usage ;;
  80                *)
  81                        strategy="$2"
  82                        shift ;;
  83                esac
  84                strategy_args="${strategy_args}-s $strategy "
  85                ;;
  86        -r|--r|--re|--reb|--reba|--rebas|--rebase)
  87                rebase=true
  88                ;;
  89        --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
  90                rebase=false
  91                ;;
  92        -h|--h|--he|--hel|--help)
  93                usage
  94                ;;
  95        *)
  96                # Pass thru anything that may be meant for fetch.
  97                break
  98                ;;
  99        esac
 100        shift
 101done
 102
 103error_on_no_merge_candidates () {
 104        exec >&2
 105        for opt
 106        do
 107                case "$opt" in
 108                -t|--t|--ta|--tag|--tags)
 109                        echo "Fetching tags only, you probably meant:"
 110                        echo "  git fetch --tags"
 111                        exit 1
 112                esac
 113        done
 114
 115        if test true = "$rebase"
 116        then
 117                op_type=rebase
 118                op_prep=against
 119        else
 120                op_type=merge
 121                op_prep=with
 122        fi
 123
 124        curr_branch=${curr_branch#refs/heads/}
 125        upstream=$(git config "branch.$curr_branch.merge")
 126        remote=$(git config "branch.$curr_branch.remote")
 127
 128        if [ $# -gt 1 ]; then
 129                if [ "$rebase" = true ]; then
 130                        printf "There is no candidate for rebasing against "
 131                else
 132                        printf "There are no candidates for merging "
 133                fi
 134                echo "among the refs that you just fetched."
 135                echo "Generally this means that you provided a wildcard refspec which had no"
 136                echo "matches on the remote end."
 137        elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
 138                echo "You asked to pull from the remote '$1', but did not specify"
 139                echo "a branch. Because this is not the default configured remote"
 140                echo "for your current branch, you must specify a branch on the command line."
 141        elif [ -z "$curr_branch" ]; then
 142                echo "You are not currently on a branch, so I cannot use any"
 143                echo "'branch.<branchname>.merge' in your configuration file."
 144                echo "Please specify which remote branch you want to use on the command"
 145                echo "line and try again (e.g. 'git pull <repository> <refspec>')."
 146                echo "See git-pull(1) for details."
 147        elif [ -z "$upstream" ]; then
 148                echo "You asked me to pull without telling me which branch you"
 149                echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
 150                echo "your configuration file does not tell me, either. Please"
 151                echo "specify which branch you want to use on the command line and"
 152                echo "try again (e.g. 'git pull <repository> <refspec>')."
 153                echo "See git-pull(1) for details."
 154                echo
 155                echo "If you often $op_type $op_prep the same branch, you may want to"
 156                echo "use something like the following in your configuration file:"
 157                echo
 158                echo "    [branch \"${curr_branch}\"]"
 159                echo "    remote = <nickname>"
 160                echo "    merge = <remote-ref>"
 161                test rebase = "$op_type" &&
 162                        echo "    rebase = true"
 163                echo
 164                echo "    [remote \"<nickname>\"]"
 165                echo "    url = <url>"
 166                echo "    fetch = <refspec>"
 167                echo
 168                echo "See git-config(1) for details."
 169        else
 170                echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
 171                echo "from the remote, but no such ref was fetched."
 172        fi
 173        exit 1
 174}
 175
 176test true = "$rebase" && {
 177        if ! git rev-parse -q --verify HEAD >/dev/null
 178        then
 179                # On an unborn branch
 180                if test -f "$GIT_DIR/index"
 181                then
 182                        die "updating an unborn branch with changes added to the index"
 183                fi
 184        else
 185                git update-index --ignore-submodules --refresh &&
 186                git diff-files --ignore-submodules --quiet &&
 187                git diff-index --ignore-submodules --cached --quiet HEAD -- ||
 188                die "refusing to pull with rebase: your working tree is not up-to-date"
 189        fi
 190        oldremoteref= &&
 191        . git-parse-remote &&
 192        remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
 193        oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
 194        for reflog in $(git rev-list -g $remoteref 2>/dev/null)
 195        do
 196                if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
 197                then
 198                        oldremoteref="$reflog"
 199                        break
 200                fi
 201        done
 202}
 203orig_head=$(git rev-parse -q --verify HEAD)
 204git fetch $verbosity --update-head-ok "$@" || exit 1
 205
 206curr_head=$(git rev-parse -q --verify HEAD)
 207if test -n "$orig_head" && test "$curr_head" != "$orig_head"
 208then
 209        # The fetch involved updating the current branch.
 210
 211        # The working tree and the index file is still based on the
 212        # $orig_head commit, but we are merging into $curr_head.
 213        # First update the working tree to match $curr_head.
 214
 215        echo >&2 "Warning: fetch updated the current branch head."
 216        echo >&2 "Warning: fast-forwarding your working tree from"
 217        echo >&2 "Warning: commit $orig_head."
 218        git update-index -q --refresh
 219        git read-tree -u -m "$orig_head" "$curr_head" ||
 220                die 'Cannot fast-forward your working tree.
 221After making sure that you saved anything precious from
 222$ git diff '$orig_head'
 223output, run
 224$ git reset --hard
 225to recover.'
 226
 227fi
 228
 229merge_head=$(sed -e '/  not-for-merge   /d' \
 230        -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
 231        tr '\012' ' ')
 232
 233case "$merge_head" in
 234'')
 235        error_on_no_merge_candidates "$@"
 236        ;;
 237?*' '?*)
 238        if test -z "$orig_head"
 239        then
 240                die "Cannot merge multiple branches into empty head"
 241        fi
 242        if test true = "$rebase"
 243        then
 244                die "Cannot rebase onto multiple branches"
 245        fi
 246        ;;
 247esac
 248
 249if test -z "$orig_head"
 250then
 251        git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
 252        git read-tree --reset -u HEAD || exit 1
 253        exit
 254fi
 255
 256merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
 257test true = "$rebase" &&
 258        exec git-rebase $diffstat $strategy_args --onto $merge_head \
 259        ${oldremoteref:-$merge_head}
 260exec git-merge $diffstat $no_commit $squash $no_ff $ff_only $log_arg $strategy_args \
 261        "$merge_name" HEAD $merge_head $verbosity