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