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] [-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 10. git-sh-setup 11set_reflog_action "pull $*" 12require_work_tree 13cd_to_toplevel 14 15test -z"$(git ls-files -u)"|| 16 die "You are in the middle of a conflicted merge." 17 18strategy_args= no_summary= no_commit= squash= 19while: 20do 21case"$1"in 22-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ 23--no-summa|--no-summar|--no-summary) 24 no_summary=-n;; 25--summary) 26 no_summary=$1 27;; 28--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) 29 no_commit=--no-commit;; 30--sq|--squ|--squa|--squas|--squash) 31 squash=--squash;; 32-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 33--strateg=*|--strategy=*|\ 34-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 35case"$#,$1"in 36*,*=*) 37 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'`;; 381,*) 39 usage ;; 40*) 41 strategy="$2" 42shift;; 43esac 44 strategy_args="${strategy_args}-s$strategy" 45;; 46-h|--h|--he|--hel|--help) 47 usage 48;; 49*) 50# Pass thru anything that may be meant for fetch. 51break 52;; 53esac 54shift 55done 56 57orig_head=$(git rev-parse --verify HEAD 2>/dev/null) 58git-fetch --update-head-ok"$@"||exit1 59 60curr_head=$(git rev-parse --verify HEAD 2>/dev/null) 61iftest"$curr_head"!="$orig_head" 62then 63# The fetch involved updating the current branch. 64 65# The working tree and the index file is still based on the 66# $orig_head commit, but we are merging into $curr_head. 67# First update the working tree to match $curr_head. 68 69echo>&2"Warning: fetch updated the current branch head." 70echo>&2"Warning: fast forwarding your working tree from" 71echo>&2"Warning: commit$orig_head." 72 git update-index --refresh2>/dev/null 73 git read-tree -u -m"$orig_head""$curr_head"|| 74 die 'Cannot fast-forward your working tree. 75After making sure that you saved anything precious from 76$ git diff '$orig_head' 77output, run 78$ git reset --hard 79to recover.' 80 81fi 82 83merge_head=$(sed-e'/ not-for-merge /d' \ 84-e's/ .*//'"$GIT_DIR"/FETCH_HEAD | \ 85tr'\012'' ') 86 87case"$merge_head"in 88'') 89 curr_branch=$(git symbolic-ref -q HEAD) 90case $? in 910) ;; 921)echo>&2"You are not currently on a branch; you must explicitly" 93echo>&2"specify which branch you wish to merge:" 94echo>&2" git pull <remote> <branch>" 95exit1;; 96*)exit $?;; 97esac 98 curr_branch=${curr_branch#refs/heads/} 99 100echo>&2"You asked me to pull without telling me which branch you" 101echo>&2"want to merge with, and 'branch.${curr_branch}.merge' in" 102echo>&2"your configuration file does not tell me either. Please" 103echo>&2"name which branch you want to merge on the command line and" 104echo>&2"try again (e.g. 'git pull <repository> <refspec>')." 105echo>&2"See git-pull(1) for details on the refspec." 106echo>&2 107echo>&2"If you often merge with the same branch, you may want to" 108echo>&2"configure the following variables in your configuration" 109echo>&2"file:" 110echo>&2 111echo>&2" branch.${curr_branch}.remote = <nickname>" 112echo>&2" branch.${curr_branch}.merge = <remote-ref>" 113echo>&2" remote.<nickname>.url = <url>" 114echo>&2" remote.<nickname>.fetch = <refspec>" 115echo>&2 116echo>&2"See git-config(1) for details." 117exit1 118;; 119?*' '?*) 120iftest -z"$orig_head" 121then 122echo>&2"Cannot merge multiple branches into empty head" 123exit1 124fi 125;; 126esac 127 128iftest -z"$orig_head" 129then 130 git update-ref -m"initial pull" HEAD $merge_head""&& 131 git read-tree --reset -u HEAD ||exit1 132exit 133fi 134 135merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD")||exit 136exec git-merge$no_summary $no_commit $squash $strategy_args \ 137"$merge_name" HEAD $merge_head