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