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