git-pull.shon commit Merge branch 'sb/maint-1.6.2-opt-filename-fix' into maint (b91ffd3)
   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
  16test -z "$(git ls-files -u)" ||
  17        die "You are in the middle of a conflicted merge."
  18
  19strategy_args= diffstat= no_commit= squash= no_ff= log_arg= verbosity=
  20curr_branch=$(git symbolic-ref -q HEAD)
  21curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
  22rebase=$(git config --bool branch.$curr_branch_short.rebase)
  23while :
  24do
  25        case "$1" in
  26        -q|--quiet)
  27                verbosity="$verbosity -q" ;;
  28        -v|--verbose)
  29                verbosity="$verbosity -v" ;;
  30        -n|--no-stat|--no-summary)
  31                diffstat=--no-stat ;;
  32        --stat|--summary)
  33                diffstat=--stat ;;
  34        --log|--no-log)
  35                log_arg=$1 ;;
  36        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
  37                no_commit=--no-commit ;;
  38        --c|--co|--com|--comm|--commi|--commit)
  39                no_commit=--commit ;;
  40        --sq|--squ|--squa|--squas|--squash)
  41                squash=--squash ;;
  42        --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
  43                squash=--no-squash ;;
  44        --ff)
  45                no_ff=--ff ;;
  46        --no-ff)
  47                no_ff=--no-ff ;;
  48        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
  49                --strateg=*|--strategy=*|\
  50        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
  51                case "$#,$1" in
  52                *,*=*)
  53                        strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
  54                1,*)
  55                        usage ;;
  56                *)
  57                        strategy="$2"
  58                        shift ;;
  59                esac
  60                strategy_args="${strategy_args}-s $strategy "
  61                ;;
  62        -r|--r|--re|--reb|--reba|--rebas|--rebase)
  63                rebase=true
  64                ;;
  65        --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
  66                rebase=false
  67                ;;
  68        -h|--h|--he|--hel|--help)
  69                usage
  70                ;;
  71        *)
  72                # Pass thru anything that may be meant for fetch.
  73                break
  74                ;;
  75        esac
  76        shift
  77done
  78
  79error_on_no_merge_candidates () {
  80        exec >&2
  81        for opt
  82        do
  83                case "$opt" in
  84                -t|--t|--ta|--tag|--tags)
  85                        echo "Fetching tags only, you probably meant:"
  86                        echo "  git fetch --tags"
  87                        exit 1
  88                esac
  89        done
  90
  91        curr_branch=${curr_branch#refs/heads/}
  92
  93        if [ -z "$curr_branch" ]; then
  94                echo "You are not currently on a branch, so I cannot use any"
  95                echo "'branch.<branchname>.merge' in your configuration file."
  96                echo "Please specify which branch you want to merge on the command"
  97                echo "line and try again (e.g. 'git pull <repository> <refspec>')."
  98                echo "See git-pull(1) for details."
  99        else
 100                echo "You asked me to pull without telling me which branch you"
 101                echo "want to merge with, and 'branch.${curr_branch}.merge' in"
 102                echo "your configuration file does not tell me either.  Please"
 103                echo "specify which branch you want to merge on the command line and"
 104                echo "try again (e.g. 'git pull <repository> <refspec>')."
 105                echo "See git-pull(1) for details."
 106                echo
 107                echo "If you often merge with the same branch, you may want to"
 108                echo "configure the following variables in your configuration"
 109                echo "file:"
 110                echo
 111                echo "    branch.${curr_branch}.remote = <nickname>"
 112                echo "    branch.${curr_branch}.merge = <remote-ref>"
 113                echo "    remote.<nickname>.url = <url>"
 114                echo "    remote.<nickname>.fetch = <refspec>"
 115                echo
 116                echo "See git-config(1) for details."
 117        fi
 118        exit 1
 119}
 120
 121test true = "$rebase" && {
 122        git update-index --ignore-submodules --refresh &&
 123        git diff-files --ignore-submodules --quiet &&
 124        git diff-index --ignore-submodules --cached --quiet HEAD -- ||
 125        die "refusing to pull with rebase: your working tree is not up-to-date"
 126
 127        . git-parse-remote &&
 128        origin="$1"
 129        test -z "$origin" && origin=$(get_default_remote)
 130        reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null |
 131                sed "s|refs/heads/\(.*\):|\1|")" &&
 132        oldremoteref="$(git rev-parse -q --verify \
 133                "refs/remotes/$origin/$reflist")"
 134}
 135orig_head=$(git rev-parse -q --verify HEAD)
 136git fetch $verbosity --update-head-ok "$@" || exit 1
 137
 138curr_head=$(git rev-parse -q --verify HEAD)
 139if test -n "$orig_head" && test "$curr_head" != "$orig_head"
 140then
 141        # The fetch involved updating the current branch.
 142
 143        # The working tree and the index file is still based on the
 144        # $orig_head commit, but we are merging into $curr_head.
 145        # First update the working tree to match $curr_head.
 146
 147        echo >&2 "Warning: fetch updated the current branch head."
 148        echo >&2 "Warning: fast forwarding your working tree from"
 149        echo >&2 "Warning: commit $orig_head."
 150        git update-index -q --refresh
 151        git read-tree -u -m "$orig_head" "$curr_head" ||
 152                die 'Cannot fast-forward your working tree.
 153After making sure that you saved anything precious from
 154$ git diff '$orig_head'
 155output, run
 156$ git reset --hard
 157to recover.'
 158
 159fi
 160
 161merge_head=$(sed -e '/  not-for-merge   /d' \
 162        -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
 163        tr '\012' ' ')
 164
 165case "$merge_head" in
 166'')
 167        case $? in
 168        0) error_on_no_merge_candidates "$@";;
 169        1) echo >&2 "You are not currently on a branch; you must explicitly"
 170           echo >&2 "specify which branch you wish to merge:"
 171           echo >&2 "  git pull <remote> <branch>"
 172           exit 1;;
 173        *) exit $?;;
 174        esac
 175        ;;
 176?*' '?*)
 177        if test -z "$orig_head"
 178        then
 179                echo >&2 "Cannot merge multiple branches into empty head"
 180                exit 1
 181        fi
 182        if test true = "$rebase"
 183        then
 184                echo >&2 "Cannot rebase onto multiple branches"
 185                exit 1
 186        fi
 187        ;;
 188esac
 189
 190if test -z "$orig_head"
 191then
 192        git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
 193        git read-tree --reset -u HEAD || exit 1
 194        exit
 195fi
 196
 197merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
 198test true = "$rebase" &&
 199        exec git-rebase $diffstat $strategy_args --onto $merge_head \
 200        ${oldremoteref:-$merge_head}
 201exec git-merge $diffstat $no_commit $squash $no_ff $log_arg $strategy_args \
 202        "$merge_name" HEAD $merge_head $verbosity