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