Skip to content

Commit 62bd999

Browse files
committed
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 <[email protected]>
1 parent 5f0a516 commit 62bd999

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

git-gui.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,7 @@ set ctxm .vpane.lower.diff.body.ctxm
35823582
menu $ctxm -tearoff 0
35833583
$ctxm add command \
35843584
-label [mc "Apply/Reverse Hunk"] \
3585-
-command {apply_hunk $cursorX $cursorY}
3585+
-command {apply_or_revert_hunk $cursorX $cursorY 0}
35863586
set ui_diff_applyhunk [$ctxm index last]
35873587
lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
35883588
$ctxm add command \
@@ -3591,6 +3591,11 @@ $ctxm add command \
35913591
set ui_diff_applyline [$ctxm index last]
35923592
lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state]
35933593
$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]
35943599
$ctxm add command \
35953600
-label [mc "Revert Line"] \
35963601
-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} {
36913696
set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}]
36923697
if {$::ui_index eq $::current_diff_side} {
36933698
set l [mc "Unstage Hunk From Commit"]
3699+
set h [mc "Revert Hunk"]
3700+
36943701
if {$has_range} {
36953702
set t [mc "Unstage Lines From Commit"]
36963703
set r [mc "Revert Lines"]
@@ -3700,6 +3707,8 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
37003707
}
37013708
} else {
37023709
set l [mc "Stage Hunk For Commit"]
3710+
set h [mc "Revert Hunk"]
3711+
37033712
if {$has_range} {
37043713
set t [mc "Stage Lines For Commit"]
37053714
set r [mc "Revert Lines"]
@@ -3734,6 +3743,9 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
37343743
$ctxm entryconf $::ui_diff_applyline -state $s -label $t
37353744
$ctxm entryconf $::ui_diff_revertline -state $revert_state \
37363745
-label $r
3746+
$ctxm entryconf $::ui_diff_reverthunk -state $revert_state \
3747+
-label $h
3748+
37373749
tk_popup $ctxm $X $Y
37383750
}
37393751
}

lib/diff.tcl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
572572
global ui_diff ui_index file_states
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
@@ -619,13 +626,17 @@ proc apply_hunk {x y} {
619626
$ui_diff delete $s_lno $e_lno
620627
$ui_diff conf -state disabled
621628

629+
# Check if the hunk was the last one in the file.
622630
if {[$ui_diff get 1.0 end] eq "\n"} {
623631
set o _
624632
} else {
625633
set o ?
626634
}
627635

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} {
629640
set mi ${o}M
630641
} elseif {[string index $mi 0] eq {_}} {
631642
set mi M$o

0 commit comments

Comments
 (0)