@@ -96,6 +96,13 @@ AUTHOR_SCRIPT="$DOTEST"/author-script
96
96
# command is processed, this file is deleted.
97
97
AMEND=" $DOTEST " /amend
98
98
99
+ # For the post-rewrite hook, we make a list of rewritten commits and
100
+ # their new sha1s. The rewritten-pending list keeps the sha1s of
101
+ # commits that have been processed, but not committed yet,
102
+ # e.g. because they are waiting for a 'squash' command.
103
+ REWRITTEN_LIST=" $DOTEST " /rewritten-list
104
+ REWRITTEN_PENDING=" $DOTEST " /rewritten-pending
105
+
99
106
PRESERVE_MERGES=
100
107
STRATEGY=
101
108
ONTO=
@@ -198,6 +205,7 @@ make_patch () {
198
205
}
199
206
200
207
die_with_patch () {
208
+ echo " $1 " > " $DOTEST " /stopped-sha
201
209
make_patch " $1 "
202
210
git rerere
203
211
die " $2 "
@@ -348,6 +356,7 @@ pick_one_preserving_merges () {
348
356
printf " %s\n" " $msg " > " $GIT_DIR " /MERGE_MSG
349
357
die_with_patch $sha1 " Error redoing merge $sha1 "
350
358
fi
359
+ echo " $sha1 $( git rev-parse HEAD^0) " >> " $REWRITTEN_LIST "
351
360
;;
352
361
* )
353
362
output git cherry-pick " $@ " ||
@@ -425,6 +434,26 @@ die_failed_squash() {
425
434
die_with_patch $1 " "
426
435
}
427
436
437
+ flush_rewritten_pending () {
438
+ test -s " $REWRITTEN_PENDING " || return
439
+ newsha1=" $( git rev-parse HEAD^0) "
440
+ sed " s/$/ $newsha1 /" < " $REWRITTEN_PENDING " >> " $REWRITTEN_LIST "
441
+ rm -f " $REWRITTEN_PENDING "
442
+ }
443
+
444
+ record_in_rewritten () {
445
+ oldsha1=" $( git rev-parse $1 ) "
446
+ echo " $oldsha1 " >> " $REWRITTEN_PENDING "
447
+
448
+ case " $( peek_next_command) " in
449
+ squash|s|fixup|f)
450
+ ;;
451
+ * )
452
+ flush_rewritten_pending
453
+ ;;
454
+ esac
455
+ }
456
+
428
457
do_next () {
429
458
rm -f " $MSG " " $AUTHOR_SCRIPT " " $AMEND " || exit
430
459
read command sha1 rest < " $TODO "
@@ -438,6 +467,7 @@ do_next () {
438
467
mark_action_done
439
468
pick_one $sha1 ||
440
469
die_with_patch $sha1 " Could not apply $sha1 ... $rest "
470
+ record_in_rewritten $sha1
441
471
;;
442
472
reword|r)
443
473
comment_for_reflog reword
@@ -446,13 +476,15 @@ do_next () {
446
476
pick_one $sha1 ||
447
477
die_with_patch $sha1 " Could not apply $sha1 ... $rest "
448
478
git commit --amend --no-post-rewrite
479
+ record_in_rewritten $sha1
449
480
;;
450
481
edit|e)
451
482
comment_for_reflog edit
452
483
453
484
mark_action_done
454
485
pick_one $sha1 ||
455
486
die_with_patch $sha1 " Could not apply $sha1 ... $rest "
487
+ echo " $1 " > " $DOTEST " /stopped-sha
456
488
make_patch $sha1
457
489
git rev-parse --verify HEAD > " $AMEND "
458
490
warn " Stopped at $sha1 ... $rest "
@@ -509,6 +541,7 @@ do_next () {
509
541
rm -f " $SQUASH_MSG " " $FIXUP_MSG "
510
542
;;
511
543
esac
544
+ record_in_rewritten $sha1
512
545
;;
513
546
* )
514
547
warn " Unknown command: $command $sha1 $rest "
@@ -537,6 +570,11 @@ do_next () {
537
570
test ! -f " $DOTEST " /verbose ||
538
571
git diff-tree --stat $( cat " $DOTEST " /head) ..HEAD
539
572
} &&
573
+ if test -x " $GIT_DIR " /hooks/post-rewrite &&
574
+ test -s " $REWRITTEN_LIST " ; then
575
+ " $GIT_DIR " /hooks/post-rewrite rebase < " $REWRITTEN_LIST "
576
+ true # we don't care if this hook failed
577
+ fi &&
540
578
rm -rf " $DOTEST " &&
541
579
git gc --auto &&
542
580
warn " Successfully rebased and updated $HEADNAME ."
@@ -571,7 +609,12 @@ skip_unnecessary_picks () {
571
609
esac
572
610
echo " $command ${sha1: + } $sha1 ${rest: + } $rest " >& $fd
573
611
done < " $TODO " > " $TODO .new" 3>> " $DONE " &&
574
- mv -f " $TODO " .new " $TODO " ||
612
+ mv -f " $TODO " .new " $TODO " &&
613
+ case " $( peek_next_command) " in
614
+ squash|s|fixup|f)
615
+ record_in_rewritten " $ONTO "
616
+ ;;
617
+ esac ||
575
618
die " Could not skip unnecessary pick commands"
576
619
}
577
620
@@ -685,6 +728,7 @@ first and then run 'git rebase --continue' again."
685
728
test -n " $amend " && git reset --soft $amend
686
729
die " Could not commit staged changes."
687
730
}
731
+ record_in_rewritten " $( cat " $DOTEST " /stopped-sha) "
688
732
fi
689
733
690
734
require_clean_work_tree
0 commit comments