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