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