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