Win32: make the lstat implementation pluggable
[git/mingw/4msysgit.git] / git-pull.sh
blob573278a6a1278472532cad82c1f2c47b84323ca4
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] [--[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)
55 dry_run=
56 while :
58 case "$1" in
59 -q|--quiet)
60 verbosity="$verbosity -q" ;;
61 -v|--verbose)
62 verbosity="$verbosity -v" ;;
63 --progress)
64 progress=--progress ;;
65 --no-progress)
66 progress=--no-progress ;;
67 -n|--no-stat|--no-summary)
68 diffstat=--no-stat ;;
69 --stat|--summary)
70 diffstat=--stat ;;
71 --log|--no-log)
72 log_arg=$1 ;;
73 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
74 no_commit=--no-commit ;;
75 --c|--co|--com|--comm|--commi|--commit)
76 no_commit=--commit ;;
77 -e|--edit)
78 edit=--edit ;;
79 --no-edit)
80 edit=--no-edit ;;
81 --sq|--squ|--squa|--squas|--squash)
82 squash=--squash ;;
83 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
84 squash=--no-squash ;;
85 --ff)
86 no_ff=--ff ;;
87 --no-ff)
88 no_ff=--no-ff ;;
89 --ff-only)
90 ff_only=--ff-only ;;
91 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
92 --strateg=*|--strategy=*|\
93 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
94 case "$#,$1" in
95 *,*=*)
96 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
97 1,*)
98 usage ;;
100 strategy="$2"
101 shift ;;
102 esac
103 strategy_args="${strategy_args}-s $strategy "
105 -X*)
106 case "$#,$1" in
107 1,-X)
108 usage ;;
109 *,-X)
110 xx="-X $(git rev-parse --sq-quote "$2")"
111 shift ;;
112 *,*)
113 xx=$(git rev-parse --sq-quote "$1") ;;
114 esac
115 merge_args="$merge_args$xx "
117 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
118 rebase="${1#*=}"
120 -r|--r|--re|--reb|--reba|--rebas|--rebase)
121 rebase=true
123 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
124 rebase=false
127 --recurse-submodules)
128 recurse_submodules=--recurse-submodules
130 --recurse-submodules=*)
131 recurse_submodules="$1"
133 --no-recurse-submodules)
134 recurse_submodules=--no-recurse-submodules
136 --verify-signatures)
137 verify_signatures=--verify-signatures
139 --no-verify-signatures)
140 verify_signatures=--no-verify-signatures
142 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
143 dry_run=--dry-run
145 -h|--help-all)
146 usage
149 # Pass thru anything that may be meant for fetch.
150 break
152 esac
153 shift
154 done
156 case "$rebase" in
157 interactive)
158 rebase=true
159 rebase_args=-i
161 preserve)
162 rebase=true
163 rebase_args=--preserve-merges
165 true|false|'')
168 echo "Invalid value for --rebase, should be true, false, iteractive or preserve"
169 usage
170 exit 1
172 esac
174 error_on_no_merge_candidates () {
175 exec >&2
176 for opt
178 case "$opt" in
179 -t|--t|--ta|--tag|--tags)
180 echo "Fetching tags only, you probably meant:"
181 echo " git fetch --tags"
182 exit 1
183 esac
184 done
186 if test true = "$rebase"
187 then
188 op_type=rebase
189 op_prep=against
190 else
191 op_type=merge
192 op_prep=with
195 upstream=$(git config "branch.$curr_branch_short.merge")
196 remote=$(git config "branch.$curr_branch_short.remote")
198 if [ $# -gt 1 ]; then
199 if [ "$rebase" = true ]; then
200 printf "There is no candidate for rebasing against "
201 else
202 printf "There are no candidates for merging "
204 echo "among the refs that you just fetched."
205 echo "Generally this means that you provided a wildcard refspec which had no"
206 echo "matches on the remote end."
207 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
208 echo "You asked to pull from the remote '$1', but did not specify"
209 echo "a branch. Because this is not the default configured remote"
210 echo "for your current branch, you must specify a branch on the command line."
211 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
212 . git-parse-remote
213 error_on_missing_default_upstream "pull" $op_type $op_prep \
214 "git pull <remote> <branch>"
215 else
216 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
217 echo "from the remote, but no such ref was fetched."
219 exit 1
222 test true = "$rebase" && {
223 if ! git rev-parse -q --verify HEAD >/dev/null
224 then
225 # On an unborn branch
226 if test -f "$GIT_DIR/index"
227 then
228 die "$(gettext "updating an unborn branch with changes added to the index")"
230 else
231 require_clean_work_tree "pull with rebase" "Please commit or stash them."
233 oldremoteref= &&
234 test -n "$curr_branch" &&
235 . git-parse-remote &&
236 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
237 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
238 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
240 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
241 then
242 oldremoteref="$reflog"
243 break
245 done
247 orig_head=$(git rev-parse -q --verify HEAD)
248 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
249 test -z "$dry_run" || exit 0
251 curr_head=$(git rev-parse -q --verify HEAD)
252 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
253 then
254 # The fetch involved updating the current branch.
256 # The working tree and the index file is still based on the
257 # $orig_head commit, but we are merging into $curr_head.
258 # First update the working tree to match $curr_head.
260 eval_gettextln "Warning: fetch updated the current branch head.
261 Warning: fast-forwarding your working tree from
262 Warning: commit \$orig_head." >&2
263 git update-index -q --refresh
264 git read-tree -u -m "$orig_head" "$curr_head" ||
265 die "$(eval_gettext "Cannot fast-forward your working tree.
266 After making sure that you saved anything precious from
267 $ git diff \$orig_head
268 output, run
269 $ git reset --hard
270 to recover.")"
274 merge_head=$(sed -e '/ not-for-merge /d' \
275 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
276 tr '\012' ' ')
278 case "$merge_head" in
280 error_on_no_merge_candidates "$@"
282 ?*' '?*)
283 if test -z "$orig_head"
284 then
285 die "$(gettext "Cannot merge multiple branches into empty head")"
287 if test true = "$rebase"
288 then
289 die "$(gettext "Cannot rebase onto multiple branches")"
292 esac
294 # Pulling into unborn branch: a shorthand for branching off
295 # FETCH_HEAD, for lazy typers.
296 if test -z "$orig_head"
297 then
298 # Two-way merge: we claim the index is based on an empty tree,
299 # and try to fast-forward to HEAD. This ensures we will not
300 # lose index/worktree changes that the user already made on
301 # the unborn branch.
302 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
303 git read-tree -m -u $empty_tree $merge_head &&
304 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
305 exit
308 if test true = "$rebase"
309 then
310 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
311 if test "$oldremoteref" = "$o"
312 then
313 unset oldremoteref
317 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
318 case "$rebase" in
319 true)
320 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
321 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
324 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
325 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
326 eval="$eval \"\$merge_name\" HEAD $merge_head"
328 esac
329 eval "exec $eval"