@@ -569,7 +569,7 @@ proc read_diff {fd conflict_size cont_info} {
569
569
570
570
proc apply_or_revert_hunk {x y revert} {
571
571
global current_diff_path current_diff_header current_diff_side
572
- global ui_diff ui_index file_states
572
+ global ui_diff ui_index file_states last_revert last_revert_enc
573
573
574
574
if {$current_diff_path eq {} || $current_diff_header eq {}} return
575
575
if {![lock_index apply_hunk]} return
@@ -610,18 +610,25 @@ proc apply_or_revert_hunk {x y revert} {
610
610
set e_lno end
611
611
}
612
612
613
+ set wholepatch " $current_diff_header [ $ui_diff get $s_lno $e_lno ] "
614
+
613
615
if {[catch {
614
616
set enc [get_path_encoding $current_diff_path ]
615
617
set p [eval git_write $apply_cmd ]
616
618
fconfigure $p -translation binary -encoding $enc
617
- puts -nonewline $p $current_diff_header
618
- puts -nonewline $p [$ui_diff get $s_lno $e_lno ]
619
+ puts -nonewline $p $wholepatch
619
620
close $p } err]} {
620
621
error_popup " $failed_msg \n\n $err "
621
622
unlock_index
622
623
return
623
624
}
624
625
626
+ if {$revert } {
627
+ # Save a copy of this patch for undoing reverts.
628
+ set last_revert $wholepatch
629
+ set last_revert_enc $enc
630
+ }
631
+
625
632
$ui_diff conf -state normal
626
633
$ui_diff delete $s_lno $e_lno
627
634
$ui_diff conf -state disabled
@@ -653,7 +660,7 @@ proc apply_or_revert_hunk {x y revert} {
653
660
654
661
proc apply_or_revert_range_or_line {x y revert} {
655
662
global current_diff_path current_diff_header current_diff_side
656
- global ui_diff ui_index file_states
663
+ global ui_diff ui_index file_states last_revert
657
664
658
665
set selected [$ui_diff tag nextrange sel 0.0]
659
666
@@ -852,5 +859,43 @@ proc apply_or_revert_range_or_line {x y revert} {
852
859
return
853
860
}
854
861
862
+ if {$revert } {
863
+ # Save a copy of this patch for undoing reverts.
864
+ set last_revert $current_diff_header$wholepatch
865
+ set last_revert_enc $enc
866
+ }
867
+
868
+ unlock_index
869
+ }
870
+
871
+ # Undo the last line/hunk reverted. When hunks and lines are reverted, a copy
872
+ # of the diff applied is saved. Re-apply that diff to undo the revert.
873
+ #
874
+ # Right now, we only use a single variable to hold the copy, and not a
875
+ # stack/deque for simplicity, so multiple undos are not possible. Maybe this
876
+ # can be added if the need for something like this is felt in the future.
877
+ proc undo_last_revert {} {
878
+ global last_revert current_diff_path current_diff_header
879
+ global last_revert_enc
880
+
881
+ if {$last_revert eq {}} return
882
+ if {![lock_index apply_hunk]} return
883
+
884
+ set apply_cmd {apply --whitespace=nowarn}
885
+ set failed_msg [mc " Failed to undo last revert." ]
886
+
887
+ if {[catch {
888
+ set enc $last_revert_enc
889
+ set p [eval git_write $apply_cmd ]
890
+ fconfigure $p -translation binary -encoding $enc
891
+ puts -nonewline $p $last_revert
892
+ close $p } err]} {
893
+ error_popup " $failed_msg \n\n $err "
894
+ unlock_index
895
+ return
896
+ }
897
+
898
+ set last_revert {}
899
+
855
900
unlock_index
856
901
}
0 commit comments