Teach 'git pull' to handle --rebase=interactive
[git/dscho.git] / git-pull.sh
blob50f4855117c83e85ebbd957a6af74cd228d594eb
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 rebase=$(git config --bool branch.$curr_branch_short.rebase)
48 if test -z "$rebase"
49 then
50 rebase=$(git config --bool pull.rebase)
52 dry_run=
53 while :
55 case "$1" in
56 -q|--quiet)
57 verbosity="$verbosity -q" ;;
58 -v|--verbose)
59 verbosity="$verbosity -v" ;;
60 --progress)
61 progress=--progress ;;
62 --no-progress)
63 progress=--no-progress ;;
64 -n|--no-stat|--no-summary)
65 diffstat=--no-stat ;;
66 --stat|--summary)
67 diffstat=--stat ;;
68 --log|--no-log)
69 log_arg=$1 ;;
70 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
71 no_commit=--no-commit ;;
72 --c|--co|--com|--comm|--commi|--commit)
73 no_commit=--commit ;;
74 --sq|--squ|--squa|--squas|--squash)
75 squash=--squash ;;
76 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
77 squash=--no-squash ;;
78 --ff)
79 no_ff=--ff ;;
80 --no-ff)
81 no_ff=--no-ff ;;
82 --ff-only)
83 ff_only=--ff-only ;;
84 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
85 --strateg=*|--strategy=*|\
86 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
87 case "$#,$1" in
88 *,*=*)
89 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
90 1,*)
91 usage ;;
93 strategy="$2"
94 shift ;;
95 esac
96 strategy_args="${strategy_args}-s $strategy "
98 -X*)
99 case "$#,$1" in
100 1,-X)
101 usage ;;
102 *,-X)
103 xx="-X $(git rev-parse --sq-quote "$2")"
104 shift ;;
105 *,*)
106 xx=$(git rev-parse --sq-quote "$1") ;;
107 esac
108 merge_args="$merge_args$xx "
110 -r|--r|--re|--reb|--reba|--rebas|--rebase)
111 rebase=true
113 --rebase=i|--rebase=interactive)
114 rebase_options=-i
115 rebase=true
117 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
118 rebase_options=
119 rebase=false
121 --recurse-submodules)
122 recurse_submodules=--recurse-submodules
124 --recurse-submodules=*)
125 recurse_submodules="$1"
127 --no-recurse-submodules)
128 recurse_submodules=--no-recurse-submodules
130 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
131 dry_run=--dry-run
133 -h|--help-all)
134 usage
137 # Pass thru anything that may be meant for fetch.
138 break
140 esac
141 shift
142 done
144 error_on_no_merge_candidates () {
145 exec >&2
146 for opt
148 case "$opt" in
149 -t|--t|--ta|--tag|--tags)
150 echo "Fetching tags only, you probably meant:"
151 echo " git fetch --tags"
152 exit 1
153 esac
154 done
156 if test true = "$rebase"
157 then
158 op_type=rebase
159 op_prep=against
160 else
161 op_type=merge
162 op_prep=with
165 curr_branch=${curr_branch#refs/heads/}
166 upstream=$(git config "branch.$curr_branch.merge")
167 remote=$(git config "branch.$curr_branch.remote")
169 if [ $# -gt 1 ]; then
170 if [ "$rebase" = true ]; then
171 printf "There is no candidate for rebasing against "
172 else
173 printf "There are no candidates for merging "
175 echo "among the refs that you just fetched."
176 echo "Generally this means that you provided a wildcard refspec which had no"
177 echo "matches on the remote end."
178 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
179 echo "You asked to pull from the remote '$1', but did not specify"
180 echo "a branch. Because this is not the default configured remote"
181 echo "for your current branch, you must specify a branch on the command line."
182 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
183 . git-parse-remote
184 error_on_missing_default_upstream "pull" $op_type $op_prep \
185 "git pull <repository> <refspec>"
186 else
187 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
188 echo "from the remote, but no such ref was fetched."
190 exit 1
193 test true = "$rebase" && {
194 if ! git rev-parse -q --verify HEAD >/dev/null
195 then
196 # On an unborn branch
197 if test -f "$GIT_DIR/index"
198 then
199 die "$(gettext "updating an unborn branch with changes added to the index")"
201 else
202 require_clean_work_tree "pull with rebase" "Please commit or stash them."
204 oldremoteref= &&
205 . git-parse-remote &&
206 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
207 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
208 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
210 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
211 then
212 oldremoteref="$reflog"
213 break
215 done
217 orig_head=$(git rev-parse -q --verify HEAD)
218 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
219 test -z "$dry_run" || exit 0
221 curr_head=$(git rev-parse -q --verify HEAD)
222 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
223 then
224 # The fetch involved updating the current branch.
226 # The working tree and the index file is still based on the
227 # $orig_head commit, but we are merging into $curr_head.
228 # First update the working tree to match $curr_head.
230 eval_gettextln "Warning: fetch updated the current branch head.
231 Warning: fast-forwarding your working tree from
232 Warning: commit \$orig_head." >&2
233 git update-index -q --refresh
234 git read-tree -u -m "$orig_head" "$curr_head" ||
235 die "$(eval_gettext "Cannot fast-forward your working tree.
236 After making sure that you saved anything precious from
237 $ git diff \$orig_head
238 output, run
239 $ git reset --hard
240 to recover.")"
244 merge_head=$(sed -e '/ not-for-merge /d' \
245 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
246 tr '\012' ' ')
248 case "$merge_head" in
250 error_on_no_merge_candidates "$@"
252 ?*' '?*)
253 if test -z "$orig_head"
254 then
255 die "$(gettext "Cannot merge multiple branches into empty head")"
257 if test true = "$rebase"
258 then
259 die "$(gettext "Cannot rebase onto multiple branches")"
262 esac
264 if test -z "$orig_head"
265 then
266 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
267 git read-tree -m -u HEAD || exit 1
268 exit
271 if test true = "$rebase"
272 then
273 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
274 if test "$oldremoteref" = "$o"
275 then
276 unset oldremoteref
280 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
281 case "$rebase" in
282 true)
283 eval="git-rebase $rebase_options $diffstat $strategy_args $merge_args"
284 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
287 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
288 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
289 eval="$eval \"\$merge_name\" HEAD $merge_head"
291 esac
292 eval "exec $eval"