setup_revisions: remember whether a ref was positive or not
[git/dscho.git] / git-pull.sh
blob34b47c4fb223e321624662113eadb99f7997872a
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= 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 --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 error_on_no_merge_candidates () {
157 exec >&2
158 for opt
160 case "$opt" in
161 -t|--t|--ta|--tag|--tags)
162 echo "Fetching tags only, you probably meant:"
163 echo " git fetch --tags"
164 exit 1
165 esac
166 done
168 if test true = "$rebase"
169 then
170 op_type=rebase
171 op_prep=against
172 else
173 op_type=merge
174 op_prep=with
177 curr_branch=${curr_branch#refs/heads/}
178 upstream=$(git config "branch.$curr_branch.merge")
179 remote=$(git config "branch.$curr_branch.remote")
181 if [ $# -gt 1 ]; then
182 if [ "$rebase" = true ]; then
183 printf "There is no candidate for rebasing against "
184 else
185 printf "There are no candidates for merging "
187 echo "among the refs that you just fetched."
188 echo "Generally this means that you provided a wildcard refspec which had no"
189 echo "matches on the remote end."
190 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
191 echo "You asked to pull from the remote '$1', but did not specify"
192 echo "a branch. Because this is not the default configured remote"
193 echo "for your current branch, you must specify a branch on the command line."
194 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
195 . git-parse-remote
196 error_on_missing_default_upstream "pull" $op_type $op_prep \
197 "git pull <remote> <branch>"
198 else
199 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
200 echo "from the remote, but no such ref was fetched."
202 exit 1
205 test true = "$rebase" && {
206 if ! git rev-parse -q --verify HEAD >/dev/null
207 then
208 # On an unborn branch
209 if test -f "$GIT_DIR/index"
210 then
211 die "$(gettext "updating an unborn branch with changes added to the index")"
213 else
214 require_clean_work_tree "pull with rebase" "Please commit or stash them."
216 oldremoteref= &&
217 . git-parse-remote &&
218 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
219 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
220 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
222 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
223 then
224 oldremoteref="$reflog"
225 break
227 done
229 orig_head=$(git rev-parse -q --verify HEAD)
230 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
231 test -z "$dry_run" || exit 0
233 curr_head=$(git rev-parse -q --verify HEAD)
234 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
235 then
236 # The fetch involved updating the current branch.
238 # The working tree and the index file is still based on the
239 # $orig_head commit, but we are merging into $curr_head.
240 # First update the working tree to match $curr_head.
242 eval_gettextln "Warning: fetch updated the current branch head.
243 Warning: fast-forwarding your working tree from
244 Warning: commit \$orig_head." >&2
245 git update-index -q --refresh
246 git read-tree -u -m "$orig_head" "$curr_head" ||
247 die "$(eval_gettext "Cannot fast-forward your working tree.
248 After making sure that you saved anything precious from
249 $ git diff \$orig_head
250 output, run
251 $ git reset --hard
252 to recover.")"
256 merge_head=$(sed -e '/ not-for-merge /d' \
257 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
258 tr '\012' ' ')
260 case "$merge_head" in
262 error_on_no_merge_candidates "$@"
264 ?*' '?*)
265 if test -z "$orig_head"
266 then
267 die "$(gettext "Cannot merge multiple branches into empty head")"
269 if test true = "$rebase"
270 then
271 die "$(gettext "Cannot rebase onto multiple branches")"
274 esac
276 if test -z "$orig_head"
277 then
278 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
279 git read-tree -m -u HEAD || exit 1
280 exit
283 if test true = "$rebase"
284 then
285 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
286 if test "$oldremoteref" = "$o"
287 then
288 unset oldremoteref
292 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
293 case "$rebase" in
294 true)
295 eval="git-rebase $rebase_options $diffstat $strategy_args $merge_args"
296 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
299 eval="git-merge $diffstat $no_commit $edit $squash $no_ff $ff_only"
300 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
301 eval="$eval \"\$merge_name\" HEAD $merge_head"
303 esac
304 eval "exec $eval"