Code

git-gui: apply color information from git diff output
authorPat Thoyts <patthoyts@users.sourceforge.net>
Fri, 22 Oct 2010 15:14:38 +0000 (16:14 +0100)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Wed, 27 Oct 2010 22:47:28 +0000 (23:47 +0100)
This patch extracts the ANSI color sequences from git diff output and
applies these to the diff view window. This ensures that the gui view
makes use of the current git configuration for whitespace display.

ANSI codes may include attributes, foreground and background in a single
sequence. Handle this and support bold and reverse attributes. Ignore
all other attributes.

Suggested-by: Tor Arvid Lund <torarvid@gmail.com>
Suggested-by: Junio C Hamano <gitster@pobox.com>
Tested-by: Tor Arvid Lund <torarvid@gmail.com>
Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
git-gui.sh
lib/diff.tcl

index efbc446d674433980cf7910006a8f6736e32039e..d3acf0d2134b40b8ffb3d0e8c3229f71fa6db663 100755 (executable)
@@ -3323,8 +3323,16 @@ pack $ui_diff -side left -fill both -expand 1
 pack .vpane.lower.diff.header -side top -fill x
 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
 
+foreach {n c} {0 black 1 red4 2 green4 3 yellow4 4 blue4 5 magenta4 6 cyan4 7 grey60} {
+       $ui_diff tag configure clr4$n -background $c
+       $ui_diff tag configure clri4$n -foreground $c
+       $ui_diff tag configure clr3$n -foreground $c
+       $ui_diff tag configure clri3$n -background $c
+}
+$ui_diff tag configure clr1 -font font_diffbold
+
 $ui_diff tag conf d_cr -elide true
-$ui_diff tag conf d_@ -foreground blue -font font_diffbold
+$ui_diff tag conf d_@ -font font_diffbold
 $ui_diff tag conf d_+ -foreground {#00a000}
 $ui_diff tag conf d_- -foreground red
 
index c628750276303fca620c9292463c4c59690c764f..dcf0711be0119a8f5ec42b48d97980330f7113b3 100644 (file)
@@ -294,7 +294,7 @@ proc start_show_diff {cont_info {add_opts {}}} {
        }
 
        lappend cmd -p
-       lappend cmd --no-color
+       lappend cmd --color
        if {$repo_config(gui.diffcontext) >= 1} {
                lappend cmd "-U$repo_config(gui.diffcontext)"
        }
@@ -332,6 +332,23 @@ proc start_show_diff {cont_info {add_opts {}}} {
        fileevent $fd readable [list read_diff $fd $cont_info]
 }
 
+proc parse_color_line {line} {
+       set start 0
+       set result ""
+       set markup [list]
+       set regexp {\033\[((?:\d+;)*\d+)?m}
+       while {[regexp -indices -start $start $regexp $line match code]} {
+               foreach {begin end} $match break
+               append result [string range $line $start [expr {$begin - 1}]]
+               lappend markup [string length $result] \
+                       [eval [linsert $code 0 string range $line]]
+               set start [incr end]
+       }
+       append result [string range $line $start end]
+       if {[llength $markup] < 4} {set markup {}}
+       return [list $result $markup]
+}
+
 proc read_diff {fd cont_info} {
        global ui_diff diff_active is_submodule_diff
        global is_3way_diff is_conflict_diff current_diff_header
@@ -340,6 +357,9 @@ proc read_diff {fd cont_info} {
 
        $ui_diff conf -state normal
        while {[gets $fd line] >= 0} {
+               foreach {line markup} [parse_color_line $line] break
+               set line [string map {\033 ^} $line]
+
                # -- Cleanup uninteresting diff header lines.
                #
                if {$::current_diff_inheader} {
@@ -434,11 +454,23 @@ proc read_diff {fd cont_info} {
                        }
                        }
                }
+               set mark [$ui_diff index "end - 1 line linestart"]
                $ui_diff insert end $line $tags
                if {[string index $line end] eq "\r"} {
                        $ui_diff tag add d_cr {end - 2c}
                }
                $ui_diff insert end "\n" $tags
+
+               foreach {posbegin colbegin posend colend} $markup {
+                       set prefix clr
+                       foreach style [split $colbegin ";"] {
+                               if {$style eq "7"} {append prefix i; continue}
+                               if {$style < 30 || $style > 47} {continue}
+                               set a "$mark linestart + $posbegin chars"
+                               set b "$mark linestart + $posend chars"
+                               catch {$ui_diff tag add $prefix$style $a $b}
+                       }
+               }
        }
        $ui_diff conf -state disabled