git-gui: allow reverting selected hunk

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

Pratyush Yadav committed Aug 18, 2019 at 00:01 UTC 62bd99934bb2b1a07f786fdf9b94d7b692768e30
2 files changed +29 -6
git-gui.sh
+13 -1
@@ -3582,7 +3582,7 @@ set ctxm .vpane.lower.diff.body.ctxm
3582 menu $ctxm -tearoff 0
3583 $ctxm add command \
3584 -label [mc "Apply/Reverse Hunk"] \
3585 - -command {apply_hunk $cursorX $cursorY}
3585 + -command {apply_or_revert_hunk $cursorX $cursorY 0}
3586 set ui_diff_applyhunk [$ctxm index last]
3587 lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
3588 $ctxm add command \
@@ -3591,6 +3591,11 @@ $ctxm add command \
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 Hunk"] \
3596 + -command {apply_or_revert_hunk $cursorX $cursorY 1}
3597 +set ui_diff_reverthunk [$ctxm index last]
3598 +lappend diff_actions [list $ctxm entryconf $ui_diff_reverthunk -state]
3599 $ctxm add command \
3600 -label [mc "Revert Line"] \
3601 -command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan}
@@ -3691,6 +3696,8 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3696 set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}]
3697 if {$::ui_index eq $::current_diff_side} {
3698 set l [mc "Unstage Hunk From Commit"]
3699 + set h [mc "Revert Hunk"]
3700 +
3701 if {$has_range} {
3702 set t [mc "Unstage Lines From Commit"]
3703 set r [mc "Revert Lines"]
@@ -3700,6 +3707,8 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3707 }
3708 } else {
3709 set l [mc "Stage Hunk For Commit"]
3710 + set h [mc "Revert Hunk"]
3711 +
3712 if {$has_range} {
3713 set t [mc "Stage Lines For Commit"]
3714 set r [mc "Revert Lines"]
@@ -3734,6 +3743,9 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
3743 $ctxm entryconf $::ui_diff_applyline -state $s -label $t
3744 $ctxm entryconf $::ui_diff_revertline -state $revert_state \
3745 -label $r
3746 + $ctxm entryconf $::ui_diff_reverthunk -state $revert_state \
3747 + -label $h
3748 +
3749 tk_popup $ctxm $X $Y
3750 }
3751 }
lib/diff.tcl
+16 -5
@@ -567,24 +567,31 @@ proc read_diff {fd conflict_size cont_info} {
567 }
568 }
569
570 -proc apply_hunk {x y} {
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
573
574 if {$current_diff_path eq {} || $current_diff_header eq {}} return
575 if {![lock_index apply_hunk]} return
576
577 - set apply_cmd {apply --cached --whitespace=nowarn}
577 + set apply_cmd {apply --whitespace=nowarn}
578 set mi [lindex $file_states($current_diff_path) 0]
579 if {$current_diff_side eq $ui_index} {
580 set failed_msg [mc "Failed to unstage selected hunk."]
581 - lappend apply_cmd --reverse
581 + lappend apply_cmd --reverse --cached
582 if {[string index $mi 0] ne {M}} {
583 unlock_index
584 return
585 }
586 } 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 +
595 if {[string index $mi 1] ne {M}} {
596 unlock_index
597 return
@@ -619,13 +626,17 @@ proc apply_hunk {x y} {
626 $ui_diff delete $s_lno $e_lno
627 $ui_diff conf -state disabled
628
629 + # Check if the hunk was the last one in the file.
630 if {[$ui_diff get 1.0 end] eq "\n"} {
631 set o _
632 } else {
633 set o ?
634 }
635
628 - if {$current_diff_side eq $ui_index} {
636 + # Update the status flags.
637 + if {$revert} {
638 + set mi [string index $mi 0]$o
639 + } elseif {$current_diff_side eq $ui_index} {
640 set mi ${o}M
641 } elseif {[string index $mi 0] eq {_}} {
642 set mi M$o