From 00860615691604c9978c3134d97b2762c5590ed9 Mon Sep 17 00:00:00 2001 From: Bert Wesarg Date: Mon, 9 Jan 2012 14:43:19 +0100 Subject: [PATCH] git-gui: support for reverting hunks and lines Signed-off-by: Bert Wesarg --- git-gui.sh | 106 ++++++++++++++++++++++++++++++++++----------------- lib/diff.tcl | 23 ++++++++--- 2 files changed, 90 insertions(+), 39 deletions(-) diff --git a/git-gui.sh b/git-gui.sh index ba4e5c1330c84f..955cbf8402896c 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -3289,36 +3289,35 @@ pack .vpane.lower.commarea.buffer -side left -fill y # -- Commit Message Buffer Context Menu # -set ctxm .vpane.lower.commarea.buffer.ctxm -menu $ctxm -tearoff 0 -$ctxm add command \ +set ui_comm_ctxm .vpane.lower.commarea.buffer.ctxm +menu $ui_comm_ctxm -tearoff 0 +$ui_comm_ctxm add command \ -label [mc Cut] \ -command {tk_textCut $ui_comm} -$ctxm add command \ +$ui_comm_ctxm add command \ -label [mc Copy] \ -command {tk_textCopy $ui_comm} -$ctxm add command \ +$ui_comm_ctxm add command \ -label [mc Paste] \ -command {tk_textPaste $ui_comm} -$ctxm add command \ +$ui_comm_ctxm add command \ -label [mc Delete] \ -command {catch {$ui_comm delete sel.first sel.last}} -$ctxm add separator -$ctxm add command \ +$ui_comm_ctxm add separator +$ui_comm_ctxm add command \ -label [mc "Select All"] \ -command {focus $ui_comm;$ui_comm tag add sel 0.0 end} -$ctxm add command \ +$ui_comm_ctxm add command \ -label [mc "Copy All"] \ -command { $ui_comm tag add sel 0.0 end tk_textCopy $ui_comm $ui_comm tag remove sel 0.0 end } -$ctxm add separator -$ctxm add command \ +$ui_comm_ctxm add separator +$ui_comm_ctxm add command \ -label [mc "Sign Off"] \ -command do_signoff -set ui_comm_ctxm $ctxm # -- Diff Header # @@ -3366,9 +3365,9 @@ tlabel .vpane.lower.diff.header.path \ pack .vpane.lower.diff.header.status -side left pack .vpane.lower.diff.header.file -side left pack .vpane.lower.diff.header.path -fill x -set ctxm .vpane.lower.diff.header.ctxm -menu $ctxm -tearoff 0 -$ctxm add command \ +set hctxm .vpane.lower.diff.header.ctxm +menu $hctxm -tearoff 0 +$hctxm add command \ -label [mc Copy] \ -command { clipboard clear @@ -3377,8 +3376,8 @@ $ctxm add command \ -type STRING \ -- $current_diff_path } -lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state] -bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y" +lappend diff_actions [list $hctxm entryconf [$hctxm index last] -state] +bind_button3 .vpane.lower.diff.header.path "tk_popup $hctxm %X %Y" # -- Diff Body # @@ -3491,29 +3490,61 @@ proc create_common_diff_popup {ctxm} { -command do_options } -set ctxm .vpane.lower.diff.body.ctxm -menu $ctxm -tearoff 0 -$ctxm add command \ +set ctxmw .vpane.lower.diff.body.ctxmw +menu $ctxmw -tearoff 0 +$ctxmw add command \ -label [mc "Apply/Reverse Hunk"] \ -command {apply_hunk $cursorX $cursorY} -set ui_diff_applyhunk [$ctxm index last] -lappend diff_actions [list $ctxm entryconf $ui_diff_applyhunk -state] -$ctxm add command \ +set ui_diff_applyhunk [$ctxmw index last] +lappend diff_actions [list $ctxmw entryconf $ui_diff_applyhunk -state] +$ctxmw add command \ -label [mc "Apply/Reverse Line"] \ -command {apply_range_or_line $cursorX $cursorY; 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 \ +set ui_diff_applyline [$ctxmw index last] +lappend diff_actions [list $ctxmw entryconf $ui_diff_applyline -state] +$ctxmw add separator +$ctxmw add command \ + -label [mc "Revert Hunk"] \ + -command {apply_hunk $cursorX $cursorY 1} +lappend diff_actions [list $ctxmw entryconf $ui_diff_applyhunk -state] +$ctxmw add command \ + -label [mc "Revert Line"] \ + -command {apply_range_or_line $cursorX $cursorY 1; do_rescan} +set ui_diff_revertline [$ctxmw index last] +lappend diff_actions [list $ctxmw entryconf $ui_diff_applyline -state] +$ctxmw add separator +$ctxmw add command \ -label [mc "Show Less Context"] \ -command show_less_context -lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state] -$ctxm add command \ +lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state] +$ctxmw add command \ -label [mc "Show More Context"] \ -command show_more_context -lappend diff_actions [list $ctxm entryconf [$ctxm index last] -state] -$ctxm add separator -create_common_diff_popup $ctxm +lappend diff_actions [list $ctxmw entryconf [$ctxmw index last] -state] +$ctxmw add separator +create_common_diff_popup $ctxmw + +set ctxmi .vpane.lower.diff.body.ctxmi +menu $ctxmi -tearoff 0 +$ctxmi add command \ + -label [mc "Apply/Reverse Hunk"] \ + -command {apply_hunk $cursorX $cursorY} +lappend diff_actions [list $ctxmi entryconf $ui_diff_applyhunk -state] +$ctxmi add command \ + -label [mc "Apply/Reverse Line"] \ + -command {apply_range_or_line $cursorX $cursorY; do_rescan} +lappend diff_actions [list $ctxmi entryconf $ui_diff_applyline -state] +$ctxmi add separator +$ctxmi add command \ + -label [mc "Show Less Context"] \ + -command show_less_context +lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state] +$ctxmi add command \ + -label [mc "Show More Context"] \ + -command show_more_context +lappend diff_actions [list $ctxmi entryconf [$ctxmi index last] -state] +$ctxmi add separator +create_common_diff_popup $ctxmi set ctxmmg .vpane.lower.diff.body.ctxmmg menu $ctxmmg -tearoff 0 @@ -3581,7 +3612,7 @@ proc has_textconv {path} { } } -proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { +proc popup_diff_menu {ctxmw ctxmi ctxmmg ctxmsm x y X Y} { global current_diff_path file_states set ::cursorX $x set ::cursorY $y @@ -3597,6 +3628,7 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { } else { set has_range [expr {[$::ui_diff tag nextrange sel 0.0] != {}}] if {$::ui_index eq $::current_diff_side} { + set ctxm $ctxmi set l [mc "Unstage Hunk From Commit"] if {$has_range} { set t [mc "Unstage Lines From Commit"] @@ -3604,11 +3636,14 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { set t [mc "Unstage Line From Commit"] } } else { + set ctxm $ctxmw 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 @@ -3624,10 +3659,13 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} { } $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l $ctxm entryconf $::ui_diff_applyline -state $s -label $t + if {$::ui_workdir eq $::current_diff_side} { + $ctxm entryconf $::ui_diff_revertline -state $s -label $r + } tk_popup $ctxm $X $Y } } -bind_button3 $ui_diff [list popup_diff_menu $ctxm $ctxmmg $ctxmsm %x %y %X %Y] +bind_button3 $ui_diff [list popup_diff_menu $ctxmw $ctxmi $ctxmmg $ctxmsm %x %y %X %Y] # -- Status Bar # diff --git a/lib/diff.tcl b/lib/diff.tcl index a750ea798ae2b1..83e6f6a24d2ab0 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -544,7 +544,7 @@ proc read_diff {fd conflict_size cont_info} { } } -proc apply_hunk {x y} { +proc apply_hunk {x y {revert 0}} { global current_diff_path current_diff_header current_diff_side global ui_diff ui_index file_states @@ -561,7 +561,12 @@ proc apply_hunk {x y} { return } } else { - set failed_msg [mc "Failed to stage selected hunk."] + if {$revert} { + set failed_msg [mc "Failed to revert selected hunk."] + set apply_cmd {apply --reverse --whitespace=nowarn} + } else { + set failed_msg [mc "Failed to stage selected hunk."] + } if {[string index $mi 1] ne {M}} { unlock_index return @@ -604,6 +609,8 @@ proc apply_hunk {x y} { if {$current_diff_side eq $ui_index} { set mi ${o}M + } elseif {$revert} { + set mi "[string index $mi 0]$o" } elseif {[string index $mi 0] eq {_}} { set mi M$o } else { @@ -617,7 +624,7 @@ proc apply_hunk {x y} { } } -proc apply_range_or_line {x y} { +proc apply_range_or_line {x y {revert 0}} { global current_diff_path current_diff_header current_diff_side global ui_diff ui_index file_states @@ -654,8 +661,14 @@ proc apply_range_or_line {x y} { 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 apply_cmd {apply --reverse --whitespace=nowarn} + set to_context {+} + } else { + set failed_msg [mc "Failed to stage selected line."] + set to_context {-} + } if {[string index $mi 1] ne {M}} { unlock_index return