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
= recurse_submodules
=
43 curr_branch
=$
(git symbolic-ref
-q HEAD
)
44 curr_branch_short
="${curr_branch#refs/heads/}"
45 rebase
=$
(git config
--bool branch.
$curr_branch_short.rebase
)
51 verbosity
="$verbosity -q" ;;
53 verbosity
="$verbosity -v" ;;
55 progress
=--progress ;;
57 progress
=--no-progress ;;
58 -n|
--no-stat|
--no-summary)
64 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
65 no_commit
=--no-commit ;;
66 --c|
--co|
--com|
--comm|
--commi|
--commit)
68 --sq|
--squ|
--squa|
--squas|
--squash)
70 --no-sq|
--no-squ|
--no-squa|
--no-squas|
--no-squash)
78 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
79 --strateg=*|
--strategy=*|\
80 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
83 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
90 strategy_args
="${strategy_args}-s $strategy "
97 xx
="-X $(git rev-parse --sq-quote "$2")"
100 xx
=$
(git rev-parse
--sq-quote "$1") ;;
102 merge_args
="$merge_args$xx "
104 -r|
--r|
--re|
--reb|
--reba|
--rebas|
--rebase)
107 --no-r|
--no-re|
--no-reb|
--no-reba|
--no-rebas|
--no-rebase)
110 --recurse-submodules)
111 recurse_submodules
=--recurse-submodules
113 --recurse-submodules=*)
114 recurse_submodules
="$1"
116 --no-recurse-submodules)
117 recurse_submodules
=--no-recurse-submodules
119 --d|
--dr|
--dry|
--dry-|
--dry-r|
--dry-ru|
--dry-run)
122 -h|
--h|
--he|
--hel|
--help|
--help-|
--help-a|
--help-al|
--help-all)
126 # Pass thru anything that may be meant for fetch.
133 error_on_no_merge_candidates
() {
138 -t|
--t|
--ta|
--tag|
--tags)
139 echo "Fetching tags only, you probably meant:"
140 echo " git fetch --tags"
145 if test true
= "$rebase"
154 curr_branch
=${curr_branch#refs/heads/}
155 upstream
=$
(git config
"branch.$curr_branch.merge")
156 remote
=$
(git config
"branch.$curr_branch.remote")
158 if [ $# -gt 1 ]; then
159 if [ "$rebase" = true
]; then
160 printf "There is no candidate for rebasing against "
162 printf "There are no candidates for merging "
164 echo "among the refs that you just fetched."
165 echo "Generally this means that you provided a wildcard refspec which had no"
166 echo "matches on the remote end."
167 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
168 echo "You asked to pull from the remote '$1', but did not specify"
169 echo "a branch. Because this is not the default configured remote"
170 echo "for your current branch, you must specify a branch on the command line."
171 elif [ -z "$curr_branch" ]; then
172 echo "You are not currently on a branch, so I cannot use any"
173 echo "'branch.<branchname>.merge' in your configuration file."
174 echo "Please specify which remote branch you want to use on the command"
175 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
176 echo "See git-pull(1) for details."
177 elif [ -z "$upstream" ]; then
178 echo "You asked me to pull without telling me which branch you"
179 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
180 echo "your configuration file does not tell me, either. Please"
181 echo "specify which branch you want to use on the command line and"
182 echo "try again (e.g. 'git pull <repository> <refspec>')."
183 echo "See git-pull(1) for details."
185 echo "If you often $op_type $op_prep the same branch, you may want to"
186 echo "use something like the following in your configuration file:"
188 echo " [branch \"${curr_branch}\"]"
189 echo " remote = <nickname>"
190 echo " merge = <remote-ref>"
191 test rebase
= "$op_type" &&
192 echo " rebase = true"
194 echo " [remote \"<nickname>\"]"
196 echo " fetch = <refspec>"
198 echo "See git-config(1) for details."
200 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
201 echo "from the remote, but no such ref was fetched."
206 test true
= "$rebase" && {
207 if ! git rev-parse
-q --verify HEAD
>/dev
/null
209 # On an unborn branch
210 if test -f "$GIT_DIR/index"
212 die
"updating an unborn branch with changes added to the index"
215 require_clean_work_tree
"pull with rebase" "Please commit or stash them."
218 . git-parse-remote
&&
219 remoteref
="$(get_remote_merge_branch "$@
" 2>/dev/null)" &&
220 oldremoteref
="$(git rev-parse -q --verify "$remoteref")" &&
221 for reflog
in $
(git rev-list
-g $remoteref 2>/dev
/null
)
223 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
225 oldremoteref
="$reflog"
230 orig_head
=$
(git rev-parse
-q --verify HEAD
)
231 git fetch
$verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" ||
exit 1
232 test -z "$dry_run" ||
exit 0
234 curr_head
=$
(git rev-parse
-q --verify HEAD
)
235 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
237 # The fetch involved updating the current branch.
239 # The working tree and the index file is still based on the
240 # $orig_head commit, but we are merging into $curr_head.
241 # First update the working tree to match $curr_head.
243 echo >&2 "Warning: fetch updated the current branch head."
244 echo >&2 "Warning: fast-forwarding your working tree from"
245 echo >&2 "Warning: commit $orig_head."
246 git update-index
-q --refresh
247 git read-tree
-u -m "$orig_head" "$curr_head" ||
248 die
'Cannot fast-forward your working tree.
249 After making sure that you saved anything precious from
250 $ git diff '$orig_head'
257 merge_head
=$
(sed -e '/ not-for-merge /d' \
258 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
261 case "$merge_head" in
263 error_on_no_merge_candidates
"$@"
266 if test -z "$orig_head"
268 die
"Cannot merge multiple branches into empty head"
270 if test true
= "$rebase"
272 die
"Cannot rebase onto multiple branches"
277 if test -z "$orig_head"
279 git update-ref
-m "initial pull" HEAD
$merge_head "$curr_head" &&
280 git read-tree
-m -u HEAD ||
exit 1
284 if test true
= "$rebase"
286 o
=$
(git show-branch
--merge-base $curr_branch $merge_head $oldremoteref)
287 if test "$oldremoteref" = "$o"
293 merge_name
=$
(git fmt-merge-msg
$log_arg <"$GIT_DIR/FETCH_HEAD") ||
exit
296 eval="git-rebase $diffstat $strategy_args $merge_args"
297 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
300 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
301 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
302 eval="$eval \"\$merge_name\" HEAD $merge_head"