README: Git for Windows is currently switching to the Git for Windows SDK
[git/mingw/4msysgit.git] / git-pull.sh
blob2a179305819a9ad3fb24e00f2387faa8c8c52bc9
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
62 only)
63 ff_only=--ff-only
65 esac
68 dry_run=
69 while :
71 case "$1" in
72 -q|--quiet)
73 verbosity="$verbosity -q" ;;
74 -v|--verbose)
75 verbosity="$verbosity -v" ;;
76 --progress)
77 progress=--progress ;;
78 --no-progress)
79 progress=--no-progress ;;
80 -n|--no-stat|--no-summary)
81 diffstat=--no-stat ;;
82 --stat|--summary)
83 diffstat=--stat ;;
84 --log|--no-log)
85 log_arg=$1 ;;
86 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
87 no_commit=--no-commit ;;
88 --c|--co|--com|--comm|--commi|--commit)
89 no_commit=--commit ;;
90 -e|--edit)
91 edit=--edit ;;
92 --no-edit)
93 edit=--no-edit ;;
94 --sq|--squ|--squa|--squas|--squash)
95 squash=--squash ;;
96 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
97 squash=--no-squash ;;
98 --ff)
99 no_ff=--ff ;;
100 --no-ff)
101 no_ff=--no-ff ;;
102 --ff-only)
103 ff_only=--ff-only ;;
104 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
105 --strateg=*|--strategy=*|\
106 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
107 case "$#,$1" in
108 *,*=*)
109 strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
110 1,*)
111 usage ;;
113 strategy="$2"
114 shift ;;
115 esac
116 strategy_args="${strategy_args}-s $strategy "
118 -X*)
119 case "$#,$1" in
120 1,-X)
121 usage ;;
122 *,-X)
123 xx="-X $(git rev-parse --sq-quote "$2")"
124 shift ;;
125 *,*)
126 xx=$(git rev-parse --sq-quote "$1") ;;
127 esac
128 merge_args="$merge_args$xx "
130 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
131 rebase="${1#*=}"
133 -r|--r|--re|--reb|--reba|--rebas|--rebase)
134 rebase=true
136 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
137 rebase=false
140 --recurse-submodules)
141 recurse_submodules=--recurse-submodules
143 --recurse-submodules=*)
144 recurse_submodules="$1"
146 --no-recurse-submodules)
147 recurse_submodules=--no-recurse-submodules
149 --verify-signatures)
150 verify_signatures=--verify-signatures
152 --no-verify-signatures)
153 verify_signatures=--no-verify-signatures
155 --gpg-sign|-S)
156 gpg_sign_args=-S
158 --gpg-sign=*)
159 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
161 -S*)
162 gpg_sign_args=$(git rev-parse --sq-quote "$1")
164 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
165 dry_run=--dry-run
167 -h|--help-all)
168 usage
171 # Pass thru anything that may be meant for fetch.
172 break
174 esac
175 shift
176 done
178 case "$rebase" in
179 i|interactive)
180 rebase=true
181 rebase_args=-i
183 preserve)
184 rebase=true
185 rebase_args=--preserve-merges
187 true|false|'')
190 echo "Invalid value for --rebase, should be true, false, interactive or preserve"
191 usage
192 exit 1
194 esac
196 error_on_no_merge_candidates () {
197 exec >&2
198 for opt
200 case "$opt" in
201 -t|--t|--ta|--tag|--tags)
202 echo "It doesn't make sense to pull all tags; you probably meant:"
203 echo " git fetch --tags"
204 exit 1
205 esac
206 done
208 if test true = "$rebase"
209 then
210 op_type=rebase
211 op_prep=against
212 else
213 op_type=merge
214 op_prep=with
217 upstream=$(git config "branch.$curr_branch_short.merge")
218 remote=$(git config "branch.$curr_branch_short.remote")
220 if [ $# -gt 1 ]; then
221 if [ "$rebase" = true ]; then
222 printf "There is no candidate for rebasing against "
223 else
224 printf "There are no candidates for merging "
226 echo "among the refs that you just fetched."
227 echo "Generally this means that you provided a wildcard refspec which had no"
228 echo "matches on the remote end."
229 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
230 echo "You asked to pull from the remote '$1', but did not specify"
231 echo "a branch. Because this is not the default configured remote"
232 echo "for your current branch, you must specify a branch on the command line."
233 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
234 . git-parse-remote
235 error_on_missing_default_upstream "pull" $op_type $op_prep \
236 "git pull <remote> <branch>"
237 else
238 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
239 echo "from the remote, but no such ref was fetched."
241 exit 1
244 test true = "$rebase" && {
245 if ! git rev-parse -q --verify HEAD >/dev/null
246 then
247 # On an unborn branch
248 if test -f "$GIT_DIR/index"
249 then
250 die "$(gettext "updating an unborn branch with changes added to the index")"
252 else
253 require_clean_work_tree "pull with rebase" "Please commit or stash them."
255 oldremoteref= &&
256 test -n "$curr_branch" &&
257 . git-parse-remote &&
258 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
259 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
261 orig_head=$(git rev-parse -q --verify HEAD)
262 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
263 test -z "$dry_run" || exit 0
265 curr_head=$(git rev-parse -q --verify HEAD)
266 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
267 then
268 # The fetch involved updating the current branch.
270 # The working tree and the index file is still based on the
271 # $orig_head commit, but we are merging into $curr_head.
272 # First update the working tree to match $curr_head.
274 eval_gettextln "Warning: fetch updated the current branch head.
275 Warning: fast-forwarding your working tree from
276 Warning: commit \$orig_head." >&2
277 git update-index -q --refresh
278 git read-tree -u -m "$orig_head" "$curr_head" ||
279 die "$(eval_gettext "Cannot fast-forward your working tree.
280 After making sure that you saved anything precious from
281 $ git diff \$orig_head
282 output, run
283 $ git reset --hard
284 to recover.")"
288 merge_head=$(sed -e '/ not-for-merge /d' \
289 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
290 tr '\012' ' ')
292 case "$merge_head" in
294 error_on_no_merge_candidates "$@"
296 ?*' '?*)
297 if test -z "$orig_head"
298 then
299 die "$(gettext "Cannot merge multiple branches into empty head")"
301 if test true = "$rebase"
302 then
303 die "$(gettext "Cannot rebase onto multiple branches")"
306 esac
308 # Pulling into unborn branch: a shorthand for branching off
309 # FETCH_HEAD, for lazy typers.
310 if test -z "$orig_head"
311 then
312 # Two-way merge: we claim the index is based on an empty tree,
313 # and try to fast-forward to HEAD. This ensures we will not
314 # lose index/worktree changes that the user already made on
315 # the unborn branch.
316 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
317 git read-tree -m -u $empty_tree $merge_head &&
318 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
319 exit
322 if test true = "$rebase"
323 then
324 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
325 if test "$oldremoteref" = "$o"
326 then
327 unset oldremoteref
331 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
332 case "$rebase" in
333 true)
334 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
335 eval="$eval $gpg_sign_args"
336 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
339 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
340 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
341 eval="$eval $gpg_sign_args"
342 eval="$eval \"\$merge_name\" HEAD $merge_head"
344 esac
345 eval "exec $eval"