]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
l10n: TEAMS: stash inactive zh_CN team members
[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
533b7039 7SUBDIRECTORY_OK=Yes
e3b601da
PT
8OPTIONS_KEEPDASHDASH=
9OPTIONS_STUCKLONG=Yes
10OPTIONS_SPEC="\
11git pull [options] [<repository> [<refspec>...]]
12
13Fetch one or more remote refs and integrate it/them with the current HEAD.
14--
15v,verbose be more verbose
16q,quiet be more quiet
17progress force progress reporting
18
19 Options related to merging
20r,rebase?false|true|preserve incorporate changes by rebasing rather than merging
21n! do not show a diffstat at the end of the merge
22stat show a diffstat at the end of the merge
23summary (synonym to --stat)
24log?n add (at most <n>) entries from shortlog to merge commit message
25squash create a single commit instead of doing a merge
26commit perform a commit if the merge succeeds (default)
27e,edit edit message before committing
28ff allow fast-forward
29ff-only! abort if fast-forward is not possible
30verify-signatures verify that the named commit has a valid GPG signature
31s,strategy=strategy merge strategy to use
32X,strategy-option=option option for selected merge strategy
33S,gpg-sign?key-id GPG sign commit
34
35 Options related to fetching
36all fetch from all remotes
37a,append append to .git/FETCH_HEAD instead of overwriting
38upload-pack=path path to upload pack on remote end
39f,force force overwrite of local branch
40t,tags fetch all tags and associated objects
41p,prune prune remote-tracking branches no longer on remote
42recurse-submodules?on-demand control recursive fetching of submodules
43dry-run dry run
44k,keep keep downloaded pack
45depth=depth deepen history of shallow clone
46unshallow convert to a complete repository
47update-shallow accept refs that update .git/shallow
48refmap=refmap specify fetch refmap
49"
50test $# -gt 0 && args="$*"
ae2b0f15 51. git-sh-setup
a9f57868 52. git-sh-i18n
e3b601da 53set_reflog_action "pull${args+ $args}"
035b5bf6 54require_work_tree_exists
533b7039 55cd_to_toplevel
b10ac50f 56
d38a30df
MM
57
58die_conflict () {
59 git diff-index --cached --name-status -r --ignore-submodules HEAD --
60 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
85af5f82 61 die "$(gettext "Pull is not possible because you have unmerged files.
d38a30df 62Please, fix them up in the work tree, and then use 'git add/rm <file>'
91e70e00 63as appropriate to mark resolution and make a commit.")"
d38a30df 64 else
85af5f82 65 die "$(gettext "Pull is not possible because you have unmerged files.")"
d38a30df
MM
66 fi
67}
68
69die_merge () {
70 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
85af5f82
ÆAB
71 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
72Please, commit your changes before you can merge.")"
d38a30df 73 else
85af5f82 74 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
d38a30df
MM
75 fi
76}
77
78test -z "$(git ls-files -u)" || die_conflict
79test -f "$GIT_DIR/MERGE_HEAD" && die_merge
d1014a17 80
66713ef3
SH
81bool_or_string_config () {
82 git config --bool "$1" 2>/dev/null || git config "$1"
83}
84
13474835 85strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
efed0022 86log_arg= verbosity= progress= recurse_submodules= verify_signatures=
eb2a8d9e
PT
87merge_args= edit= rebase_args= all= append= upload_pack= force= tags= prune=
88keep= depth= unshallow= update_shallow= refmap=
cd67e4d4 89curr_branch=$(git symbolic-ref -q HEAD)
0d12e59f 90curr_branch_short="${curr_branch#refs/heads/}"
66713ef3 91rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
6b37dff1
ÆAB
92if test -z "$rebase"
93then
66713ef3 94 rebase=$(bool_or_string_config pull.rebase)
6b37dff1 95fi
b814da89
DA
96
97# Setup default fast-forward options via `pull.ff`
db9bb280 98pull_ff=$(bool_or_string_config pull.ff)
b814da89 99case "$pull_ff" in
eb8dc05c
PT
100true)
101 no_ff=--ff
102 ;;
b814da89
DA
103false)
104 no_ff=--no-ff
b814da89
DA
105 ;;
106only)
107 ff_only=--ff-only
b814da89
DA
108 ;;
109esac
110
111
29609e68 112dry_run=
822f7c73 113while :
60fb5b2c
JH
114do
115 case "$1" in
7f87aff2 116 -q|--quiet)
c6576f91 117 verbosity="$verbosity -q" ;;
7f87aff2 118 -v|--verbose)
c6576f91 119 verbosity="$verbosity -v" ;;
9839018e
TRC
120 --progress)
121 progress=--progress ;;
bebd2fd7
JK
122 --no-progress)
123 progress=--no-progress ;;
d8abe148 124 -n|--no-stat|--no-summary)
a334e125 125 diffstat=--no-stat ;;
d8abe148 126 --stat|--summary)
a334e125 127 diffstat=--stat ;;
5061a44b
PT
128 --log|--log=*|--no-log)
129 log_arg="$1" ;;
e3b601da 130 --no-commit)
123ee3ca 131 no_commit=--no-commit ;;
e3b601da 132 --commit)
5072a323 133 no_commit=--commit ;;
85808300
LT
134 -e|--edit)
135 edit=--edit ;;
136 --no-edit)
137 edit=--no-edit ;;
e3b601da 138 --squash)
7d0c6887 139 squash=--squash ;;
e3b601da 140 --no-squash)
5072a323
LH
141 squash=--no-squash ;;
142 --ff)
143 no_ff=--ff ;;
144 --no-ff)
145 no_ff=--no-ff ;;
13474835
BG
146 --ff-only)
147 ff_only=--ff-only ;;
e3b601da
PT
148 -s*|--strategy=*)
149 strategy_args="$strategy_args $1"
150 ;;
151 -X*|--strategy-option=*)
152 merge_args="$merge_args $(git rev-parse --sq-quote "$1")"
153 ;;
154 -r*|--rebase=*)
66713ef3
SH
155 rebase="${1#*=}"
156 ;;
e3b601da 157 --rebase)
cd67e4d4
JS
158 rebase=true
159 ;;
e3b601da 160 --no-rebase)
cd67e4d4
JS
161 rebase=false
162 ;;
7dce19d3
JL
163 --recurse-submodules)
164 recurse_submodules=--recurse-submodules
165 ;;
8f0700dd
JL
166 --recurse-submodules=*)
167 recurse_submodules="$1"
168 ;;
be254a0e
JL
169 --no-recurse-submodules)
170 recurse_submodules=--no-recurse-submodules
171 ;;
efed0022
SG
172 --verify-signatures)
173 verify_signatures=--verify-signatures
174 ;;
175 --no-verify-signatures)
176 verify_signatures=--no-verify-signatures
177 ;;
ea230d8b 178 --gpg-sign|-S)
179 gpg_sign_args=-S
180 ;;
181 --gpg-sign=*)
182 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
183 ;;
184 -S*)
185 gpg_sign_args=$(git rev-parse --sq-quote "$1")
186 ;;
e3b601da 187 --dry-run)
29609e68
JK
188 dry_run=--dry-run
189 ;;
eb2a8d9e
PT
190 --all|--no-all)
191 all=$1 ;;
192 -a|--append|--no-append)
193 append=$1 ;;
194 --upload-pack=*|--no-upload-pack)
195 upload_pack=$1 ;;
196 -f|--force|--no-force)
197 force="$force $1" ;;
198 -t|--tags|--no-tags)
199 tags=$1 ;;
200 -p|--prune|--no-prune)
201 prune=$1 ;;
202 -k|--keep|--no-keep)
203 keep=$1 ;;
204 --depth=*|--no-depth)
205 depth=$1 ;;
206 --unshallow|--no-unshallow)
207 unshallow=$1 ;;
208 --update-shallow|--no-update-shallow)
209 update_shallow=$1 ;;
210 --refmap=*|--no-refmap)
211 refmap=$1 ;;
87182b17 212 -h|--help-all)
93d69d86
JL
213 usage
214 ;;
eb2a8d9e
PT
215 --)
216 shift
217 break
218 ;;
822f7c73 219 *)
e3b601da 220 usage
60fb5b2c
JH
221 ;;
222 esac
223 shift
224done
225
66713ef3
SH
226case "$rebase" in
227preserve)
228 rebase=true
229 rebase_args=--preserve-merges
230 ;;
231true|false|'')
232 ;;
233*)
234 echo "Invalid value for --rebase, should be true, false, or preserve"
235 usage
236 exit 1
237 ;;
238esac
239
441ed413
JH
240error_on_no_merge_candidates () {
241 exec >&2
441ed413 242
995fc2f7
JK
243 if test true = "$rebase"
244 then
245 op_type=rebase
246 op_prep=against
247 else
248 op_type=merge
249 op_prep=with
250 fi
251
b07f7296
RS
252 upstream=$(git config "branch.$curr_branch_short.merge")
253 remote=$(git config "branch.$curr_branch_short.remote")
441ed413 254
a8c9bef4 255 if [ $# -gt 1 ]; then
995fc2f7
JK
256 if [ "$rebase" = true ]; then
257 printf "There is no candidate for rebasing against "
258 else
259 printf "There are no candidates for merging "
260 fi
261 echo "among the refs that you just fetched."
a8c9bef4
JK
262 echo "Generally this means that you provided a wildcard refspec which had no"
263 echo "matches on the remote end."
264 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
265 echo "You asked to pull from the remote '$1', but did not specify"
995fc2f7 266 echo "a branch. Because this is not the default configured remote"
a8c9bef4 267 echo "for your current branch, you must specify a branch on the command line."
15a147e6
MZ
268 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
269 . git-parse-remote
270 error_on_missing_default_upstream "pull" $op_type $op_prep \
3c02396a 271 "git pull <remote> <branch>"
a6dbf881 272 else
995fc2f7
JK
273 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
274 echo "from the remote, but no such ref was fetched."
61e6108d 275 fi
441ed413
JH
276 exit 1
277}
278
c85c7927 279test true = "$rebase" && {
19a7fcbf
JK
280 if ! git rev-parse -q --verify HEAD >/dev/null
281 then
282 # On an unborn branch
c697b577 283 if test -f "$(git rev-parse --git-path index)"
19a7fcbf 284 then
85af5f82 285 die "$(gettext "updating an unborn branch with changes added to the index")"
19a7fcbf
JK
286 fi
287 else
92c62a3f 288 require_clean_work_tree "pull with rebase" "Please commit or stash them."
19a7fcbf 289 fi
d44e7126 290 oldremoteref= &&
e980765c 291 test -n "$curr_branch" &&
c85c7927 292 . git-parse-remote &&
d44e7126 293 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
ffc2b483 294 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
c85c7927 295}
2d179857 296orig_head=$(git rev-parse -q --verify HEAD)
eb2a8d9e
PT
297git fetch $verbosity $progress $dry_run $recurse_submodules $all $append \
298$upload_pack $force $tags $prune $keep $depth $unshallow $update_shallow \
299$refmap --update-head-ok "$@" || exit 1
29609e68 300test -z "$dry_run" || exit 0
b10ac50f 301
2d179857 302curr_head=$(git rev-parse -q --verify HEAD)
b0ad11ea 303if test -n "$orig_head" && test "$curr_head" != "$orig_head"
b10ac50f
JH
304then
305 # The fetch involved updating the current branch.
306
307 # The working tree and the index file is still based on the
308 # $orig_head commit, but we are merging into $curr_head.
309 # First update the working tree to match $curr_head.
310
c2b1a95c 311 eval_gettextln "Warning: fetch updated the current branch head.
3320a973 312Warning: fast-forwarding your working tree from
c2b1a95c 313Warning: commit \$orig_head." >&2
7bd93c1c 314 git update-index -q --refresh
5be60078 315 git read-tree -u -m "$orig_head" "$curr_head" ||
9f35aaa9 316 die "$(eval_gettext "Cannot fast-forward your working tree.
8323124a 317After making sure that you saved anything precious from
9f35aaa9 318$ git diff \$orig_head
8323124a
JH
319output, run
320$ git reset --hard
9f35aaa9 321to recover.")"
8323124a 322
b10ac50f
JH
323fi
324
05dd8e2e
JH
325merge_head=$(sed -e '/ not-for-merge /d' \
326 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
327 tr '\012' ' ')
e0bfc81e
JH
328
329case "$merge_head" in
521003ff 330'')
4973aa22 331 error_on_no_merge_candidates "$@"
521003ff 332 ;;
60fb5b2c 333?*' '?*)
d09e79cb
LT
334 if test -z "$orig_head"
335 then
85af5f82 336 die "$(gettext "Cannot merge multiple branches into empty head")"
d09e79cb 337 fi
51b2ead0
JS
338 if test true = "$rebase"
339 then
85af5f82 340 die "$(gettext "Cannot rebase onto multiple branches")"
51b2ead0 341 fi
60fb5b2c
JH
342 ;;
343esac
344
b4dc085a
TR
345# Pulling into unborn branch: a shorthand for branching off
346# FETCH_HEAD, for lazy typers.
d09e79cb
LT
347if test -z "$orig_head"
348then
b4dc085a
TR
349 # Two-way merge: we claim the index is based on an empty tree,
350 # and try to fast-forward to HEAD. This ensures we will not
351 # lose index/worktree changes that the user already made on
352 # the unborn branch.
353 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
354 git read-tree -m -u $empty_tree $merge_head &&
9f48f2bd 355 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
d09e79cb
LT
356 exit
357fi
358
cf65426d
EN
359if test true = "$rebase"
360then
361 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
362 if test "$oldremoteref" = "$o"
363 then
364 unset oldremoteref
365 fi
366fi
367
14e5d40c
JH
368case "$rebase" in
369true)
66713ef3 370 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
ea230d8b 371 eval="$eval $gpg_sign_args"
14e5d40c
JH
372 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
373 ;;
374*)
efed0022 375 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
ea230d8b 376 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
377 eval="$eval $gpg_sign_args"
d45366e8 378 eval="$eval FETCH_HEAD"
14e5d40c
JH
379 ;;
380esac
381eval "exec $eval"