3 # Copyright (c) 2005 Junio C Hamano
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
7 USAGE
='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE
='Fetch one or more remote refs and merge it/them into the current HEAD.'
12 set_reflog_action
"pull $*"
16 test -z "$(git ls-files -u)" ||
17 die
"You are in the middle of a conflicted merge."
19 strategy_args
= no_stat
= no_commit
= squash
= no_ff
=
20 curr_branch
=$
(git symbolic-ref
-q HEAD
)
21 curr_branch_short
=$
(echo "$curr_branch" |
sed "s|refs/heads/||")
22 rebase
=$
(git config
--bool branch.
$curr_branch_short.rebase
)
26 -n|
--no-stat|
--no-summary)
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)
34 --sq|
--squ|
--squa|
--squas|
--squash)
36 --no-sq|
--no-squ|
--no-squa|
--no-squas|
--no-squash)
42 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
43 --strateg=*|
--strategy=*|\
44 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
47 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
54 strategy_args
="${strategy_args}-s $strategy "
56 -r|
--r|
--re|
--reb|
--reba|
--rebas|
--rebase)
59 --no-r|
--no-re|
--no-reb|
--no-reba|
--no-rebas|
--no-rebase)
62 -h|
--h|
--he|
--hel|
--help)
66 # Pass thru anything that may be meant for fetch.
73 error_on_no_merge_candidates
() {
78 -t|
--t|
--ta|
--tag|
--tags)
79 echo "Fetching tags only, you probably meant:"
80 echo " git fetch --tags"
85 curr_branch
=${curr_branch#refs/heads/}
87 echo "You asked me to pull without telling me which branch you"
88 echo "want to merge with, and 'branch.${curr_branch}.merge' in"
89 echo "your configuration file does not tell me either. Please"
90 echo "name which branch you want to merge on the command line and"
91 echo "try again (e.g. 'git pull <repository> <refspec>')."
92 echo "See git-pull(1) for details on the refspec."
94 echo "If you often merge with the same branch, you may want to"
95 echo "configure the following variables in your configuration"
98 echo " branch.${curr_branch}.remote = <nickname>"
99 echo " branch.${curr_branch}.merge = <remote-ref>"
100 echo " remote.<nickname>.url = <url>"
101 echo " remote.<nickname>.fetch = <refspec>"
103 echo "See git-config(1) for details."
107 test true
= "$rebase" && {
108 . git-parse-remote
&&
110 test -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)"
116 orig_head
=$
(git rev-parse
--verify HEAD
2>/dev
/null
)
117 git-fetch
--update-head-ok "$@" ||
exit 1
119 curr_head
=$
(git rev-parse
--verify HEAD
2>/dev
/null
)
120 if test "$curr_head" != "$orig_head"
122 # The fetch involved updating the current branch.
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.
128 echo >&2 "Warning: fetch updated the current branch head."
129 echo >&2 "Warning: fast forwarding your working tree from"
130 echo >&2 "Warning: commit $orig_head."
131 git update-index
--refresh 2>/dev
/null
132 git read-tree
-u -m "$orig_head" "$curr_head" ||
133 die
'Cannot fast-forward your working tree.
134 After making sure that you saved anything precious from
135 $ git diff '$orig_head'
142 merge_head
=$
(sed -e '/ not-for-merge /d' \
143 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
146 case "$merge_head" in
149 0) error_on_no_merge_candidates
"$@";;
150 1) echo >&2 "You are not currently on a branch; you must explicitly"
151 echo >&2 "specify which branch you wish to merge:"
152 echo >&2 " git pull <remote> <branch>"
158 if test -z "$orig_head"
160 echo >&2 "Cannot merge multiple branches into empty head"
166 if test -z "$orig_head"
168 git update-ref
-m "initial pull" HEAD
$merge_head "" &&
169 git read-tree
--reset -u HEAD ||
exit 1
173 merge_name
=$
(git fmt-merge-msg
<"$GIT_DIR/FETCH_HEAD") ||
exit
174 test true
= "$rebase" &&
175 exec git-rebase
$strategy_args --onto $merge_head \
176 ${oldremoteref:-$merge_head}
177 exec git-merge
$no_stat $no_commit $squash $no_ff $strategy_args \
178 "$merge_name" HEAD
$merge_head