From 5f0a516de9e8e6abdd37a79546f904a0ddbce58d Mon Sep 17 00:00:00 2001 From: Pratyush Yadav Date: Mon, 26 Aug 2019 01:35:27 +0530 Subject: [PATCH 1/4] 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 --- git-gui.sh | 25 ++++++++++++++++++++++++- lib/diff.tcl | 20 ++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/git-gui.sh b/git-gui.sh index 5bc21b878d..6d4d002559 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -3587,10 +3587,16 @@ set ui_diff_applyhunk [$ctxm index last] lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state] $ctxm add command \ -label [mc "Apply/Reverse Line"] \ - -command {apply_range_or_line $cursorX $cursorY; do_rescan} + -command {apply_or_revert_range_or_line $cursorX $cursorY 0; do_rescan} set ui_diff_applyline [$ctxm index last] lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state] $ctxm add separator +$ctxm add command \ + -label [mc "Revert Line"] \ + -command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan} +set ui_diff_revertline [$ctxm index last] +lappend diff_actions [list $ctxm entryconf $ui_diff_revertline -state] +$ctxm add separator $ctxm add command \ -label [mc "Show Less Context"] \ -command show_less_context @@ -3687,15 +3693,19 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { set l [mc "Unstage Hunk From Commit"] if {$has_range} { set t [mc "Unstage Lines From Commit"] + set r [mc "Revert Lines"] } else { set t [mc "Unstage Line From Commit"] + set r [mc "Revert Line"] } } else { set l [mc "Stage Hunk For Commit"] if {$has_range} { set t [mc "Stage Lines For Commit"] + set r [mc "Revert Lines"] } else { set t [mc "Stage Line For Commit"] + set r [mc "Revert Line"] } } if {$::is_3way_diff @@ -3706,11 +3716,24 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { || [string match {T?} $state] || [has_textconv $current_diff_path]} { set s disabled + set revert_state disabled } else { set s normal + + # Only allow reverting changes in the working tree. If + # the user wants to revert changes in the index, they + # need to unstage those first. + if {$::ui_workdir eq $::current_diff_side} { + set revert_state normal + } else { + set revert_state disabled + } } + $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l $ctxm entryconf $::ui_diff_applyline -state $s -label $t + $ctxm entryconf $::ui_diff_revertline -state $revert_state \ + -label $r tk_popup $ctxm $X $Y } } diff --git a/lib/diff.tcl b/lib/diff.tcl index 4cae10a4c7..d6bee29a13 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -55,7 +55,7 @@ proc reshow_diff {{after {}}} { proc force_diff_encoding {enc} { global current_diff_path - + if {$current_diff_path ne {}} { force_path_encoding $current_diff_path $enc reshow_diff @@ -640,7 +640,7 @@ proc apply_hunk {x y} { } } -proc apply_range_or_line {x y} { +proc apply_or_revert_range_or_line {x y revert} { global current_diff_path current_diff_header current_diff_side global ui_diff ui_index file_states @@ -660,19 +660,27 @@ proc apply_range_or_line {x y} { if {$current_diff_path eq {} || $current_diff_header eq {}} return if {![lock_index apply_hunk]} return - set apply_cmd {apply --cached --whitespace=nowarn} + set apply_cmd {apply --whitespace=nowarn} set mi [lindex $file_states($current_diff_path) 0] if {$current_diff_side eq $ui_index} { set failed_msg [mc "Failed to unstage selected line."] set to_context {+} - lappend apply_cmd --reverse + lappend apply_cmd --reverse --cached if {[string index $mi 0] ne {M}} { unlock_index return } } else { - set failed_msg [mc "Failed to stage selected line."] - set to_context {-} + if {$revert} { + set failed_msg [mc "Failed to revert selected line."] + set to_context {+} + lappend apply_cmd --reverse + } else { + set failed_msg [mc "Failed to stage selected line."] + set to_context {-} + lappend apply_cmd --cached + } + if {[string index $mi 1] ne {M}} { unlock_index return From 62bd99934bb2b1a07f786fdf9b94d7b692768e30 Mon Sep 17 00:00:00 2001 From: Pratyush Yadav Date: Sun, 18 Aug 2019 00:01:43 +0530 Subject: [PATCH 2/4] 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 --- git-gui.sh | 14 +++++++++++++- lib/diff.tcl | 21 ++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/git-gui.sh b/git-gui.sh index 6d4d002559..1592544830 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -3582,7 +3582,7 @@ set ctxm .vpane.lower.diff.body.ctxm menu $ctxm -tearoff 0 $ctxm add command \ -label [mc "Apply/Reverse Hunk"] \ - -command {apply_hunk $cursorX $cursorY} + -command {apply_or_revert_hunk $cursorX $cursorY 0} set ui_diff_applyhunk [$ctxm index last] lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state] $ctxm add command \ @@ -3591,6 +3591,11 @@ $ctxm add command \ set ui_diff_applyline [$ctxm index last] lappend diff_actions [list $ctxm entryconf $ui_diff_applyline -state] $ctxm add separator +$ctxm add command \ + -label [mc "Revert Hunk"] \ + -command {apply_or_revert_hunk $cursorX $cursorY 1} +set ui_diff_reverthunk [$ctxm index last] +lappend diff_actions [list $ctxm entryconf $ui_diff_reverthunk -state] $ctxm add command \ -label [mc "Revert Line"] \ -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} { set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}] if {$::ui_index eq $::current_diff_side} { set l [mc "Unstage Hunk From Commit"] + set h [mc "Revert Hunk"] + if {$has_range} { set t [mc "Unstage Lines From Commit"] set r [mc "Revert Lines"] @@ -3700,6 +3707,8 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { } } else { set l [mc "Stage Hunk For Commit"] + set h [mc "Revert Hunk"] + if {$has_range} { set t [mc "Stage Lines For Commit"] set r [mc "Revert Lines"] @@ -3734,6 +3743,9 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { $ctxm entryconf $::ui_diff_applyline -state $s -label $t $ctxm entryconf $::ui_diff_revertline -state $revert_state \ -label $r + $ctxm entryconf $::ui_diff_reverthunk -state $revert_state \ + -label $h + tk_popup $ctxm $X $Y } } diff --git a/lib/diff.tcl b/lib/diff.tcl index d6bee29a13..ffca7880e1 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -567,24 +567,31 @@ proc read_diff {fd conflict_size cont_info} { } } -proc apply_hunk {x y} { +proc apply_or_revert_hunk {x y revert} { global current_diff_path current_diff_header current_diff_side global ui_diff ui_index file_states if {$current_diff_path eq {} || $current_diff_header eq {}} return if {![lock_index apply_hunk]} return - set apply_cmd {apply --cached --whitespace=nowarn} + set apply_cmd {apply --whitespace=nowarn} set mi [lindex $file_states($current_diff_path) 0] if {$current_diff_side eq $ui_index} { set failed_msg [mc "Failed to unstage selected hunk."] - lappend apply_cmd --reverse + lappend apply_cmd --reverse --cached if {[string index $mi 0] ne {M}} { unlock_index return } } else { - set failed_msg [mc "Failed to stage selected hunk."] + if {$revert} { + set failed_msg [mc "Failed to revert selected hunk."] + lappend apply_cmd --reverse + } else { + set failed_msg [mc "Failed to stage selected hunk."] + lappend apply_cmd --cached + } + if {[string index $mi 1] ne {M}} { unlock_index return @@ -619,13 +626,17 @@ proc apply_hunk {x y} { $ui_diff delete $s_lno $e_lno $ui_diff conf -state disabled + # Check if the hunk was the last one in the file. if {[$ui_diff get 1.0 end] eq "\n"} { set o _ } else { set o ? } - if {$current_diff_side eq $ui_index} { + # Update the status flags. + if {$revert} { + set mi [string index $mi 0]$o + } elseif {$current_diff_side eq $ui_index} { set mi ${o}M } elseif {[string index $mi 0] eq {_}} { set mi M$o From 2ccdfb1c78e1695c4df714887b73ab417ca60f22 Mon Sep 17 00:00:00 2001 From: Pratyush Yadav Date: Mon, 26 Aug 2019 04:23:13 +0530 Subject: [PATCH 3/4] git-gui: return early when patch fails to apply In the procedure apply_or_revert_range_or_line, if the patch does not apply successfully, a dialog is shown, but execution proceeds after that. Instead, return early on error so the parts that come after this don't work on top of an error state. Signed-off-by: Pratyush Yadav --- lib/diff.tcl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/diff.tcl b/lib/diff.tcl index ffca7880e1..0659029632 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -848,6 +848,8 @@ proc apply_or_revert_range_or_line {x y revert} { puts -nonewline $p $wholepatch close $p} err]} { error_popup "$failed_msg\n\n$err" + unlock_index + return } unlock_index From a4fa2f0a4c1a1ef7f2987fb9d38342fcaad78a75 Mon Sep 17 00:00:00 2001 From: Pratyush Yadav Date: Mon, 26 Aug 2019 01:43:23 +0530 Subject: [PATCH 4/4] git-gui: allow undoing last revert Accidental clicks on the revert hunk/lines buttons can cause loss of work, and can be frustrating. So, allow undoing the last revert. Right now, a stack or deque are not being used for the sake of simplicity, so only one undo is possible. Any reverts before the previous one are lost. Signed-off-by: Pratyush Yadav --- git-gui.sh | 18 +++++++++++++++++- lib/diff.tcl | 53 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/git-gui.sh b/git-gui.sh index 1592544830..e03a2d2c28 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -1350,6 +1350,8 @@ set is_submodule_diff 0 set is_conflict_diff 0 set selected_commit_type new set diff_empty_count 0 +set last_revert {} +set last_revert_enc {} set nullid "0000000000000000000000000000000000000000" set nullid2 "0000000000000000000000000000000000000001" @@ -3601,6 +3603,11 @@ $ctxm add command \ -command {apply_or_revert_range_or_line $cursorX $cursorY 1; do_rescan} set ui_diff_revertline [$ctxm index last] lappend diff_actions [list $ctxm entryconf $ui_diff_revertline -state] +$ctxm add command \ + -label [mc "Undo Last Revert"] \ + -command {undo_last_revert; do_rescan} +set ui_diff_undorevert [$ctxm index last] +lappend diff_actions [list $ctxm entryconf $ui_diff_undorevert -state] $ctxm add separator $ctxm add command \ -label [mc "Show Less Context"] \ @@ -3680,7 +3687,7 @@ proc has_textconv {path} { } proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { - global current_diff_path file_states + global current_diff_path file_states last_revert set ::cursorX $x set ::cursorY $y if {[info exists file_states($current_diff_path)]} { @@ -3694,6 +3701,7 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { tk_popup $ctxmsm $X $Y } else { set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}] + set u [mc "Undo Last Revert"] if {$::ui_index eq $::current_diff_side} { set l [mc "Unstage Hunk From Commit"] set h [mc "Revert Hunk"] @@ -3739,12 +3747,20 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { } } + if {$last_revert eq {}} { + set undo_state disabled + } else { + set undo_state normal + } + $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l $ctxm entryconf $::ui_diff_applyline -state $s -label $t $ctxm entryconf $::ui_diff_revertline -state $revert_state \ -label $r $ctxm entryconf $::ui_diff_reverthunk -state $revert_state \ -label $h + $ctxm entryconf $::ui_diff_undorevert -state $undo_state \ + -label $u tk_popup $ctxm $X $Y } diff --git a/lib/diff.tcl b/lib/diff.tcl index 0659029632..96288fcc33 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -569,7 +569,7 @@ proc read_diff {fd conflict_size cont_info} { proc apply_or_revert_hunk {x y revert} { global current_diff_path current_diff_header current_diff_side - global ui_diff ui_index file_states + global ui_diff ui_index file_states last_revert last_revert_enc if {$current_diff_path eq {} || $current_diff_header eq {}} return if {![lock_index apply_hunk]} return @@ -610,18 +610,25 @@ proc apply_or_revert_hunk {x y revert} { set e_lno end } + set wholepatch "$current_diff_header[$ui_diff get $s_lno $e_lno]" + if {[catch { set enc [get_path_encoding $current_diff_path] set p [eval git_write $apply_cmd] fconfigure $p -translation binary -encoding $enc - puts -nonewline $p $current_diff_header - puts -nonewline $p [$ui_diff get $s_lno $e_lno] + puts -nonewline $p $wholepatch close $p} err]} { error_popup "$failed_msg\n\n$err" unlock_index return } + if {$revert} { + # Save a copy of this patch for undoing reverts. + set last_revert $wholepatch + set last_revert_enc $enc + } + $ui_diff conf -state normal $ui_diff delete $s_lno $e_lno $ui_diff conf -state disabled @@ -653,7 +660,7 @@ proc apply_or_revert_hunk {x y revert} { proc apply_or_revert_range_or_line {x y revert} { global current_diff_path current_diff_header current_diff_side - global ui_diff ui_index file_states + global ui_diff ui_index file_states last_revert set selected [$ui_diff tag nextrange sel 0.0] @@ -852,5 +859,43 @@ proc apply_or_revert_range_or_line {x y revert} { return } + if {$revert} { + # Save a copy of this patch for undoing reverts. + set last_revert $current_diff_header$wholepatch + set last_revert_enc $enc + } + + unlock_index +} + +# Undo the last line/hunk reverted. When hunks and lines are reverted, a copy +# of the diff applied is saved. Re-apply that diff to undo the revert. +# +# Right now, we only use a single variable to hold the copy, and not a +# stack/deque for simplicity, so multiple undos are not possible. Maybe this +# can be added if the need for something like this is felt in the future. +proc undo_last_revert {} { + global last_revert current_diff_path current_diff_header + global last_revert_enc + + if {$last_revert eq {}} return + if {![lock_index apply_hunk]} return + + set apply_cmd {apply --whitespace=nowarn} + set failed_msg [mc "Failed to undo last revert."] + + if {[catch { + set enc $last_revert_enc + set p [eval git_write $apply_cmd] + fconfigure $p -translation binary -encoding $enc + puts -nonewline $p $last_revert + close $p} err]} { + error_popup "$failed_msg\n\n$err" + unlock_index + return + } + + set last_revert {} + unlock_index }