blob: a09a44ec4ce3d82cebc5f972f09e94c7c35dc948 [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=
Tay Ray Chuan98390182010-02-24 20:50:28 +080041log_arg= verbosity= progress=
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 ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020056 -n|--no-stat|--no-summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010057 diffstat=--no-stat ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020058 --stat|--summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010059 diffstat=--stat ;;
SZEDER Gáborefb779f2008-04-06 03:23:46 +020060 --log|--no-log)
61 log_arg=$1 ;;
Junio C Hamano123ee3c2005-11-01 19:30:11 -080062 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
63 no_commit=--no-commit ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010064 --c|--co|--com|--comm|--commi|--commit)
65 no_commit=--commit ;;
Junio C Hamano7d0c6882006-06-23 01:37:02 -070066 --sq|--squ|--squa|--squas|--squash)
67 squash=--squash ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010068 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
69 squash=--no-squash ;;
70 --ff)
71 no_ff=--ff ;;
72 --no-ff)
73 no_ff=--no-ff ;;
Björn Gustavsson13474832009-10-29 23:08:31 +010074 --ff-only)
75 ff_only=--ff-only ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070076 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
77 --strateg=*|--strategy=*|\
78 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
79 case "$#,$1" in
80 *,*=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +020081 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070082 1,*)
83 usage ;;
84 *)
85 strategy="$2"
86 shift ;;
87 esac
88 strategy_args="${strategy_args}-s $strategy "
89 ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -050090 -X*)
91 case "$#,$1" in
92 1,-X)
93 usage ;;
94 *,-X)
Junio C Hamano14e5d402010-01-17 22:31:38 -080095 xx="-X $(git rev-parse --sq-quote "$2")"
Avery Pennarunee2c7952009-11-25 21:23:57 -050096 shift ;;
97 *,*)
Junio C Hamano14e5d402010-01-17 22:31:38 -080098 xx=$(git rev-parse --sq-quote "$1") ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -050099 esac
100 merge_args="$merge_args$xx "
101 ;;
Johannes Schindelincd67e4d2007-11-28 13:11:07 +0000102 -r|--r|--re|--reb|--reba|--rebas|--rebase)
103 rebase=true
104 ;;
105 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
106 rebase=false
107 ;;
Jeff King29609e62010-05-25 02:07:25 -0400108 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
109 dry_run=--dry-run
110 ;;
Jon Loeliger93d69d82005-11-06 23:30:56 -0600111 -h|--h|--he|--hel|--help)
112 usage
113 ;;
David Kastrup822f7c72007-09-23 22:42:08 +0200114 *)
115 # Pass thru anything that may be meant for fetch.
Junio C Hamano619e5a02005-10-03 15:45:44 -0700116 break
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700117 ;;
118 esac
119 shift
120done
121
Junio C Hamano441ed412007-12-28 13:58:43 -0800122error_on_no_merge_candidates () {
123 exec >&2
124 for opt
125 do
126 case "$opt" in
127 -t|--t|--ta|--tag|--tags)
128 echo "Fetching tags only, you probably meant:"
129 echo " git fetch --tags"
130 exit 1
131 esac
132 done
133
Jan Krüger995fc2f2009-11-27 08:17:05 -0600134 if test true = "$rebase"
135 then
136 op_type=rebase
137 op_prep=against
138 else
139 op_type=merge
140 op_prep=with
141 fi
142
Junio C Hamano441ed412007-12-28 13:58:43 -0800143 curr_branch=${curr_branch#refs/heads/}
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700144 upstream=$(git config "branch.$curr_branch.merge")
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400145 remote=$(git config "branch.$curr_branch.remote")
Junio C Hamano441ed412007-12-28 13:58:43 -0800146
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400147 if [ $# -gt 1 ]; then
Jan Krüger995fc2f2009-11-27 08:17:05 -0600148 if [ "$rebase" = true ]; then
149 printf "There is no candidate for rebasing against "
150 else
151 printf "There are no candidates for merging "
152 fi
153 echo "among the refs that you just fetched."
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400154 echo "Generally this means that you provided a wildcard refspec which had no"
155 echo "matches on the remote end."
156 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
157 echo "You asked to pull from the remote '$1', but did not specify"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600158 echo "a branch. Because this is not the default configured remote"
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400159 echo "for your current branch, you must specify a branch on the command line."
160 elif [ -z "$curr_branch" ]; then
Matthieu Moy61e61082009-04-08 09:24:03 +0200161 echo "You are not currently on a branch, so I cannot use any"
162 echo "'branch.<branchname>.merge' in your configuration file."
Jan Krüger995fc2f2009-11-27 08:17:05 -0600163 echo "Please specify which remote branch you want to use on the command"
Matthieu Moy61e61082009-04-08 09:24:03 +0200164 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
165 echo "See git-pull(1) for details."
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700166 elif [ -z "$upstream" ]; then
Matthieu Moy61e61082009-04-08 09:24:03 +0200167 echo "You asked me to pull without telling me which branch you"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600168 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
169 echo "your configuration file does not tell me, either. Please"
170 echo "specify which branch you want to use on the command line and"
Matthieu Moy61e61082009-04-08 09:24:03 +0200171 echo "try again (e.g. 'git pull <repository> <refspec>')."
172 echo "See git-pull(1) for details."
173 echo
Jan Krüger995fc2f2009-11-27 08:17:05 -0600174 echo "If you often $op_type $op_prep the same branch, you may want to"
175 echo "use something like the following in your configuration file:"
Matthieu Moy61e61082009-04-08 09:24:03 +0200176 echo
Jan Krüger995fc2f2009-11-27 08:17:05 -0600177 echo " [branch \"${curr_branch}\"]"
178 echo " remote = <nickname>"
179 echo " merge = <remote-ref>"
180 test rebase = "$op_type" &&
181 echo " rebase = true"
182 echo
183 echo " [remote \"<nickname>\"]"
184 echo " url = <url>"
185 echo " fetch = <refspec>"
Matthieu Moy61e61082009-04-08 09:24:03 +0200186 echo
187 echo "See git-config(1) for details."
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700188 else
Jan Krüger995fc2f2009-11-27 08:17:05 -0600189 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
190 echo "from the remote, but no such ref was fetched."
Matthieu Moy61e61082009-04-08 09:24:03 +0200191 fi
Junio C Hamano441ed412007-12-28 13:58:43 -0800192 exit 1
193}
194
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000195test true = "$rebase" && {
Jeff King19a7fcb2009-08-11 23:27:40 -0400196 if ! git rev-parse -q --verify HEAD >/dev/null
197 then
198 # On an unborn branch
199 if test -f "$GIT_DIR/index"
200 then
201 die "updating an unborn branch with changes added to the index"
202 fi
203 else
204 git update-index --ignore-submodules --refresh &&
205 git diff-files --ignore-submodules --quiet &&
206 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
207 die "refusing to pull with rebase: your working tree is not up-to-date"
208 fi
Santi Béjard44e7122009-07-19 09:45:16 +0200209 oldremoteref= &&
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000210 . git-parse-remote &&
Santi Béjard44e7122009-07-19 09:45:16 +0200211 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
212 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
213 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
214 do
215 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
216 then
217 oldremoteref="$reflog"
218 break
219 fi
220 done
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000221}
Miklos Vajna2d179852008-12-03 14:26:50 +0100222orig_head=$(git rev-parse -q --verify HEAD)
Jeff King29609e62010-05-25 02:07:25 -0400223git fetch $verbosity $progress $dry_run --update-head-ok "$@" || exit 1
224test -z "$dry_run" || exit 0
Junio C Hamanob10ac502005-08-25 18:15:32 -0700225
Miklos Vajna2d179852008-12-03 14:26:50 +0100226curr_head=$(git rev-parse -q --verify HEAD)
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700227if test -n "$orig_head" && test "$curr_head" != "$orig_head"
Junio C Hamanob10ac502005-08-25 18:15:32 -0700228then
229 # The fetch involved updating the current branch.
230
231 # The working tree and the index file is still based on the
232 # $orig_head commit, but we are merging into $curr_head.
233 # First update the working tree to match $curr_head.
234
235 echo >&2 "Warning: fetch updated the current branch head."
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300236 echo >&2 "Warning: fast-forwarding your working tree from"
Junio C Hamanoa057f802006-10-10 23:00:29 -0700237 echo >&2 "Warning: commit $orig_head."
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400238 git update-index -q --refresh
Junio C Hamano5be60072007-07-02 22:52:14 -0700239 git read-tree -u -m "$orig_head" "$curr_head" ||
Junio C Hamano83231242006-03-22 01:57:11 -0800240 die 'Cannot fast-forward your working tree.
241After making sure that you saved anything precious from
242$ git diff '$orig_head'
243output, run
244$ git reset --hard
245to recover.'
246
Junio C Hamanob10ac502005-08-25 18:15:32 -0700247fi
248
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700249merge_head=$(sed -e '/ not-for-merge /d' \
250 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
251 tr '\012' ' ')
Junio C Hamanoe0bfc812005-08-20 02:57:26 -0700252
253case "$merge_head" in
Junio C Hamano521003f2005-08-22 21:57:59 -0700254'')
Junio C Hamano4973aa22009-10-05 12:03:25 -0700255 error_on_no_merge_candidates "$@"
Junio C Hamano521003f2005-08-22 21:57:59 -0700256 ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700257?*' '?*)
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800258 if test -z "$orig_head"
259 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700260 die "Cannot merge multiple branches into empty head"
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800261 fi
Jay Soffian51b2ead2009-02-18 08:44:02 -0500262 if test true = "$rebase"
263 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700264 die "Cannot rebase onto multiple branches"
Jay Soffian51b2ead2009-02-18 08:44:02 -0500265 fi
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700266 ;;
267esac
268
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800269if test -z "$orig_head"
270then
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700271 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700272 git read-tree --reset -u HEAD || exit 1
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800273 exit
274fi
275
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200276merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
Junio C Hamano14e5d402010-01-17 22:31:38 -0800277case "$rebase" in
278true)
279 eval="git-rebase $diffstat $strategy_args $merge_args"
280 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
281 ;;
282*)
283 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
284 eval="$eval $log_arg $strategy_args $merge_args"
Junio C Hamano7ecee332010-01-24 10:11:27 -0800285 eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity"
Junio C Hamano14e5d402010-01-17 22:31:38 -0800286 ;;
287esac
288eval "exec $eval"