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= no_stat= no_commit= squash= no_ff= log_arg= verbosity= 20curr_branch=$(git symbolic-ref -q HEAD) 21curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||") 22rebase=$(git config --bool branch.$curr_branch_short.rebase) 23while: 24do 25case"$1"in 26-q|--quiet) 27 verbosity="$verbosity-q";; 28-v|--verbose) 29 verbosity="$verbosity-v";; 30-n|--no-stat|--no-summary) 31 no_stat=-n;; 32--stat|--summary) 33 no_stat=$1;; 34--log|--no-log) 35 log_arg=$1;; 36--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) 37 no_commit=--no-commit;; 38--c|--co|--com|--comm|--commi|--commit) 39 no_commit=--commit;; 40--sq|--squ|--squa|--squas|--squash) 41 squash=--squash;; 42--no-sq|--no-squ|--no-squa|--no-squas|--no-squash) 43 squash=--no-squash;; 44--ff) 45 no_ff=--ff;; 46--no-ff) 47 no_ff=--no-ff;; 48-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 49--strateg=*|--strategy=*|\ 50-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 51case"$#,$1"in 52*,*=*) 53 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'`;; 541,*) 55 usage ;; 56*) 57 strategy="$2" 58shift;; 59esac 60 strategy_args="${strategy_args}-s$strategy" 61;; 62-r|--r|--re|--reb|--reba|--rebas|--rebase) 63 rebase=true 64;; 65--no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase) 66 rebase=false 67;; 68-h|--h|--he|--hel|--help) 69 usage 70;; 71*) 72# Pass thru anything that may be meant for fetch. 73break 74;; 75esac 76shift 77done 78 79error_on_no_merge_candidates () { 80exec>&2 81for opt 82do 83case"$opt"in 84-t|--t|--ta|--tag|--tags) 85echo"Fetching tags only, you probably meant:" 86echo" git fetch --tags" 87exit1 88esac 89done 90 91 curr_branch=${curr_branch#refs/heads/} 92 93echo"You asked me to pull without telling me which branch you" 94echo"want to merge with, and 'branch.${curr_branch}.merge' in" 95echo"your configuration file does not tell me either. Please" 96echo"name which branch you want to merge on the command line and" 97echo"try again (e.g. 'git pull <repository> <refspec>')." 98echo"See git-pull(1) for details on the refspec." 99echo 100echo"If you often merge with the same branch, you may want to" 101echo"configure the following variables in your configuration" 102echo"file:" 103echo 104echo" branch.${curr_branch}.remote = <nickname>" 105echo" branch.${curr_branch}.merge = <remote-ref>" 106echo" remote.<nickname>.url = <url>" 107echo" remote.<nickname>.fetch = <refspec>" 108echo 109echo"See git-config(1) for details." 110exit1 111} 112 113test true ="$rebase"&& { 114 git update-index --ignore-submodules --refresh&& 115 git diff-files --ignore-submodules --quiet&& 116 git diff-index --ignore-submodules --cached --quiet HEAD --|| 117 die "refusing to pull with rebase: your working tree is not up-to-date" 118 119 . git-parse-remote&& 120 origin="$1" 121test -z"$origin"&& origin=$(get_default_remote) 122 reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null | 123 sed "s|refs/heads/\(.*\):|\1|")"&& 124 oldremoteref="$(git rev-parse -q --verify \ 125 "refs/remotes/$origin/$reflist")" 126} 127orig_head=$(git rev-parse -q --verify HEAD) 128git fetch $verbosity--update-head-ok"$@"||exit1 129 130curr_head=$(git rev-parse -q --verify HEAD) 131iftest -n"$orig_head"&&test"$curr_head"!="$orig_head" 132then 133# The fetch involved updating the current branch. 134 135# The working tree and the index file is still based on the 136# $orig_head commit, but we are merging into $curr_head. 137# First update the working tree to match $curr_head. 138 139echo>&2"Warning: fetch updated the current branch head." 140echo>&2"Warning: fast forwarding your working tree from" 141echo>&2"Warning: commit$orig_head." 142 git update-index --refresh2>/dev/null 143 git read-tree -u -m"$orig_head""$curr_head"|| 144 die 'Cannot fast-forward your working tree. 145After making sure that you saved anything precious from 146$ git diff '$orig_head' 147output, run 148$ git reset --hard 149to recover.' 150 151fi 152 153merge_head=$(sed-e'/ not-for-merge /d' \ 154-e's/ .*//'"$GIT_DIR"/FETCH_HEAD | \ 155tr'\012'' ') 156 157case"$merge_head"in 158'') 159case $? in 1600) error_on_no_merge_candidates "$@";; 1611)echo>&2"You are not currently on a branch; you must explicitly" 162echo>&2"specify which branch you wish to merge:" 163echo>&2" git pull <remote> <branch>" 164exit1;; 165*)exit $?;; 166esac 167;; 168?*' '?*) 169iftest -z"$orig_head" 170then 171echo>&2"Cannot merge multiple branches into empty head" 172exit1 173fi 174iftest true ="$rebase" 175then 176echo>&2"Cannot rebase onto multiple branches" 177exit1 178fi 179;; 180esac 181 182iftest -z"$orig_head" 183then 184 git update-ref -m"initial pull" HEAD $merge_head"$curr_head"&& 185 git read-tree --reset -u HEAD ||exit1 186exit 187fi 188 189merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD")||exit 190test true ="$rebase"&& 191exec git-rebase$strategy_args--onto$merge_head \ 192${oldremoteref:-$merge_head} 193exec git-merge$no_stat $no_commit $squash $no_ff $log_arg $strategy_args \ 194"$merge_name" HEAD $merge_head $verbosity