Merge branch 'fc/master' into travis-ci
[git] / git-pull.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
6
7 USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
9 SUBDIRECTORY_OK=Yes
10 OPTIONS_SPEC=
11 . git-sh-setup
12 . git-sh-i18n
13 set_reflog_action "pull${1+ $*}"
14 require_work_tree_exists
15 cd_to_toplevel
16
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
21         die "$(gettext "Pull is not possible because you have unmerged files.
22 Please, fix them up in the work tree, and then use 'git add/rm <file>'
23 as appropriate to mark resolution, or use 'git commit -a'.")"
24     else
25         die "$(gettext "Pull is not possible because you have unmerged files.")"
26     fi
27 }
28
29 die_merge () {
30     if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31         die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32 Please, commit your changes before you can merge.")"
33     else
34         die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
35     fi
36 }
37
38 test -z "$(git ls-files -u)" || die_conflict
39 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40
41 bool_or_string_config () {
42         git config --bool "$1" 2>/dev/null || git config "$1"
43 }
44
45 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
46 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
47 merge_args= edit= rebase_args=
48 curr_branch=$(git symbolic-ref -q HEAD)
49 curr_branch_short="${curr_branch#refs/heads/}"
50 mode=$(git config branch.${curr_branch_short}.pullmode)
51 if test -z "$mode"
52 then
53         mode=$(git config pull.mode)
54 fi
55 case "$mode" in
56 merge|rebase|merge-ff-only|'')
57         ;;
58 rebase-preserve)
59         mode="rebase"
60         rebase_args="--preserve-merges"
61         ;;
62 *)
63         echo "Invalid value for 'mode'"
64         usage
65         exit 1
66         ;;
67 esac
68
69 if test -z "$mode"
70 then
71         rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
72         if test -z "$rebase"
73         then
74                 rebase=$(bool_or_string_config pull.rebase)
75         fi
76         if test -n "$rebase"
77         then
78                 echo "The configurations pull.rebase and branch.<name>.rebase are deprecated."
79                 echo "Please use pull.mode and branch.<name>.pullmode instead."
80         fi
81 fi
82 test -z "$mode" && mode=default
83 dry_run=
84 while :
85 do
86         case "$1" in
87         -q|--quiet)
88                 verbosity="$verbosity -q" ;;
89         -v|--verbose)
90                 verbosity="$verbosity -v" ;;
91         --progress)
92                 progress=--progress ;;
93         --no-progress)
94                 progress=--no-progress ;;
95         -n|--no-stat|--no-summary)
96                 diffstat=--no-stat ;;
97         --stat|--summary)
98                 diffstat=--stat ;;
99         --log|--no-log)
100                 log_arg=$1 ;;
101         --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
102                 no_commit=--no-commit ;;
103         --c|--co|--com|--comm|--commi|--commit)
104                 no_commit=--commit ;;
105         -e|--edit)
106                 edit=--edit ;;
107         --no-edit)
108                 edit=--no-edit ;;
109         --sq|--squ|--squa|--squas|--squash)
110                 squash=--squash ;;
111         --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
112                 squash=--no-squash ;;
113         --ff)
114                 no_ff=--ff ;;
115         --no-ff)
116                 no_ff=--no-ff ;;
117         --ff-only)
118                 ff_only=--ff-only ;;
119         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
120                 --strateg=*|--strategy=*|\
121         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
122                 case "$#,$1" in
123                 *,*=*)
124                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
125                 1,*)
126                         usage ;;
127                 *)
128                         strategy="$2"
129                         shift ;;
130                 esac
131                 strategy_args="${strategy_args}-s $strategy "
132                 ;;
133         -X*)
134                 case "$#,$1" in
135                 1,-X)
136                         usage ;;
137                 *,-X)
138                         xx="-X $(git rev-parse --sq-quote "$2")"
139                         shift ;;
140                 *,*)
141                         xx=$(git rev-parse --sq-quote "$1") ;;
142                 esac
143                 merge_args="$merge_args$xx "
144                 ;;
145         -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
146                 rebase="${1#*=}"
147                 ;;
148         -r|--r|--re|--reb|--reba|--rebas|--rebase)
149                 mode=rebase
150                 ;;
151         -m|--m|--me|--mer|--merg|--merge)
152                 mode=merge
153                 ;;
154         --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
155                 mode=merge
156                 echo "--no-rebase is deprecated, please use --merge instead"
157                 ;;
158         --recurse-submodules)
159                 recurse_submodules=--recurse-submodules
160                 ;;
161         --recurse-submodules=*)
162                 recurse_submodules="$1"
163                 ;;
164         --no-recurse-submodules)
165                 recurse_submodules=--no-recurse-submodules
166                 ;;
167         --verify-signatures)
168                 verify_signatures=--verify-signatures
169                 ;;
170         --no-verify-signatures)
171                 verify_signatures=--no-verify-signatures
172                 ;;
173         --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
174                 dry_run=--dry-run
175                 ;;
176         -h|--help-all)
177                 usage
178                 ;;
179         *)
180                 # Pass thru anything that may be meant for fetch.
181                 break
182                 ;;
183         esac
184         shift
185 done
186
187 if test -n "$rebase"
188 then
189         case "$rebase" in
190         true)
191                 mode="rebase"
192                 ;;
193         false)
194                 mode="merge"
195                 ;;
196         preserve)
197                 mode="rebase"
198                 rebase_args=--preserve-merges
199                 ;;
200         *)
201                 echo "Invalid value for --rebase, should be true, false, or preserve"
202                 usage
203                 exit 1
204                 ;;
205         esac
206 fi
207
208 error_on_no_merge_candidates () {
209         exec >&2
210         for opt
211         do
212                 case "$opt" in
213                 -t|--t|--ta|--tag|--tags)
214                         echo "It doesn't make sense to pull all tags; you probably meant:"
215                         echo "  git fetch --tags"
216                         exit 1
217                 esac
218         done
219
220         if test "$mode" = rebase
221         then
222                 op_type=rebase
223                 op_prep=against
224         else
225                 op_type=merge
226                 op_prep=with
227         fi
228
229         upstream=$(git config "branch.$curr_branch_short.merge")
230         remote=$(git config "branch.$curr_branch_short.remote")
231
232         if [ $# -gt 1 ]; then
233                 if [ "$mode" = rebase ]; then
234                         printf "There is no candidate for rebasing against "
235                 else
236                         printf "There are no candidates for merging "
237                 fi
238                 echo "among the refs that you just fetched."
239                 echo "Generally this means that you provided a wildcard refspec which had no"
240                 echo "matches on the remote end."
241         elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
242                 echo "You asked to pull from the remote '$1', but did not specify"
243                 echo "a branch. Because this is not the default configured remote"
244                 echo "for your current branch, you must specify a branch on the command line."
245         elif [ -z "$curr_branch" -o -z "$upstream" ]; then
246                 . git-parse-remote
247                 error_on_missing_default_upstream "pull" $op_type $op_prep \
248                         "git pull <remote> <branch>"
249         else
250                 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
251                 echo "from the remote, but no such ref was fetched."
252         fi
253         exit 1
254 }
255
256 test "$mode" = rebase && {
257         if ! git rev-parse -q --verify HEAD >/dev/null
258         then
259                 # On an unborn branch
260                 if test -f "$GIT_DIR/index"
261                 then
262                         die "$(gettext "updating an unborn branch with changes added to the index")"
263                 fi
264         else
265                 require_clean_work_tree "pull with rebase" "Please commit or stash them."
266         fi
267         oldremoteref= &&
268         test -n "$curr_branch" &&
269         . git-parse-remote &&
270         remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
271         oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
272 }
273 orig_head=$(git rev-parse -q --verify HEAD)
274 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
275 test -z "$dry_run" || exit 0
276
277 curr_head=$(git rev-parse -q --verify HEAD)
278 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
279 then
280         # The fetch involved updating the current branch.
281
282         # The working tree and the index file is still based on the
283         # $orig_head commit, but we are merging into $curr_head.
284         # First update the working tree to match $curr_head.
285
286         eval_gettextln "Warning: fetch updated the current branch head.
287 Warning: fast-forwarding your working tree from
288 Warning: commit \$orig_head." >&2
289         git update-index -q --refresh
290         git read-tree -u -m "$orig_head" "$curr_head" ||
291                 die "$(eval_gettext "Cannot fast-forward your working tree.
292 After making sure that you saved anything precious from
293 $ git diff \$orig_head
294 output, run
295 $ git reset --hard
296 to recover.")"
297
298 fi
299
300 merge_head=$(sed -e '/  not-for-merge   /d' \
301         -e 's/  .*//' "$GIT_DIR"/FETCH_HEAD | \
302         tr '\012' ' ')
303
304 case "$merge_head" in
305 '')
306         error_on_no_merge_candidates "$@"
307         ;;
308 ?*' '?*)
309         if test -z "$orig_head"
310         then
311                 die "$(gettext "Cannot merge multiple branches into empty head")"
312         fi
313         if test "$mode" = rebase
314         then
315                 die "$(gettext "Cannot rebase onto multiple branches")"
316         fi
317         ;;
318 *)
319         # check if a non-fast-foward merge would be needed
320         merge_head=${merge_head% }
321         if test -z "$no_ff$ff_only${squash#--no-squash}" &&
322                 test -n "$orig_head" &&
323                 ! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
324                 ! git merge-base --is-ancestor "$merge_head" "$orig_head"
325         then
326                 case "$mode" in
327                 merge-ff-only)
328                         die "$(gettext "The pull was not fast-forward, please either merge or rebase.
329 If unsure, run 'git pull --merge'.")"
330                         ;;
331                 default)
332                         echo "$(gettext "warning: the pull was not fast-forward, in the future you would have to choose
333 a merge or a rebase, falling back to old style for now (git pull --merge).
334 Read 'git pull --help' for more information.")" >&2
335                         ;;
336                 esac
337         fi
338         ;;
339 esac
340
341 # Pulling into unborn branch: a shorthand for branching off
342 # FETCH_HEAD, for lazy typers.
343 if test -z "$orig_head"
344 then
345         # Two-way merge: we claim the index is based on an empty tree,
346         # and try to fast-forward to HEAD.  This ensures we will not
347         # lose index/worktree changes that the user already made on
348         # the unborn branch.
349         empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
350         git read-tree -m -u $empty_tree $merge_head &&
351         git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
352         exit
353 fi
354
355 if test "$mode" = rebase
356 then
357         o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
358         if test "$oldremoteref" = "$o"
359         then
360                 unset oldremoteref
361         fi
362 fi
363
364 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
365 case "$mode" in
366 rebase)
367         eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
368         eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
369         ;;
370 *)
371         eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
372         eval="$eval  $log_arg $strategy_args $merge_args $verbosity $progress"
373         eval="$eval \"\$merge_name\" HEAD $merge_head"
374         ;;
375 esac
376 eval "exec $eval"