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 $*"
18 git diff-index
--cached --name-status -r --ignore-submodules HEAD
--
19 if [ $
(git config
--bool --get advice.resolveConflict ||
echo true
) = "true" ]; then
20 die
"Pull is not possible because you have unmerged files.
21 Please, fix them up in the work tree, and then use 'git add/rm <file>'
22 as appropriate to mark resolution, or use 'git commit -a'."
24 die
"Pull is not possible because you have unmerged files."
29 if [ $
(git config
--bool --get advice.resolveConflict ||
echo true
) = "true" ]; then
30 die
"You have not concluded your merge (MERGE_HEAD exists).
31 Please, commit your changes before you can merge."
33 die
"You have not concluded your merge (MERGE_HEAD exists)."
37 test -z "$(git ls-files -u)" || die_conflict
38 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40 strategy_args
= diffstat
= no_commit
= squash
= no_ff
= ff_only
=
41 log_arg
= verbosity
= progress
=
43 curr_branch
=$
(git symbolic-ref
-q HEAD
)
44 curr_branch_short
=$
(echo "$curr_branch" |
sed "s|refs/heads/||")
45 rebase
=$
(git config
--bool branch.
$curr_branch_short.rebase
)
50 verbosity
="$verbosity -q" ;;
52 verbosity
="$verbosity -v" ;;
54 progress
=--progress ;;
55 -n|
--no-stat|
--no-summary)
61 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
62 no_commit
=--no-commit ;;
63 --c|
--co|
--com|
--comm|
--commi|
--commit)
65 --sq|
--squ|
--squa|
--squas|
--squash)
67 --no-sq|
--no-squ|
--no-squa|
--no-squas|
--no-squash)
75 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
76 --strateg=*|
--strategy=*|\
77 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
80 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
87 strategy_args
="${strategy_args}-s $strategy "
94 xx
="-X $(git rev-parse --sq-quote "$2")"
97 xx
=$
(git rev-parse
--sq-quote "$1") ;;
99 merge_args
="$merge_args$xx "
101 -r|
--r|
--re|
--reb|
--reba|
--rebas|
--rebase)
104 --no-r|
--no-re|
--no-reb|
--no-reba|
--no-rebas|
--no-rebase)
107 -h|
--h|
--he|
--hel|
--help)
111 # Pass thru anything that may be meant for fetch.
118 error_on_no_merge_candidates
() {
123 -t|
--t|
--ta|
--tag|
--tags)
124 echo "Fetching tags only, you probably meant:"
125 echo " git fetch --tags"
130 if test true
= "$rebase"
139 curr_branch
=${curr_branch#refs/heads/}
140 upstream
=$
(git config
"branch.$curr_branch.merge")
141 remote
=$
(git config
"branch.$curr_branch.remote")
143 if [ $# -gt 1 ]; then
144 if [ "$rebase" = true
]; then
145 printf "There is no candidate for rebasing against "
147 printf "There are no candidates for merging "
149 echo "among the refs that you just fetched."
150 echo "Generally this means that you provided a wildcard refspec which had no"
151 echo "matches on the remote end."
152 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
153 echo "You asked to pull from the remote '$1', but did not specify"
154 echo "a branch. Because this is not the default configured remote"
155 echo "for your current branch, you must specify a branch on the command line."
156 elif [ -z "$curr_branch" ]; then
157 echo "You are not currently on a branch, so I cannot use any"
158 echo "'branch.<branchname>.merge' in your configuration file."
159 echo "Please specify which remote branch you want to use on the command"
160 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
161 echo "See git-pull(1) for details."
162 elif [ -z "$upstream" ]; then
163 echo "You asked me to pull without telling me which branch you"
164 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
165 echo "your configuration file does not tell me, either. Please"
166 echo "specify which branch you want to use on the command line and"
167 echo "try again (e.g. 'git pull <repository> <refspec>')."
168 echo "See git-pull(1) for details."
170 echo "If you often $op_type $op_prep the same branch, you may want to"
171 echo "use something like the following in your configuration file:"
173 echo " [branch \"${curr_branch}\"]"
174 echo " remote = <nickname>"
175 echo " merge = <remote-ref>"
176 test rebase
= "$op_type" &&
177 echo " rebase = true"
179 echo " [remote \"<nickname>\"]"
181 echo " fetch = <refspec>"
183 echo "See git-config(1) for details."
185 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
186 echo "from the remote, but no such ref was fetched."
191 test true
= "$rebase" && {
192 if ! git rev-parse
-q --verify HEAD
>/dev
/null
194 # On an unborn branch
195 if test -f "$GIT_DIR/index"
197 die
"updating an unborn branch with changes added to the index"
200 git update-index
--ignore-submodules --refresh &&
201 git diff-files
--ignore-submodules --quiet &&
202 git diff-index
--ignore-submodules --cached --quiet HEAD
-- ||
203 die
"refusing to pull with rebase: your working tree is not up-to-date"
206 . git-parse-remote
&&
207 remoteref
="$(get_remote_merge_branch "$@
" 2>/dev/null)" &&
208 oldremoteref
="$(git rev-parse -q --verify "$remoteref")" &&
209 for reflog
in $
(git rev-list
-g $remoteref 2>/dev
/null
)
211 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
213 oldremoteref
="$reflog"
218 orig_head
=$
(git rev-parse
-q --verify HEAD
)
219 git fetch
$verbosity $progress --update-head-ok "$@" ||
exit 1
221 curr_head
=$
(git rev-parse
-q --verify HEAD
)
222 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
224 # The fetch involved updating the current branch.
226 # The working tree and the index file is still based on the
227 # $orig_head commit, but we are merging into $curr_head.
228 # First update the working tree to match $curr_head.
230 echo >&2 "Warning: fetch updated the current branch head."
231 echo >&2 "Warning: fast-forwarding your working tree from"
232 echo >&2 "Warning: commit $orig_head."
233 git update-index
-q --refresh
234 git read-tree
-u -m "$orig_head" "$curr_head" ||
235 die
'Cannot fast-forward your working tree.
236 After making sure that you saved anything precious from
237 $ git diff '$orig_head'
244 merge_head
=$
(sed -e '/ not-for-merge /d' \
245 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
248 case "$merge_head" in
250 error_on_no_merge_candidates
"$@"
253 if test -z "$orig_head"
255 die
"Cannot merge multiple branches into empty head"
257 if test true
= "$rebase"
259 die
"Cannot rebase onto multiple branches"
264 if test -z "$orig_head"
266 git update-ref
-m "initial pull" HEAD
$merge_head "$curr_head" &&
267 git read-tree
--reset -u HEAD ||
exit 1
271 merge_name
=$
(git fmt-merge-msg
$log_arg <"$GIT_DIR/FETCH_HEAD") ||
exit
274 eval="git-rebase $diffstat $strategy_args $merge_args"
275 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
278 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
279 eval="$eval $log_arg $strategy_args $merge_args"
280 eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity"