blob: 9868a0bfb478707b361f664a252870b3d1939138 [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
Ævar Arnfjörð Bjarmasona9f57862011-05-21 18:43:54 +000012. git-sh-i18n
Ori Avtalionc98d1e42011-07-29 10:19:26 +030013set_reflog_action "pull${1+ $*}"
Jeff King035b5bf2011-10-13 11:59:24 -040014require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080015cd_to_toplevel
Junio C Hamanob10ac502005-08-25 18:15:32 -070016
Matthieu Moyd38a30d2010-01-12 10:54:44 +010017
18die_conflict () {
19 git diff-index --cached --name-status -r --ignore-submodules HEAD --
20 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +000021 die "$(gettext "Pull is not possible because you have unmerged files.
Matthieu Moyd38a30d2010-01-12 10:54:44 +010022Please, fix them up in the work tree, and then use 'git add/rm <file>'
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +000023as appropriate to mark resolution, or use 'git commit -a'.")"
Matthieu Moyd38a30d2010-01-12 10:54:44 +010024 else
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +000025 die "$(gettext "Pull is not possible because you have unmerged files.")"
Matthieu Moyd38a30d2010-01-12 10:54:44 +010026 fi
27}
28
29die_merge () {
30 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +000031 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32Please, commit your changes before you can merge.")"
Matthieu Moyd38a30d2010-01-12 10:54:44 +010033 else
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +000034 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
Matthieu Moyd38a30d2010-01-12 10:54:44 +010035 fi
36}
37
38test -z "$(git ls-files -u)" || die_conflict
39test -f "$GIT_DIR/MERGE_HEAD" && die_merge
Junio C Hamanod1014a12006-12-31 23:21:50 -080040
Björn Gustavsson13474832009-10-29 23:08:31 +010041strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
Jens Lehmann7dce19d2010-11-12 13:54:52 +010042log_arg= verbosity= progress= recurse_submodules=
Avery Pennarunee2c7952009-11-25 21:23:57 -050043merge_args=
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000044curr_branch=$(git symbolic-ref -q HEAD)
Stephen Boyd0d12e592010-03-17 22:10:45 -070045curr_branch_short="${curr_branch#refs/heads/}"
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000046rebase=$(git config --bool branch.$curr_branch_short.rebase)
Jeff King29609e62010-05-25 02:07:25 -040047dry_run=
David Kastrup822f7c72007-09-23 22:42:08 +020048while :
Junio C Hamano60fb5b22005-09-25 19:43:51 -070049do
50 case "$1" in
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010051 -q|--quiet)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010052 verbosity="$verbosity -q" ;;
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010053 -v|--verbose)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010054 verbosity="$verbosity -v" ;;
Tay Ray Chuan98390182010-02-24 20:50:28 +080055 --progress)
56 progress=--progress ;;
Jeff Kingbebd2fd2011-02-20 04:56:56 -050057 --no-progress)
58 progress=--no-progress ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020059 -n|--no-stat|--no-summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010060 diffstat=--no-stat ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020061 --stat|--summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010062 diffstat=--stat ;;
SZEDER Gáborefb779f2008-04-06 03:23:46 +020063 --log|--no-log)
64 log_arg=$1 ;;
Junio C Hamano123ee3c2005-11-01 19:30:11 -080065 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
66 no_commit=--no-commit ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010067 --c|--co|--com|--comm|--commi|--commit)
68 no_commit=--commit ;;
Junio C Hamano7d0c6882006-06-23 01:37:02 -070069 --sq|--squ|--squa|--squas|--squash)
70 squash=--squash ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010071 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
72 squash=--no-squash ;;
73 --ff)
74 no_ff=--ff ;;
75 --no-ff)
76 no_ff=--no-ff ;;
Björn Gustavsson13474832009-10-29 23:08:31 +010077 --ff-only)
78 ff_only=--ff-only ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070079 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
80 --strateg=*|--strategy=*|\
81 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
82 case "$#,$1" in
83 *,*=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +020084 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070085 1,*)
86 usage ;;
87 *)
88 strategy="$2"
89 shift ;;
90 esac
91 strategy_args="${strategy_args}-s $strategy "
92 ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -050093 -X*)
94 case "$#,$1" in
95 1,-X)
96 usage ;;
97 *,-X)
Junio C Hamano14e5d402010-01-17 22:31:38 -080098 xx="-X $(git rev-parse --sq-quote "$2")"
Avery Pennarunee2c7952009-11-25 21:23:57 -050099 shift ;;
100 *,*)
Junio C Hamano14e5d402010-01-17 22:31:38 -0800101 xx=$(git rev-parse --sq-quote "$1") ;;
Avery Pennarunee2c7952009-11-25 21:23:57 -0500102 esac
103 merge_args="$merge_args$xx "
104 ;;
Johannes Schindelincd67e4d2007-11-28 13:11:07 +0000105 -r|--r|--re|--reb|--reba|--rebas|--rebase)
106 rebase=true
107 ;;
108 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
109 rebase=false
110 ;;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100111 --recurse-submodules)
112 recurse_submodules=--recurse-submodules
113 ;;
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100114 --recurse-submodules=*)
115 recurse_submodules="$1"
116 ;;
Jens Lehmannbe254a02010-11-11 00:55:02 +0100117 --no-recurse-submodules)
118 recurse_submodules=--no-recurse-submodules
119 ;;
Jeff King29609e62010-05-25 02:07:25 -0400120 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
121 dry_run=--dry-run
122 ;;
Clemens Buchacher87182b12011-10-03 20:21:36 +0200123 -h|--help-all)
Jon Loeliger93d69d82005-11-06 23:30:56 -0600124 usage
125 ;;
David Kastrup822f7c72007-09-23 22:42:08 +0200126 *)
127 # Pass thru anything that may be meant for fetch.
Junio C Hamano619e5a02005-10-03 15:45:44 -0700128 break
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700129 ;;
130 esac
131 shift
132done
133
Junio C Hamano441ed412007-12-28 13:58:43 -0800134error_on_no_merge_candidates () {
135 exec >&2
136 for opt
137 do
138 case "$opt" in
139 -t|--t|--ta|--tag|--tags)
140 echo "Fetching tags only, you probably meant:"
141 echo " git fetch --tags"
142 exit 1
143 esac
144 done
145
Jan Krüger995fc2f2009-11-27 08:17:05 -0600146 if test true = "$rebase"
147 then
148 op_type=rebase
149 op_prep=against
150 else
151 op_type=merge
152 op_prep=with
153 fi
154
Junio C Hamano441ed412007-12-28 13:58:43 -0800155 curr_branch=${curr_branch#refs/heads/}
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700156 upstream=$(git config "branch.$curr_branch.merge")
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400157 remote=$(git config "branch.$curr_branch.remote")
Junio C Hamano441ed412007-12-28 13:58:43 -0800158
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400159 if [ $# -gt 1 ]; then
Jan Krüger995fc2f2009-11-27 08:17:05 -0600160 if [ "$rebase" = true ]; then
161 printf "There is no candidate for rebasing against "
162 else
163 printf "There are no candidates for merging "
164 fi
165 echo "among the refs that you just fetched."
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400166 echo "Generally this means that you provided a wildcard refspec which had no"
167 echo "matches on the remote end."
168 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
169 echo "You asked to pull from the remote '$1', but did not specify"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600170 echo "a branch. Because this is not the default configured remote"
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400171 echo "for your current branch, you must specify a branch on the command line."
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500172 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
173 . git-parse-remote
174 error_on_missing_default_upstream "pull" $op_type $op_prep \
175 "git pull <repository> <refspec>"
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700176 else
Jan Krüger995fc2f2009-11-27 08:17:05 -0600177 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
178 echo "from the remote, but no such ref was fetched."
Matthieu Moy61e61082009-04-08 09:24:03 +0200179 fi
Junio C Hamano441ed412007-12-28 13:58:43 -0800180 exit 1
181}
182
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000183test true = "$rebase" && {
Jeff King19a7fcb2009-08-11 23:27:40 -0400184 if ! git rev-parse -q --verify HEAD >/dev/null
185 then
186 # On an unborn branch
187 if test -f "$GIT_DIR/index"
188 then
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +0000189 die "$(gettext "updating an unborn branch with changes added to the index")"
Jeff King19a7fcb2009-08-11 23:27:40 -0400190 fi
191 else
Ramkumar Ramachandra92c62a32010-10-19 20:09:28 +0530192 require_clean_work_tree "pull with rebase" "Please commit or stash them."
Jeff King19a7fcb2009-08-11 23:27:40 -0400193 fi
Santi Béjard44e7122009-07-19 09:45:16 +0200194 oldremoteref= &&
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000195 . git-parse-remote &&
Santi Béjard44e7122009-07-19 09:45:16 +0200196 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
197 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
198 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
199 do
200 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
201 then
202 oldremoteref="$reflog"
203 break
204 fi
205 done
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000206}
Miklos Vajna2d179852008-12-03 14:26:50 +0100207orig_head=$(git rev-parse -q --verify HEAD)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100208git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
Jeff King29609e62010-05-25 02:07:25 -0400209test -z "$dry_run" || exit 0
Junio C Hamanob10ac502005-08-25 18:15:32 -0700210
Miklos Vajna2d179852008-12-03 14:26:50 +0100211curr_head=$(git rev-parse -q --verify HEAD)
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700212if test -n "$orig_head" && test "$curr_head" != "$orig_head"
Junio C Hamanob10ac502005-08-25 18:15:32 -0700213then
214 # The fetch involved updating the current branch.
215
216 # The working tree and the index file is still based on the
217 # $orig_head commit, but we are merging into $curr_head.
218 # First update the working tree to match $curr_head.
219
Jon Seymourc2b1a952011-08-07 21:58:15 +1000220 eval_gettextln "Warning: fetch updated the current branch head.
Ævar Arnfjörð Bjarmason3320a972011-05-21 18:43:57 +0000221Warning: fast-forwarding your working tree from
Jon Seymourc2b1a952011-08-07 21:58:15 +1000222Warning: commit \$orig_head." >&2
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400223 git update-index -q --refresh
Junio C Hamano5be60072007-07-02 22:52:14 -0700224 git read-tree -u -m "$orig_head" "$curr_head" ||
Ævar Arnfjörð Bjarmason9f35aaa2011-05-21 18:43:56 +0000225 die "$(eval_gettext "Cannot fast-forward your working tree.
Junio C Hamano83231242006-03-22 01:57:11 -0800226After making sure that you saved anything precious from
Ævar Arnfjörð Bjarmason9f35aaa2011-05-21 18:43:56 +0000227$ git diff \$orig_head
Junio C Hamano83231242006-03-22 01:57:11 -0800228output, run
229$ git reset --hard
Ævar Arnfjörð Bjarmason9f35aaa2011-05-21 18:43:56 +0000230to recover.")"
Junio C Hamano83231242006-03-22 01:57:11 -0800231
Junio C Hamanob10ac502005-08-25 18:15:32 -0700232fi
233
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700234merge_head=$(sed -e '/ not-for-merge /d' \
235 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
236 tr '\012' ' ')
Junio C Hamanoe0bfc812005-08-20 02:57:26 -0700237
238case "$merge_head" in
Junio C Hamano521003f2005-08-22 21:57:59 -0700239'')
Junio C Hamano4973aa22009-10-05 12:03:25 -0700240 error_on_no_merge_candidates "$@"
Junio C Hamano521003f2005-08-22 21:57:59 -0700241 ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700242?*' '?*)
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800243 if test -z "$orig_head"
244 then
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +0000245 die "$(gettext "Cannot merge multiple branches into empty head")"
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800246 fi
Jay Soffian51b2ead2009-02-18 08:44:02 -0500247 if test true = "$rebase"
248 then
Ævar Arnfjörð Bjarmason85af5f82011-05-21 18:43:55 +0000249 die "$(gettext "Cannot rebase onto multiple branches")"
Jay Soffian51b2ead2009-02-18 08:44:02 -0500250 fi
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700251 ;;
252esac
253
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800254if test -z "$orig_head"
255then
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700256 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
Jeff King4b3ffe52011-03-25 14:13:31 -0400257 git read-tree -m -u HEAD || exit 1
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800258 exit
259fi
260
Elijah Newrencf654262010-08-12 19:50:50 -0600261if test true = "$rebase"
262then
263 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
264 if test "$oldremoteref" = "$o"
265 then
266 unset oldremoteref
267 fi
268fi
269
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200270merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
Junio C Hamano14e5d402010-01-17 22:31:38 -0800271case "$rebase" in
272true)
273 eval="git-rebase $diffstat $strategy_args $merge_args"
274 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
275 ;;
276*)
277 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
Jeff Kingbebd2fd2011-02-20 04:56:56 -0500278 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
279 eval="$eval \"\$merge_name\" HEAD $merge_head"
Junio C Hamano14e5d402010-01-17 22:31:38 -0800280 ;;
281esac
282eval "exec $eval"