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 | |
Junio C Hamano | d1014a1 | 2006-12-31 23:21:50 -0800 | [diff] [blame] | 16 | test -z "$(git ls-files -u)" || |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 17 | die "You are in the middle of a conflicted merge." |
Junio C Hamano | d1014a1 | 2006-12-31 23:21:50 -0800 | [diff] [blame] | 18 | |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame^] | 19 | strategy_args= no_stat= no_commit= squash= no_ff= log_arg= |
Johannes Schindelin | cd67e4d | 2007-11-28 13:11:07 +0000 | [diff] [blame] | 20 | curr_branch=$(git symbolic-ref -q HEAD) |
| 21 | curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||") |
| 22 | rebase=$(git config --bool branch.$curr_branch_short.rebase) |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 23 | while : |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 24 | do |
| 25 | case "$1" in |
SZEDER Gábor | d8abe14 | 2008-04-06 03:23:43 +0200 | [diff] [blame] | 26 | -n|--no-stat|--no-summary) |
| 27 | no_stat=-n ;; |
| 28 | --stat|--summary) |
| 29 | no_stat=$1 ;; |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame^] | 30 | --log|--no-log) |
| 31 | log_arg=$1 ;; |
Junio C Hamano | 123ee3c | 2005-11-01 19:30:11 -0800 | [diff] [blame] | 32 | --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) |
| 33 | no_commit=--no-commit ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 34 | --c|--co|--com|--comm|--commi|--commit) |
| 35 | no_commit=--commit ;; |
Junio C Hamano | 7d0c688 | 2006-06-23 01:37:02 -0700 | [diff] [blame] | 36 | --sq|--squ|--squa|--squas|--squash) |
| 37 | squash=--squash ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 38 | --no-sq|--no-squ|--no-squa|--no-squas|--no-squash) |
| 39 | squash=--no-squash ;; |
| 40 | --ff) |
| 41 | no_ff=--ff ;; |
| 42 | --no-ff) |
| 43 | no_ff=--no-ff ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 44 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ |
| 45 | --strateg=*|--strategy=*|\ |
| 46 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) |
| 47 | case "$#,$1" in |
| 48 | *,*=*) |
Dennis Stosberg | 8096fae | 2006-06-27 18:54:26 +0200 | [diff] [blame] | 49 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 50 | 1,*) |
| 51 | usage ;; |
| 52 | *) |
| 53 | strategy="$2" |
| 54 | shift ;; |
| 55 | esac |
| 56 | strategy_args="${strategy_args}-s $strategy " |
| 57 | ;; |
Johannes Schindelin | cd67e4d | 2007-11-28 13:11:07 +0000 | [diff] [blame] | 58 | -r|--r|--re|--reb|--reba|--rebas|--rebase) |
| 59 | rebase=true |
| 60 | ;; |
| 61 | --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase) |
| 62 | rebase=false |
| 63 | ;; |
Jon Loeliger | 93d69d8 | 2005-11-06 23:30:56 -0600 | [diff] [blame] | 64 | -h|--h|--he|--hel|--help) |
| 65 | usage |
| 66 | ;; |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 67 | *) |
| 68 | # Pass thru anything that may be meant for fetch. |
Junio C Hamano | 619e5a0 | 2005-10-03 15:45:44 -0700 | [diff] [blame] | 69 | break |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 70 | ;; |
| 71 | esac |
| 72 | shift |
| 73 | done |
| 74 | |
Junio C Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 75 | error_on_no_merge_candidates () { |
| 76 | exec >&2 |
| 77 | for opt |
| 78 | do |
| 79 | case "$opt" in |
| 80 | -t|--t|--ta|--tag|--tags) |
| 81 | echo "Fetching tags only, you probably meant:" |
| 82 | echo " git fetch --tags" |
| 83 | exit 1 |
| 84 | esac |
| 85 | done |
| 86 | |
| 87 | curr_branch=${curr_branch#refs/heads/} |
| 88 | |
| 89 | echo "You asked me to pull without telling me which branch you" |
| 90 | echo "want to merge with, and 'branch.${curr_branch}.merge' in" |
| 91 | echo "your configuration file does not tell me either. Please" |
| 92 | echo "name which branch you want to merge on the command line and" |
| 93 | echo "try again (e.g. 'git pull <repository> <refspec>')." |
| 94 | echo "See git-pull(1) for details on the refspec." |
| 95 | echo |
| 96 | echo "If you often merge with the same branch, you may want to" |
| 97 | echo "configure the following variables in your configuration" |
| 98 | echo "file:" |
| 99 | echo |
| 100 | echo " branch.${curr_branch}.remote = <nickname>" |
| 101 | echo " branch.${curr_branch}.merge = <remote-ref>" |
| 102 | echo " remote.<nickname>.url = <url>" |
| 103 | echo " remote.<nickname>.fetch = <refspec>" |
| 104 | echo |
| 105 | echo "See git-config(1) for details." |
| 106 | exit 1 |
| 107 | } |
| 108 | |
Johannes Schindelin | c85c792 | 2008-01-26 18:04:37 +0000 | [diff] [blame] | 109 | test true = "$rebase" && { |
| 110 | . git-parse-remote && |
| 111 | origin="$1" |
| 112 | test -z "$origin" && origin=$(get_default_remote) |
| 113 | reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null | |
| 114 | sed "s|refs/heads/\(.*\):|\1|")" && |
| 115 | oldremoteref="$(git rev-parse --verify \ |
| 116 | "refs/remotes/$origin/$reflist" 2>/dev/null)" |
| 117 | } |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 118 | orig_head=$(git rev-parse --verify HEAD 2>/dev/null) |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 119 | git-fetch --update-head-ok "$@" || exit 1 |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 120 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 121 | curr_head=$(git rev-parse --verify HEAD 2>/dev/null) |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 122 | if test "$curr_head" != "$orig_head" |
| 123 | then |
| 124 | # The fetch involved updating the current branch. |
| 125 | |
| 126 | # The working tree and the index file is still based on the |
| 127 | # $orig_head commit, but we are merging into $curr_head. |
| 128 | # First update the working tree to match $curr_head. |
| 129 | |
| 130 | echo >&2 "Warning: fetch updated the current branch head." |
Junio C Hamano | cf46e7b | 2006-03-22 01:09:43 -0800 | [diff] [blame] | 131 | echo >&2 "Warning: fast forwarding your working tree from" |
Junio C Hamano | a057f80 | 2006-10-10 23:00:29 -0700 | [diff] [blame] | 132 | echo >&2 "Warning: commit $orig_head." |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 133 | git update-index --refresh 2>/dev/null |
| 134 | git read-tree -u -m "$orig_head" "$curr_head" || |
Junio C Hamano | 8323124 | 2006-03-22 01:57:11 -0800 | [diff] [blame] | 135 | die 'Cannot fast-forward your working tree. |
| 136 | After making sure that you saved anything precious from |
| 137 | $ git diff '$orig_head' |
| 138 | output, run |
| 139 | $ git reset --hard |
| 140 | to recover.' |
| 141 | |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 142 | fi |
| 143 | |
Junio C Hamano | 05dd8e2 | 2005-09-25 22:54:23 -0700 | [diff] [blame] | 144 | merge_head=$(sed -e '/ not-for-merge /d' \ |
| 145 | -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \ |
| 146 | tr '\012' ' ') |
Junio C Hamano | e0bfc81 | 2005-08-20 02:57:26 -0700 | [diff] [blame] | 147 | |
| 148 | case "$merge_head" in |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 149 | '') |
Jeff King | a74b170 | 2007-01-15 17:25:33 -0500 | [diff] [blame] | 150 | case $? in |
Junio C Hamano | 441ed41 | 2007-12-28 13:58:43 -0800 | [diff] [blame] | 151 | 0) error_on_no_merge_candidates "$@";; |
| 152 | 1) echo >&2 "You are not currently on a branch; you must explicitly" |
| 153 | echo >&2 "specify which branch you wish to merge:" |
| 154 | echo >&2 " git pull <remote> <branch>" |
| 155 | exit 1;; |
| 156 | *) exit $?;; |
Jeff King | a74b170 | 2007-01-15 17:25:33 -0500 | [diff] [blame] | 157 | esac |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 158 | ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 159 | ?*' '?*) |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 160 | if test -z "$orig_head" |
| 161 | then |
| 162 | echo >&2 "Cannot merge multiple branches into empty head" |
| 163 | exit 1 |
| 164 | fi |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 165 | ;; |
| 166 | esac |
| 167 | |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 168 | if test -z "$orig_head" |
| 169 | then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 170 | git update-ref -m "initial pull" HEAD $merge_head "" && |
| 171 | git read-tree --reset -u HEAD || exit 1 |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 172 | exit |
| 173 | fi |
| 174 | |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame^] | 175 | merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit |
Johannes Schindelin | c85c792 | 2008-01-26 18:04:37 +0000 | [diff] [blame] | 176 | test true = "$rebase" && |
Jay Soffian | 0d2dd19 | 2008-02-22 19:52:29 -0500 | [diff] [blame] | 177 | exec git-rebase $strategy_args --onto $merge_head \ |
| 178 | ${oldremoteref:-$merge_head} |
SZEDER Gábor | efb779f | 2008-04-06 03:23:46 +0200 | [diff] [blame^] | 179 | exec git-merge $no_stat $no_commit $squash $no_ff $log_arg $strategy_args \ |
Junio C Hamano | 7d0c688 | 2006-06-23 01:37:02 -0700 | [diff] [blame] | 180 | "$merge_name" HEAD $merge_head |