MSVC: fix poll-related macro redefines
[git/dscho.git] / git-pull.sh
blobeec078300c917b527d3c2ad13dfacc98e13d4a6c
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 merge it/them into 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=
43 merge_args=
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 --sq|--squ|--squa|--squas|--squash)
83 squash=--squash ;;
84 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
85 squash=--no-squash ;;
86 --ff)
87 no_ff=--ff ;;
88 --no-ff)
89 no_ff=--no-ff ;;
90 --ff-only)
91 ff_only=--ff-only ;;
92 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
93 --strateg=*|--strategy=*|\
94 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
95 case "$#,$1" in
96 *,*=*)
97 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
98 1,*)
99 usage ;;
101 strategy="$2"
102 shift ;;
103 esac
104 strategy_args="${strategy_args}-s $strategy "
106 -X*)
107 case "$#,$1" in
108 1,-X)
109 usage ;;
110 *,-X)
111 xx="-X $(git rev-parse --sq-quote "$2")"
112 shift ;;
113 *,*)
114 xx=$(git rev-parse --sq-quote "$1") ;;
115 esac
116 merge_args="$merge_args$xx "
118 -r|--r|--re|--reb|--reba|--rebas|--rebase)
119 rebase=true
121 --rebase=i|--rebase=interactive)
122 rebase_options=-i
123 rebase=true
125 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
126 rebase_options=
127 rebase=false
129 --recurse-submodules)
130 recurse_submodules=--recurse-submodules
132 --recurse-submodules=*)
133 recurse_submodules="$1"
135 --no-recurse-submodules)
136 recurse_submodules=--no-recurse-submodules
138 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
139 dry_run=--dry-run
141 -h|--help-all)
142 usage
145 # Pass thru anything that may be meant for fetch.
146 break
148 esac
149 shift
150 done
152 error_on_no_merge_candidates () {
153 exec >&2
154 for opt
156 case "$opt" in
157 -t|--t|--ta|--tag|--tags)
158 echo "Fetching tags only, you probably meant:"
159 echo " git fetch --tags"
160 exit 1
161 esac
162 done
164 if test true = "$rebase"
165 then
166 op_type=rebase
167 op_prep=against
168 else
169 op_type=merge
170 op_prep=with
173 curr_branch=${curr_branch#refs/heads/}
174 upstream=$(git config "branch.$curr_branch.merge")
175 remote=$(git config "branch.$curr_branch.remote")
177 if [ $# -gt 1 ]; then
178 if [ "$rebase" = true ]; then
179 printf "There is no candidate for rebasing against "
180 else
181 printf "There are no candidates for merging "
183 echo "among the refs that you just fetched."
184 echo "Generally this means that you provided a wildcard refspec which had no"
185 echo "matches on the remote end."
186 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
187 echo "You asked to pull from the remote '$1', but did not specify"
188 echo "a branch. Because this is not the default configured remote"
189 echo "for your current branch, you must specify a branch on the command line."
190 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
191 . git-parse-remote
192 error_on_missing_default_upstream "pull" $op_type $op_prep \
193 "git pull <repository> <refspec>"
194 else
195 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
196 echo "from the remote, but no such ref was fetched."
198 exit 1
201 test true = "$rebase" && {
202 if ! git rev-parse -q --verify HEAD >/dev/null
203 then
204 # On an unborn branch
205 if test -f "$GIT_DIR/index"
206 then
207 die "$(gettext "updating an unborn branch with changes added to the index")"
209 else
210 require_clean_work_tree "pull with rebase" "Please commit or stash them."
212 oldremoteref= &&
213 . git-parse-remote &&
214 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
215 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
216 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
218 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
219 then
220 oldremoteref="$reflog"
221 break
223 done
225 orig_head=$(git rev-parse -q --verify HEAD)
226 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
227 test -z "$dry_run" || exit 0
229 curr_head=$(git rev-parse -q --verify HEAD)
230 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
231 then
232 # The fetch involved updating the current branch.
234 # The working tree and the index file is still based on the
235 # $orig_head commit, but we are merging into $curr_head.
236 # First update the working tree to match $curr_head.
238 eval_gettextln "Warning: fetch updated the current branch head.
239 Warning: fast-forwarding your working tree from
240 Warning: commit \$orig_head." >&2
241 git update-index -q --refresh
242 git read-tree -u -m "$orig_head" "$curr_head" ||
243 die "$(eval_gettext "Cannot fast-forward your working tree.
244 After making sure that you saved anything precious from
245 $ git diff \$orig_head
246 output, run
247 $ git reset --hard
248 to recover.")"
252 merge_head=$(sed -e '/ not-for-merge /d' \
253 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
254 tr '\012' ' ')
256 case "$merge_head" in
258 error_on_no_merge_candidates "$@"
260 ?*' '?*)
261 if test -z "$orig_head"
262 then
263 die "$(gettext "Cannot merge multiple branches into empty head")"
265 if test true = "$rebase"
266 then
267 die "$(gettext "Cannot rebase onto multiple branches")"
270 esac
272 if test -z "$orig_head"
273 then
274 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
275 git read-tree -m -u HEAD || exit 1
276 exit
279 if test true = "$rebase"
280 then
281 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
282 if test "$oldremoteref" = "$o"
283 then
284 unset oldremoteref
288 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
289 case "$rebase" in
290 true)
291 eval="git-rebase $rebase_options $diffstat $strategy_args $merge_args"
292 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
295 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
296 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
297 eval="$eval \"\$merge_name\" HEAD $merge_head"
299 esac
300 eval "exec $eval"