blob: 9e69ada413bd81948591a9e99287f97499573673 [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
Junio C Hamanod1014a12006-12-31 23:21:50 -080016test -z "$(git ls-files -u)" ||
Junio C Hamano533b7032007-01-12 12:52:03 -080017 die "You are in the middle of a conflicted merge."
Junio C Hamanod1014a12006-12-31 23:21:50 -080018
Björn Gustavsson13474832009-10-29 23:08:31 +010019strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
20log_arg= verbosity=
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000021curr_branch=$(git symbolic-ref -q HEAD)
22curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
23rebase=$(git config --bool branch.$curr_branch_short.rebase)
David Kastrup822f7c72007-09-23 22:42:08 +020024while :
Junio C Hamano60fb5b22005-09-25 19:43:51 -070025do
26 case "$1" in
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010027 -q|--quiet)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010028 verbosity="$verbosity -q" ;;
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010029 -v|--verbose)
Tuncer Ayazc6576f92008-11-17 23:09:30 +010030 verbosity="$verbosity -v" ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020031 -n|--no-stat|--no-summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010032 diffstat=--no-stat ;;
SZEDER Gábord8abe142008-04-06 03:23:43 +020033 --stat|--summary)
Tor Arne Vestbøa334e122009-03-01 22:28:28 +010034 diffstat=--stat ;;
SZEDER Gáborefb779f2008-04-06 03:23:46 +020035 --log|--no-log)
36 log_arg=$1 ;;
Junio C Hamano123ee3c2005-11-01 19:30:11 -080037 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
38 no_commit=--no-commit ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010039 --c|--co|--com|--comm|--commi|--commit)
40 no_commit=--commit ;;
Junio C Hamano7d0c6882006-06-23 01:37:02 -070041 --sq|--squ|--squa|--squas|--squash)
42 squash=--squash ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010043 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
44 squash=--no-squash ;;
45 --ff)
46 no_ff=--ff ;;
47 --no-ff)
48 no_ff=--no-ff ;;
Björn Gustavsson13474832009-10-29 23:08:31 +010049 --ff-only)
50 ff_only=--ff-only ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070051 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
52 --strateg=*|--strategy=*|\
53 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
54 case "$#,$1" in
55 *,*=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +020056 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070057 1,*)
58 usage ;;
59 *)
60 strategy="$2"
61 shift ;;
62 esac
63 strategy_args="${strategy_args}-s $strategy "
64 ;;
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000065 -r|--r|--re|--reb|--reba|--rebas|--rebase)
66 rebase=true
67 ;;
68 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
69 rebase=false
70 ;;
Jon Loeliger93d69d82005-11-06 23:30:56 -060071 -h|--h|--he|--hel|--help)
72 usage
73 ;;
David Kastrup822f7c72007-09-23 22:42:08 +020074 *)
75 # Pass thru anything that may be meant for fetch.
Junio C Hamano619e5a02005-10-03 15:45:44 -070076 break
Junio C Hamano60fb5b22005-09-25 19:43:51 -070077 ;;
78 esac
79 shift
80done
81
Junio C Hamano441ed412007-12-28 13:58:43 -080082error_on_no_merge_candidates () {
83 exec >&2
84 for opt
85 do
86 case "$opt" in
87 -t|--t|--ta|--tag|--tags)
88 echo "Fetching tags only, you probably meant:"
89 echo " git fetch --tags"
90 exit 1
91 esac
92 done
93
Jan Krüger995fc2f2009-11-27 08:17:05 -060094 if test true = "$rebase"
95 then
96 op_type=rebase
97 op_prep=against
98 else
99 op_type=merge
100 op_prep=with
101 fi
102
Junio C Hamano441ed412007-12-28 13:58:43 -0800103 curr_branch=${curr_branch#refs/heads/}
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700104 upstream=$(git config "branch.$curr_branch.merge")
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400105 remote=$(git config "branch.$curr_branch.remote")
Junio C Hamano441ed412007-12-28 13:58:43 -0800106
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400107 if [ $# -gt 1 ]; then
Jan Krüger995fc2f2009-11-27 08:17:05 -0600108 if [ "$rebase" = true ]; then
109 printf "There is no candidate for rebasing against "
110 else
111 printf "There are no candidates for merging "
112 fi
113 echo "among the refs that you just fetched."
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400114 echo "Generally this means that you provided a wildcard refspec which had no"
115 echo "matches on the remote end."
116 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
117 echo "You asked to pull from the remote '$1', but did not specify"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600118 echo "a branch. Because this is not the default configured remote"
Jeff Kinga8c9bef2009-10-05 15:35:16 -0400119 echo "for your current branch, you must specify a branch on the command line."
120 elif [ -z "$curr_branch" ]; then
Matthieu Moy61e61082009-04-08 09:24:03 +0200121 echo "You are not currently on a branch, so I cannot use any"
122 echo "'branch.<branchname>.merge' in your configuration file."
Jan Krüger995fc2f2009-11-27 08:17:05 -0600123 echo "Please specify which remote branch you want to use on the command"
Matthieu Moy61e61082009-04-08 09:24:03 +0200124 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
125 echo "See git-pull(1) for details."
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700126 elif [ -z "$upstream" ]; then
Matthieu Moy61e61082009-04-08 09:24:03 +0200127 echo "You asked me to pull without telling me which branch you"
Jan Krüger995fc2f2009-11-27 08:17:05 -0600128 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
129 echo "your configuration file does not tell me, either. Please"
130 echo "specify which branch you want to use on the command line and"
Matthieu Moy61e61082009-04-08 09:24:03 +0200131 echo "try again (e.g. 'git pull <repository> <refspec>')."
132 echo "See git-pull(1) for details."
133 echo
Jan Krüger995fc2f2009-11-27 08:17:05 -0600134 echo "If you often $op_type $op_prep the same branch, you may want to"
135 echo "use something like the following in your configuration file:"
Matthieu Moy61e61082009-04-08 09:24:03 +0200136 echo
Jan Krüger995fc2f2009-11-27 08:17:05 -0600137 echo " [branch \"${curr_branch}\"]"
138 echo " remote = <nickname>"
139 echo " merge = <remote-ref>"
140 test rebase = "$op_type" &&
141 echo " rebase = true"
142 echo
143 echo " [remote \"<nickname>\"]"
144 echo " url = <url>"
145 echo " fetch = <refspec>"
Matthieu Moy61e61082009-04-08 09:24:03 +0200146 echo
147 echo "See git-config(1) for details."
Junio C Hamanoa6dbf882009-09-13 13:38:48 -0700148 else
Jan Krüger995fc2f2009-11-27 08:17:05 -0600149 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
150 echo "from the remote, but no such ref was fetched."
Matthieu Moy61e61082009-04-08 09:24:03 +0200151 fi
Junio C Hamano441ed412007-12-28 13:58:43 -0800152 exit 1
153}
154
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000155test true = "$rebase" && {
Jeff King19a7fcb2009-08-11 23:27:40 -0400156 if ! git rev-parse -q --verify HEAD >/dev/null
157 then
158 # On an unborn branch
159 if test -f "$GIT_DIR/index"
160 then
161 die "updating an unborn branch with changes added to the index"
162 fi
163 else
164 git update-index --ignore-submodules --refresh &&
165 git diff-files --ignore-submodules --quiet &&
166 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
167 die "refusing to pull with rebase: your working tree is not up-to-date"
168 fi
Santi Béjard44e7122009-07-19 09:45:16 +0200169 oldremoteref= &&
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000170 . git-parse-remote &&
Santi Béjard44e7122009-07-19 09:45:16 +0200171 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
172 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
173 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
174 do
175 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
176 then
177 oldremoteref="$reflog"
178 break
179 fi
180 done
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000181}
Miklos Vajna2d179852008-12-03 14:26:50 +0100182orig_head=$(git rev-parse -q --verify HEAD)
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100183git fetch $verbosity --update-head-ok "$@" || exit 1
Junio C Hamanob10ac502005-08-25 18:15:32 -0700184
Miklos Vajna2d179852008-12-03 14:26:50 +0100185curr_head=$(git rev-parse -q --verify HEAD)
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700186if test -n "$orig_head" && test "$curr_head" != "$orig_head"
Junio C Hamanob10ac502005-08-25 18:15:32 -0700187then
188 # The fetch involved updating the current branch.
189
190 # The working tree and the index file is still based on the
191 # $orig_head commit, but we are merging into $curr_head.
192 # First update the working tree to match $curr_head.
193
194 echo >&2 "Warning: fetch updated the current branch head."
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300195 echo >&2 "Warning: fast-forwarding your working tree from"
Junio C Hamanoa057f802006-10-10 23:00:29 -0700196 echo >&2 "Warning: commit $orig_head."
Dan Loewenherz7bd93c12009-04-22 21:46:02 -0400197 git update-index -q --refresh
Junio C Hamano5be60072007-07-02 22:52:14 -0700198 git read-tree -u -m "$orig_head" "$curr_head" ||
Junio C Hamano83231242006-03-22 01:57:11 -0800199 die 'Cannot fast-forward your working tree.
200After making sure that you saved anything precious from
201$ git diff '$orig_head'
202output, run
203$ git reset --hard
204to recover.'
205
Junio C Hamanob10ac502005-08-25 18:15:32 -0700206fi
207
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700208merge_head=$(sed -e '/ not-for-merge /d' \
209 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
210 tr '\012' ' ')
Junio C Hamanoe0bfc812005-08-20 02:57:26 -0700211
212case "$merge_head" in
Junio C Hamano521003f2005-08-22 21:57:59 -0700213'')
Junio C Hamano4973aa22009-10-05 12:03:25 -0700214 error_on_no_merge_candidates "$@"
Junio C Hamano521003f2005-08-22 21:57:59 -0700215 ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700216?*' '?*)
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800217 if test -z "$orig_head"
218 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700219 die "Cannot merge multiple branches into empty head"
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800220 fi
Jay Soffian51b2ead2009-02-18 08:44:02 -0500221 if test true = "$rebase"
222 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700223 die "Cannot rebase onto multiple branches"
Jay Soffian51b2ead2009-02-18 08:44:02 -0500224 fi
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700225 ;;
226esac
227
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800228if test -z "$orig_head"
229then
Junio C Hamanob0ad11e2008-10-14 15:32:20 -0700230 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700231 git read-tree --reset -u HEAD || exit 1
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800232 exit
233fi
234
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200235merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000236test true = "$rebase" &&
Junio C Hamano77c29b42009-12-08 15:21:02 -0800237 exec git-rebase $diffstat $strategy_args --onto $merge_head \
Jay Soffian0d2dd192008-02-22 19:52:29 -0500238 ${oldremoteref:-$merge_head}
Junio C Hamano77c29b42009-12-08 15:21:02 -0800239exec git-merge $diffstat $no_commit $squash $no_ff $ff_only $log_arg $strategy_args \
240 "$merge_name" HEAD $merge_head $verbosity