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