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= recurse_submodules= 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--recurse-submodules) 109 recurse_submodules=--recurse-submodules 110;; 111--recurse-submodules=*) 112 recurse_submodules="$1" 113;; 114--no-recurse-submodules) 115 recurse_submodules=--no-recurse-submodules 116;; 117--d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run) 118 dry_run=--dry-run 119;; 120-h|--h|--he|--hel|--help|--help-|--help-a|--help-al|--help-all) 121 usage 122;; 123*) 124# Pass thru anything that may be meant for fetch. 125break 126;; 127esac 128shift 129done 130 131error_on_no_merge_candidates () { 132exec>&2 133for opt 134do 135case"$opt"in 136-t|--t|--ta|--tag|--tags) 137echo"Fetching tags only, you probably meant:" 138echo" git fetch --tags" 139exit1 140esac 141done 142 143iftest true ="$rebase" 144then 145 op_type=rebase 146 op_prep=against 147else 148 op_type=merge 149 op_prep=with 150fi 151 152 curr_branch=${curr_branch#refs/heads/} 153 upstream=$(git config "branch.$curr_branch.merge") 154 remote=$(git config "branch.$curr_branch.remote") 155 156if[$#-gt1];then 157if["$rebase"= true ];then 158printf"There is no candidate for rebasing against " 159else 160printf"There are no candidates for merging " 161fi 162echo"among the refs that you just fetched." 163echo"Generally this means that you provided a wildcard refspec which had no" 164echo"matches on the remote end." 165elif[$#-gt0] && ["$1"!="$remote"];then 166echo"You asked to pull from the remote '$1', but did not specify" 167echo"a branch. Because this is not the default configured remote" 168echo"for your current branch, you must specify a branch on the command line." 169elif[-z"$curr_branch"];then 170echo"You are not currently on a branch, so I cannot use any" 171echo"'branch.<branchname>.merge' in your configuration file." 172echo"Please specify which remote branch you want to use on the command" 173echo"line and try again (e.g. 'git pull <repository> <refspec>')." 174echo"See git-pull(1) for details." 175elif[-z"$upstream"];then 176echo"You asked me to pull without telling me which branch you" 177echo"want to$op_type$op_prep, and 'branch.${curr_branch}.merge' in" 178echo"your configuration file does not tell me, either. Please" 179echo"specify which branch you want to use on the command line and" 180echo"try again (e.g. 'git pull <repository> <refspec>')." 181echo"See git-pull(1) for details." 182echo 183echo"If you often$op_type$op_prepthe same branch, you may want to" 184echo"use something like the following in your configuration file:" 185echo 186echo" [branch\"${curr_branch}\"]" 187echo" remote = <nickname>" 188echo" merge = <remote-ref>" 189test rebase ="$op_type"&& 190echo" rebase = true" 191echo 192echo" [remote\"<nickname>\"]" 193echo" url = <url>" 194echo" fetch = <refspec>" 195echo 196echo"See git-config(1) for details." 197else 198echo"Your configuration specifies to$op_type$op_prepthe ref '${upstream#refs/heads/}'" 199echo"from the remote, but no such ref was fetched." 200fi 201exit1 202} 203 204test true ="$rebase"&& { 205if! git rev-parse -q --verify HEAD >/dev/null 206then 207# On an unborn branch 208iftest -f"$GIT_DIR/index" 209then 210 die "updating an unborn branch with changes added to the index" 211fi 212else 213 require_clean_work_tree "pull with rebase""Please commit or stash them." 214fi 215 oldremoteref= && 216 . git-parse-remote&& 217 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)"&& 218 oldremoteref="$(git rev-parse -q --verify "$remoteref")"&& 219for reflog in$(git rev-list -g $remoteref 2>/dev/null) 220do 221iftest"$reflog"="$(git merge-base $reflog $curr_branch)" 222then 223 oldremoteref="$reflog" 224break 225fi 226done 227} 228orig_head=$(git rev-parse -q --verify HEAD) 229git fetch $verbosity $progress $dry_run $recurse_submodules--update-head-ok"$@"||exit1 230test -z"$dry_run"||exit0 231 232curr_head=$(git rev-parse -q --verify HEAD) 233iftest -n"$orig_head"&&test"$curr_head"!="$orig_head" 234then 235# The fetch involved updating the current branch. 236 237# The working tree and the index file is still based on the 238# $orig_head commit, but we are merging into $curr_head. 239# First update the working tree to match $curr_head. 240 241echo>&2"Warning: fetch updated the current branch head." 242echo>&2"Warning: fast-forwarding your working tree from" 243echo>&2"Warning: commit$orig_head." 244 git update-index -q --refresh 245 git read-tree -u -m"$orig_head""$curr_head"|| 246 die 'Cannot fast-forward your working tree. 247After making sure that you saved anything precious from 248$ git diff '$orig_head' 249output, run 250$ git reset --hard 251to recover.' 252 253fi 254 255merge_head=$(sed-e'/ not-for-merge /d' \ 256-e's/ .*//'"$GIT_DIR"/FETCH_HEAD | \ 257tr'\012'' ') 258 259case"$merge_head"in 260'') 261 error_on_no_merge_candidates "$@" 262;; 263?*' '?*) 264iftest -z"$orig_head" 265then 266 die "Cannot merge multiple branches into empty head" 267fi 268iftest true ="$rebase" 269then 270 die "Cannot rebase onto multiple branches" 271fi 272;; 273esac 274 275iftest -z"$orig_head" 276then 277 git update-ref -m"initial pull" HEAD $merge_head"$curr_head"&& 278 git read-tree --reset -u HEAD ||exit1 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" 295eval="$eval--onto$merge_head${oldremoteref:-$merge_head}" 296;; 297*) 298eval="git-merge$diffstat$no_commit$squash$no_ff$ff_only" 299eval="$eval$log_arg$strategy_args$merge_args" 300eval="$eval\"\$merge_name\"HEAD$merge_head$verbosity" 301;; 302esac 303eval"exec$eval"