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