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