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