git-gui: allow reverting selected lines

Just like the user can select lines to stage or unstage, add the ability to revert selected lines. Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>

Pratyush Yadav committed Aug 26, 2019 at 01:35 UTC 5f0a516de9e8e6abdd37a79546f904a0ddbce58d
2 files changed +38 -7
git-gui.sh
+24 -1
@@ -3587,10 +3587,16 @@ set ui_diff_applyhunk [$ctxm index last]
3587 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
3588 $ctxm add command \
3589 -label [mc "Apply/Reverse Line"] \
3590 - -command {apply_range_or_line $cursorX $cursorY; do_rescan}
3590 + -command {apply_or_revert_range_or_line $cursorX $cursorY 0; do_rescan}
3591 set ui_diff_applyline [$ctxm index last]
3592 lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state]
3593 $ctxm add separator
3594 +$ctxm add command \
3595 + -label [mc "Revert Line"] \
3596 + -command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan}
3597 +set ui_diff_revertline [$ctxm index last]
3598 +lappend diff_actions [list $ctxm entryconf $ui_diff_revertline -state]
3599 +$ctxm add separator
3600 $ctxm add command \
3601 -label [mc "Show Less Context"] \
3602 -command show_less_context
@@ -3687,15 +3693,19 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3693 set l [mc "Unstage Hunk From Commit"]
3694 if {$has_range} {
3695 set t [mc "Unstage Lines From Commit"]
3696 + set r [mc "Revert Lines"]
3697 } else {
3698 set t [mc "Unstage Line From Commit"]
3699 + set r [mc "Revert Line"]
3700 }
3701 } else {
3702 set l [mc "Stage Hunk For Commit"]
3703 if {$has_range} {
3704 set t [mc "Stage Lines For Commit"]
3705 + set r [mc "Revert Lines"]
3706 } else {
3707 set t [mc "Stage Line For Commit"]
3708 + set r [mc "Revert Line"]
3709 }
3710 }
3711 if {$::is_3way_diff
@@ -3706,11 +3716,24 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3716 || [string match {T?} $state]
3717 || [has_textconv $current_diff_path]} {
3718 set s disabled
3719 + set revert_state disabled
3720 } else {
3721 set s normal
3722 +
3723 + # Only allow reverting changes in the working tree. If
3724 + # the user wants to revert changes in the index, they
3725 + # need to unstage those first.
3726 + if {$::ui_workdir eq $::current_diff_side} {
3727 + set revert_state normal
3728 + } else {
3729 + set revert_state disabled
3730 + }
3731 }
3732 +
3733 $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
3734 $ctxm entryconf $::ui_diff_applyline -state $s -label $t
3735 + $ctxm entryconf $::ui_diff_revertline -state $revert_state \
3736 + -label $r
3737 tk_popup $ctxm $X $Y
3738 }
3739 }
lib/diff.tcl
+14 -6
@@ -55,7 +55,7 @@ proc reshow_diff {{after {}}} {
55
56 proc force_diff_encoding {enc} {
57 global current_diff_path
58 -
58 +
59 if {$current_diff_path ne {}} {
60 force_path_encoding $current_diff_path $enc
61 reshow_diff
@@ -640,7 +640,7 @@ proc apply_hunk {x y} {
640 }
641 }
642
643 -proc apply_range_or_line {x y} {
643 +proc apply_or_revert_range_or_line {x y revert} {
644 global current_diff_path current_diff_header current_diff_side
645 global ui_diff ui_index file_states
646
@@ -660,19 +660,27 @@ proc apply_range_or_line {x y} {
660 if {$current_diff_path eq {} || $current_diff_header eq {}} return
661 if {![lock_index apply_hunk]} return
662
663 - set apply_cmd {apply --cached --whitespace=nowarn}
663 + set apply_cmd {apply --whitespace=nowarn}
664 set mi [lindex $file_states($current_diff_path) 0]
665 if {$current_diff_side eq $ui_index} {
666 set failed_msg [mc "Failed to unstage selected line."]
667 set to_context {+}
668 - lappend apply_cmd --reverse
668 + lappend apply_cmd --reverse --cached
669 if {[string index $mi 0] ne {M}} {
670 unlock_index
671 return
672 }
673 } else {
674 - set failed_msg [mc "Failed to stage selected line."]
675 - set to_context {-}
674 + if {$revert} {
675 + set failed_msg [mc "Failed to revert selected line."]
676 + set to_context {+}
677 + lappend apply_cmd --reverse
678 + } else {
679 + set failed_msg [mc "Failed to stage selected line."]
680 + set to_context {-}
681 + lappend apply_cmd --cached
682 + }
683 +
684 if {[string index $mi 1] ne {M}} {
685 unlock_index
686 return