From 8a965b8ee28bedc58641453e13d03f80180320dd Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 13 Jun 2008 03:42:10 +0530 Subject: git-gui: Move on to the next filename after staging/unstaging a change Suppose the "Unstaged Changes" pane contains a list of files, and one of them is selected (i.e., that diff is currently being displayed). If one clicks on the icon to stage the change, git-gui clears the diff and one has to click on another filename to see the next diff in the list. This patch changes that behaviour. If one clicks on the icon to stage (or unstage) the file whose diff is being displayed, git-gui will move on to the next filename in the list and display that diff instead of a blank diff pane. If the selected file was at the end of the list, the diff pane will display the previous diff instead; if the selected file was the only one listed, the diff pane will become blank. If no diff is currently being displayed, this patch changes nothing. Signed-off-by: Abhijit Menon-Sen Signed-off-by: Shawn O. Pearce diff --git a/git-gui.sh b/git-gui.sh index e6e8890..23d7dfe 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -1774,6 +1774,11 @@ proc do_commit {} { commit_tree } +proc next_diff {} { + global next_diff_p next_diff_w next_diff_i + show_diff $next_diff_p $next_diff_w $next_diff_i +} + proc toggle_or_diff {w x y} { global file_states file_lists current_diff_path ui_index ui_workdir global last_clicked selected_paths @@ -1793,11 +1798,31 @@ proc toggle_or_diff {w x y} { $ui_workdir tag remove in_sel 0.0 end if {$col == 0} { - if {$current_diff_path eq $path} { + set i [expr {$lno-1}] + set ll [expr {[llength $file_lists($w)]-1}] + + if {$i == $ll && $i == 0} { set after {reshow_diff;} } else { - set after {} + global next_diff_p next_diff_w next_diff_i + + if {$i < $ll} { + set i [expr {$i + 1}] + } else { + set i [expr {$i - 1}] + } + + set next_diff_i $i + set next_diff_w $w + set next_diff_p [lindex $file_lists($w) $i] + + if {$next_diff_p ne {} && $current_diff_path ne {}} { + set after {next_diff;} + } else { + set after {} + } } + if {$w eq $ui_index} { update_indexinfo \ "Unstaging [short_path $path] from commit" \ -- cgit v0.10.2-6-g49f6 From cead78edef6a2f1339f699cd7d656b6834707b94 Mon Sep 17 00:00:00 2001 From: Richard Quirk Date: Fri, 20 Jun 2008 16:58:15 +0200 Subject: git-gui: Fix accidental staged state toggle when clicking top pixel row If a text widget is asked the index at x,y with y == 0 or y == 1 it will always return 1.0 as the nearest index, regardless of the x position. This means that clicking the top 2 pixels of the Unstaged/Staged Changes lists caused the state of the file there to be toggled. This patch checks that the pixel clicked is greater than 1, so there is less chance of accidentally staging or unstaging changes. Signed-off-by: Richard Quirk Signed-off-by: Shawn O. Pearce diff --git a/git-gui.sh b/git-gui.sh index 23d7dfe..980dc0b 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -1797,7 +1797,7 @@ proc toggle_or_diff {w x y} { $ui_index tag remove in_sel 0.0 end $ui_workdir tag remove in_sel 0.0 end - if {$col == 0} { + if {$col == 0 && $y > 1} { set i [expr {$lno-1}] set ll [expr {[llength $file_lists($w)]-1}] -- cgit v0.10.2-6-g49f6 From f531e463f0471ce41c51e19074a118482f36910f Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Wed, 25 Jun 2008 16:06:50 +0530 Subject: git-gui: Don't select the wrong file if the last listed file is staged. Johannes Sixt noticed that if the last file in the list was staged, my earlier patch would display the diff for the penultimate file, but show the file _before_ that as being selected. This was due to my misunderstanding the lno argument to show_diff. This patch fixes the problem: lno is not decremented in the special case to handle the last item in the list (though we still need to use $lno-1 to find the right path for the next diff). Signed-off-by: Abhijit Menon-Sen Tested-by: Johannes Sixt Signed-off-by: Shawn O. Pearce diff --git a/git-gui.sh b/git-gui.sh index 980dc0b..1bbae15 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -1806,14 +1806,16 @@ proc toggle_or_diff {w x y} { } else { global next_diff_p next_diff_w next_diff_i + set next_diff_w $w + if {$i < $ll} { set i [expr {$i + 1}] + set next_diff_i $i } else { + set next_diff_i $i set i [expr {$i - 1}] } - set next_diff_i $i - set next_diff_w $w set next_diff_p [lindex $file_lists($w) $i] if {$next_diff_p ne {} && $current_diff_path ne {}} { -- cgit v0.10.2-6-g49f6 From 5821988f97b827f6ba81dfeebff932067c88ba6c Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 27 Jun 2008 09:22:01 +0200 Subject: git-gui: Implement "Stage/Unstage Line" This adds a context menu entry below "Stage/Unstage Hunk" that stages or unstages just the line under the mouse pointer. This is by itself useful, for example, if there are unrelated changes in the same hunk and the hunk cannot be split by reducing the context. The feature can also be used to split a hunk by staging a number of additions (or unstaging a number of removals) until there are enough context lines that the hunk gets split. The implementation reads the complete hunk that the line lives in, and constructs a new hunk by picking existing context lines, removing unneeded change lines and transforming other change lines to context lines. The resulting hunk is fed through 'git apply' just like in the "Stage/Unstage Hunk" case. Signed-off-by: Johannes Sixt Signed-off-by: Shawn O. Pearce diff --git a/git-gui.sh b/git-gui.sh index 1bbae15..d89f156 100755 --- a/git-gui.sh +++ b/git-gui.sh @@ -2666,6 +2666,11 @@ $ctxm add command \ -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 \ + -label [mc "Apply/Reverse Line"] \ + -command {apply_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 \ -label [mc "Show Less Context"] \ @@ -2714,8 +2719,10 @@ proc popup_diff_menu {ctxm x y X Y} { set ::cursorY $y if {$::ui_index eq $::current_diff_side} { set l [mc "Unstage Hunk From Commit"] + set t [mc "Unstage Line From Commit"] } else { set l [mc "Stage Hunk For Commit"] + set t [mc "Stage Line For Commit"] } if {$::is_3way_diff || $current_diff_path eq {} @@ -2726,6 +2733,7 @@ proc popup_diff_menu {ctxm x y X Y} { set s normal } $ctxm entryconf $::ui_diff_applyhunk -state $s -label $l + $ctxm entryconf $::ui_diff_applyline -state $s -label $t tk_popup $ctxm $X $Y } bind_button3 $ui_diff [list popup_diff_menu $ctxm %x %y %X %Y] diff --git a/lib/diff.tcl b/lib/diff.tcl index d04f6db..96ba949 100644 --- a/lib/diff.tcl +++ b/lib/diff.tcl @@ -362,3 +362,90 @@ proc apply_hunk {x y} { set current_diff_path $current_diff_path } } + +proc apply_line {x y} { + 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 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 + if {[string index $mi 0] ne {M}} { + unlock_index + return + } + } else { + set failed_msg [mc "Failed to stage selected line."] + set to_context {-} + if {[string index $mi 1] ne {M}} { + unlock_index + return + } + } + + set the_l [$ui_diff index @$x,$y] + + # operate only on change lines + set c1 [$ui_diff get "$the_l linestart"] + if {$c1 ne {+} && $c1 ne {-}} { + unlock_index + return + } + set sign $c1 + + set i_l [$ui_diff search -backwards -regexp ^@@ $the_l 0.0] + if {$i_l eq {}} { + unlock_index + return + } + # $i_l is now at the beginning of a line + + # pick start line number from hunk header + set hh [$ui_diff get $i_l "$i_l + 1 lines"] + set hh [lindex [split $hh ,] 0] + set hln [lindex [split $hh -] 1] + + set n 0 + set i_l [$ui_diff index "$i_l + 1 lines"] + set patch {} + while {[$ui_diff compare $i_l < "end - 1 chars"] && + [$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} { + set next_l [$ui_diff index "$i_l + 1 lines"] + set c1 [$ui_diff get $i_l] + if {[$ui_diff compare $i_l <= $the_l] && + [$ui_diff compare $the_l < $next_l]} { + # the line to stage/unstage + set ln [$ui_diff get $i_l $next_l] + set patch "$patch$ln" + } elseif {$c1 ne {-} && $c1 ne {+}} { + # context line + set ln [$ui_diff get $i_l $next_l] + set patch "$patch$ln" + set n [expr $n+1] + } elseif {$c1 eq $to_context} { + # turn change line into context line + set ln [$ui_diff get "$i_l + 1 chars" $next_l] + set patch "$patch $ln" + set n [expr $n+1] + } + set i_l $next_l + } + set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch" + + if {[catch { + set p [eval git_write $apply_cmd] + fconfigure $p -translation binary -encoding binary + puts -nonewline $p $current_diff_header + puts -nonewline $p $patch + close $p} err]} { + error_popup [append $failed_msg "\n\n$err"] + } + + unlock_index +} -- cgit v0.10.2-6-g49f6