MSVC: fix poll-related macro redefines
[git/mingw/4msysgit.git] / git-pull.sh
blob8ff00a3334f85af7f1b4a4d40afda8f550f60028
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] [-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 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
42 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
43 merge_args= edit=
44 curr_branch=$(git symbolic-ref -q HEAD)
45 curr_branch_short="${curr_branch#refs/heads/}"
46 rebase_options=
47 case "$(git config branch.$curr_branch_short.rebase)" in
48 interactive)
49 rebase_options=-i
50 rebase=true
53 rebase=$(git config --bool branch.$curr_branch_short.rebase)
55 esac
56 if test -z "$rebase"
57 then
58 rebase=$(git config --bool pull.rebase)
60 dry_run=
61 while :
63 case "$1" in
64 -q|--quiet)
65 verbosity="$verbosity -q" ;;
66 -v|--verbose)
67 verbosity="$verbosity -v" ;;
68 --progress)
69 progress=--progress ;;
70 --no-progress)
71 progress=--no-progress ;;
72 -n|--no-stat|--no-summary)
73 diffstat=--no-stat ;;
74 --stat|--summary)
75 diffstat=--stat ;;
76 --log|--no-log)
77 log_arg=$1 ;;
78 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
79 no_commit=--no-commit ;;
80 --c|--co|--com|--comm|--commi|--commit)
81 no_commit=--commit ;;
82 -e|--edit)
83 edit=--edit ;;
84 --no-edit)
85 edit=--no-edit ;;
86 --sq|--squ|--squa|--squas|--squash)
87 squash=--squash ;;
88 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
89 squash=--no-squash ;;
90 --ff)
91 no_ff=--ff ;;
92 --no-ff)
93 no_ff=--no-ff ;;
94 --ff-only)
95 ff_only=--ff-only ;;
96 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
97 --strateg=*|--strategy=*|\
98 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
99 case "$#,$1" in
100 *,*=*)
101 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
102 1,*)
103 usage ;;
105 strategy="$2"
106 shift ;;
107 esac
108 strategy_args="${strategy_args}-s $strategy "
110 -X*)
111 case "$#,$1" in
112 1,-X)
113 usage ;;
114 *,-X)
115 xx="-X $(git rev-parse --sq-quote "$2")"
116 shift ;;
117 *,*)
118 xx=$(git rev-parse --sq-quote "$1") ;;
119 esac
120 merge_args="$merge_args$xx "
122 -r|--r|--re|--reb|--reba|--rebas|--rebase)
123 rebase=true
125 --rebase=i|--rebase=interactive)
126 rebase_options=-i
127 rebase=true
129 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
130 rebase_options=
131 rebase=false
133 --recurse-submodules)
134 recurse_submodules=--recurse-submodules
136 --recurse-submodules=*)
137 recurse_submodules="$1"
139 --no-recurse-submodules)
140 recurse_submodules=--no-recurse-submodules
142 --verify-signatures)
143 verify_signatures=--verify-signatures
145 --no-verify-signatures)
146 verify_signatures=--no-verify-signatures
148 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
149 dry_run=--dry-run
151 -h|--help-all)
152 usage
155 # Pass thru anything that may be meant for fetch.
156 break
158 esac
159 shift
160 done
162 error_on_no_merge_candidates () {
163 exec >&2
164 for opt
166 case "$opt" in
167 -t|--t|--ta|--tag|--tags)
168 echo "Fetching tags only, you probably meant:"
169 echo " git fetch --tags"
170 exit 1
171 esac
172 done
174 if test true = "$rebase"
175 then
176 op_type=rebase
177 op_prep=against
178 else
179 op_type=merge
180 op_prep=with
183 curr_branch=${curr_branch#refs/heads/}
184 upstream=$(git config "branch.$curr_branch.merge")
185 remote=$(git config "branch.$curr_branch.remote")
187 if [ $# -gt 1 ]; then
188 if [ "$rebase" = true ]; then
189 printf "There is no candidate for rebasing against "
190 else
191 printf "There are no candidates for merging "
193 echo "among the refs that you just fetched."
194 echo "Generally this means that you provided a wildcard refspec which had no"
195 echo "matches on the remote end."
196 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
197 echo "You asked to pull from the remote '$1', but did not specify"
198 echo "a branch. Because this is not the default configured remote"
199 echo "for your current branch, you must specify a branch on the command line."
200 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
201 . git-parse-remote
202 error_on_missing_default_upstream "pull" $op_type $op_prep \
203 "git pull <remote> <branch>"
204 else
205 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
206 echo "from the remote, but no such ref was fetched."
208 exit 1
211 test true = "$rebase" && {
212 if ! git rev-parse -q --verify HEAD >/dev/null
213 then
214 # On an unborn branch
215 if test -f "$GIT_DIR/index"
216 then
217 die "$(gettext "updating an unborn branch with changes added to the index")"
219 else
220 require_clean_work_tree "pull with rebase" "Please commit or stash them."
222 oldremoteref= &&
223 test -n "$curr_branch" &&
224 . git-parse-remote &&
225 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
226 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
227 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
229 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
230 then
231 oldremoteref="$reflog"
232 break
234 done
236 orig_head=$(git rev-parse -q --verify HEAD)
237 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
238 test -z "$dry_run" || exit 0
240 curr_head=$(git rev-parse -q --verify HEAD)
241 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
242 then
243 # The fetch involved updating the current branch.
245 # The working tree and the index file is still based on the
246 # $orig_head commit, but we are merging into $curr_head.
247 # First update the working tree to match $curr_head.
249 eval_gettextln "Warning: fetch updated the current branch head.
250 Warning: fast-forwarding your working tree from
251 Warning: commit \$orig_head." >&2
252 git update-index -q --refresh
253 git read-tree -u -m "$orig_head" "$curr_head" ||
254 die "$(eval_gettext "Cannot fast-forward your working tree.
255 After making sure that you saved anything precious from
256 $ git diff \$orig_head
257 output, run
258 $ git reset --hard
259 to recover.")"
263 merge_head=$(sed -e '/ not-for-merge /d' \
264 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
265 tr '\012' ' ')
267 case "$merge_head" in
269 error_on_no_merge_candidates "$@"
271 ?*' '?*)
272 if test -z "$orig_head"
273 then
274 die "$(gettext "Cannot merge multiple branches into empty head")"
276 if test true = "$rebase"
277 then
278 die "$(gettext "Cannot rebase onto multiple branches")"
281 esac
283 # Pulling into unborn branch: a shorthand for branching off
284 # FETCH_HEAD, for lazy typers.
285 if test -z "$orig_head"
286 then
287 # Two-way merge: we claim the index is based on an empty tree,
288 # and try to fast-forward to HEAD. This ensures we will not
289 # lose index/worktree changes that the user already made on
290 # the unborn branch.
291 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
292 git read-tree -m -u $empty_tree $merge_head &&
293 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
294 exit
297 if test true = "$rebase"
298 then
299 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
300 if test "$oldremoteref" = "$o"
301 then
302 unset oldremoteref
306 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
307 case "$rebase" in
308 true)
309 eval="git-rebase $rebase_options $diffstat $strategy_args $merge_args $verbosity"
310 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
313 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
314 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
315 eval="$eval \"\$merge_name\" HEAD $merge_head"
317 esac
318 eval "exec $eval"