Linus Torvalds | 839a7a0 | 2005-04-18 12:15:10 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 3 | # 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ábor | d8abe14 | 2008-04-06 03:23:43 +0200 | [diff] [blame] | 7 | USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...' |
freku045@student.liu.se | 806f36d | 2005-12-13 23:30:31 +0100 | [diff] [blame] | 8 | LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.' |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 9 | SUBDIRECTORY_OK=Yes |
Junio C Hamano | 8f321a3 | 2007-11-06 01:50:02 -0800 | [diff] [blame] | 10 | OPTIONS_SPEC= |
Junio C Hamano | ae2b0f1 | 2005-11-24 00:12:11 -0800 | [diff] [blame] | 11 | . git-sh-setup |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 12 | set_reflog_action "pull $*" |
Shawn O. Pearce | 7eff28a | 2006-12-30 23:32:38 -0500 | [diff] [blame] | 13 | require_work_tree |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 14 | cd_to_toplevel |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 15 | |
Matthieu Moy | d38a30d | 2010-01-12 10:54:44 +0100 | [diff] [blame] | 16 | |
| 17 | die_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. |
| 21 | Please, fix them up in the work tree, and then use 'git add/rm <file>' |
| 22 | as 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 | |
| 28 | die_merge () { |
| 29 | if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then |
| 30 | die "You have not concluded your merge (MERGE_HEAD exists). |
| 31 | Please, commit your changes before you can merge." |
| 32 | else |
| 33 | die "You have not concluded your merge (MERGE_HEAD exists)." |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | test -z "$(git ls-files -u)" || die_conflict |
| 38 | test -f "$GIT_DIR/MERGE_HEAD" && die_merge |
Junio C Hamano | d1014a1 | 2006-12-31 23:21:50 -0800 | [diff] [blame] | 39 | |
Björn Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 40 | strategy_args= diffstat= no_commit= squash= no_ff= ff_only= |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 41 | log_arg= verbosity= progress= recurse_submodules= |
Avery Pennarun | ee2c795 | 2009-11-25 21:23:57 -0500 | [diff] [blame] | 42 | merge_args= |
Johannes Schindelin | cd67e4d | 2007-11-28 13:11:07 +0000 | [diff] [blame] | 43 | curr_branch=$(git symbolic-ref -q HEAD) |
Stephen Boyd | 0d12e59 | 2010-03-17 22:10:45 -0700 | [diff] [blame] | 44 | curr_branch_short="${curr_branch#refs/heads/}" |
Johannes Schindelin | cd67e4d | 2007-11-28 13:11:07 +0000 | [diff] [blame] | 45 | rebase=$(git config --bool branch.$curr_branch_short.rebase) |
Jeff King | 29609e6 | 2010-05-25 02:07:25 -0400 | [diff] [blame] | 46 | dry_run= |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 47 | while : |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 48 | do |
| 49 | case "$1" in |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 50 | -q|--quiet) |
Tuncer Ayaz | c6576f9 | 2008-11-17 23:09:30 +0100 | [diff] [blame] | 51 | verbosity="$verbosity -q" ;; |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 52 | -v|--verbose) |
Tuncer Ayaz | c6576f9 | 2008-11-17 23:09:30 +0100 | [diff] [blame] | 53 | verbosity="$verbosity -v" ;; |
Tay Ray Chuan | 9839018 | 2010-02-24 20:50:28 +0800 | [diff] [blame] | 54 | --progress) |
| 55 | progress=--progress ;; |
Jeff King | bebd2fd | 2011-02-20 04:56:56 -0500 | [diff] [blame] | 56 | --no-progress) |
| 57 | progress=--no-progress ;; |
SZEDER Gábor | d8abe14 | 2008-04-06 03:23:43 +0200 | [diff] [blame] | 58 | -n|--no-stat|--no-summary) |
Tor Arne Vestbø | a334e12 | 2009-03-01 22:28:28 +0100 | [diff] [blame] | 59 | diffstat=--no-stat ;; |
SZEDER Gábor | d8abe14 | 2008-04-06 03:23:43 +0200 | [diff] [blame] | 60 | --stat|--summary) |
Tor Arne Vestbø | a334e12 | 2009-03-01 22:28:28 +0100 | [diff] [blame] | 61 | diffstat=--stat ;; |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame] | 62 | --log|--no-log) |
| 63 | log_arg=$1 ;; |
Junio C Hamano | 123ee3c | 2005-11-01 19:30:11 -0800 | [diff] [blame] | 64 | --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) |
| 65 | no_commit=--no-commit ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 66 | --c|--co|--com|--comm|--commi|--commit) |
| 67 | no_commit=--commit ;; |
Junio C Hamano | 7d0c688 | 2006-06-23 01:37:02 -0700 | [diff] [blame] | 68 | --sq|--squ|--squa|--squas|--squash) |
| 69 | squash=--squash ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 70 | --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 Gustavsson | 1347483 | 2009-10-29 23:08:31 +0100 | [diff] [blame] | 76 | --ff-only) |
| 77 | ff_only=--ff-only ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 78 | -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 Stosberg | 8096fae | 2006-06-27 18:54:26 +0200 | [diff] [blame] | 83 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 84 | 1,*) |
| 85 | usage ;; |
| 86 | *) |
| 87 | strategy="$2" |
| 88 | shift ;; |
| 89 | esac |
| 90 | strategy_args="${strategy_args}-s $strategy " |
| 91 | ;; |
Avery Pennarun | ee2c795 | 2009-11-25 21:23:57 -0500 | [diff] [blame] | 92 | -X*) |
| 93 | case "$#,$1" in |
| 94 | 1,-X) |
| 95 | usage ;; |
| 96 | *,-X) |
Junio C Hamano | 14e5d40 | 2010-01-17 22:31:38 -0800 | [diff] [blame] | 97 | xx="-X $(git rev-parse --sq-quote "$2")" |
Avery Pennarun | ee2c795 | 2009-11-25 21:23:57 -0500 | [diff] [blame] | 98 | shift ;; |
| 99 | *,*) |
Junio C Hamano | 14e5d40 | 2010-01-17 22:31:38 -0800 | [diff] [blame] | 100 | xx=$(git rev-parse --sq-quote "$1") ;; |
Avery Pennarun | ee2c795 | 2009-11-25 21:23:57 -0500 | [diff] [blame] | 101 | esac |
| 102 | merge_args="$merge_args$xx " |
| 103 | ;; |
Johannes Schindelin | cd67e4d | 2007-11-28 13:11:07 +0000 | [diff] [blame] | 104 | -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 Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 110 | --recurse-submodules) |
| 111 | recurse_submodules=--recurse-submodules |
| 112 | ;; |
Jens Lehmann | 8f0700d | 2011-03-06 23:11:21 +0100 | [diff] [blame] | 113 | --recurse-submodules=*) |
| 114 | recurse_submodules="$1" |
| 115 | ;; |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 116 | --no-recurse-submodules) |
| 117 | recurse_submodules=--no-recurse-submodules |
| 118 | ;; |
Jeff King | 29609e6 | 2010-05-25 02:07:25 -0400 | [diff] [blame] | 119 | --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run) |
| 120 | dry_run=--dry-run |
| 121 | ;; |
Michael J Gruber | 24231e0 | 2011-02-14 17:48:08 +0100 | [diff] [blame] | 122 | -h|--h|--he|--hel|--help|--help-|--help-a|--help-al|--help-all) |
Jon Loeliger | 93d69d8 | 2005-11-06 23:30:56 -0600 | [diff] [blame] | 123 | usage |
| 124 | ;; |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 125 | *) |
| 126 | # Pass thru anything that may be meant for fetch. |
Junio C Hamano | 619e5a0 | 2005-10-03 15:45:44 -0700 | [diff] [blame] | 127 | break |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 128 | ;; |
| 129 | esac |
| 130 | shift |
| 131 | done |
| 132 | |
Junio C Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 133 | error_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üger | 995fc2f | 2009-11-27 08:17:05 -0600 | [diff] [blame] | 145 | 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 Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 154 | curr_branch=${curr_branch#refs/heads/} |
Junio C Hamano | a6dbf88 | 2009-09-13 13:38:48 -0700 | [diff] [blame] | 155 | upstream=$(git config "branch.$curr_branch.merge") |
Jeff King | a8c9bef | 2009-10-05 15:35:16 -0400 | [diff] [blame] | 156 | remote=$(git config "branch.$curr_branch.remote") |
Junio C Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 157 | |
Jeff King | a8c9bef | 2009-10-05 15:35:16 -0400 | [diff] [blame] | 158 | if [ $# -gt 1 ]; then |
Jan Krüger | 995fc2f | 2009-11-27 08:17:05 -0600 | [diff] [blame] | 159 | 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 King | a8c9bef | 2009-10-05 15:35:16 -0400 | [diff] [blame] | 165 | 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üger | 995fc2f | 2009-11-27 08:17:05 -0600 | [diff] [blame] | 169 | echo "a branch. Because this is not the default configured remote" |
Jeff King | a8c9bef | 2009-10-05 15:35:16 -0400 | [diff] [blame] | 170 | echo "for your current branch, you must specify a branch on the command line." |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 171 | 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 Hamano | a6dbf88 | 2009-09-13 13:38:48 -0700 | [diff] [blame] | 175 | else |
Jan Krüger | 995fc2f | 2009-11-27 08:17:05 -0600 | [diff] [blame] | 176 | 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 Moy | 61e6108 | 2009-04-08 09:24:03 +0200 | [diff] [blame] | 178 | fi |
Junio C Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 179 | exit 1 |
| 180 | } |
| 181 | |
Johannes Schindelin | c85c792 | 2008-01-26 18:04:37 +0000 | [diff] [blame] | 182 | test true = "$rebase" && { |
Jeff King | 19a7fcb | 2009-08-11 23:27:40 -0400 | [diff] [blame] | 183 | 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 Ramachandra | 92c62a3 | 2010-10-19 20:09:28 +0530 | [diff] [blame] | 191 | require_clean_work_tree "pull with rebase" "Please commit or stash them." |
Jeff King | 19a7fcb | 2009-08-11 23:27:40 -0400 | [diff] [blame] | 192 | fi |
Santi Béjar | d44e712 | 2009-07-19 09:45:16 +0200 | [diff] [blame] | 193 | oldremoteref= && |
Johannes Schindelin | c85c792 | 2008-01-26 18:04:37 +0000 | [diff] [blame] | 194 | . git-parse-remote && |
Santi Béjar | d44e712 | 2009-07-19 09:45:16 +0200 | [diff] [blame] | 195 | 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 Schindelin | c85c792 | 2008-01-26 18:04:37 +0000 | [diff] [blame] | 205 | } |
Miklos Vajna | 2d17985 | 2008-12-03 14:26:50 +0100 | [diff] [blame] | 206 | orig_head=$(git rev-parse -q --verify HEAD) |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 207 | git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1 |
Jeff King | 29609e6 | 2010-05-25 02:07:25 -0400 | [diff] [blame] | 208 | test -z "$dry_run" || exit 0 |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 209 | |
Miklos Vajna | 2d17985 | 2008-12-03 14:26:50 +0100 | [diff] [blame] | 210 | curr_head=$(git rev-parse -q --verify HEAD) |
Junio C Hamano | b0ad11e | 2008-10-14 15:32:20 -0700 | [diff] [blame] | 211 | if test -n "$orig_head" && test "$curr_head" != "$orig_head" |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 212 | then |
| 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 Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 220 | echo >&2 "Warning: fast-forwarding your working tree from" |
Junio C Hamano | a057f80 | 2006-10-10 23:00:29 -0700 | [diff] [blame] | 221 | echo >&2 "Warning: commit $orig_head." |
Dan Loewenherz | 7bd93c1 | 2009-04-22 21:46:02 -0400 | [diff] [blame] | 222 | git update-index -q --refresh |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 223 | git read-tree -u -m "$orig_head" "$curr_head" || |
Junio C Hamano | 8323124 | 2006-03-22 01:57:11 -0800 | [diff] [blame] | 224 | die 'Cannot fast-forward your working tree. |
| 225 | After making sure that you saved anything precious from |
| 226 | $ git diff '$orig_head' |
| 227 | output, run |
| 228 | $ git reset --hard |
| 229 | to recover.' |
| 230 | |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 231 | fi |
| 232 | |
Junio C Hamano | 05dd8e2 | 2005-09-25 22:54:23 -0700 | [diff] [blame] | 233 | merge_head=$(sed -e '/ not-for-merge /d' \ |
| 234 | -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \ |
| 235 | tr '\012' ' ') |
Junio C Hamano | e0bfc81 | 2005-08-20 02:57:26 -0700 | [diff] [blame] | 236 | |
| 237 | case "$merge_head" in |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 238 | '') |
Junio C Hamano | 4973aa2 | 2009-10-05 12:03:25 -0700 | [diff] [blame] | 239 | error_on_no_merge_candidates "$@" |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 240 | ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 241 | ?*' '?*) |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 242 | if test -z "$orig_head" |
| 243 | then |
Stephen Boyd | bc2bbc4 | 2009-06-14 16:08:56 -0700 | [diff] [blame] | 244 | die "Cannot merge multiple branches into empty head" |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 245 | fi |
Jay Soffian | 51b2ead | 2009-02-18 08:44:02 -0500 | [diff] [blame] | 246 | if test true = "$rebase" |
| 247 | then |
Stephen Boyd | bc2bbc4 | 2009-06-14 16:08:56 -0700 | [diff] [blame] | 248 | die "Cannot rebase onto multiple branches" |
Jay Soffian | 51b2ead | 2009-02-18 08:44:02 -0500 | [diff] [blame] | 249 | fi |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 250 | ;; |
| 251 | esac |
| 252 | |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 253 | if test -z "$orig_head" |
| 254 | then |
Junio C Hamano | b0ad11e | 2008-10-14 15:32:20 -0700 | [diff] [blame] | 255 | git update-ref -m "initial pull" HEAD $merge_head "$curr_head" && |
Jeff King | 4b3ffe5 | 2011-03-25 14:13:31 -0400 | [diff] [blame] | 256 | git read-tree -m -u HEAD || exit 1 |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 257 | exit |
| 258 | fi |
| 259 | |
Elijah Newren | cf65426 | 2010-08-12 19:50:50 -0600 | [diff] [blame] | 260 | if test true = "$rebase" |
| 261 | then |
| 262 | o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref) |
| 263 | if test "$oldremoteref" = "$o" |
| 264 | then |
| 265 | unset oldremoteref |
| 266 | fi |
| 267 | fi |
| 268 | |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame] | 269 | merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit |
Junio C Hamano | 14e5d40 | 2010-01-17 22:31:38 -0800 | [diff] [blame] | 270 | case "$rebase" in |
| 271 | true) |
| 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 King | bebd2fd | 2011-02-20 04:56:56 -0500 | [diff] [blame] | 277 | eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress" |
| 278 | eval="$eval \"\$merge_name\" HEAD $merge_head" |
Junio C Hamano | 14e5d40 | 2010-01-17 22:31:38 -0800 | [diff] [blame] | 279 | ;; |
| 280 | esac |
| 281 | eval "exec $eval" |