]> git.ipfire.org Git - thirdparty/git.git/blame - git-pull.sh
Teach git-pull to pass -X<option> to git-merge
[thirdparty/git.git] / git-pull.sh
CommitLineData
839a7a06
LT
1#!/bin/sh
2#
521003ff
JH
3# Copyright (c) 2005 Junio C Hamano
4#
5# Fetch one or more remote refs and merge it/them into the current HEAD.
6
d8abe148 7USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s strategy]... [<fetch-options>] <repo> <head>...'
806f36d4 8LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
533b7039 9SUBDIRECTORY_OK=Yes
8f321a39 10OPTIONS_SPEC=
ae2b0f15 11. git-sh-setup
f9474132 12set_reflog_action "pull $*"
7eff28a9 13require_work_tree
533b7039 14cd_to_toplevel
b10ac50f 15
d1014a17 16test -z "$(git ls-files -u)" ||
533b7039 17 die "You are in the middle of a conflicted merge."
d1014a17 18
13474835
BG
19strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
20log_arg= verbosity=
ee2c7955 21merge_args=
cd67e4d4
JS
22curr_branch=$(git symbolic-ref -q HEAD)
23curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
24rebase=$(git config --bool branch.$curr_branch_short.rebase)
822f7c73 25while :
60fb5b2c
JH
26do
27 case "$1" in
7f87aff2 28 -q|--quiet)
c6576f91 29 verbosity="$verbosity -q" ;;
7f87aff2 30 -v|--verbose)
c6576f91 31 verbosity="$verbosity -v" ;;
d8abe148 32 -n|--no-stat|--no-summary)
a334e125 33 diffstat=--no-stat ;;
d8abe148 34 --stat|--summary)
a334e125 35 diffstat=--stat ;;
efb779f8
SG
36 --log|--no-log)
37 log_arg=$1 ;;
123ee3ca
JH
38 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
39 no_commit=--no-commit ;;
5072a323
LH
40 --c|--co|--com|--comm|--commi|--commit)
41 no_commit=--commit ;;
7d0c6887
JH
42 --sq|--squ|--squa|--squas|--squash)
43 squash=--squash ;;
5072a323
LH
44 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
45 squash=--no-squash ;;
46 --ff)
47 no_ff=--ff ;;
48 --no-ff)
49 no_ff=--no-ff ;;
13474835
BG
50 --ff-only)
51 ff_only=--ff-only ;;
60fb5b2c
JH
52 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
53 --strateg=*|--strategy=*|\
54 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
55 case "$#,$1" in
56 *,*=*)
8096fae7 57 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60fb5b2c
JH
58 1,*)
59 usage ;;
60 *)
61 strategy="$2"
62 shift ;;
63 esac
64 strategy_args="${strategy_args}-s $strategy "
65 ;;
ee2c7955
AP
66 -X*)
67 case "$#,$1" in
68 1,-X)
69 usage ;;
70 *,-X)
71 xx="-X $2"
72 shift ;;
73 *,*)
74 xx="$1" ;;
75 esac
76 merge_args="$merge_args$xx "
77 ;;
cd67e4d4
JS
78 -r|--r|--re|--reb|--reba|--rebas|--rebase)
79 rebase=true
80 ;;
81 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
82 rebase=false
83 ;;
93d69d86
JL
84 -h|--h|--he|--hel|--help)
85 usage
86 ;;
822f7c73
DK
87 *)
88 # Pass thru anything that may be meant for fetch.
619e5a0e 89 break
60fb5b2c
JH
90 ;;
91 esac
92 shift
93done
94
441ed413
JH
95error_on_no_merge_candidates () {
96 exec >&2
97 for opt
98 do
99 case "$opt" in
100 -t|--t|--ta|--tag|--tags)
101 echo "Fetching tags only, you probably meant:"
102 echo " git fetch --tags"
103 exit 1
104 esac
105 done
106
107 curr_branch=${curr_branch#refs/heads/}
a6dbf881 108 upstream=$(git config "branch.$curr_branch.merge")
a8c9bef4 109 remote=$(git config "branch.$curr_branch.remote")
441ed413 110
a8c9bef4
JK
111 if [ $# -gt 1 ]; then
112 echo "There are no candidates for merging in the refs that you just fetched."
113 echo "Generally this means that you provided a wildcard refspec which had no"
114 echo "matches on the remote end."
115 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
116 echo "You asked to pull from the remote '$1', but did not specify"
117 echo "a branch to merge. Because this is not the default configured remote"
118 echo "for your current branch, you must specify a branch on the command line."
119 elif [ -z "$curr_branch" ]; then
61e6108d
MM
120 echo "You are not currently on a branch, so I cannot use any"
121 echo "'branch.<branchname>.merge' in your configuration file."
122 echo "Please specify which branch you want to merge on the command"
123 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
124 echo "See git-pull(1) for details."
a6dbf881 125 elif [ -z "$upstream" ]; then
61e6108d
MM
126 echo "You asked me to pull without telling me which branch you"
127 echo "want to merge with, and 'branch.${curr_branch}.merge' in"
128 echo "your configuration file does not tell me either. Please"
129 echo "specify which branch you want to merge on the command line and"
130 echo "try again (e.g. 'git pull <repository> <refspec>')."
131 echo "See git-pull(1) for details."
132 echo
133 echo "If you often merge with the same branch, you may want to"
134 echo "configure the following variables in your configuration"
135 echo "file:"
136 echo
137 echo " branch.${curr_branch}.remote = <nickname>"
138 echo " branch.${curr_branch}.merge = <remote-ref>"
139 echo " remote.<nickname>.url = <url>"
140 echo " remote.<nickname>.fetch = <refspec>"
141 echo
142 echo "See git-config(1) for details."
a6dbf881 143 else
a8c9bef4
JK
144 echo "Your configuration specifies to merge the ref '${upstream#refs/heads/}' from the"
145 echo "remote, but no such ref was fetched."
61e6108d 146 fi
441ed413
JH
147 exit 1
148}
149
c85c7927 150test true = "$rebase" && {
19a7fcbf
JK
151 if ! git rev-parse -q --verify HEAD >/dev/null
152 then
153 # On an unborn branch
154 if test -f "$GIT_DIR/index"
155 then
156 die "updating an unborn branch with changes added to the index"
157 fi
158 else
159 git update-index --ignore-submodules --refresh &&
160 git diff-files --ignore-submodules --quiet &&
161 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
162 die "refusing to pull with rebase: your working tree is not up-to-date"
163 fi
d44e7126 164 oldremoteref= &&
c85c7927 165 . git-parse-remote &&
d44e7126
SB
166 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
167 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
168 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
169 do
170 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
171 then
172 oldremoteref="$reflog"
173 break
174 fi
175 done
c85c7927 176}
2d179857 177orig_head=$(git rev-parse -q --verify HEAD)
7f87aff2 178git fetch $verbosity --update-head-ok "$@" || exit 1
b10ac50f 179
2d179857 180curr_head=$(git rev-parse -q --verify HEAD)
b0ad11ea 181if test -n "$orig_head" && test "$curr_head" != "$orig_head"
b10ac50f
JH
182then
183 # The fetch involved updating the current branch.
184
185 # The working tree and the index file is still based on the
186 # $orig_head commit, but we are merging into $curr_head.
187 # First update the working tree to match $curr_head.
188
189 echo >&2 "Warning: fetch updated the current branch head."
a75d7b54 190 echo >&2 "Warning: fast-forwarding your working tree from"
a057f806 191 echo >&2 "Warning: commit $orig_head."
7bd93c1c 192 git update-index -q --refresh
5be60078 193 git read-tree -u -m "$orig_head" "$curr_head" ||
8323124a
JH
194 die 'Cannot fast-forward your working tree.
195After making sure that you saved anything precious from
196$ git diff '$orig_head'
197output, run
198$ git reset --hard
199to recover.'
200
b10ac50f
JH
201fi
202
05dd8e2e
JH
203merge_head=$(sed -e '/ not-for-merge /d' \
204 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
205 tr '\012' ' ')
e0bfc81e
JH
206
207case "$merge_head" in
521003ff 208'')
4973aa22 209 error_on_no_merge_candidates "$@"
521003ff 210 ;;
60fb5b2c 211?*' '?*)
d09e79cb
LT
212 if test -z "$orig_head"
213 then
bc2bbc45 214 die "Cannot merge multiple branches into empty head"
d09e79cb 215 fi
51b2ead0
JS
216 if test true = "$rebase"
217 then
bc2bbc45 218 die "Cannot rebase onto multiple branches"
51b2ead0 219 fi
60fb5b2c
JH
220 ;;
221esac
222
d09e79cb
LT
223if test -z "$orig_head"
224then
b0ad11ea 225 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
5be60078 226 git read-tree --reset -u HEAD || exit 1
d09e79cb
LT
227 exit
228fi
229
efb779f8 230merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
c85c7927 231test true = "$rebase" &&
ee2c7955 232 exec git-rebase $diffstat $strategy_args $merge_args --onto $merge_head \
0d2dd191 233 ${oldremoteref:-$merge_head}
ee2c7955 234exec git-merge $diffstat $no_commit $squash $no_ff $ff_only $log_arg $strategy_args $merge_args \
7f87aff2 235 "$merge_name" HEAD $merge_head $verbosity