623ba7a84dc200172e081fbb8b7f94a16dcb3d94
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-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff|--ff-only] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
8LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
9SUBDIRECTORY_OK=Yes
10OPTIONS_SPEC=
11. git-sh-setup
12. git-sh-i18n
13set_reflog_action "pull${1+ $*}"
14require_work_tree_exists
15cd_to_toplevel
16
17
18die_conflict () {
19 git diff-index --cached --name-status -r --ignore-submodules HEAD --
20 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
21 die "$(gettext "Pull is not possible because you have unmerged files.
22Please, fix them up in the work tree, and then use 'git add/rm <file>'
23as appropriate to mark resolution and make a commit.")"
24 else
25 die "$(gettext "Pull is not possible because you have unmerged files.")"
26 fi
27}
28
29die_merge () {
30 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32Please, commit your changes before you can merge.")"
33 else
34 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
35 fi
36}
37
38test -z "$(git ls-files -u)" || die_conflict
39test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40
41bool_or_string_config () {
42 git config --bool "$1" 2>/dev/null || git config "$1"
43}
44
45strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
46log_arg= verbosity= progress= recurse_submodules= verify_signatures=
47merge_args= edit= rebase_args= all= append= upload_pack= force= tags= prune=
48keep= depth= unshallow= update_shallow= refmap=
49curr_branch=$(git symbolic-ref -q HEAD)
50curr_branch_short="${curr_branch#refs/heads/}"
51rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
52if test -z "$rebase"
53then
54 rebase=$(bool_or_string_config pull.rebase)
55fi
56
57# Setup default fast-forward options via `pull.ff`
58pull_ff=$(bool_or_string_config pull.ff)
59case "$pull_ff" in
60true)
61 no_ff=--ff
62 ;;
63false)
64 no_ff=--no-ff
65 ;;
66only)
67 ff_only=--ff-only
68 ;;
69esac
70
71
72dry_run=
73while :
74do
75 case "$1" in
76 -q|--quiet)
77 verbosity="$verbosity -q" ;;
78 -v|--verbose)
79 verbosity="$verbosity -v" ;;
80 --progress)
81 progress=--progress ;;
82 --no-progress)
83 progress=--no-progress ;;
84 -n|--no-stat|--no-summary)
85 diffstat=--no-stat ;;
86 --stat|--summary)
87 diffstat=--stat ;;
88 --log|--log=*|--no-log)
89 log_arg="$1" ;;
90 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
91 no_commit=--no-commit ;;
92 --c|--co|--com|--comm|--commi|--commit)
93 no_commit=--commit ;;
94 -e|--edit)
95 edit=--edit ;;
96 --no-edit)
97 edit=--no-edit ;;
98 --sq|--squ|--squa|--squas|--squash)
99 squash=--squash ;;
100 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
101 squash=--no-squash ;;
102 --ff)
103 no_ff=--ff ;;
104 --no-ff)
105 no_ff=--no-ff ;;
106 --ff-only)
107 ff_only=--ff-only ;;
108 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
109 --strateg=*|--strategy=*|\
110 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
111 case "$#,$1" in
112 *,*=*)
113 strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
114 1,*)
115 usage ;;
116 *)
117 strategy="$2"
118 shift ;;
119 esac
120 strategy_args="${strategy_args}-s $strategy "
121 ;;
122 -X*)
123 case "$#,$1" in
124 1,-X)
125 usage ;;
126 *,-X)
127 xx="-X $(git rev-parse --sq-quote "$2")"
128 shift ;;
129 *,*)
130 xx=$(git rev-parse --sq-quote "$1") ;;
131 esac
132 merge_args="$merge_args$xx "
133 ;;
134 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
135 rebase="${1#*=}"
136 ;;
137 -r|--r|--re|--reb|--reba|--rebas|--rebase)
138 rebase=true
139 ;;
140 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
141 rebase=false
142 ;;
143 --recurse-submodules)
144 recurse_submodules=--recurse-submodules
145 ;;
146 --recurse-submodules=*)
147 recurse_submodules="$1"
148 ;;
149 --no-recurse-submodules)
150 recurse_submodules=--no-recurse-submodules
151 ;;
152 --verify-signatures)
153 verify_signatures=--verify-signatures
154 ;;
155 --no-verify-signatures)
156 verify_signatures=--no-verify-signatures
157 ;;
158 --gpg-sign|-S)
159 gpg_sign_args=-S
160 ;;
161 --gpg-sign=*)
162 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
163 ;;
164 -S*)
165 gpg_sign_args=$(git rev-parse --sq-quote "$1")
166 ;;
167 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
168 dry_run=--dry-run
169 ;;
170 --all|--no-all)
171 all=$1 ;;
172 -a|--append|--no-append)
173 append=$1 ;;
174 --upload-pack=*|--no-upload-pack)
175 upload_pack=$1 ;;
176 -f|--force|--no-force)
177 force="$force $1" ;;
178 -t|--tags|--no-tags)
179 tags=$1 ;;
180 -p|--prune|--no-prune)
181 prune=$1 ;;
182 -k|--keep|--no-keep)
183 keep=$1 ;;
184 --depth=*|--no-depth)
185 depth=$1 ;;
186 --unshallow|--no-unshallow)
187 unshallow=$1 ;;
188 --update-shallow|--no-update-shallow)
189 update_shallow=$1 ;;
190 --refmap=*|--no-refmap)
191 refmap=$1 ;;
192 -h|--help-all)
193 usage
194 ;;
195 --)
196 shift
197 break
198 ;;
199 -*)
200 usage
201 ;;
202 *)
203 break
204 ;;
205 esac
206 shift
207done
208
209case "$rebase" in
210preserve)
211 rebase=true
212 rebase_args=--preserve-merges
213 ;;
214true|false|'')
215 ;;
216*)
217 echo "Invalid value for --rebase, should be true, false, or preserve"
218 usage
219 exit 1
220 ;;
221esac
222
223error_on_no_merge_candidates () {
224 exec >&2
225
226 if test true = "$rebase"
227 then
228 op_type=rebase
229 op_prep=against
230 else
231 op_type=merge
232 op_prep=with
233 fi
234
235 upstream=$(git config "branch.$curr_branch_short.merge")
236 remote=$(git config "branch.$curr_branch_short.remote")
237
238 if [ $# -gt 1 ]; then
239 if [ "$rebase" = true ]; then
240 printf "There is no candidate for rebasing against "
241 else
242 printf "There are no candidates for merging "
243 fi
244 echo "among the refs that you just fetched."
245 echo "Generally this means that you provided a wildcard refspec which had no"
246 echo "matches on the remote end."
247 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
248 echo "You asked to pull from the remote '$1', but did not specify"
249 echo "a branch. Because this is not the default configured remote"
250 echo "for your current branch, you must specify a branch on the command line."
251 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
252 . git-parse-remote
253 error_on_missing_default_upstream "pull" $op_type $op_prep \
254 "git pull <remote> <branch>"
255 else
256 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
257 echo "from the remote, but no such ref was fetched."
258 fi
259 exit 1
260}
261
262test true = "$rebase" && {
263 if ! git rev-parse -q --verify HEAD >/dev/null
264 then
265 # On an unborn branch
266 if test -f "$(git rev-parse --git-path index)"
267 then
268 die "$(gettext "updating an unborn branch with changes added to the index")"
269 fi
270 else
271 require_clean_work_tree "pull with rebase" "Please commit or stash them."
272 fi
273 oldremoteref= &&
274 test -n "$curr_branch" &&
275 . git-parse-remote &&
276 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
277 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
278}
279orig_head=$(git rev-parse -q --verify HEAD)
280git fetch $verbosity $progress $dry_run $recurse_submodules $all $append \
281$upload_pack $force $tags $prune $keep $depth $unshallow $update_shallow \
282$refmap --update-head-ok "$@" || exit 1
283test -z "$dry_run" || exit 0
284
285curr_head=$(git rev-parse -q --verify HEAD)
286if test -n "$orig_head" && test "$curr_head" != "$orig_head"
287then
288 # The fetch involved updating the current branch.
289
290 # The working tree and the index file is still based on the
291 # $orig_head commit, but we are merging into $curr_head.
292 # First update the working tree to match $curr_head.
293
294 eval_gettextln "Warning: fetch updated the current branch head.
295Warning: fast-forwarding your working tree from
296Warning: commit \$orig_head." >&2
297 git update-index -q --refresh
298 git read-tree -u -m "$orig_head" "$curr_head" ||
299 die "$(eval_gettext "Cannot fast-forward your working tree.
300After making sure that you saved anything precious from
301$ git diff \$orig_head
302output, run
303$ git reset --hard
304to recover.")"
305
306fi
307
308merge_head=$(sed -e '/ not-for-merge /d' \
309 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
310 tr '\012' ' ')
311
312case "$merge_head" in
313'')
314 error_on_no_merge_candidates "$@"
315 ;;
316?*' '?*)
317 if test -z "$orig_head"
318 then
319 die "$(gettext "Cannot merge multiple branches into empty head")"
320 fi
321 if test true = "$rebase"
322 then
323 die "$(gettext "Cannot rebase onto multiple branches")"
324 fi
325 ;;
326esac
327
328# Pulling into unborn branch: a shorthand for branching off
329# FETCH_HEAD, for lazy typers.
330if test -z "$orig_head"
331then
332 # Two-way merge: we claim the index is based on an empty tree,
333 # and try to fast-forward to HEAD. This ensures we will not
334 # lose index/worktree changes that the user already made on
335 # the unborn branch.
336 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
337 git read-tree -m -u $empty_tree $merge_head &&
338 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
339 exit
340fi
341
342if test true = "$rebase"
343then
344 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
345 if test "$oldremoteref" = "$o"
346 then
347 unset oldremoteref
348 fi
349fi
350
351case "$rebase" in
352true)
353 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
354 eval="$eval $gpg_sign_args"
355 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
356 ;;
357*)
358 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
359 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
360 eval="$eval $gpg_sign_args"
361 eval="$eval FETCH_HEAD"
362 ;;
363esac
364eval "exec $eval"