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