Skip to content

Commit c77abf0

Browse files
committed
Merge branch 'py/revert-hunks-lines'
git-gui learned to revert selected lines and hunks, just like it can stage selected lines and hunks. To provide a safety net for accidental revert, the most recent revert can be undone. * py/revert-hunks-lines: git-gui: allow undoing last revert git-gui: return early when patch fails to apply git-gui: allow reverting selected hunk git-gui: allow reverting selected lines
2 parents 5a2bb62 + a4fa2f0 commit c77abf0

File tree

2 files changed

+135
-18
lines changed

2 files changed

+135
-18
lines changed

git-gui.sh

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,8 @@ set is_submodule_diff 0
13501350
set is_conflict_diff 0
13511351
set selected_commit_type new
13521352
set diff_empty_count 0
1353+
set last_revert {}
1354+
set last_revert_enc {}
13531355
13541356
set nullid "0000000000000000000000000000000000000000"
13551357
set nullid2 "0000000000000000000000000000000000000001"
@@ -3604,15 +3606,31 @@ set ctxm .vpane.lower.diff.body.ctxm
36043606
menu $ctxm -tearoff 0
36053607
$ctxm add command \
36063608
-label [mc "Apply/Reverse Hunk"] \
3607-
-command {apply_hunk $cursorX $cursorY}
3609+
-command {apply_or_revert_hunk $cursorX $cursorY 0}
36083610
set ui_diff_applyhunk [$ctxm index last]
36093611
lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
36103612
$ctxm add command \
36113613
-label [mc "Apply/Reverse Line"] \
3612-
-command {apply_range_or_line $cursorX $cursorY; do_rescan}
3614+
-command {apply_or_revert_range_or_line $cursorX $cursorY 0; do_rescan}
36133615
set ui_diff_applyline [$ctxm index last]
36143616
lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state]
36153617
$ctxm add separator
3618+
$ctxm add command \
3619+
-label [mc "Revert Hunk"] \
3620+
-command {apply_or_revert_hunk $cursorX $cursorY 1}
3621+
set ui_diff_reverthunk [$ctxm index last]
3622+
lappend diff_actions [list $ctxm entryconf $ui_diff_reverthunk -state]
3623+
$ctxm add command \
3624+
-label [mc "Revert Line"] \
3625+
-command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan}
3626+
set ui_diff_revertline [$ctxm index last]
3627+
lappend diff_actions [list $ctxm entryconf $ui_diff_revertline -state]
3628+
$ctxm add command \
3629+
-label [mc "Undo Last Revert"] \
3630+
-command {undo_last_revert; do_rescan}
3631+
set ui_diff_undorevert [$ctxm index last]
3632+
lappend diff_actions [list $ctxm entryconf $ui_diff_undorevert -state]
3633+
$ctxm add separator
36163634
$ctxm add command \
36173635
-label [mc "Show Less Context"] \
36183636
-command show_less_context
@@ -3691,7 +3709,7 @@ proc has_textconv {path} {
36913709
}
36923710
36933711
proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3694-
global current_diff_path file_states
3712+
global current_diff_path file_states last_revert
36953713
set ::cursorX $x
36963714
set ::cursorY $y
36973715
if {[info exists file_states($current_diff_path)]} {
@@ -3705,19 +3723,28 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
37053723
tk_popup $ctxmsm $X $Y
37063724
} else {
37073725
set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}]
3726+
set u [mc "Undo Last Revert"]
37083727
if {$::ui_index eq $::current_diff_side} {
37093728
set l [mc "Unstage Hunk From Commit"]
3729+
set h [mc "Revert Hunk"]
3730+
37103731
if {$has_range} {
37113732
set t [mc "Unstage Lines From Commit"]
3733+
set r [mc "Revert Lines"]
37123734
} else {
37133735
set t [mc "Unstage Line From Commit"]
3736+
set r [mc "Revert Line"]
37143737
}
37153738
} else {
37163739
set l [mc "Stage Hunk For Commit"]
3740+
set h [mc "Revert Hunk"]
3741+
37173742
if {$has_range} {
37183743
set t [mc "Stage Lines For Commit"]
3744+
set r [mc "Revert Lines"]
37193745
} else {
37203746
set t [mc "Stage Line For Commit"]
3747+
set r [mc "Revert Line"]
37213748
}
37223749
}
37233750
if {$::is_3way_diff
@@ -3728,11 +3755,35 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
37283755
|| [string match {T?} $state]
37293756
|| [has_textconv $current_diff_path]} {
37303757
set s disabled
3758+
set revert_state disabled
37313759
} else {
37323760
set s normal
3761+
3762+
# Only allow reverting changes in the working tree. If
3763+
# the user wants to revert changes in the index, they
3764+
# need to unstage those first.
3765+
if {$::ui_workdir eq $::current_diff_side} {
3766+
set revert_state normal
3767+
} else {
3768+
set revert_state disabled
3769+
}
37333770
}
3771+
3772+
if {$last_revert eq {}} {
3773+
set undo_state disabled
3774+
} else {
3775+
set undo_state normal
3776+
}
3777+
37343778
$ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
37353779
$ctxm entryconf $::ui_diff_applyline -state $s -label $t
3780+
$ctxm entryconf $::ui_diff_revertline -state $revert_state \
3781+
-label $r
3782+
$ctxm entryconf $::ui_diff_reverthunk -state $revert_state \
3783+
-label $h
3784+
$ctxm entryconf $::ui_diff_undorevert -state $undo_state \
3785+
-label $u
3786+
37363787
tk_popup $ctxm $X $Y
37373788
}
37383789
}

lib/diff.tcl

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ proc reshow_diff {{after {}}} {
5555

5656
proc force_diff_encoding {enc} {
5757
global current_diff_path
58-
58+
5959
if {$current_diff_path ne {}} {
6060
force_path_encoding $current_diff_path $enc
6161
reshow_diff
@@ -567,24 +567,31 @@ proc read_diff {fd conflict_size cont_info} {
567567
}
568568
}
569569

570-
proc apply_hunk {x y} {
570+
proc apply_or_revert_hunk {x y revert} {
571571
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
573573

574574
if {$current_diff_path eq {} || $current_diff_header eq {}} return
575575
if {![lock_index apply_hunk]} return
576576

577-
set apply_cmd {apply --cached --whitespace=nowarn}
577+
set apply_cmd {apply --whitespace=nowarn}
578578
set mi [lindex $file_states($current_diff_path) 0]
579579
if {$current_diff_side eq $ui_index} {
580580
set failed_msg [mc "Failed to unstage selected hunk."]
581-
lappend apply_cmd --reverse
581+
lappend apply_cmd --reverse --cached
582582
if {[string index $mi 0] ne {M}} {
583583
unlock_index
584584
return
585585
}
586586
} else {
587-
set failed_msg [mc "Failed to stage selected hunk."]
587+
if {$revert} {
588+
set failed_msg [mc "Failed to revert selected hunk."]
589+
lappend apply_cmd --reverse
590+
} else {
591+
set failed_msg [mc "Failed to stage selected hunk."]
592+
lappend apply_cmd --cached
593+
}
594+
588595
if {[string index $mi 1] ne {M}} {
589596
unlock_index
590597
return
@@ -603,29 +610,40 @@ proc apply_hunk {x y} {
603610
set e_lno end
604611
}
605612

613+
set wholepatch "$current_diff_header[$ui_diff get $s_lno $e_lno]"
614+
606615
if {[catch {
607616
set enc [get_path_encoding $current_diff_path]
608617
set p [eval git_write $apply_cmd]
609618
fconfigure $p -translation binary -encoding $enc
610-
puts -nonewline $p $current_diff_header
611-
puts -nonewline $p [$ui_diff get $s_lno $e_lno]
619+
puts -nonewline $p $wholepatch
612620
close $p} err]} {
613621
error_popup "$failed_msg\n\n$err"
614622
unlock_index
615623
return
616624
}
617625

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+
618632
$ui_diff conf -state normal
619633
$ui_diff delete $s_lno $e_lno
620634
$ui_diff conf -state disabled
621635

636+
# Check if the hunk was the last one in the file.
622637
if {[$ui_diff get 1.0 end] eq "\n"} {
623638
set o _
624639
} else {
625640
set o ?
626641
}
627642

628-
if {$current_diff_side eq $ui_index} {
643+
# Update the status flags.
644+
if {$revert} {
645+
set mi [string index $mi 0]$o
646+
} elseif {$current_diff_side eq $ui_index} {
629647
set mi ${o}M
630648
} elseif {[string index $mi 0] eq {_}} {
631649
set mi M$o
@@ -640,9 +658,9 @@ proc apply_hunk {x y} {
640658
}
641659
}
642660

643-
proc apply_range_or_line {x y} {
661+
proc apply_or_revert_range_or_line {x y revert} {
644662
global current_diff_path current_diff_header current_diff_side
645-
global ui_diff ui_index file_states
663+
global ui_diff ui_index file_states last_revert
646664

647665
set selected [$ui_diff tag nextrange sel 0.0]
648666

@@ -660,19 +678,27 @@ proc apply_range_or_line {x y} {
660678
if {$current_diff_path eq {} || $current_diff_header eq {}} return
661679
if {![lock_index apply_hunk]} return
662680

663-
set apply_cmd {apply --cached --whitespace=nowarn}
681+
set apply_cmd {apply --whitespace=nowarn}
664682
set mi [lindex $file_states($current_diff_path) 0]
665683
if {$current_diff_side eq $ui_index} {
666684
set failed_msg [mc "Failed to unstage selected line."]
667685
set to_context {+}
668-
lappend apply_cmd --reverse
686+
lappend apply_cmd --reverse --cached
669687
if {[string index $mi 0] ne {M}} {
670688
unlock_index
671689
return
672690
}
673691
} else {
674-
set failed_msg [mc "Failed to stage selected line."]
675-
set to_context {-}
692+
if {$revert} {
693+
set failed_msg [mc "Failed to revert selected line."]
694+
set to_context {+}
695+
lappend apply_cmd --reverse
696+
} else {
697+
set failed_msg [mc "Failed to stage selected line."]
698+
set to_context {-}
699+
lappend apply_cmd --cached
700+
}
701+
676702
if {[string index $mi 1] ne {M}} {
677703
unlock_index
678704
return
@@ -829,7 +855,47 @@ proc apply_range_or_line {x y} {
829855
puts -nonewline $p $wholepatch
830856
close $p} err]} {
831857
error_popup "$failed_msg\n\n$err"
858+
unlock_index
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
832866
}
833867

834868
unlock_index
835869
}
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+
}

0 commit comments

Comments
 (0)