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