git-gui: allow undoing last revert
Accidental clicks on the revert hunk/lines buttons can cause loss of work, and can be frustrating. So, allow undoing the last revert. Right now, a stack or deque are not being used for the sake of simplicity, so only one undo is possible. Any reverts before the previous one are lost. Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
Pratyush Yadav committed
Aug 26, 2019 at 01:43 UTC
a4fa2f0a4c1a1ef7f2987fb9d38342fcaad78a75
2 files changed
+66
-5
git-gui.sh
+17
-1
@@ -1350,6 +1350,8 @@ set is_submodule_diff 0
1350
set is_conflict_diff 0
1351
set selected_commit_type new
1352
set diff_empty_count 0
1353
+set last_revert {}
1354
+set last_revert_enc {}
1355
1356
set nullid "0000000000000000000000000000000000000000"
1357
set nullid2 "0000000000000000000000000000000000000001"
@@ -3601,6 +3603,11 @@ $ctxm add command \
3603
-command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan}
3604
set ui_diff_revertline [$ctxm index last]
3605
lappend diff_actions [list $ctxm entryconf $ui_diff_revertline -state]
3606
+$ctxm add command \
3607
+ -label [mc "Undo Last Revert"] \
3608
+ -command {undo_last_revert; do_rescan}
3609
+set ui_diff_undorevert [$ctxm index last]
3610
+lappend diff_actions [list $ctxm entryconf $ui_diff_undorevert -state]
3611
$ctxm add separator
3612
$ctxm add command \
3613
-label [mc "Show Less Context"] \
@@ -3680,7 +3687,7 @@ proc has_textconv {path} {
3687
}
3688
3689
proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3683
- global current_diff_path file_states
3690
+ global current_diff_path file_states last_revert
3691
set ::cursorX $x
3692
set ::cursorY $y
3693
if {[info exists file_states($current_diff_path)]} {
@@ -3694,6 +3701,7 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3701
tk_popup $ctxmsm $X $Y
3702
} else {
3703
set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}]
3704
+ set u [mc "Undo Last Revert"]
3705
if {$::ui_index eq $::current_diff_side} {
3706
set l [mc "Unstage Hunk From Commit"]
3707
set h [mc "Revert Hunk"]
@@ -3739,12 +3747,20 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3747
}
3748
}
3749
3750
+ if {$last_revert eq {}} {
3751
+ set undo_state disabled
3752
+ } else {
3753
+ set undo_state normal
3754
+ }
3755
+
3756
$ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
3757
$ctxm entryconf $::ui_diff_applyline -state $s -label $t
3758
$ctxm entryconf $::ui_diff_revertline -state $revert_state \
3759
-label $r
3760
$ctxm entryconf $::ui_diff_reverthunk -state $revert_state \
3761
-label $h
3762
+ $ctxm entryconf $::ui_diff_undorevert -state $undo_state \
3763
+ -label $u
3764
3765
tk_popup $ctxm $X $Y
3766
}
lib/diff.tcl
+49
-4
@@ -569,7 +569,7 @@ proc read_diff {fd conflict_size cont_info} {
569
570
proc apply_or_revert_hunk {x y revert} {
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
574
if {$current_diff_path eq {} || $current_diff_header eq {}} return
575
if {![lock_index apply_hunk]} return
@@ -610,18 +610,25 @@ proc apply_or_revert_hunk {x y revert} {
610
set e_lno end
611
}
612
613
+ set wholepatch "$current_diff_header[$ui_diff get $s_lno $e_lno]"
614
+
615
if {[catch {
616
set enc [get_path_encoding $current_diff_path]
617
set p [eval git_write $apply_cmd]
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
620
close $p} err]} {
621
error_popup "$failed_msg\n\n$err"
622
unlock_index
623
return
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
+
632
$ui_diff conf -state normal
633
$ui_diff delete $s_lno $e_lno
634
$ui_diff conf -state disabled
@@ -653,7 +660,7 @@ proc apply_or_revert_hunk {x y revert} {
660
661
proc apply_or_revert_range_or_line {x y revert} {
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
664
665
set selected [$ui_diff tag nextrange sel 0.0]
666
@@ -852,5 +859,43 @@ proc apply_or_revert_range_or_line {x y revert} {
859
return
860
}
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
+
900
unlock_index
901
}