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