rebase -i -p: handle index and workdir correctly
[git/dscho.git] / git-rebase--interactive.sh
blob58126bd80cd73993c6cedd548e200c0fa461a8af
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
18 Available options are
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 m,merge always used (no-op)
24 i,interactive always used (no-op)
25 Actions:
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
31 . git-sh-setup
32 require_work_tree
34 DOTEST="$GIT_DIR/rebase-merge"
35 TODO="$DOTEST"/git-rebase-todo
36 DONE="$DOTEST"/done
37 MSG="$DOTEST"/message
38 SQUASH_MSG="$DOTEST"/message-squash
39 REWRITTEN="$DOTEST"/rewritten
40 PRESERVE_MERGES=
41 STRATEGY=
42 ONTO=
43 VERBOSE=
45 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
46 mark the corrected paths with 'git add <paths>', and
47 run 'git rebase --continue'"
48 export GIT_CHERRY_PICK_HELP
50 warn () {
51 echo "$*" >&2
54 output () {
55 case "$VERBOSE" in
56 '')
57 output=$("$@" 2>&1 )
58 status=$?
59 test $status != 0 && printf "%s\n" "$output"
60 return $status
63 "$@"
65 esac
68 require_clean_work_tree () {
69 # test if working tree is dirty
70 git rev-parse --verify HEAD > /dev/null &&
71 git update-index --ignore-submodules --refresh &&
72 git diff-files --quiet --ignore-submodules &&
73 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
74 die "Working tree is dirty"
77 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
79 comment_for_reflog () {
80 case "$ORIG_REFLOG_ACTION" in
81 ''|rebase*)
82 GIT_REFLOG_ACTION="rebase -i ($1)"
83 export GIT_REFLOG_ACTION
85 esac
88 last_count=
89 mark_action_done () {
90 sed -e 1q < "$TODO" >> "$DONE"
91 sed -e 1d < "$TODO" >> "$TODO".new
92 mv -f "$TODO".new "$TODO"
93 count=$(grep -c '^[^#]' < "$DONE")
94 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
95 if test "$last_count" != "$count"
96 then
97 last_count=$count
98 printf "Rebasing (%d/%d)\r" $count $total
99 test -z "$VERBOSE" || echo
103 make_patch () {
104 parent_sha1=$(git rev-parse --verify "$1"^) ||
105 die "Cannot get patch for $1^"
106 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
107 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
113 die_with_patch () {
114 make_patch "$1"
115 git rerere
116 die "$2"
119 die_abort () {
120 rm -rf "$DOTEST"
121 die "$1"
124 has_action () {
125 grep '^[^#]' "$1" >/dev/null
128 pick_one () {
129 no_ff=
130 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
131 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
132 test -d "$REWRITTEN" &&
133 pick_one_preserving_merges "$@" && return
134 parent_sha1=$(git rev-parse --verify $sha1^) ||
135 die "Could not get the parent of $sha1"
136 current_sha1=$(git rev-parse --verify HEAD)
137 if test "$no_ff$current_sha1" = "$parent_sha1"; then
138 output git reset --hard $sha1
139 test "a$1" = a-n && output git reset --soft $current_sha1
140 sha1=$(git rev-parse --short $sha1)
141 output warn Fast forward to $sha1
142 else
143 output git cherry-pick "$@"
147 pick_one_preserving_merges () {
148 fast_forward=t
149 case "$1" in
151 fast_forward=f
152 sha1=$2
155 sha1=$1
157 esac
158 sha1=$(git rev-parse $sha1)
160 if test -f "$DOTEST"/current-commit
161 then
162 current_commit=$(cat "$DOTEST"/current-commit) &&
163 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
164 rm "$DOTEST"/current-commit ||
165 die "Cannot write current commit's replacement sha1"
168 # rewrite parents; if none were rewritten, we can fast-forward.
169 preserve=t
170 new_parents=
171 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
173 if test -f "$REWRITTEN"/$p
174 then
175 preserve=f
176 new_p=$(cat "$REWRITTEN"/$p)
177 test $p != $new_p && fast_forward=f
178 case "$new_parents" in
179 *$new_p*)
180 ;; # do nothing; that parent is already there
182 new_parents="$new_parents $new_p"
184 esac
185 else
186 new_parents="$new_parents $p"
188 done
189 case $fast_forward in
191 output warn "Fast forward to $sha1"
192 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
193 output git reset --hard $sha1 ||
194 die "Cannot fast forward to $sha1"
197 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
199 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
200 # detach HEAD to current parent
201 output git checkout $first_parent 2> /dev/null ||
202 die "Cannot move HEAD to $first_parent"
204 echo $sha1 > "$DOTEST"/current-commit
205 case "$new_parents" in
206 ' '*' '*)
207 # redo merge
208 author_script=$(get_author_ident_from_commit $sha1)
209 eval "$author_script"
210 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
211 # No point in merging the first parent, that's HEAD
212 new_parents=${new_parents# $first_parent}
213 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
214 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
215 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
216 output git merge $STRATEGY -m "$msg" \
217 $new_parents
218 then
219 git rerere
220 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
221 die Error redoing merge $sha1
225 output git cherry-pick "$@" ||
226 die_with_patch $sha1 "Could not pick $sha1"
228 esac
230 esac
233 nth_string () {
234 case "$1" in
235 *1[0-9]|*[04-9]) echo "$1"th;;
236 *1) echo "$1"st;;
237 *2) echo "$1"nd;;
238 *3) echo "$1"rd;;
239 esac
242 make_squash_message () {
243 if test -f "$SQUASH_MSG"; then
244 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
245 < "$SQUASH_MSG" | sed -ne '$p')+1))
246 echo "# This is a combination of $COUNT commits."
247 sed -e 1d -e '2,/^./{
248 /^$/d
249 }' <"$SQUASH_MSG"
250 else
251 COUNT=2
252 echo "# This is a combination of two commits."
253 echo "# The first commit's message is:"
254 echo
255 git cat-file commit HEAD | sed -e '1,/^$/d'
257 echo
258 echo "# This is the $(nth_string $COUNT) commit message:"
259 echo
260 git cat-file commit $1 | sed -e '1,/^$/d'
263 peek_next_command () {
264 sed -n "1s/ .*$//p" < "$TODO"
267 do_next () {
268 rm -f "$DOTEST"/message "$DOTEST"/author-script \
269 "$DOTEST"/amend || exit
270 read command sha1 rest < "$TODO"
271 case "$command" in
272 '#'*|'')
273 mark_action_done
275 pick|p)
276 comment_for_reflog pick
278 mark_action_done
279 pick_one $sha1 ||
280 die_with_patch $sha1 "Could not apply $sha1... $rest"
282 edit|e)
283 comment_for_reflog edit
285 mark_action_done
286 pick_one $sha1 ||
287 die_with_patch $sha1 "Could not apply $sha1... $rest"
288 make_patch $sha1
289 : > "$DOTEST"/amend
290 warn "Stopped at $sha1... $rest"
291 warn "You can amend the commit now, with"
292 warn
293 warn " git commit --amend"
294 warn
295 warn "Once you are satisfied with your changes, run"
296 warn
297 warn " git rebase --continue"
298 warn
299 exit 0
301 squash|s)
302 comment_for_reflog squash
304 has_action "$DONE" ||
305 die "Cannot 'squash' without a previous commit"
307 mark_action_done
308 make_squash_message $sha1 > "$MSG"
309 case "$(peek_next_command)" in
310 squash|s)
311 EDIT_COMMIT=
312 USE_OUTPUT=output
313 cp "$MSG" "$SQUASH_MSG"
316 EDIT_COMMIT=-e
317 USE_OUTPUT=
318 rm -f "$SQUASH_MSG" || exit
320 esac
322 failed=f
323 author_script=$(get_author_ident_from_commit HEAD)
324 output git reset --soft HEAD^
325 pick_one -n $sha1 || failed=t
326 echo "$author_script" > "$DOTEST"/author-script
327 if test $failed = f
328 then
329 # This is like --amend, but with a different message
330 eval "$author_script"
331 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
332 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
333 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
334 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
336 if test $failed = t
337 then
338 cp "$MSG" "$GIT_DIR"/MERGE_MSG
339 warn
340 warn "Could not apply $sha1... $rest"
341 die_with_patch $sha1 ""
345 warn "Unknown command: $command $sha1 $rest"
346 die_with_patch $sha1 "Please fix this in the file $TODO."
348 esac
349 test -s "$TODO" && return
351 comment_for_reflog finish &&
352 HEADNAME=$(cat "$DOTEST"/head-name) &&
353 OLDHEAD=$(cat "$DOTEST"/head) &&
354 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
355 if test -d "$REWRITTEN"
356 then
357 test -f "$DOTEST"/current-commit &&
358 current_commit=$(cat "$DOTEST"/current-commit) &&
359 git rev-parse HEAD > "$REWRITTEN"/$current_commit
360 if test -f "$REWRITTEN"/$OLDHEAD
361 then
362 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
363 else
364 NEWHEAD=$OLDHEAD
366 else
367 NEWHEAD=$(git rev-parse HEAD)
368 fi &&
369 case $HEADNAME in
370 refs/*)
371 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
372 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
373 git symbolic-ref HEAD $HEADNAME
375 esac && {
376 test ! -f "$DOTEST"/verbose ||
377 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
378 } &&
379 rm -rf "$DOTEST" &&
380 git gc --auto &&
381 warn "Successfully rebased and updated $HEADNAME."
383 exit
386 do_rest () {
387 while :
389 do_next
390 done
393 # check if no other options are set
394 is_standalone () {
395 test $# -eq 2 -a "$2" = '--' &&
396 test -z "$ONTO" &&
397 test -z "$PRESERVE_MERGES" &&
398 test -z "$STRATEGY" &&
399 test -z "$VERBOSE"
402 get_saved_options () {
403 test -d "$REWRITTEN" && PRESERVE_MERGES=t
404 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
405 test -f "$DOTEST"/verbose && VERBOSE=t
408 while test $# != 0
410 case "$1" in
411 --continue)
412 is_standalone "$@" || usage
413 get_saved_options
414 comment_for_reflog continue
416 test -d "$DOTEST" || die "No interactive rebase running"
418 # Sanity check
419 git rev-parse --verify HEAD >/dev/null ||
420 die "Cannot read HEAD"
421 git update-index --ignore-submodules --refresh &&
422 git diff-files --quiet --ignore-submodules ||
423 die "Working tree is dirty"
425 # do we have anything to commit?
426 if git diff-index --cached --quiet --ignore-submodules HEAD --
427 then
428 : Nothing to commit -- skip this
429 else
430 . "$DOTEST"/author-script ||
431 die "Cannot find the author identity"
432 if test -f "$DOTEST"/amend
433 then
434 git reset --soft HEAD^ ||
435 die "Cannot rewind the HEAD"
437 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
438 git commit --no-verify -F "$DOTEST"/message -e ||
439 die "Could not commit staged changes."
442 require_clean_work_tree
443 do_rest
445 --abort)
446 is_standalone "$@" || usage
447 get_saved_options
448 comment_for_reflog abort
450 git rerere clear
451 test -d "$DOTEST" || die "No interactive rebase running"
453 HEADNAME=$(cat "$DOTEST"/head-name)
454 HEAD=$(cat "$DOTEST"/head)
455 case $HEADNAME in
456 refs/*)
457 git symbolic-ref HEAD $HEADNAME
459 esac &&
460 output git reset --hard $HEAD &&
461 rm -rf "$DOTEST"
462 exit
464 --skip)
465 is_standalone "$@" || usage
466 get_saved_options
467 comment_for_reflog skip
469 git rerere clear
470 test -d "$DOTEST" || die "No interactive rebase running"
472 output git reset --hard && do_rest
475 case "$#,$1" in
476 *,*=*)
477 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
478 1,*)
479 usage ;;
481 STRATEGY="-s $2"
482 shift ;;
483 esac
486 # we use merge anyway
489 VERBOSE=t
492 PRESERVE_MERGES=t
495 # yeah, we know
497 --onto)
498 shift
499 ONTO=$(git rev-parse --verify "$1") ||
500 die "Does not point to a valid commit: $1"
503 shift
504 test $# -eq 1 -o $# -eq 2 || usage
505 test -d "$DOTEST" &&
506 die "Interactive rebase already started"
508 git var GIT_COMMITTER_IDENT >/dev/null ||
509 die "You need to set your committer info first"
511 comment_for_reflog start
513 require_clean_work_tree
515 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
516 test -z "$ONTO" && ONTO=$UPSTREAM
518 if test ! -z "$2"
519 then
520 output git show-ref --verify --quiet "refs/heads/$2" ||
521 die "Invalid branchname: $2"
522 output git checkout "$2" ||
523 die "Could not checkout $2"
526 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
527 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
529 : > "$DOTEST"/interactive || die "Could not mark as interactive"
530 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
531 echo "detached HEAD" > "$DOTEST"/head-name
533 echo $HEAD > "$DOTEST"/head
534 echo $UPSTREAM > "$DOTEST"/upstream
535 echo $ONTO > "$DOTEST"/onto
536 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
537 test t = "$VERBOSE" && : > "$DOTEST"/verbose
538 if test t = "$PRESERVE_MERGES"
539 then
540 # $REWRITTEN contains files for each commit that is
541 # reachable by at least one merge base of $HEAD and
542 # $UPSTREAM. They are not necessarily rewritten, but
543 # their children might be.
544 # This ensures that commits on merged, but otherwise
545 # unrelated side branches are left alone. (Think "X"
546 # in the man page's example.)
547 mkdir "$REWRITTEN" &&
548 for c in $(git merge-base --all $HEAD $UPSTREAM)
550 echo $ONTO > "$REWRITTEN"/$c ||
551 die "Could not init rewritten commits"
552 done
553 MERGES_OPTION=
554 else
555 MERGES_OPTION=--no-merges
558 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
559 SHORTHEAD=$(git rev-parse --short $HEAD)
560 SHORTONTO=$(git rev-parse --short $ONTO)
561 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
562 --abbrev=7 --reverse --left-right --cherry-pick \
563 $UPSTREAM...$HEAD | \
564 sed -n "s/^>/pick /p" > "$TODO"
565 cat >> "$TODO" << EOF
567 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
569 # Commands:
570 # p, pick = use commit
571 # e, edit = use commit, but stop for amending
572 # s, squash = use commit, but meld into previous commit
574 # If you remove a line here THAT COMMIT WILL BE LOST.
575 # However, if you remove everything, the rebase will be aborted.
579 has_action "$TODO" ||
580 die_abort "Nothing to do"
582 cp "$TODO" "$TODO".backup
583 git_editor "$TODO" ||
584 die "Could not execute editor"
586 has_action "$TODO" ||
587 die_abort "Nothing to do"
589 git update-ref ORIG_HEAD $HEAD
590 output git checkout $ONTO && do_rest
592 esac
593 shift
594 done