blob: 809e537a4d81233966aad23df11c13931d6250c1 [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
SZEDER Gáborefb779f2008-04-06 03:23:46 +020019strategy_args= no_stat= no_commit= squash= no_ff= log_arg=
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000020curr_branch=$(git symbolic-ref -q HEAD)
21curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22rebase=$(git config --bool branch.$curr_branch_short.rebase)
David Kastrup822f7c72007-09-23 22:42:08 +020023while :
Junio C Hamano60fb5b22005-09-25 19:43:51 -070024do
25 case "$1" in
SZEDER Gábord8abe142008-04-06 03:23:43 +020026 -n|--no-stat|--no-summary)
27 no_stat=-n ;;
28 --stat|--summary)
29 no_stat=$1 ;;
SZEDER Gáborefb779f2008-04-06 03:23:46 +020030 --log|--no-log)
31 log_arg=$1 ;;
Junio C Hamano123ee3c2005-11-01 19:30:11 -080032 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
33 no_commit=--no-commit ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010034 --c|--co|--com|--comm|--commi|--commit)
35 no_commit=--commit ;;
Junio C Hamano7d0c6882006-06-23 01:37:02 -070036 --sq|--squ|--squa|--squas|--squash)
37 squash=--squash ;;
Lars Hjemli5072a322007-10-29 09:41:18 +010038 --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 Hamano60fb5b22005-09-25 19:43:51 -070044 -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 Stosberg8096fae2006-06-27 18:54:26 +020049 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -070050 1,*)
51 usage ;;
52 *)
53 strategy="$2"
54 shift ;;
55 esac
56 strategy_args="${strategy_args}-s $strategy "
57 ;;
Johannes Schindelincd67e4d2007-11-28 13:11:07 +000058 -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 Loeliger93d69d82005-11-06 23:30:56 -060064 -h|--h|--he|--hel|--help)
65 usage
66 ;;
David Kastrup822f7c72007-09-23 22:42:08 +020067 *)
68 # Pass thru anything that may be meant for fetch.
Junio C Hamano619e5a02005-10-03 15:45:44 -070069 break
Junio C Hamano60fb5b22005-09-25 19:43:51 -070070 ;;
71 esac
72 shift
73done
74
Junio C Hamano441ed412007-12-28 13:58:43 -080075error_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 Schindelinc85c7922008-01-26 18:04:37 +0000109test true = "$rebase" && {
Johannes Schindelinf9189cf2008-05-21 12:32:16 +0100110 git update-index --refresh &&
111 git diff-files --quiet &&
112 git diff-index --cached --quiet HEAD -- ||
113 die "refusing to pull with rebase: your working tree is not up-to-date"
114
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000115 . git-parse-remote &&
116 origin="$1"
117 test -z "$origin" && origin=$(get_default_remote)
118 reflist="$(get_remote_refs_for_fetch "$@" 2>/dev/null |
119 sed "s|refs/heads/\(.*\):|\1|")" &&
120 oldremoteref="$(git rev-parse --verify \
121 "refs/remotes/$origin/$reflist" 2>/dev/null)"
122}
Junio C Hamano5be60072007-07-02 22:52:14 -0700123orig_head=$(git rev-parse --verify HEAD 2>/dev/null)
Shawn O. Pearcef9474132006-12-28 02:34:48 -0500124git-fetch --update-head-ok "$@" || exit 1
Junio C Hamanob10ac502005-08-25 18:15:32 -0700125
Junio C Hamano5be60072007-07-02 22:52:14 -0700126curr_head=$(git rev-parse --verify HEAD 2>/dev/null)
Junio C Hamanob10ac502005-08-25 18:15:32 -0700127if test "$curr_head" != "$orig_head"
128then
129 # The fetch involved updating the current branch.
130
131 # The working tree and the index file is still based on the
132 # $orig_head commit, but we are merging into $curr_head.
133 # First update the working tree to match $curr_head.
134
135 echo >&2 "Warning: fetch updated the current branch head."
Junio C Hamanocf46e7b2006-03-22 01:09:43 -0800136 echo >&2 "Warning: fast forwarding your working tree from"
Junio C Hamanoa057f802006-10-10 23:00:29 -0700137 echo >&2 "Warning: commit $orig_head."
Junio C Hamano5be60072007-07-02 22:52:14 -0700138 git update-index --refresh 2>/dev/null
139 git read-tree -u -m "$orig_head" "$curr_head" ||
Junio C Hamano83231242006-03-22 01:57:11 -0800140 die 'Cannot fast-forward your working tree.
141After making sure that you saved anything precious from
142$ git diff '$orig_head'
143output, run
144$ git reset --hard
145to recover.'
146
Junio C Hamanob10ac502005-08-25 18:15:32 -0700147fi
148
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700149merge_head=$(sed -e '/ not-for-merge /d' \
150 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
151 tr '\012' ' ')
Junio C Hamanoe0bfc812005-08-20 02:57:26 -0700152
153case "$merge_head" in
Junio C Hamano521003f2005-08-22 21:57:59 -0700154'')
Jeff Kinga74b1702007-01-15 17:25:33 -0500155 case $? in
Junio C Hamano441ed412007-12-28 13:58:43 -0800156 0) error_on_no_merge_candidates "$@";;
157 1) echo >&2 "You are not currently on a branch; you must explicitly"
158 echo >&2 "specify which branch you wish to merge:"
159 echo >&2 " git pull <remote> <branch>"
160 exit 1;;
161 *) exit $?;;
Jeff Kinga74b1702007-01-15 17:25:33 -0500162 esac
Junio C Hamano521003f2005-08-22 21:57:59 -0700163 ;;
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700164?*' '?*)
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800165 if test -z "$orig_head"
166 then
167 echo >&2 "Cannot merge multiple branches into empty head"
168 exit 1
169 fi
Junio C Hamano60fb5b22005-09-25 19:43:51 -0700170 ;;
171esac
172
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800173if test -z "$orig_head"
174then
Junio C Hamano5be60072007-07-02 22:52:14 -0700175 git update-ref -m "initial pull" HEAD $merge_head "" &&
176 git read-tree --reset -u HEAD || exit 1
Linus Torvaldsd09e79c2006-11-16 11:47:22 -0800177 exit
178fi
179
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200180merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
Johannes Schindelinc85c7922008-01-26 18:04:37 +0000181test true = "$rebase" &&
Jay Soffian0d2dd192008-02-22 19:52:29 -0500182 exec git-rebase $strategy_args --onto $merge_head \
183 ${oldremoteref:-$merge_head}
SZEDER Gáborefb779f2008-04-06 03:23:46 +0200184exec git-merge $no_stat $no_commit $squash $no_ff $log_arg $strategy_args \
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700185 "$merge_name" HEAD $merge_head