]>
Commit | Line | Data |
---|---|---|
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 | 7 | SUBDIRECTORY_OK=Yes |
e3b601da PT |
8 | OPTIONS_KEEPDASHDASH= |
9 | OPTIONS_STUCKLONG=Yes | |
10 | OPTIONS_SPEC="\ | |
11 | git pull [options] [<repository> [<refspec>...]] | |
12 | ||
13 | Fetch one or more remote refs and integrate it/them with the current HEAD. | |
14 | -- | |
15 | v,verbose be more verbose | |
16 | q,quiet be more quiet | |
17 | progress force progress reporting | |
18 | ||
19 | Options related to merging | |
20 | r,rebase?false|true|preserve incorporate changes by rebasing rather than merging | |
21 | n! do not show a diffstat at the end of the merge | |
22 | stat show a diffstat at the end of the merge | |
23 | summary (synonym to --stat) | |
24 | log?n add (at most <n>) entries from shortlog to merge commit message | |
25 | squash create a single commit instead of doing a merge | |
26 | commit perform a commit if the merge succeeds (default) | |
27 | e,edit edit message before committing | |
28 | ff allow fast-forward | |
29 | ff-only! abort if fast-forward is not possible | |
30 | verify-signatures verify that the named commit has a valid GPG signature | |
31 | s,strategy=strategy merge strategy to use | |
32 | X,strategy-option=option option for selected merge strategy | |
33 | S,gpg-sign?key-id GPG sign commit | |
34 | ||
35 | Options related to fetching | |
36 | all fetch from all remotes | |
37 | a,append append to .git/FETCH_HEAD instead of overwriting | |
38 | upload-pack=path path to upload pack on remote end | |
39 | f,force force overwrite of local branch | |
40 | t,tags fetch all tags and associated objects | |
41 | p,prune prune remote-tracking branches no longer on remote | |
42 | recurse-submodules?on-demand control recursive fetching of submodules | |
43 | dry-run dry run | |
44 | k,keep keep downloaded pack | |
45 | depth=depth deepen history of shallow clone | |
46 | unshallow convert to a complete repository | |
47 | update-shallow accept refs that update .git/shallow | |
48 | refmap=refmap specify fetch refmap | |
49 | " | |
50 | test $# -gt 0 && args="$*" | |
ae2b0f15 | 51 | . git-sh-setup |
a9f57868 | 52 | . git-sh-i18n |
e3b601da | 53 | set_reflog_action "pull${args+ $args}" |
035b5bf6 | 54 | require_work_tree_exists |
533b7039 | 55 | cd_to_toplevel |
b10ac50f | 56 | |
d38a30df MM |
57 | |
58 | die_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 | 62 | Please, fix them up in the work tree, and then use 'git add/rm <file>' |
91e70e00 | 63 | as 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 | ||
69 | die_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). |
72 | Please, 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 | ||
78 | test -z "$(git ls-files -u)" || die_conflict | |
79 | test -f "$GIT_DIR/MERGE_HEAD" && die_merge | |
d1014a17 | 80 | |
66713ef3 SH |
81 | bool_or_string_config () { |
82 | git config --bool "$1" 2>/dev/null || git config "$1" | |
83 | } | |
84 | ||
13474835 | 85 | strategy_args= diffstat= no_commit= squash= no_ff= ff_only= |
efed0022 | 86 | log_arg= verbosity= progress= recurse_submodules= verify_signatures= |
eb2a8d9e PT |
87 | merge_args= edit= rebase_args= all= append= upload_pack= force= tags= prune= |
88 | keep= depth= unshallow= update_shallow= refmap= | |
cd67e4d4 | 89 | curr_branch=$(git symbolic-ref -q HEAD) |
0d12e59f | 90 | curr_branch_short="${curr_branch#refs/heads/}" |
66713ef3 | 91 | rebase=$(bool_or_string_config branch.$curr_branch_short.rebase) |
6b37dff1 ÆAB |
92 | if test -z "$rebase" |
93 | then | |
66713ef3 | 94 | rebase=$(bool_or_string_config pull.rebase) |
6b37dff1 | 95 | fi |
b814da89 DA |
96 | |
97 | # Setup default fast-forward options via `pull.ff` | |
db9bb280 | 98 | pull_ff=$(bool_or_string_config pull.ff) |
b814da89 | 99 | case "$pull_ff" in |
eb8dc05c PT |
100 | true) |
101 | no_ff=--ff | |
102 | ;; | |
b814da89 DA |
103 | false) |
104 | no_ff=--no-ff | |
b814da89 DA |
105 | ;; |
106 | only) | |
107 | ff_only=--ff-only | |
b814da89 DA |
108 | ;; |
109 | esac | |
110 | ||
111 | ||
29609e68 | 112 | dry_run= |
822f7c73 | 113 | while : |
60fb5b2c JH |
114 | do |
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 | |
224 | done | |
225 | ||
66713ef3 SH |
226 | case "$rebase" in |
227 | preserve) | |
228 | rebase=true | |
229 | rebase_args=--preserve-merges | |
230 | ;; | |
231 | true|false|'') | |
232 | ;; | |
233 | *) | |
234 | echo "Invalid value for --rebase, should be true, false, or preserve" | |
235 | usage | |
236 | exit 1 | |
237 | ;; | |
238 | esac | |
239 | ||
441ed413 JH |
240 | error_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 | 279 | test 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 | 296 | orig_head=$(git rev-parse -q --verify HEAD) |
eb2a8d9e PT |
297 | git 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 | 300 | test -z "$dry_run" || exit 0 |
b10ac50f | 301 | |
2d179857 | 302 | curr_head=$(git rev-parse -q --verify HEAD) |
b0ad11ea | 303 | if test -n "$orig_head" && test "$curr_head" != "$orig_head" |
b10ac50f JH |
304 | then |
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 | 312 | Warning: fast-forwarding your working tree from |
c2b1a95c | 313 | Warning: 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 | 317 | After making sure that you saved anything precious from |
9f35aaa9 | 318 | $ git diff \$orig_head |
8323124a JH |
319 | output, run |
320 | $ git reset --hard | |
9f35aaa9 | 321 | to recover.")" |
8323124a | 322 | |
b10ac50f JH |
323 | fi |
324 | ||
05dd8e2e JH |
325 | merge_head=$(sed -e '/ not-for-merge /d' \ |
326 | -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \ | |
327 | tr '\012' ' ') | |
e0bfc81e JH |
328 | |
329 | case "$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 | ;; |
343 | esac | |
344 | ||
b4dc085a TR |
345 | # Pulling into unborn branch: a shorthand for branching off |
346 | # FETCH_HEAD, for lazy typers. | |
d09e79cb LT |
347 | if test -z "$orig_head" |
348 | then | |
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 |
357 | fi | |
358 | ||
cf65426d EN |
359 | if test true = "$rebase" |
360 | then | |
361 | o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref) | |
362 | if test "$oldremoteref" = "$o" | |
363 | then | |
364 | unset oldremoteref | |
365 | fi | |
366 | fi | |
367 | ||
14e5d40c JH |
368 | case "$rebase" in |
369 | true) | |
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 | ;; |
380 | esac | |
381 | eval "exec $eval" |