merge, pull: stop advising 'commit -a' in case of conflict
[git/git.git] / git-pull.sh
CommitLineData
839a7a06
LT
1#!/bin/sh
2#
521003ff
JH
3# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
ef93e3a4 7USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff|--ff-only] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
153d7265 8LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
533b7039 9SUBDIRECTORY_OK=Yes
8f321a39 10OPTIONS_SPEC=
ae2b0f15 11. git-sh-setup
a9f57868 12. git-sh-i18n
c98d1e41 13set_reflog_action "pull${1+ $*}"
035b5bf6 14require_work_tree_exists
533b7039 15cd_to_toplevel
b10ac50f 16
d38a30df
MM
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
85af5f82 21 die "$(gettext "Pull is not possible because you have unmerged files.
d38a30df 22Please, fix them up in the work tree, and then use 'git add/rm <file>'
91e70e00 23as appropriate to mark resolution and make a commit.")"
d38a30df 24 else
85af5f82 25 die "$(gettext "Pull is not possible because you have unmerged files.")"
d38a30df
MM
26 fi
27}
28
29die_merge () {
30 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
85af5f82
ÆAB
31 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32Please, commit your changes before you can merge.")"
d38a30df 33 else
85af5f82 34 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
d38a30df
MM
35 fi
36}
37
38test -z "$(git ls-files -u)" || die_conflict
39test -f "$GIT_DIR/MERGE_HEAD" && die_merge
d1014a17 40
66713ef3
SH
41bool_or_string_config () {
42 git config --bool "$1" 2>/dev/null || git config "$1"
43}
44
13474835 45strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
efed0022 46log_arg= verbosity= progress= recurse_submodules= verify_signatures=
66713ef3 47merge_args= edit= rebase_args=
cd67e4d4 48curr_branch=$(git symbolic-ref -q HEAD)
0d12e59f 49curr_branch_short="${curr_branch#refs/heads/}"
66713ef3 50rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
6b37dff1
ÆAB
51if test -z "$rebase"
52then
66713ef3 53 rebase=$(bool_or_string_config pull.rebase)
6b37dff1 54fi
b814da89
DA
55
56# Setup default fast-forward options via `pull.ff`
57pull_ff=$(git config pull.ff)
58case "$pull_ff" in
59false)
60 no_ff=--no-ff
b814da89
DA
61 ;;
62only)
63 ff_only=--ff-only
b814da89
DA
64 ;;
65esac
66
67
29609e68 68dry_run=
822f7c73 69while :
60fb5b2c
JH
70do
71 case "$1" in
7f87aff2 72 -q|--quiet)
c6576f91 73 verbosity="$verbosity -q" ;;
7f87aff2 74 -v|--verbose)
c6576f91 75 verbosity="$verbosity -v" ;;
9839018e
TRC
76 --progress)
77 progress=--progress ;;
bebd2fd7
JK
78 --no-progress)
79 progress=--no-progress ;;
d8abe148 80 -n|--no-stat|--no-summary)
a334e125 81 diffstat=--no-stat ;;
d8abe148 82 --stat|--summary)
a334e125 83 diffstat=--stat ;;
efb779f8
SG
84 --log|--no-log)
85 log_arg=$1 ;;
123ee3ca
JH
86 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
87 no_commit=--no-commit ;;
5072a323
LH
88 --c|--co|--com|--comm|--commi|--commit)
89 no_commit=--commit ;;
85808300
LT
90 -e|--edit)
91 edit=--edit ;;
92 --no-edit)
93 edit=--no-edit ;;
7d0c6887
JH
94 --sq|--squ|--squa|--squas|--squash)
95 squash=--squash ;;
5072a323
LH
96 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
97 squash=--no-squash ;;
98 --ff)
99 no_ff=--ff ;;
100 --no-ff)
101 no_ff=--no-ff ;;
13474835
BG
102 --ff-only)
103 ff_only=--ff-only ;;
60fb5b2c
JH
104 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
105 --strateg=*|--strategy=*|\
106 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
107 case "$#,$1" in
108 *,*=*)
eadf619c 109 strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
60fb5b2c
JH
110 1,*)
111 usage ;;
112 *)
113 strategy="$2"
114 shift ;;
115 esac
116 strategy_args="${strategy_args}-s $strategy "
117 ;;
ee2c7955
AP
118 -X*)
119 case "$#,$1" in
120 1,-X)
121 usage ;;
122 *,-X)
14e5d40c 123 xx="-X $(git rev-parse --sq-quote "$2")"
ee2c7955
AP
124 shift ;;
125 *,*)
14e5d40c 126 xx=$(git rev-parse --sq-quote "$1") ;;
ee2c7955
AP
127 esac
128 merge_args="$merge_args$xx "
129 ;;
66713ef3
SH
130 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
131 rebase="${1#*=}"
132 ;;
cd67e4d4
JS
133 -r|--r|--re|--reb|--reba|--rebas|--rebase)
134 rebase=true
135 ;;
136 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
137 rebase=false
138 ;;
7dce19d3
JL
139 --recurse-submodules)
140 recurse_submodules=--recurse-submodules
141 ;;
8f0700dd
JL
142 --recurse-submodules=*)
143 recurse_submodules="$1"
144 ;;
be254a0e
JL
145 --no-recurse-submodules)
146 recurse_submodules=--no-recurse-submodules
147 ;;
efed0022
SG
148 --verify-signatures)
149 verify_signatures=--verify-signatures
150 ;;
151 --no-verify-signatures)
152 verify_signatures=--no-verify-signatures
153 ;;
ea230d8b 154 --gpg-sign|-S)
155 gpg_sign_args=-S
156 ;;
157 --gpg-sign=*)
158 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
159 ;;
160 -S*)
161 gpg_sign_args=$(git rev-parse --sq-quote "$1")
162 ;;
29609e68
JK
163 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
164 dry_run=--dry-run
165 ;;
87182b17 166 -h|--help-all)
93d69d86
JL
167 usage
168 ;;
822f7c73
DK
169 *)
170 # Pass thru anything that may be meant for fetch.
619e5a0e 171 break
60fb5b2c
JH
172 ;;
173 esac
174 shift
175done
176
66713ef3
SH
177case "$rebase" in
178preserve)
179 rebase=true
180 rebase_args=--preserve-merges
181 ;;
182true|false|'')
183 ;;
184*)
185 echo "Invalid value for --rebase, should be true, false, or preserve"
186 usage
187 exit 1
188 ;;
189esac
190
441ed413
JH
191error_on_no_merge_candidates () {
192 exec >&2
193 for opt
194 do
195 case "$opt" in
196 -t|--t|--ta|--tag|--tags)
c5a84e92 197 echo "It doesn't make sense to pull all tags; you probably meant:"
441ed413
JH
198 echo " git fetch --tags"
199 exit 1
200 esac
201 done
202
995fc2f7
JK
203 if test true = "$rebase"
204 then
205 op_type=rebase
206 op_prep=against
207 else
208 op_type=merge
209 op_prep=with
210 fi
211
b07f7296
RS
212 upstream=$(git config "branch.$curr_branch_short.merge")
213 remote=$(git config "branch.$curr_branch_short.remote")
441ed413 214
a8c9bef4 215 if [ $# -gt 1 ]; then
995fc2f7
JK
216 if [ "$rebase" = true ]; then
217 printf "There is no candidate for rebasing against "
218 else
219 printf "There are no candidates for merging "
220 fi
221 echo "among the refs that you just fetched."
a8c9bef4
JK
222 echo "Generally this means that you provided a wildcard refspec which had no"
223 echo "matches on the remote end."
224 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
225 echo "You asked to pull from the remote '$1', but did not specify"
995fc2f7 226 echo "a branch. Because this is not the default configured remote"
a8c9bef4 227 echo "for your current branch, you must specify a branch on the command line."
15a147e6
MZ
228 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
229 . git-parse-remote
230 error_on_missing_default_upstream "pull" $op_type $op_prep \
3c02396a 231 "git pull <remote> <branch>"
a6dbf881 232 else
995fc2f7
JK
233 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
234 echo "from the remote, but no such ref was fetched."
61e6108d 235 fi
441ed413
JH
236 exit 1
237}
238
c85c7927 239test true = "$rebase" && {
19a7fcbf
JK
240 if ! git rev-parse -q --verify HEAD >/dev/null
241 then
242 # On an unborn branch
243 if test -f "$GIT_DIR/index"
244 then
85af5f82 245 die "$(gettext "updating an unborn branch with changes added to the index")"
19a7fcbf
JK
246 fi
247 else
92c62a3f 248 require_clean_work_tree "pull with rebase" "Please commit or stash them."
19a7fcbf 249 fi
d44e7126 250 oldremoteref= &&
e980765c 251 test -n "$curr_branch" &&
c85c7927 252 . git-parse-remote &&
d44e7126 253 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
ffc2b483 254 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
c85c7927 255}
2d179857 256orig_head=$(git rev-parse -q --verify HEAD)
7dce19d3 257git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
29609e68 258test -z "$dry_run" || exit 0
b10ac50f 259
2d179857 260curr_head=$(git rev-parse -q --verify HEAD)
b0ad11ea 261if test -n "$orig_head" && test "$curr_head" != "$orig_head"
b10ac50f
JH
262then
263 # The fetch involved updating the current branch.
264
265 # The working tree and the index file is still based on the
266 # $orig_head commit, but we are merging into $curr_head.
267 # First update the working tree to match $curr_head.
268
c2b1a95c 269 eval_gettextln "Warning: fetch updated the current branch head.
3320a973 270Warning: fast-forwarding your working tree from
c2b1a95c 271Warning: commit \$orig_head." >&2
7bd93c1c 272 git update-index -q --refresh
5be60078 273 git read-tree -u -m "$orig_head" "$curr_head" ||
9f35aaa9 274 die "$(eval_gettext "Cannot fast-forward your working tree.
8323124a 275After making sure that you saved anything precious from
9f35aaa9 276$ git diff \$orig_head
8323124a
JH
277output, run
278$ git reset --hard
9f35aaa9 279to recover.")"
8323124a 280
b10ac50f
JH
281fi
282
05dd8e2e
JH
283merge_head=$(sed -e '/ not-for-merge /d' \
284 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
285 tr '\012' ' ')
e0bfc81e
JH
286
287case "$merge_head" in
521003ff 288'')
4973aa22 289 error_on_no_merge_candidates "$@"
521003ff 290 ;;
60fb5b2c 291?*' '?*)
d09e79cb
LT
292 if test -z "$orig_head"
293 then
85af5f82 294 die "$(gettext "Cannot merge multiple branches into empty head")"
d09e79cb 295 fi
51b2ead0
JS
296 if test true = "$rebase"
297 then
85af5f82 298 die "$(gettext "Cannot rebase onto multiple branches")"
51b2ead0 299 fi
60fb5b2c
JH
300 ;;
301esac
302
b4dc085a
TR
303# Pulling into unborn branch: a shorthand for branching off
304# FETCH_HEAD, for lazy typers.
d09e79cb
LT
305if test -z "$orig_head"
306then
b4dc085a
TR
307 # Two-way merge: we claim the index is based on an empty tree,
308 # and try to fast-forward to HEAD. This ensures we will not
309 # lose index/worktree changes that the user already made on
310 # the unborn branch.
311 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
312 git read-tree -m -u $empty_tree $merge_head &&
9f48f2bd 313 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
d09e79cb
LT
314 exit
315fi
316
cf65426d
EN
317if test true = "$rebase"
318then
319 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
320 if test "$oldremoteref" = "$o"
321 then
322 unset oldremoteref
323 fi
324fi
325
efb779f8 326merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
14e5d40c
JH
327case "$rebase" in
328true)
66713ef3 329 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
ea230d8b 330 eval="$eval $gpg_sign_args"
14e5d40c
JH
331 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
332 ;;
333*)
efed0022 334 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
ea230d8b 335 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
336 eval="$eval $gpg_sign_args"
bebd2fd7 337 eval="$eval \"\$merge_name\" HEAD $merge_head"
14e5d40c
JH
338 ;;
339esac
340eval "exec $eval"