Skip to content

Commit 5f0a516

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

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

git-gui.sh

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3587,10 +3587,16 @@ set ui_diff_applyhunk [$ctxm index last]
35873587
lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state]
35883588
$ctxm add command \
35893589
-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}
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 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
35943600
$ctxm add command \
35953601
-label [mc "Show Less Context"] \
35963602
-command show_less_context
@@ -3687,15 +3693,19 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
36873693
set l [mc "Unstage Hunk From Commit"]
36883694
if {$has_range} {
36893695
set t [mc "Unstage Lines From Commit"]
3696+
set r [mc "Revert Lines"]
36903697
} else {
36913698
set t [mc "Unstage Line From Commit"]
3699+
set r [mc "Revert Line"]
36923700
}
36933701
} else {
36943702
set l [mc "Stage Hunk For Commit"]
36953703
if {$has_range} {
36963704
set t [mc "Stage Lines For Commit"]
3705+
set r [mc "Revert Lines"]
36973706
} else {
36983707
set t [mc "Stage Line For Commit"]
3708+
set r [mc "Revert Line"]
36993709
}
37003710
}
37013711
if {$::is_3way_diff
@@ -3706,11 +3716,24 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
37063716
|| [string match {T?} $state]
37073717
|| [has_textconv $current_diff_path]} {
37083718
set s disabled
3719+
set revert_state disabled
37093720
} else {
37103721
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+
}
37113731
}
3732+
37123733
$ctxm entryconf $::ui_diff_applyhunk -state $s -label $l
37133734
$ctxm entryconf $::ui_diff_applyline -state $s -label $t
3735+
$ctxm entryconf $::ui_diff_revertline -state $revert_state \
3736+
-label $r
37143737
tk_popup $ctxm $X $Y
37153738
}
37163739
}

lib/diff.tcl

Lines changed: 14 additions & 6 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
@@ -640,7 +640,7 @@ proc apply_hunk {x y} {
640640
}
641641
}
642642

643-
proc apply_range_or_line {x y} {
643+
proc apply_or_revert_range_or_line {x y revert} {
644644
global current_diff_path current_diff_header current_diff_side
645645
global ui_diff ui_index file_states
646646

@@ -660,19 +660,27 @@ proc apply_range_or_line {x y} {
660660
if {$current_diff_path eq {} || $current_diff_header eq {}} return
661661
if {![lock_index apply_hunk]} return
662662

663-
set apply_cmd {apply --cached --whitespace=nowarn}
663+
set apply_cmd {apply --whitespace=nowarn}
664664
set mi [lindex $file_states($current_diff_path) 0]
665665
if {$current_diff_side eq $ui_index} {
666666
set failed_msg [mc "Failed to unstage selected line."]
667667
set to_context {+}
668-
lappend apply_cmd --reverse
668+
lappend apply_cmd --reverse --cached
669669
if {[string index $mi 0] ne {M}} {
670670
unlock_index
671671
return
672672
}
673673
} 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+
676684
if {[string index $mi 1] ne {M}} {
677685
unlock_index
678686
return

0 commit comments

Comments
 (0)