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