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