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 | |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 7 | USAGE='[-n | --no-summary] [--[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 | |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 19 | strategy_args= no_summary= no_commit= squash= no_ff= |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 20 | while : |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 21 | do |
| 22 | case "$1" in |
| 23 | -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ |
| 24 | --no-summa|--no-summar|--no-summary) |
| 25 | no_summary=-n ;; |
Alex Riesen | 51e7ecf | 2007-05-23 23:01:29 +0200 | [diff] [blame] | 26 | --summary) |
| 27 | no_summary=$1 |
| 28 | ;; |
Junio C Hamano | 123ee3c | 2005-11-01 19:30:11 -0800 | [diff] [blame] | 29 | --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) |
| 30 | no_commit=--no-commit ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 31 | --c|--co|--com|--comm|--commi|--commit) |
| 32 | no_commit=--commit ;; |
Junio C Hamano | 7d0c688 | 2006-06-23 01:37:02 -0700 | [diff] [blame] | 33 | --sq|--squ|--squa|--squas|--squash) |
| 34 | squash=--squash ;; |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 35 | --no-sq|--no-squ|--no-squa|--no-squas|--no-squash) |
| 36 | squash=--no-squash ;; |
| 37 | --ff) |
| 38 | no_ff=--ff ;; |
| 39 | --no-ff) |
| 40 | no_ff=--no-ff ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 41 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ |
| 42 | --strateg=*|--strategy=*|\ |
| 43 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) |
| 44 | case "$#,$1" in |
| 45 | *,*=*) |
Dennis Stosberg | 8096fae | 2006-06-27 18:54:26 +0200 | [diff] [blame] | 46 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 47 | 1,*) |
| 48 | usage ;; |
| 49 | *) |
| 50 | strategy="$2" |
| 51 | shift ;; |
| 52 | esac |
| 53 | strategy_args="${strategy_args}-s $strategy " |
| 54 | ;; |
Jon Loeliger | 93d69d8 | 2005-11-06 23:30:56 -0600 | [diff] [blame] | 55 | -h|--h|--he|--hel|--help) |
| 56 | usage |
| 57 | ;; |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 58 | *) |
| 59 | # Pass thru anything that may be meant for fetch. |
Junio C Hamano | 619e5a0 | 2005-10-03 15:45:44 -0700 | [diff] [blame] | 60 | break |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 61 | ;; |
| 62 | esac |
| 63 | shift |
| 64 | done |
| 65 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 66 | orig_head=$(git rev-parse --verify HEAD 2>/dev/null) |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 67 | git-fetch --update-head-ok "$@" || exit 1 |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 68 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 69 | curr_head=$(git rev-parse --verify HEAD 2>/dev/null) |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 70 | if test "$curr_head" != "$orig_head" |
| 71 | then |
| 72 | # The fetch involved updating the current branch. |
| 73 | |
| 74 | # The working tree and the index file is still based on the |
| 75 | # $orig_head commit, but we are merging into $curr_head. |
| 76 | # First update the working tree to match $curr_head. |
| 77 | |
| 78 | echo >&2 "Warning: fetch updated the current branch head." |
Junio C Hamano | cf46e7b | 2006-03-22 01:09:43 -0800 | [diff] [blame] | 79 | echo >&2 "Warning: fast forwarding your working tree from" |
Junio C Hamano | a057f80 | 2006-10-10 23:00:29 -0700 | [diff] [blame] | 80 | echo >&2 "Warning: commit $orig_head." |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 81 | git update-index --refresh 2>/dev/null |
| 82 | git read-tree -u -m "$orig_head" "$curr_head" || |
Junio C Hamano | 8323124 | 2006-03-22 01:57:11 -0800 | [diff] [blame] | 83 | die 'Cannot fast-forward your working tree. |
| 84 | After making sure that you saved anything precious from |
| 85 | $ git diff '$orig_head' |
| 86 | output, run |
| 87 | $ git reset --hard |
| 88 | to recover.' |
| 89 | |
Junio C Hamano | b10ac50 | 2005-08-25 18:15:32 -0700 | [diff] [blame] | 90 | fi |
| 91 | |
Junio C Hamano | 05dd8e2 | 2005-09-25 22:54:23 -0700 | [diff] [blame] | 92 | merge_head=$(sed -e '/ not-for-merge /d' \ |
| 93 | -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \ |
| 94 | tr '\012' ' ') |
Junio C Hamano | e0bfc81 | 2005-08-20 02:57:26 -0700 | [diff] [blame] | 95 | |
| 96 | case "$merge_head" in |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 97 | '') |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 98 | curr_branch=$(git symbolic-ref -q HEAD) |
Jeff King | a74b170 | 2007-01-15 17:25:33 -0500 | [diff] [blame] | 99 | case $? in |
| 100 | 0) ;; |
| 101 | 1) echo >&2 "You are not currently on a branch; you must explicitly" |
| 102 | echo >&2 "specify which branch you wish to merge:" |
| 103 | echo >&2 " git pull <remote> <branch>" |
| 104 | exit 1;; |
| 105 | *) exit $?;; |
| 106 | esac |
| 107 | curr_branch=${curr_branch#refs/heads/} |
| 108 | |
Federico Mena Quintero | 8fc293c | 2007-10-02 18:36:30 -0500 | [diff] [blame] | 109 | echo >&2 "You asked me to pull without telling me which branch you" |
| 110 | echo >&2 "want to merge with, and 'branch.${curr_branch}.merge' in" |
| 111 | echo >&2 "your configuration file does not tell me either. Please" |
| 112 | echo >&2 "name which branch you want to merge on the command line and" |
| 113 | echo >&2 "try again (e.g. 'git pull <repository> <refspec>')." |
| 114 | echo >&2 "See git-pull(1) for details on the refspec." |
| 115 | echo >&2 |
| 116 | echo >&2 "If you often merge with the same branch, you may want to" |
| 117 | echo >&2 "configure the following variables in your configuration" |
| 118 | echo >&2 "file:" |
| 119 | echo >&2 |
| 120 | echo >&2 " branch.${curr_branch}.remote = <nickname>" |
| 121 | echo >&2 " branch.${curr_branch}.merge = <remote-ref>" |
| 122 | echo >&2 " remote.<nickname>.url = <url>" |
| 123 | echo >&2 " remote.<nickname>.fetch = <refspec>" |
| 124 | echo >&2 |
| 125 | echo >&2 "See git-config(1) for details." |
| 126 | exit 1 |
Junio C Hamano | 521003f | 2005-08-22 21:57:59 -0700 | [diff] [blame] | 127 | ;; |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 128 | ?*' '?*) |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 129 | if test -z "$orig_head" |
| 130 | then |
| 131 | echo >&2 "Cannot merge multiple branches into empty head" |
| 132 | exit 1 |
| 133 | fi |
Junio C Hamano | 60fb5b2 | 2005-09-25 19:43:51 -0700 | [diff] [blame] | 134 | ;; |
| 135 | esac |
| 136 | |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 137 | if test -z "$orig_head" |
| 138 | then |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 139 | git update-ref -m "initial pull" HEAD $merge_head "" && |
| 140 | git read-tree --reset -u HEAD || exit 1 |
Linus Torvalds | d09e79c | 2006-11-16 11:47:22 -0800 | [diff] [blame] | 141 | exit |
| 142 | fi |
| 143 | |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 144 | merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit |
Lars Hjemli | 5072a32 | 2007-10-29 09:41:18 +0100 | [diff] [blame] | 145 | exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \ |
Junio C Hamano | 7d0c688 | 2006-06-23 01:37:02 -0700 | [diff] [blame] | 146 | "$merge_name" HEAD $merge_head |