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