blob: f83e00fe8fec63d5c58f375ec49ee8bf865f1344 [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
Johannes Schindelinf09c9b82007-06-25 18:59:43 +010013USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010015
Junio C Hamano8f321a32007-11-06 01:50:02 -080016OPTIONS_SPEC=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010017. git-sh-setup
18require_work_tree
19
20DOTEST="$GIT_DIR/.dotest-merge"
Seth Falconc22486c2007-07-28 16:44:40 -070021TODO="$DOTEST"/git-rebase-todo
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010022DONE="$DOTEST"/done
Johannes Schindelin6368f3f2007-07-21 18:09:41 +010023MSG="$DOTEST"/message
24SQUASH_MSG="$DOTEST"/message-squash
Johannes Schindelinf09c9b82007-06-25 18:59:43 +010025REWRITTEN="$DOTEST"/rewritten
26PRESERVE_MERGES=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010027STRATEGY=
28VERBOSE=
Johannes Schindelin8e4a91b2007-07-08 03:02:47 +010029test -d "$REWRITTEN" && PRESERVE_MERGES=t
30test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31test -f "$DOTEST"/verbose && VERBOSE=t
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010032
Wincent Colaiuta804c7172007-11-28 00:06:36 -080033GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34mark the corrected paths with 'git add <paths>', and
35run 'git rebase --continue'"
36export GIT_CHERRY_PICK_HELP
37
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010038warn () {
39 echo "$*" >&2
40}
41
Johannes Schindelindfa49f32007-07-23 23:45:49 +010042output () {
43 case "$VERBOSE" in
44 '')
Matt Kraai51668102007-09-25 18:30:13 -070045 output=$("$@" 2>&1 )
Johannes Schindelindfa49f32007-07-23 23:45:49 +010046 status=$?
Matt Kraai51668102007-09-25 18:30:13 -070047 test $status != 0 && printf "%s\n" "$output"
Johannes Schindelindfa49f32007-07-23 23:45:49 +010048 return $status
Johannes Schindelin376ccb82007-09-25 16:42:51 +010049 ;;
Johannes Schindelindfa49f32007-07-23 23:45:49 +010050 *)
51 "$@"
Johannes Schindelin376ccb82007-09-25 16:42:51 +010052 ;;
Johannes Schindelindfa49f32007-07-23 23:45:49 +010053 esac
54}
55
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010056require_clean_work_tree () {
57 # test if working tree is dirty
58 git rev-parse --verify HEAD > /dev/null &&
59 git update-index --refresh &&
60 git diff-files --quiet &&
Junio C Hamano38762c42007-11-28 16:15:04 -080061 git diff-index --cached --quiet HEAD -- ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010062 die "Working tree is dirty"
63}
64
65ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
66
67comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
69 ''|rebase*)
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
Johannes Schindelin376ccb82007-09-25 16:42:51 +010072 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010073 esac
74}
75
76mark_action_done () {
77 sed -e 1q < "$TODO" >> "$DONE"
78 sed -e 1d < "$TODO" >> "$TODO".new
79 mv -f "$TODO".new "$TODO"
Johannes Schindelindad4e322007-09-25 16:43:04 +010080 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
81 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
Johannes Schindelindfa49f32007-07-23 23:45:49 +010082 printf "Rebasing (%d/%d)\r" $count $total
83 test -z "$VERBOSE" || echo
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010084}
85
86make_patch () {
Johannes Schindelinbe6ff202007-09-25 16:42:36 +010087 parent_sha1=$(git rev-parse --verify "$1"^) ||
88 die "Cannot get patch for $1^"
Johannes Schindelinf3d5e462007-10-09 13:59:43 +010089 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
Johannes Schindelinbe6ff202007-09-25 16:42:36 +010090 test -f "$DOTEST"/message ||
91 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
92 test -f "$DOTEST"/author-script ||
93 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010094}
95
96die_with_patch () {
97 make_patch "$1"
Johannes Schindelinecfe72f2007-11-22 11:18:10 +000098 git rerere
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010099 die "$2"
100}
101
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100102die_abort () {
103 rm -rf "$DOTEST"
104 die "$1"
105}
106
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100107has_action () {
108 grep -vqe '^$' -e '^#' "$1"
109}
110
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100111pick_one () {
Johannes Schindelin1d25c8c2007-08-23 09:55:41 +0100112 no_ff=
113 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100114 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100115 test -d "$REWRITTEN" &&
116 pick_one_preserving_merges "$@" && return
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100117 parent_sha1=$(git rev-parse --verify $sha1^) ||
118 die "Could not get the parent of $sha1"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100119 current_sha1=$(git rev-parse --verify HEAD)
Michael W. Olson28580282007-10-15 13:48:27 -0400120 if test "$no_ff$current_sha1" = "$parent_sha1"; then
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100121 output git reset --hard $sha1
122 test "a$1" = a-n && output git reset --soft $current_sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100123 sha1=$(git rev-parse --short $sha1)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100124 output warn Fast forward to $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100125 else
Björn Steinbrink2a9c53e2007-10-31 03:20:31 +0100126 output git cherry-pick "$@"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100127 fi
128}
129
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100130pick_one_preserving_merges () {
131 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
132 sha1=$(git rev-parse $sha1)
133
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100134 if test -f "$DOTEST"/current-commit
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100135 then
136 current_commit=$(cat "$DOTEST"/current-commit) &&
137 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
138 rm "$DOTEST"/current-commit ||
139 die "Cannot write current commit's replacement sha1"
140 fi
141
142 # rewrite parents; if none were rewritten, we can fast-forward.
143 fast_forward=t
144 preserve=t
145 new_parents=
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100146 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100147 do
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100148 if test -f "$REWRITTEN"/$p
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100149 then
150 preserve=f
151 new_p=$(cat "$REWRITTEN"/$p)
152 test $p != $new_p && fast_forward=f
153 case "$new_parents" in
154 *$new_p*)
155 ;; # do nothing; that parent is already there
156 *)
157 new_parents="$new_parents $new_p"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100158 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100159 esac
160 fi
161 done
162 case $fast_forward in
163 t)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100164 output warn "Fast forward to $sha1"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100165 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100166 ;;
167 f)
168 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
169
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100170 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100171 # detach HEAD to current parent
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100172 output git checkout $first_parent 2> /dev/null ||
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100173 die "Cannot move HEAD to $first_parent"
174
175 echo $sha1 > "$DOTEST"/current-commit
176 case "$new_parents" in
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100177 ' '*' '*)
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100178 # redo merge
179 author_script=$(get_author_ident_from_commit $sha1)
180 eval "$author_script"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100181 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
Björn Steinbrinkf91333d2007-10-31 03:20:32 +0100182 # No point in merging the first parent, that's HEAD
183 new_parents=${new_parents# $first_parent}
Johannes Schindelinae830ed2007-09-25 16:43:44 +0100184 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
185 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
186 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
187 output git merge $STRATEGY -m "$msg" \
188 $new_parents
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100189 then
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000190 git rerere
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100191 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
Johannes Schindelin18640d92007-07-08 03:01:29 +0100192 die Error redoing merge $sha1
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100193 fi
194 ;;
195 *)
Björn Steinbrink2a9c53e2007-10-31 03:20:31 +0100196 output git cherry-pick "$@" ||
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100197 die_with_patch $sha1 "Could not pick $sha1"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100198 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100199 esac
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100200 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100201 esac
202}
203
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100204nth_string () {
205 case "$1" in
206 *1[0-9]|*[04-9]) echo "$1"th;;
207 *1) echo "$1"st;;
208 *2) echo "$1"nd;;
209 *3) echo "$1"rd;;
210 esac
211}
212
213make_squash_message () {
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100214 if test -f "$SQUASH_MSG"; then
Junio C Hamanoc7965af2007-09-01 02:17:28 -0700215 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100216 < "$SQUASH_MSG" | tail -n 1)+1))
217 echo "# This is a combination of $COUNT commits."
218 sed -n "2,\$p" < "$SQUASH_MSG"
219 else
220 COUNT=2
221 echo "# This is a combination of two commits."
222 echo "# The first commit's message is:"
223 echo
224 git cat-file commit HEAD | sed -e '1,/^$/d'
225 echo
226 fi
227 echo "# This is the $(nth_string $COUNT) commit message:"
228 echo
229 git cat-file commit $1 | sed -e '1,/^$/d'
230}
231
232peek_next_command () {
233 sed -n "1s/ .*$//p" < "$TODO"
234}
235
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100236do_next () {
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100237 rm -f "$DOTEST"/message "$DOTEST"/author-script \
238 "$DOTEST"/amend || exit
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100239 read command sha1 rest < "$TODO"
240 case "$command" in
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100241 '#'*|'')
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100242 mark_action_done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100243 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100244 pick|p)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100245 comment_for_reflog pick
246
247 mark_action_done
248 pick_one $sha1 ||
249 die_with_patch $sha1 "Could not apply $sha1... $rest"
250 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100251 edit|e)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100252 comment_for_reflog edit
253
254 mark_action_done
255 pick_one $sha1 ||
256 die_with_patch $sha1 "Could not apply $sha1... $rest"
257 make_patch $sha1
Johannes Schindelinbe6ff202007-09-25 16:42:36 +0100258 : > "$DOTEST"/amend
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100259 warn
260 warn "You can amend the commit now, with"
261 warn
262 warn " git commit --amend"
263 warn
264 exit 0
265 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100266 squash|s)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100267 comment_for_reflog squash
268
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100269 has_action "$DONE" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100270 die "Cannot 'squash' without a previous commit"
271
272 mark_action_done
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100273 make_squash_message $sha1 > "$MSG"
274 case "$(peek_next_command)" in
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100275 squash|s)
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100276 EDIT_COMMIT=
Johannes Schindelin91e1ee72007-07-26 07:35:51 +0100277 USE_OUTPUT=output
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100278 cp "$MSG" "$SQUASH_MSG"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100279 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100280 *)
281 EDIT_COMMIT=-e
Johannes Schindelin91e1ee72007-07-26 07:35:51 +0100282 USE_OUTPUT=
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100283 rm -f "$SQUASH_MSG" || exit
284 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100285 esac
286
Alex Riesen793ad042007-07-13 00:30:35 +0200287 failed=f
Johannes Schindelin81ab1cb2007-09-30 00:34:23 +0100288 author_script=$(get_author_ident_from_commit HEAD)
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100289 output git reset --soft HEAD^
Johannes Schindelinfb47cfb2007-07-24 21:43:09 +0100290 pick_one -n $sha1 || failed=t
Johannes Schindelin18640d92007-07-08 03:01:29 +0100291 echo "$author_script" > "$DOTEST"/author-script
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100292 case $failed in
293 f)
294 # This is like --amend, but with a different message
295 eval "$author_script"
Johannes Schindelinae830ed2007-09-25 16:43:44 +0100296 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
297 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
298 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
Johannes Schindelin91e1ee72007-07-26 07:35:51 +0100299 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100300 ;;
301 t)
302 cp "$MSG" "$GIT_DIR"/MERGE_MSG
303 warn
304 warn "Could not apply $sha1... $rest"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100305 die_with_patch $sha1 ""
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100306 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100307 esac
308 ;;
309 *)
310 warn "Unknown command: $command $sha1 $rest"
311 die_with_patch $sha1 "Please fix this in the file $TODO."
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100312 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100313 esac
314 test -s "$TODO" && return
315
Johannes Schindelin68a163c2007-06-25 18:58:28 +0100316 comment_for_reflog finish &&
317 HEADNAME=$(cat "$DOTEST"/head-name) &&
318 OLDHEAD=$(cat "$DOTEST"/head) &&
319 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100320 if test -d "$REWRITTEN"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100321 then
322 test -f "$DOTEST"/current-commit &&
323 current_commit=$(cat "$DOTEST"/current-commit) &&
324 git rev-parse HEAD > "$REWRITTEN"/$current_commit
325 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
326 else
327 NEWHEAD=$(git rev-parse HEAD)
328 fi &&
Johannes Schindelin73697a02007-09-25 16:43:15 +0100329 case $HEADNAME in
330 refs/*)
331 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
332 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
333 git symbolic-ref HEAD $HEADNAME
334 ;;
335 esac && {
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100336 test ! -f "$DOTEST"/verbose ||
Johannes Schindelinf3d5e462007-10-09 13:59:43 +0100337 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100338 } &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100339 rm -rf "$DOTEST" &&
Johannes Schindelin73697a02007-09-25 16:43:15 +0100340 git gc --auto &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100341 warn "Successfully rebased and updated $HEADNAME."
342
343 exit
344}
345
346do_rest () {
347 while :
348 do
349 do_next
350 done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100351}
352
David Kastrup822f7c72007-09-23 22:42:08 +0200353while test $# != 0
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100354do
355 case "$1" in
356 --continue)
357 comment_for_reflog continue
358
359 test -d "$DOTEST" || die "No interactive rebase running"
360
Johannes Schindelin18640d92007-07-08 03:01:29 +0100361 # commit if necessary
362 git rev-parse --verify HEAD > /dev/null &&
363 git update-index --refresh &&
364 git diff-files --quiet &&
Junio C Hamano38762c42007-11-28 16:15:04 -0800365 ! git diff-index --cached --quiet HEAD -- &&
Johannes Schindelinbe6ff202007-09-25 16:42:36 +0100366 . "$DOTEST"/author-script && {
367 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
368 } &&
Johannes Schindelin18640d92007-07-08 03:01:29 +0100369 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
370 git commit -F "$DOTEST"/message -e
371
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100372 require_clean_work_tree
373 do_rest
374 ;;
375 --abort)
376 comment_for_reflog abort
377
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000378 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100379 test -d "$DOTEST" || die "No interactive rebase running"
380
381 HEADNAME=$(cat "$DOTEST"/head-name)
382 HEAD=$(cat "$DOTEST"/head)
Johannes Schindelin73697a02007-09-25 16:43:15 +0100383 case $HEADNAME in
384 refs/*)
385 git symbolic-ref HEAD $HEADNAME
386 ;;
387 esac &&
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100388 output git reset --hard $HEAD &&
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100389 rm -rf "$DOTEST"
390 exit
391 ;;
392 --skip)
393 comment_for_reflog skip
394
Johannes Schindelinecfe72f2007-11-22 11:18:10 +0000395 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100396 test -d "$DOTEST" || die "No interactive rebase running"
397
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100398 output git reset --hard && do_rest
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100399 ;;
400 -s|--strategy)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100401 case "$#,$1" in
402 *,*=*)
Ralf Wildenhuesb5e960b2007-11-08 22:47:36 +0100403 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100404 1,*)
405 usage ;;
406 *)
407 STRATEGY="-s $2"
408 shift ;;
409 esac
410 ;;
411 --merge)
412 # we use merge anyway
413 ;;
414 -C*)
415 die "Interactive rebase uses merge, so $1 does not make sense"
416 ;;
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100417 -v|--verbose)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100418 VERBOSE=t
419 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100420 -p|--preserve-merges)
421 PRESERVE_MERGES=t
422 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100423 -i|--interactive)
424 # yeah, we know
425 ;;
426 ''|-h)
427 usage
428 ;;
429 *)
430 test -d "$DOTEST" &&
431 die "Interactive rebase already started"
432
433 git var GIT_COMMITTER_IDENT >/dev/null ||
434 die "You need to set your committer info first"
435
436 comment_for_reflog start
437
438 ONTO=
439 case "$1" in
440 --onto)
441 ONTO=$(git rev-parse --verify "$2") ||
442 die "Does not point to a valid commit: $2"
443 shift; shift
444 ;;
445 esac
446
447 require_clean_work_tree
448
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100449 if test ! -z "$2"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100450 then
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100451 output git show-ref --verify --quiet "refs/heads/$2" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100452 die "Invalid branchname: $2"
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100453 output git checkout "$2" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100454 die "Could not checkout $2"
455 fi
456
457 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
458 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
459
Matt Kraai51668102007-09-25 18:30:13 -0700460 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
461
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100462 test -z "$ONTO" && ONTO=$UPSTREAM
463
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100464 : > "$DOTEST"/interactive || die "Could not mark as interactive"
Johannes Schindelin73697a02007-09-25 16:43:15 +0100465 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
466 echo "detached HEAD" > "$DOTEST"/head-name
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100467
468 echo $HEAD > "$DOTEST"/head
469 echo $UPSTREAM > "$DOTEST"/upstream
470 echo $ONTO > "$DOTEST"/onto
Johannes Schindelin8e4a91b2007-07-08 03:02:47 +0100471 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100472 test t = "$VERBOSE" && : > "$DOTEST"/verbose
Johannes Schindelin3b38ec12007-07-24 03:18:28 +0100473 if test t = "$PRESERVE_MERGES"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100474 then
475 # $REWRITTEN contains files for each commit that is
476 # reachable by at least one merge base of $HEAD and
477 # $UPSTREAM. They are not necessarily rewritten, but
478 # their children might be.
479 # This ensures that commits on merged, but otherwise
480 # unrelated side branches are left alone. (Think "X"
481 # in the man page's example.)
482 mkdir "$REWRITTEN" &&
483 for c in $(git merge-base --all $HEAD $UPSTREAM)
484 do
485 echo $ONTO > "$REWRITTEN"/$c ||
486 die "Could not init rewritten commits"
487 done
488 MERGES_OPTION=
489 else
490 MERGES_OPTION=--no-merges
491 fi
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100492
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100493 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
494 SHORTHEAD=$(git rev-parse --short $HEAD)
495 SHORTONTO=$(git rev-parse --short $ONTO)
Johannes Schindelin6047a232007-11-22 12:30:10 +0000496 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
497 --abbrev=7 --reverse --left-right --cherry-pick \
498 $UPSTREAM...$HEAD | \
499 sed -n "s/^>/pick /p" > "$TODO"
500 cat >> "$TODO" << EOF
501
502# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100503#
504# Commands:
505# pick = use commit
506# edit = use commit, but stop for amending
507# squash = use commit, but meld into previous commit
Johannes Schindelin82576dd2007-07-08 21:32:22 +0100508#
509# If you remove a line here THAT COMMIT WILL BE LOST.
Johannes Schindelin6047a232007-11-22 12:30:10 +0000510# However, if you remove everything, the rebase will be aborted.
Johannes Schindelin82576dd2007-07-08 21:32:22 +0100511#
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100512EOF
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100513
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100514 has_action "$TODO" ||
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100515 die_abort "Nothing to do"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100516
517 cp "$TODO" "$TODO".backup
Adam Robenef0c2ab2007-07-19 22:09:35 -0700518 git_editor "$TODO" ||
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100519 die "Could not execute editor"
520
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100521 has_action "$TODO" ||
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100522 die_abort "Nothing to do"
523
Johannes Schindelindfa49f32007-07-23 23:45:49 +0100524 output git checkout $ONTO && do_rest
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100525 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100526 esac
527 shift
528done