Merge pull request #196 from kasal/msysgit-2.0.0
[git/mingw/4msysgit.git] / git-pull.sh
blob9d4c1908dcbed088c040f06210b9ca943b914906
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
7 USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff|--ff-only] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
9 SUBDIRECTORY_OK=Yes
10 OPTIONS_SPEC=
11 . git-sh-setup
12 . git-sh-i18n
13 set_reflog_action "pull${1+ $*}"
14 require_work_tree_exists
15 cd_to_toplevel
18 die_conflict () {
19 git diff-index --cached --name-status -r --ignore-submodules HEAD --
20 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
21 die "$(gettext "Pull is not possible because you have unmerged files.
22 Please, fix them up in the work tree, and then use 'git add/rm <file>'
23 as appropriate to mark resolution, or use 'git commit -a'.")"
24 else
25 die "$(gettext "Pull is not possible because you have unmerged files.")"
29 die_merge () {
30 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32 Please, commit your changes before you can merge.")"
33 else
34 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
38 test -z "$(git ls-files -u)" || die_conflict
39 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
41 bool_or_string_config () {
42 git config --bool "$1" 2>/dev/null || git config "$1"
45 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
46 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
47 merge_args= edit= rebase_args=
48 curr_branch=$(git symbolic-ref -q HEAD)
49 curr_branch_short="${curr_branch#refs/heads/}"
50 rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
51 if test -z "$rebase"
52 then
53 rebase=$(bool_or_string_config pull.rebase)
56 # Setup default fast-forward options via `pull.ff`
57 pull_ff=$(git config pull.ff)
58 case "$pull_ff" in
59 false)
60 no_ff=--no-ff
61 break
63 only)
64 ff_only=--ff-only
65 break
67 esac
70 dry_run=
71 while :
73 case "$1" in
74 -q|--quiet)
75 verbosity="$verbosity -q" ;;
76 -v|--verbose)
77 verbosity="$verbosity -v" ;;
78 --progress)
79 progress=--progress ;;
80 --no-progress)
81 progress=--no-progress ;;
82 -n|--no-stat|--no-summary)
83 diffstat=--no-stat ;;
84 --stat|--summary)
85 diffstat=--stat ;;
86 --log|--no-log)
87 log_arg=$1 ;;
88 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
89 no_commit=--no-commit ;;
90 --c|--co|--com|--comm|--commi|--commit)
91 no_commit=--commit ;;
92 -e|--edit)
93 edit=--edit ;;
94 --no-edit)
95 edit=--no-edit ;;
96 --sq|--squ|--squa|--squas|--squash)
97 squash=--squash ;;
98 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
99 squash=--no-squash ;;
100 --ff)
101 no_ff=--ff ;;
102 --no-ff)
103 no_ff=--no-ff ;;
104 --ff-only)
105 ff_only=--ff-only ;;
106 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
107 --strateg=*|--strategy=*|\
108 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
109 case "$#,$1" in
110 *,*=*)
111 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
112 1,*)
113 usage ;;
115 strategy="$2"
116 shift ;;
117 esac
118 strategy_args="${strategy_args}-s $strategy "
120 -X*)
121 case "$#,$1" in
122 1,-X)
123 usage ;;
124 *,-X)
125 xx="-X $(git rev-parse --sq-quote "$2")"
126 shift ;;
127 *,*)
128 xx=$(git rev-parse --sq-quote "$1") ;;
129 esac
130 merge_args="$merge_args$xx "
132 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
133 rebase="${1#*=}"
135 -r|--r|--re|--reb|--reba|--rebas|--rebase)
136 rebase=true
138 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
139 rebase=false
142 --recurse-submodules)
143 recurse_submodules=--recurse-submodules
145 --recurse-submodules=*)
146 recurse_submodules="$1"
148 --no-recurse-submodules)
149 recurse_submodules=--no-recurse-submodules
151 --verify-signatures)
152 verify_signatures=--verify-signatures
154 --no-verify-signatures)
155 verify_signatures=--no-verify-signatures
157 --gpg-sign|-S)
158 gpg_sign_args=-S
160 --gpg-sign=*)
161 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
163 -S*)
164 gpg_sign_args=$(git rev-parse --sq-quote "$1")
166 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
167 dry_run=--dry-run
169 -h|--help-all)
170 usage
173 # Pass thru anything that may be meant for fetch.
174 break
176 esac
177 shift
178 done
180 case "$rebase" in
181 i|interactive)
182 rebase=true
183 rebase_args=-i
185 preserve)
186 rebase=true
187 rebase_args=--preserve-merges
189 true|false|'')
192 echo "Invalid value for --rebase, should be true, false, interactive or preserve"
193 usage
194 exit 1
196 esac
198 error_on_no_merge_candidates () {
199 exec >&2
200 for opt
202 case "$opt" in
203 -t|--t|--ta|--tag|--tags)
204 echo "It doesn't make sense to pull all tags; you probably meant:"
205 echo " git fetch --tags"
206 exit 1
207 esac
208 done
210 if test true = "$rebase"
211 then
212 op_type=rebase
213 op_prep=against
214 else
215 op_type=merge
216 op_prep=with
219 upstream=$(git config "branch.$curr_branch_short.merge")
220 remote=$(git config "branch.$curr_branch_short.remote")
222 if [ $# -gt 1 ]; then
223 if [ "$rebase" = true ]; then
224 printf "There is no candidate for rebasing against "
225 else
226 printf "There are no candidates for merging "
228 echo "among the refs that you just fetched."
229 echo "Generally this means that you provided a wildcard refspec which had no"
230 echo "matches on the remote end."
231 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
232 echo "You asked to pull from the remote '$1', but did not specify"
233 echo "a branch. Because this is not the default configured remote"
234 echo "for your current branch, you must specify a branch on the command line."
235 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
236 . git-parse-remote
237 error_on_missing_default_upstream "pull" $op_type $op_prep \
238 "git pull <remote> <branch>"
239 else
240 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
241 echo "from the remote, but no such ref was fetched."
243 exit 1
246 test true = "$rebase" && {
247 if ! git rev-parse -q --verify HEAD >/dev/null
248 then
249 # On an unborn branch
250 if test -f "$GIT_DIR/index"
251 then
252 die "$(gettext "updating an unborn branch with changes added to the index")"
254 else
255 require_clean_work_tree "pull with rebase" "Please commit or stash them."
257 oldremoteref= &&
258 test -n "$curr_branch" &&
259 . git-parse-remote &&
260 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
261 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
263 orig_head=$(git rev-parse -q --verify HEAD)
264 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
265 test -z "$dry_run" || exit 0
267 curr_head=$(git rev-parse -q --verify HEAD)
268 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
269 then
270 # The fetch involved updating the current branch.
272 # The working tree and the index file is still based on the
273 # $orig_head commit, but we are merging into $curr_head.
274 # First update the working tree to match $curr_head.
276 eval_gettextln "Warning: fetch updated the current branch head.
277 Warning: fast-forwarding your working tree from
278 Warning: commit \$orig_head." >&2
279 git update-index -q --refresh
280 git read-tree -u -m "$orig_head" "$curr_head" ||
281 die "$(eval_gettext "Cannot fast-forward your working tree.
282 After making sure that you saved anything precious from
283 $ git diff \$orig_head
284 output, run
285 $ git reset --hard
286 to recover.")"
290 merge_head=$(sed -e '/ not-for-merge /d' \
291 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
292 tr '\012' ' ')
294 case "$merge_head" in
296 error_on_no_merge_candidates "$@"
298 ?*' '?*)
299 if test -z "$orig_head"
300 then
301 die "$(gettext "Cannot merge multiple branches into empty head")"
303 if test true = "$rebase"
304 then
305 die "$(gettext "Cannot rebase onto multiple branches")"
308 esac
310 # Pulling into unborn branch: a shorthand for branching off
311 # FETCH_HEAD, for lazy typers.
312 if test -z "$orig_head"
313 then
314 # Two-way merge: we claim the index is based on an empty tree,
315 # and try to fast-forward to HEAD. This ensures we will not
316 # lose index/worktree changes that the user already made on
317 # the unborn branch.
318 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
319 git read-tree -m -u $empty_tree $merge_head &&
320 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
321 exit
324 if test true = "$rebase"
325 then
326 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
327 if test "$oldremoteref" = "$o"
328 then
329 unset oldremoteref
333 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
334 case "$rebase" in
335 true)
336 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
337 eval="$eval $gpg_sign_args"
338 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
341 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
342 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
343 eval="$eval $gpg_sign_args"
344 eval="$eval \"\$merge_name\" HEAD $merge_head"
346 esac
347 eval "exec $eval"