blob: e63a864c7b1b5057737fda1e52c88d1d5e40322f [file] [log] [blame]
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4
5# SHORT DESCRIPTION
6#
7# This script makes it easy to fix up commits in the middle of a series,
8# and rearrange commits.
9#
10# The original idea comes from Eric W. Biederman, in
11# http://article.gmane.org/gmane.comp.version-control.git/22407
12
Stephan Beyer7970aaf2008-07-12 17:48:20 +020013OPTIONS_KEEPDASHDASH=
14OPTIONS_SPEC="\
15git-rebase [-i] [options] [--] <upstream> [<branch>]
16git-rebase [-i] (--continue | --abort | --skip)
17--
18 Available options are
19v,verbose display a diffstat of what changed upstream
20onto= rebase onto given branch instead of upstream
21p,preserve-merges try to recreate merges instead of ignoring them
22s,strategy= use the given merge strategy
23m,merge always used (no-op)
24i,interactive always used (no-op)
25 Actions:
26continue continue rebasing process
27abort abort rebasing process and restore original branch
28skip skip current patch and continue rebasing process
29"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010030
31. git-sh-setup
32require_work_tree
33
Johannes Schindelin28ed6e72008-07-16 03:33:44 +020034DOTEST="$GIT_DIR/rebase-merge"
Seth Falconc22486c2007-07-28 16:44:40 -070035TODO="$DOTEST"/git-rebase-todo
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010036DONE="$DOTEST"/done
Johannes Schindelin6368f3f2007-07-21 18:09:41 +010037MSG="$DOTEST"/message
38SQUASH_MSG="$DOTEST"/message-squash
Johannes Schindelinf09c9b82007-06-25 18:59:43 +010039REWRITTEN="$DOTEST"/rewritten
40PRESERVE_MERGES=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010041STRATEGY=
Stephan Beyer7970aaf2008-07-12 17:48:20 +020042ONTO=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010043VERBOSE=
44
Wincent Colaiuta804c7172007-11-28 00:06:36 -080045GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
46mark the corrected paths with 'git add <paths>', and
47run 'git rebase --continue'"
48export GIT_CHERRY_PICK_HELP
49
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010050warn () {
51 echo "$*" >&2
52}
53
Johannes Schindelindfa49f32007-07-23 23:45:49 +010054output () {
55 case "$VERBOSE" in
56 '')
Matt Kraai51668102007-09-25 18:30:13 -070057 output=$("$@" 2>&1 )
Johannes Schindelindfa49f32007-07-23 23:45:49 +010058 status=$?
Matt Kraai51668102007-09-25 18:30:13 -070059 test $status != 0 && printf "%s\n" "$output"
Johannes Schindelindfa49f32007-07-23 23:45:49 +010060 return $status
Johannes Schindelin376ccb82007-09-25 16:42:51 +010061 ;;
Johannes Schindelindfa49f32007-07-23 23:45:49 +010062 *)
63 "$@"
Johannes Schindelin376ccb82007-09-25 16:42:51 +010064 ;;
Johannes Schindelindfa49f32007-07-23 23:45:49 +010065 esac
66}
67
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010068require_clean_work_tree () {
69 # test if working tree is dirty
70 git rev-parse --verify HEAD > /dev/null &&
Johannes Schindelin6848d582008-05-14 18:03:59 +010071 git update-index --ignore-submodules --refresh &&
72 git diff-files --quiet --ignore-submodules &&
73 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010074 die "Working tree is dirty"
75}
76
77ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
78
79comment_for_reflog () {
80 case "$ORIG_REFLOG_ACTION" in
81 ''|rebase*)
82 GIT_REFLOG_ACTION="rebase -i ($1)"
83 export GIT_REFLOG_ACTION
Johannes Schindelin376ccb82007-09-25 16:42:51 +010084 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010085 esac
86}
87
Junio C Hamano4e673872008-01-14 20:01:21 -080088last_count=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010089mark_action_done () {
90 sed -e 1q < "$TODO" >> "$DONE"
91 sed -e 1d < "$TODO" >> "$TODO".new
92 mv -f "$TODO".new "$TODO"
Jeff Kingaadbe442008-03-12 17:32:17 -040093 count=$(grep -c '^[^#]' < "$DONE")
94 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
Junio C Hamano4e673872008-01-14 20:01:21 -080095 if test "$last_count" != "$count"
96 then
97 last_count=$count
98 printf "Rebasing (%d/%d)\r" $count $total
99 test -z "$VERBOSE" || echo
100 fi
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100101}
102
103make_patch () {
Johannes Schindelinbe6ff202007-09-25 16:42:36 +0100104 parent_sha1=$(git rev-parse --verify "$1"^) ||
105 die "Cannot get patch for $1^"
Johannes Schindelinf3d5e462007-10-09 13:59:43 +0100106 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
Johannes Schindelinbe6ff202007-09-25 16:42:36 +0100107 test -f "$DOTEST"/message ||
108 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
109 test -f "$DOTEST"/author-script ||
110 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100111}
112
113die_with_patch () {
114 make_patch "$1"
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000115 git rerere
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100116 die "$2"
117}
118
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100119die_abort () {
120 rm -rf "$DOTEST"
121 die "$1"
122}
123
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100124has_action () {
Jeff Kingaadbe442008-03-12 17:32:17 -0400125 grep '^[^#]' "$1" >/dev/null
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100126}
127
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100128pick_one () {
Johannes Schindelin1d25c8c2007-08-23 09:55:41 +0100129 no_ff=
130 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100131 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100132 test -d "$REWRITTEN" &&
133 pick_one_preserving_merges "$@" && return
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100134 parent_sha1=$(git rev-parse --verify $sha1^) ||
135 die "Could not get the parent of $sha1"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100136 current_sha1=$(git rev-parse --verify HEAD)
Michael W. Olson28580282007-10-15 13:48:27 -0400137 if test "$no_ff$current_sha1" = "$parent_sha1"; then
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100138 output git reset --hard $sha1
139 test "a$1" = a-n && output git reset --soft $current_sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100140 sha1=$(git rev-parse --short $sha1)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100141 output warn Fast forward to $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100142 else
Björn Steinbrink2a9c53e2007-10-31 03:20:31 +0100143 output git cherry-pick "$@"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100144 fi
145}
146
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100147pick_one_preserving_merges () {
148 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
149 sha1=$(git rev-parse $sha1)
150
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100151 if test -f "$DOTEST"/current-commit
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100152 then
153 current_commit=$(cat "$DOTEST"/current-commit) &&
154 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
155 rm "$DOTEST"/current-commit ||
156 die "Cannot write current commit's replacement sha1"
157 fi
158
159 # rewrite parents; if none were rewritten, we can fast-forward.
160 fast_forward=t
161 preserve=t
162 new_parents=
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100163 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100164 do
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100165 if test -f "$REWRITTEN"/$p
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100166 then
167 preserve=f
168 new_p=$(cat "$REWRITTEN"/$p)
169 test $p != $new_p && fast_forward=f
170 case "$new_parents" in
171 *$new_p*)
172 ;; # do nothing; that parent is already there
173 *)
174 new_parents="$new_parents $new_p"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100175 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100176 esac
Stephan Beyer1c5fa0a2008-07-16 03:51:49 +0200177 else
178 new_parents="$new_parents $p"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100179 fi
180 done
181 case $fast_forward in
182 t)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100183 output warn "Fast forward to $sha1"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100184 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100185 ;;
186 f)
187 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
188
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100189 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100190 # detach HEAD to current parent
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100191 output git checkout $first_parent 2> /dev/null ||
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100192 die "Cannot move HEAD to $first_parent"
193
194 echo $sha1 > "$DOTEST"/current-commit
195 case "$new_parents" in
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100196 ' '*' '*)
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100197 # redo merge
198 author_script=$(get_author_ident_from_commit $sha1)
199 eval "$author_script"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100200 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
Björn Steinbrinkf91333d2007-10-31 03:20:32 +0100201 # No point in merging the first parent, that's HEAD
202 new_parents=${new_parents# $first_parent}
Johannes Schindelinae830ed2007-09-25 16:43:44 +0100203 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
204 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
205 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
206 output git merge $STRATEGY -m "$msg" \
207 $new_parents
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100208 then
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000209 git rerere
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100210 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
Johannes Schindelin18640d92007-07-08 03:01:29 +0100211 die Error redoing merge $sha1
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100212 fi
213 ;;
214 *)
Björn Steinbrink2a9c53e2007-10-31 03:20:31 +0100215 output git cherry-pick "$@" ||
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100216 die_with_patch $sha1 "Could not pick $sha1"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100217 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100218 esac
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100219 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100220 esac
221}
222
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100223nth_string () {
224 case "$1" in
225 *1[0-9]|*[04-9]) echo "$1"th;;
226 *1) echo "$1"st;;
227 *2) echo "$1"nd;;
228 *3) echo "$1"rd;;
229 esac
230}
231
232make_squash_message () {
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100233 if test -f "$SQUASH_MSG"; then
Junio C Hamanoc7965af2007-09-01 02:17:28 -0700234 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
Jeff Kingb4ce54f2008-03-12 17:34:34 -0400235 < "$SQUASH_MSG" | sed -ne '$p')+1))
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100236 echo "# This is a combination of $COUNT commits."
Junio C Hamano8ad10652007-12-30 12:37:59 -0800237 sed -e 1d -e '2,/^./{
238 /^$/d
239 }' <"$SQUASH_MSG"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100240 else
241 COUNT=2
242 echo "# This is a combination of two commits."
243 echo "# The first commit's message is:"
244 echo
245 git cat-file commit HEAD | sed -e '1,/^$/d'
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100246 fi
Junio C Hamano8ad10652007-12-30 12:37:59 -0800247 echo
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100248 echo "# This is the $(nth_string $COUNT) commit message:"
249 echo
250 git cat-file commit $1 | sed -e '1,/^$/d'
251}
252
253peek_next_command () {
254 sed -n "1s/ .*$//p" < "$TODO"
255}
256
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100257do_next () {
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100258 rm -f "$DOTEST"/message "$DOTEST"/author-script \
259 "$DOTEST"/amend || exit
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100260 read command sha1 rest < "$TODO"
261 case "$command" in
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100262 '#'*|'')
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100263 mark_action_done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100264 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100265 pick|p)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100266 comment_for_reflog pick
267
268 mark_action_done
269 pick_one $sha1 ||
270 die_with_patch $sha1 "Could not apply $sha1... $rest"
271 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100272 edit|e)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100273 comment_for_reflog edit
274
275 mark_action_done
276 pick_one $sha1 ||
277 die_with_patch $sha1 "Could not apply $sha1... $rest"
278 make_patch $sha1
Johannes Schindelinbe6ff202007-09-25 16:42:36 +0100279 : > "$DOTEST"/amend
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100280 warn
281 warn "You can amend the commit now, with"
282 warn
283 warn " git commit --amend"
284 warn
Jonathan del Strother0460fb42008-02-27 12:50:22 +0000285 warn "Once you are satisfied with your changes, run"
286 warn
287 warn " git rebase --continue"
288 warn
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100289 exit 0
290 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100291 squash|s)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100292 comment_for_reflog squash
293
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100294 has_action "$DONE" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100295 die "Cannot 'squash' without a previous commit"
296
297 mark_action_done
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100298 make_squash_message $sha1 > "$MSG"
299 case "$(peek_next_command)" in
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100300 squash|s)
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100301 EDIT_COMMIT=
Johannes Schindelin91e1ee72007-07-26 07:35:51 +0100302 USE_OUTPUT=output
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100303 cp "$MSG" "$SQUASH_MSG"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100304 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100305 *)
306 EDIT_COMMIT=-e
Johannes Schindelin91e1ee72007-07-26 07:35:51 +0100307 USE_OUTPUT=
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100308 rm -f "$SQUASH_MSG" || exit
309 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100310 esac
311
Alex Riesen793ad042007-07-13 00:30:35 +0200312 failed=f
Johannes Schindelin81ab1cb2007-09-30 00:34:23 +0100313 author_script=$(get_author_ident_from_commit HEAD)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100314 output git reset --soft HEAD^
Johannes Schindelinfb47cfb2007-07-24 21:43:09 +0100315 pick_one -n $sha1 || failed=t
Johannes Schindelin18640d92007-07-08 03:01:29 +0100316 echo "$author_script" > "$DOTEST"/author-script
Shawn O. Pearcedbedf972007-12-19 01:45:00 -0500317 if test $failed = f
318 then
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100319 # This is like --amend, but with a different message
320 eval "$author_script"
Johannes Schindelinae830ed2007-09-25 16:43:44 +0100321 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
322 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
323 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
Shawn O. Pearcedbedf972007-12-19 01:45:00 -0500324 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
325 fi
326 if test $failed = t
327 then
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100328 cp "$MSG" "$GIT_DIR"/MERGE_MSG
329 warn
330 warn "Could not apply $sha1... $rest"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100331 die_with_patch $sha1 ""
Shawn O. Pearcedbedf972007-12-19 01:45:00 -0500332 fi
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100333 ;;
334 *)
335 warn "Unknown command: $command $sha1 $rest"
336 die_with_patch $sha1 "Please fix this in the file $TODO."
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100337 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100338 esac
339 test -s "$TODO" && return
340
Johannes Schindelin68a163c2007-06-25 18:58:28 +0100341 comment_for_reflog finish &&
342 HEADNAME=$(cat "$DOTEST"/head-name) &&
343 OLDHEAD=$(cat "$DOTEST"/head) &&
344 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100345 if test -d "$REWRITTEN"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100346 then
347 test -f "$DOTEST"/current-commit &&
348 current_commit=$(cat "$DOTEST"/current-commit) &&
349 git rev-parse HEAD > "$REWRITTEN"/$current_commit
Johannes Schindelin34454e82007-12-17 21:01:25 +0000350 if test -f "$REWRITTEN"/$OLDHEAD
351 then
352 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
353 else
354 NEWHEAD=$OLDHEAD
355 fi
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100356 else
357 NEWHEAD=$(git rev-parse HEAD)
358 fi &&
Johannes Schindelin73697a02007-09-25 16:43:15 +0100359 case $HEADNAME in
360 refs/*)
361 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
362 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
363 git symbolic-ref HEAD $HEADNAME
364 ;;
365 esac && {
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100366 test ! -f "$DOTEST"/verbose ||
Johannes Schindelinf3d5e462007-10-09 13:59:43 +0100367 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100368 } &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100369 rm -rf "$DOTEST" &&
Johannes Schindelin73697a02007-09-25 16:43:15 +0100370 git gc --auto &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100371 warn "Successfully rebased and updated $HEADNAME."
372
373 exit
374}
375
376do_rest () {
377 while :
378 do
379 do_next
380 done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100381}
382
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200383# check if no other options are set
384is_standalone () {
385 test $# -eq 2 -a "$2" = '--' &&
386 test -z "$ONTO" &&
387 test -z "$PRESERVE_MERGES" &&
388 test -z "$STRATEGY" &&
389 test -z "$VERBOSE"
390}
391
392get_saved_options () {
393 test -d "$REWRITTEN" && PRESERVE_MERGES=t
394 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
395 test -f "$DOTEST"/verbose && VERBOSE=t
396}
397
David Kastrup822f7c72007-09-23 22:42:08 +0200398while test $# != 0
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100399do
400 case "$1" in
401 --continue)
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200402 is_standalone "$@" || usage
403 get_saved_options
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100404 comment_for_reflog continue
405
406 test -d "$DOTEST" || die "No interactive rebase running"
407
Junio C Hamanoab119032007-12-30 12:51:42 -0800408 # Sanity check
409 git rev-parse --verify HEAD >/dev/null ||
410 die "Cannot read HEAD"
Johannes Schindelin6848d582008-05-14 18:03:59 +0100411 git update-index --ignore-submodules --refresh &&
412 git diff-files --quiet --ignore-submodules ||
Junio C Hamanoab119032007-12-30 12:51:42 -0800413 die "Working tree is dirty"
414
415 # do we have anything to commit?
Johannes Schindelin6848d582008-05-14 18:03:59 +0100416 if git diff-index --cached --quiet --ignore-submodules HEAD --
Shawn O. Pearce03270622007-12-20 02:12:12 -0500417 then
Junio C Hamanoab119032007-12-30 12:51:42 -0800418 : Nothing to commit -- skip this
419 else
420 . "$DOTEST"/author-script ||
421 die "Cannot find the author identity"
422 if test -f "$DOTEST"/amend
423 then
424 git reset --soft HEAD^ ||
425 die "Cannot rewind the HEAD"
426 fi
427 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
428 git commit --no-verify -F "$DOTEST"/message -e ||
Shawn O. Pearcedbedf972007-12-19 01:45:00 -0500429 die "Could not commit staged changes."
Shawn O. Pearce03270622007-12-20 02:12:12 -0500430 fi
Johannes Schindelin18640d92007-07-08 03:01:29 +0100431
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100432 require_clean_work_tree
433 do_rest
434 ;;
435 --abort)
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200436 is_standalone "$@" || usage
437 get_saved_options
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100438 comment_for_reflog abort
439
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000440 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100441 test -d "$DOTEST" || die "No interactive rebase running"
442
443 HEADNAME=$(cat "$DOTEST"/head-name)
444 HEAD=$(cat "$DOTEST"/head)
Johannes Schindelin73697a02007-09-25 16:43:15 +0100445 case $HEADNAME in
446 refs/*)
447 git symbolic-ref HEAD $HEADNAME
448 ;;
449 esac &&
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100450 output git reset --hard $HEAD &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100451 rm -rf "$DOTEST"
452 exit
453 ;;
454 --skip)
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200455 is_standalone "$@" || usage
456 get_saved_options
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100457 comment_for_reflog skip
458
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000459 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100460 test -d "$DOTEST" || die "No interactive rebase running"
461
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100462 output git reset --hard && do_rest
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100463 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200464 -s)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100465 case "$#,$1" in
466 *,*=*)
Ralf Wildenhuesb5e960b2007-11-08 22:47:36 +0100467 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100468 1,*)
469 usage ;;
470 *)
471 STRATEGY="-s $2"
472 shift ;;
473 esac
474 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200475 -m)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100476 # we use merge anyway
477 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200478 -v)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100479 VERBOSE=t
480 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200481 -p)
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100482 PRESERVE_MERGES=t
483 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200484 -i)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100485 # yeah, we know
486 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200487 --onto)
488 shift
489 ONTO=$(git rev-parse --verify "$1") ||
490 die "Does not point to a valid commit: $1"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100491 ;;
Stephan Beyer7970aaf2008-07-12 17:48:20 +0200492 --)
493 shift
494 test $# -eq 1 -o $# -eq 2 || usage
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100495 test -d "$DOTEST" &&
496 die "Interactive rebase already started"
497
498 git var GIT_COMMITTER_IDENT >/dev/null ||
499 die "You need to set your committer info first"
500
501 comment_for_reflog start
502
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100503 require_clean_work_tree
504
Johannes Sixt69e66f52008-06-02 16:01:40 +0200505 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
506 test -z "$ONTO" && ONTO=$UPSTREAM
507
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100508 if test ! -z "$2"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100509 then
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100510 output git show-ref --verify --quiet "refs/heads/$2" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100511 die "Invalid branchname: $2"
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100512 output git checkout "$2" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100513 die "Could not checkout $2"
514 fi
515
516 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
Matt Kraai51668102007-09-25 18:30:13 -0700517 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
518
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100519 : > "$DOTEST"/interactive || die "Could not mark as interactive"
Johannes Schindelin73697a02007-09-25 16:43:15 +0100520 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
521 echo "detached HEAD" > "$DOTEST"/head-name
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100522
523 echo $HEAD > "$DOTEST"/head
524 echo $UPSTREAM > "$DOTEST"/upstream
525 echo $ONTO > "$DOTEST"/onto
Johannes Schindelin8e4a91b2007-07-08 03:02:47 +0100526 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100527 test t = "$VERBOSE" && : > "$DOTEST"/verbose
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100528 if test t = "$PRESERVE_MERGES"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100529 then
530 # $REWRITTEN contains files for each commit that is
531 # reachable by at least one merge base of $HEAD and
532 # $UPSTREAM. They are not necessarily rewritten, but
533 # their children might be.
534 # This ensures that commits on merged, but otherwise
535 # unrelated side branches are left alone. (Think "X"
536 # in the man page's example.)
537 mkdir "$REWRITTEN" &&
538 for c in $(git merge-base --all $HEAD $UPSTREAM)
539 do
540 echo $ONTO > "$REWRITTEN"/$c ||
541 die "Could not init rewritten commits"
542 done
543 MERGES_OPTION=
544 else
545 MERGES_OPTION=--no-merges
546 fi
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100547
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100548 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
549 SHORTHEAD=$(git rev-parse --short $HEAD)
550 SHORTONTO=$(git rev-parse --short $ONTO)
Johannes Schindelin6047a232007-11-22 12:30:10 +0000551 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
552 --abbrev=7 --reverse --left-right --cherry-pick \
553 $UPSTREAM...$HEAD | \
554 sed -n "s/^>/pick /p" > "$TODO"
555 cat >> "$TODO" << EOF
556
557# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100558#
559# Commands:
Miklos Vajna88b1f0b2008-06-07 18:20:19 +0200560# p, pick = use commit
561# e, edit = use commit, but stop for amending
562# s, squash = use commit, but meld into previous commit
Johannes Schindelin82576dd2007-07-08 21:32:22 +0100563#
564# If you remove a line here THAT COMMIT WILL BE LOST.
Johannes Schindelin6047a232007-11-22 12:30:10 +0000565# However, if you remove everything, the rebase will be aborted.
Johannes Schindelin82576dd2007-07-08 21:32:22 +0100566#
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100567EOF
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100568
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100569 has_action "$TODO" ||
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100570 die_abort "Nothing to do"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100571
572 cp "$TODO" "$TODO".backup
Adam Robenef0c2ab2007-07-19 22:09:35 -0700573 git_editor "$TODO" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100574 die "Could not execute editor"
575
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100576 has_action "$TODO" ||
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100577 die_abort "Nothing to do"
578
Junio C Hamano22e40792008-07-07 00:16:38 -0700579 git update-ref ORIG_HEAD $HEAD
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100580 output git checkout $ONTO && do_rest
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100581 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100582 esac
583 shift
584done