Skip to content

Commit

Permalink
git-gui: support for reverting hunks and lines
Browse files Browse the repository at this point in the history
Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
  • Loading branch information
bertwesarg authored and j6t committed Jan 9, 2012
1 parent f12b294 commit 0086061
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 39 deletions.
106 changes: 72 additions & 34 deletions git-gui.sh
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand All @@ -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
#
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -3597,18 +3628,22 @@ 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"]
} else {
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
Expand All @@ -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
#
Expand Down
23 changes: 18 additions & 5 deletions lib/diff.tcl
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0086061

Please sign in to comment.