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