blob: fb9e2df9312e96ecf8ad5ab2bde4b74b979fe02e [file] [log] [blame]
Linus Torvalds839a7a02005-04-18 12:15:10 -07001#!/bin/sh
2#
Junio C Hamano521003f2005-08-22 21:57:59 -07003# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
SZEDER Gábord8abe142008-04-06 03:23:43 +02007USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
freku045@student.liu.se806f36d2005-12-13 23:30:31 +01008LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
Junio C Hamano533b7032007-01-12 12:52:03 -08009SUBDIRECTORY_OK=Yes
Junio C Hamano8f321a32007-11-06 01:50:02 -080010OPTIONS_SPEC=
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080011. git-sh-setup
Shawn O. Pearcef9474132006-12-28 02:34:48 -050012set_reflog_action "pull $*"
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050013require_work_tree
Junio C Hamano533b7032007-01-12 12:52:03 -080014cd_to_toplevel
Junio C Hamanob10ac502005-08-25 18:15:32 -070015
Matthieu Moyd38a30d2010-01-12 10:54:44 +010016
17die_conflict () {
18 git diff-index --cached --name-status -r --ignore-submodules HEAD --
19 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
20 die "Pull is not possible because you have unmerged files.
21Please, fix them up in the work tree, and then use 'git add/rm <file>'
22as appropriate to mark resolution, or use 'git commit -a'."
23 else
24 die "Pull is not possible because you have unmerged files."
25 fi
26}
27
28die_merge () {
29 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
30 die "You have not concluded your merge (MERGE_HEAD exists).
31Please, commit your changes before you can merge."
32 else
33 die "You have not concluded your merge (MERGE_HEAD exists)."
34 fi
35}
36
37test -z "$(git ls-files -u)" || die_conflict
38test -f "$GIT_DIR/MERGE_HEAD" && die_merge
Junio C Hamanod1014a12006-12-31 23:21:50 -080039
Björn Gustavsson13474832009-10-29 23:08:31 +010040strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
Jens Lehmann7dce19d2010-11-12 13:54:52 +010041log_arg= verbosity= progress= recurse_submodules=
Avery Pennarunee2c7952009-11-25 21:23:57 -050042merge_args=
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000043curr_branch=$(git symbolic-ref -q HEAD)
Stephen Boyd0d12e592010-03-17 22:10:45 -070044curr_branch_short="${curr_branch#refs/heads/}"
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000045rebase=$(git config --bool branch.$curr_branch_short.rebase)
Jeff King29609e62010-05-25 02:07:25 -040046dry_run=
David Kastrup822f7c72007-09-23 22:42:08 +020047while :
Junio C Hamano60fb5b22005-09-25 19:43:51 -070048do
49 case "$1" in
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010050 -q|--quiet)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010051 verbosity="$verbosity -q" ;;
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010052 -v|--verbose)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010053 verbosity="$verbosity -v" ;;
Tay Ray Chuan98390182010-02-24 20:50:28 +080054 --progress)
55 progress=--progress ;;
Jeff Kingbebd2fd2011-02-20 04:56:56 -050056 --no-progress)
57 progress=--no-progress ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020058 -n|--no-stat|--no-summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010059 diffstat=--no-stat ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020060 --stat|--summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010061 diffstat=--stat ;;
SZEDER Gáborefb779f2008-04-06 03:23:46 +020062 --log|--no-log)
63 log_arg=$1 ;;
Junio C Hamano123ee3c2005-11-01 19:30:11 -080064 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
65 no_commit=--no-commit ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010066 --c|--co|--com|--comm|--commi|--commit)
67 no_commit=--commit ;;
Junio C Hamano7d0c6882006-06-23 01:37:02 -070068 --sq|--squ|--squa|--squas|--squash)
69 squash=--squash ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010070 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
71 squash=--no-squash ;;
72 --ff)
73 no_ff=--ff ;;
74 --no-ff)
75 no_ff=--no-ff ;;
Björn Gustavsson13474832009-10-29 23:08:31 +010076 --ff-only)
77 ff_only=--ff-only ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070078 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
79 --strateg=*|--strategy=*|\
80 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
81 case "$#,$1" in
82 *,*=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +020083 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070084 1,*)
85 usage ;;
86 *)
87 strategy="$2"
88 shift ;;
89 esac
90 strategy_args="${strategy_args}-s $strategy "
91 ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -050092 -X*)
93 case "$#,$1" in
94 1,-X)
95 usage ;;
96 *,-X)
Junio C Hamano14e5d402010-01-17 22:31:38 -080097 xx="-X $(git rev-parse --sq-quote "$2")"
Avery Pennarunee2c7952009-11-25 21:23:57 -050098 shift ;;
99 *,*)
Junio C Hamano14e5d402010-01-17 22:31:38 -0800100 xx=$(git rev-parse --sq-quote "$1") ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -0500101 esac
102 merge_args="$merge_args$xx "
103 ;;
Johannes Schindelincd67e4d2007-11-28 13:11:07 +0000104 -r|--r|--re|--reb|--reba|--rebas|--rebase)
105 rebase=true
106 ;;
107 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
108 rebase=false
109 ;;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100110 --recurse-submodules)
111 recurse_submodules=--recurse-submodules
112 ;;
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100113 --recurse-submodules=*)
114 recurse_submodules="$1"
115 ;;
Jens Lehmannbe254a02010-11-11 00:55:02 +0100116 --no-recurse-submodules)
117 recurse_submodules=--no-recurse-submodules
118 ;;
Jeff King29609e62010-05-25 02:07:25 -0400119 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
120 dry_run=--dry-run
121 ;;
Michael J Gruber24231e02011-02-14 17:48:08 +0100122 -h|--h|--he|--hel|--help|--help-|--help-a|--help-al|--help-all)
Jon Loeliger93d69d82005-11-06 23:30:56 -0600123 usage
124 ;;
David Kastrup822f7c72007-09-23 22:42:08 +0200125 *)
126 # Pass thru anything that may be meant for fetch.
Junio C Hamano619e5a02005-10-03 15:45:44 -0700127 break
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700128 ;;
129 esac
130 shift
131done
132
Junio C Hamano441ed412007-12-28 13:58:43 -0800133error_on_no_merge_candidates () {
134 exec >&2
135 for opt
136 do
137 case "$opt" in
138 -t|--t|--ta|--tag|--tags)
139 echo "Fetching tags only, you probably meant:"
140 echo " git fetch --tags"
141 exit 1
142 esac
143 done
144
Jan Krüger995fc2f2009-11-27 08:17:05 -0600145 if test true = "$rebase"
146 then
147 op_type=rebase
148 op_prep=against
149 else
150 op_type=merge
151 op_prep=with
152 fi
153
Junio C Hamano441ed412007-12-28 13:58:43 -0800154 curr_branch=${curr_branch#refs/heads/}
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700155 upstream=$(git config "branch.$curr_branch.merge")
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400156 remote=$(git config "branch.$curr_branch.remote")
Junio C Hamano441ed412007-12-28 13:58:43 -0800157
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400158 if [ $# -gt 1 ]; then
Jan Krüger995fc2f2009-11-27 08:17:05 -0600159 if [ "$rebase" = true ]; then
160 printf "There is no candidate for rebasing against "
161 else
162 printf "There are no candidates for merging "
163 fi
164 echo "among the refs that you just fetched."
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400165 echo "Generally this means that you provided a wildcard refspec which had no"
166 echo "matches on the remote end."
167 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
168 echo "You asked to pull from the remote '$1', but did not specify"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600169 echo "a branch. Because this is not the default configured remote"
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400170 echo "for your current branch, you must specify a branch on the command line."
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500171 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
172 . git-parse-remote
173 error_on_missing_default_upstream "pull" $op_type $op_prep \
174 "git pull <repository> <refspec>"
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700175 else
Jan Krüger995fc2f2009-11-27 08:17:05 -0600176 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
177 echo "from the remote, but no such ref was fetched."
Matthieu Moy61e61082009-04-08 09:24:03 +0200178 fi
Junio C Hamano441ed412007-12-28 13:58:43 -0800179 exit 1
180}
181
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000182test true = "$rebase" && {
Jeff King19a7fcb2009-08-11 23:27:40 -0400183 if ! git rev-parse -q --verify HEAD >/dev/null
184 then
185 # On an unborn branch
186 if test -f "$GIT_DIR/index"
187 then
188 die "updating an unborn branch with changes added to the index"
189 fi
190 else
Ramkumar Ramachandra92c62a32010-10-19 20:09:28 +0530191 require_clean_work_tree "pull with rebase" "Please commit or stash them."
Jeff King19a7fcb2009-08-11 23:27:40 -0400192 fi
Santi Béjard44e7122009-07-19 09:45:16 +0200193 oldremoteref= &&
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000194 . git-parse-remote &&
Santi Béjard44e7122009-07-19 09:45:16 +0200195 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
196 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
197 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
198 do
199 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
200 then
201 oldremoteref="$reflog"
202 break
203 fi
204 done
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000205}
Miklos Vajna2d179852008-12-03 14:26:50 +0100206orig_head=$(git rev-parse -q --verify HEAD)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100207git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
Jeff King29609e62010-05-25 02:07:25 -0400208test -z "$dry_run" || exit 0
Junio C Hamanob10ac502005-08-25 18:15:32 -0700209
Miklos Vajna2d179852008-12-03 14:26:50 +0100210curr_head=$(git rev-parse -q --verify HEAD)
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700211if test -n "$orig_head" && test "$curr_head" != "$orig_head"
Junio C Hamanob10ac502005-08-25 18:15:32 -0700212then
213 # The fetch involved updating the current branch.
214
215 # The working tree and the index file is still based on the
216 # $orig_head commit, but we are merging into $curr_head.
217 # First update the working tree to match $curr_head.
218
219 echo >&2 "Warning: fetch updated the current branch head."
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300220 echo >&2 "Warning: fast-forwarding your working tree from"
Junio C Hamanoa057f802006-10-10 23:00:29 -0700221 echo >&2 "Warning: commit $orig_head."
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400222 git update-index -q --refresh
Junio C Hamano5be60072007-07-02 22:52:14 -0700223 git read-tree -u -m "$orig_head" "$curr_head" ||
Junio C Hamano83231242006-03-22 01:57:11 -0800224 die 'Cannot fast-forward your working tree.
225After making sure that you saved anything precious from
226$ git diff '$orig_head'
227output, run
228$ git reset --hard
229to recover.'
230
Junio C Hamanob10ac502005-08-25 18:15:32 -0700231fi
232
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700233merge_head=$(sed -e '/ not-for-merge /d' \
234 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
235 tr '\012' ' ')
Junio C Hamanoe0bfc812005-08-20 02:57:26 -0700236
237case "$merge_head" in
Junio C Hamano521003f2005-08-22 21:57:59 -0700238'')
Junio C Hamano4973aa22009-10-05 12:03:25 -0700239 error_on_no_merge_candidates "$@"
Junio C Hamano521003f2005-08-22 21:57:59 -0700240 ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700241?*' '?*)
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800242 if test -z "$orig_head"
243 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700244 die "Cannot merge multiple branches into empty head"
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800245 fi
Jay Soffian51b2ead2009-02-18 08:44:02 -0500246 if test true = "$rebase"
247 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700248 die "Cannot rebase onto multiple branches"
Jay Soffian51b2ead2009-02-18 08:44:02 -0500249 fi
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700250 ;;
251esac
252
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800253if test -z "$orig_head"
254then
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700255 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
Jeff King4b3ffe52011-03-25 14:13:31 -0400256 git read-tree -m -u HEAD || exit 1
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800257 exit
258fi
259
Elijah Newrencf654262010-08-12 19:50:50 -0600260if test true = "$rebase"
261then
262 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
263 if test "$oldremoteref" = "$o"
264 then
265 unset oldremoteref
266 fi
267fi
268
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200269merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
Junio C Hamano14e5d402010-01-17 22:31:38 -0800270case "$rebase" in
271true)
272 eval="git-rebase $diffstat $strategy_args $merge_args"
273 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
274 ;;
275*)
276 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
Jeff Kingbebd2fd2011-02-20 04:56:56 -0500277 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
278 eval="$eval \"\$merge_name\" HEAD $merge_head"
Junio C Hamano14e5d402010-01-17 22:31:38 -0800279 ;;
280esac
281eval "exec $eval"