Skip to content

Commit 8f85599

Browse files
committed
git-gui: apply color information from git diff output
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 <[email protected]> Suggested-by: Junio C Hamano <[email protected]> Tested-by: Tor Arvid Lund <[email protected]> Signed-off-by: Pat Thoyts <[email protected]>
1 parent c744086 commit 8f85599

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

git-gui.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3323,8 +3323,16 @@ pack $ui_diff -side left -fill both -expand 1
33233323
pack .vpane.lower.diff.header -side top -fill x
33243324
pack .vpane.lower.diff.body -side bottom -fill both -expand 1
33253325
3326+
foreach {n c} {0 black 1 red4 2 green4 3 yellow4 4 blue4 5 magenta4 6 cyan4 7 grey60} {
3327+
$ui_diff tag configure clr4$n -background $c
3328+
$ui_diff tag configure clri4$n -foreground $c
3329+
$ui_diff tag configure clr3$n -foreground $c
3330+
$ui_diff tag configure clri3$n -background $c
3331+
}
3332+
$ui_diff tag configure clr1 -font font_diffbold
3333+
33263334
$ui_diff tag conf d_cr -elide true
3327-
$ui_diff tag conf d_@ -foreground blue -font font_diffbold
3335+
$ui_diff tag conf d_@ -font font_diffbold
33283336
$ui_diff tag conf d_+ -foreground {#00a000}
33293337
$ui_diff tag conf d_- -foreground red
33303338

lib/diff.tcl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ proc start_show_diff {cont_info {add_opts {}}} {
294294
}
295295

296296
lappend cmd -p
297-
lappend cmd --no-color
297+
lappend cmd --color
298298
if {$repo_config(gui.diffcontext) >= 1} {
299299
lappend cmd "-U$repo_config(gui.diffcontext)"
300300
}
@@ -332,6 +332,23 @@ proc start_show_diff {cont_info {add_opts {}}} {
332332
fileevent $fd readable [list read_diff $fd $cont_info]
333333
}
334334

335+
proc parse_color_line {line} {
336+
set start 0
337+
set result ""
338+
set markup [list]
339+
set regexp {\033\[((?:\d+;)*\d+)?m}
340+
while {[regexp -indices -start $start $regexp $line match code]} {
341+
foreach {begin end} $match break
342+
append result [string range $line $start [expr {$begin - 1}]]
343+
lappend markup [string length $result] \
344+
[eval [linsert $code 0 string range $line]]
345+
set start [incr end]
346+
}
347+
append result [string range $line $start end]
348+
if {[llength $markup] < 4} {set markup {}}
349+
return [list $result $markup]
350+
}
351+
335352
proc read_diff {fd cont_info} {
336353
global ui_diff diff_active is_submodule_diff
337354
global is_3way_diff is_conflict_diff current_diff_header
@@ -340,6 +357,9 @@ proc read_diff {fd cont_info} {
340357

341358
$ui_diff conf -state normal
342359
while {[gets $fd line] >= 0} {
360+
foreach {line markup} [parse_color_line $line] break
361+
set line [string map {\033 ^} $line]
362+
343363
# -- Cleanup uninteresting diff header lines.
344364
#
345365
if {$::current_diff_inheader} {
@@ -434,11 +454,23 @@ proc read_diff {fd cont_info} {
434454
}
435455
}
436456
}
457+
set mark [$ui_diff index "end - 1 line linestart"]
437458
$ui_diff insert end $line $tags
438459
if {[string index $line end] eq "\r"} {
439460
$ui_diff tag add d_cr {end - 2c}
440461
}
441462
$ui_diff insert end "\n" $tags
463+
464+
foreach {posbegin colbegin posend colend} $markup {
465+
set prefix clr
466+
foreach style [split $colbegin ";"] {
467+
if {$style eq "7"} {append prefix i; continue}
468+
if {$style < 30 || $style > 47} {continue}
469+
set a "$mark linestart + $posbegin chars"
470+
set b "$mark linestart + $posend chars"
471+
catch {$ui_diff tag add $prefix$style $a $b}
472+
}
473+
}
442474
}
443475
$ui_diff conf -state disabled
444476

0 commit comments

Comments
 (0)