Skip to content

Commit 5d198d6

Browse files
committed
git-gui: Use three colors for the blame viewer background
To prevent neighboring lines that are different commits from using the same background color we now use 3 colors and assign them by selecting the color that is not used before or after the line in question. We still color "on the fly" as we receive hunks from git-blame, but we delay our color decisions until we are getting the original location data (the slower -M -C -C pass) as that is usually more fine-grained than the current location data. Credit goes to Martin Waitz for the tri-coloring concept. Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 0dfed77 commit 5d198d6

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

lib/blame.tcl

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ field old_height ; # last known height of $w.file_pane
2626

2727
# Tk UI colors
2828
#
29-
field active_color #c0edc5
30-
field group_colors {
29+
variable active_color #c0edc5
30+
variable group_colors {
3131
#d6d6d6
3232
#e1e1e1
3333
#ececec
@@ -42,11 +42,9 @@ field current_fd {} ; # background process running
4242
field highlight_line -1 ; # current line selected
4343
field highlight_column {} ; # current commit column selected
4444
field highlight_commit {} ; # sha1 of commit selected
45-
field old_bgcolor {} ; # background of current selection
4645

4746
field total_lines 0 ; # total length of file
4847
field blame_lines 0 ; # number of lines computed
49-
field have_commit ; # array commit -> 1
5048
field amov_data ; # list of {commit origfile origline}
5149
field asim_data ; # list of {commit origfile origline}
5250

@@ -62,6 +60,8 @@ field tooltip_commit {} ; # Commit(s) in tooltip
6260

6361
constructor new {i_commit i_path} {
6462
global cursor_ptr
63+
variable active_color
64+
variable group_colors
6565

6666
set commit $i_commit
6767
set path $i_path
@@ -250,6 +250,10 @@ constructor new {i_commit i_path} {
250250
-command [cb _copycommit]
251251

252252
foreach i $w_columns {
253+
for {set g 0} {$g < [llength $group_colors]} {incr g} {
254+
$i tag conf color$g -background [lindex $group_colors $g]
255+
}
256+
253257
$i conf -cursor $cursor_ptr
254258
$i conf -yscrollcommand [list many2scrollbar \
255259
$w_columns yview $w.file_pane.out.sby]
@@ -314,6 +318,8 @@ constructor new {i_commit i_path} {
314318
}
315319

316320
method _load {jump} {
321+
variable group_colors
322+
317323
_hide_tooltip $this
318324

319325
if {$total_lines != 0 || $current_fd ne {}} {
@@ -325,8 +331,10 @@ method _load {jump} {
325331
foreach i $w_columns {
326332
$i conf -state normal
327333
$i delete 0.0 end
328-
foreach cmit [array names have_commit] {
329-
$i tag delete g$cmit
334+
foreach g [$i tag names] {
335+
if {[regexp {^g[0-9a-f]{40}$} $g]} {
336+
$i tag delete $g
337+
}
330338
}
331339
$i conf -state disabled
332340
}
@@ -339,7 +347,6 @@ method _load {jump} {
339347
set highlight_column {}
340348
set highlight_commit {}
341349
set total_lines 0
342-
array unset have_commit
343350
}
344351

345352
if {[winfo exists $w.status.c]} {
@@ -494,6 +501,7 @@ method _exec_blame {cur_w cur_d options cur_s} {
494501

495502
method _read_blame {fd cur_w cur_d cur_s} {
496503
upvar #0 $cur_d line_data
504+
variable group_colors
497505

498506
if {$fd ne $current_fd} {
499507
catch {close $fd}
@@ -508,16 +516,6 @@ method _read_blame {fd cur_w cur_d cur_s} {
508516
set r_orig_line $original_line
509517
set r_final_line $final_line
510518
set r_line_count $line_count
511-
512-
if {[catch {set g $have_commit($cmit)}]} {
513-
set bg [lindex $group_colors 0]
514-
set group_colors [lrange $group_colors 1 end]
515-
lappend group_colors $bg
516-
foreach i $w_columns {
517-
$i tag conf g$cmit -background $bg
518-
}
519-
set have_commit($cmit) 1
520-
}
521519
} elseif {[string match {filename *} $line]} {
522520
set file [string range $line 9 end]
523521
set n $r_line_count
@@ -563,6 +561,30 @@ method _read_blame {fd cur_w cur_d cur_s} {
563561
incr first_lno -1
564562
}
565563

564+
set color {}
565+
if {$first_lno < $lno} {
566+
foreach g [$w_file tag names $first_lno.0] {
567+
if {[regexp {^color[0-9]+$} $g]} {
568+
set color $g
569+
break
570+
}
571+
}
572+
} else {
573+
set i [lsort [concat \
574+
[$w_file tag names "[expr {$first_lno - 1}].0"] \
575+
[$w_file tag names "[expr {$lno + $n}].0"] \
576+
]]
577+
for {set g 0} {$g < [llength $group_colors]} {incr g} {
578+
if {[lsearch -sorted -exact $i color$g] == -1} {
579+
set color color$g
580+
break
581+
}
582+
}
583+
}
584+
if {$color eq {}} {
585+
set color color0
586+
}
587+
566588
while {$n > 0} {
567589
set lno_e "$lno.0 lineend + 1c"
568590
if {[lindex $line_data $lno] ne {}} {
@@ -583,6 +605,14 @@ method _read_blame {fd cur_w cur_d cur_s} {
583605
}
584606

585607
foreach i $w_columns {
608+
if {$cur_w eq $w_amov} {
609+
for {set g 0} \
610+
{$g < [llength $group_colors]} \
611+
{incr g} {
612+
$i tag remove color$g $lno.0 $lno_e
613+
}
614+
$i tag add $color $lno.0 $lno_e
615+
}
586616
$i tag add g$cmit $lno.0 $lno_e
587617
}
588618

@@ -616,6 +646,18 @@ method _read_blame {fd cur_w cur_d cur_s} {
616646
} else {
617647
$cur_w insert $lno.0 { |}
618648
}
649+
650+
if {$cur_w eq $w_amov} {
651+
foreach i $w_columns {
652+
for {set g 0} \
653+
{$g < [llength $group_colors]} \
654+
{incr g} {
655+
$i tag remove color$g $lno.0 $lno_e
656+
}
657+
$i tag add $color $lno.0 $lno_e
658+
}
659+
}
660+
619661
incr lno
620662
}
621663

@@ -678,10 +720,11 @@ method _load_commit {cur_w cur_d pos} {
678720

679721
method _showcommit {cur_w lno} {
680722
global repo_config
723+
variable active_color
681724

682725
if {$highlight_commit ne {}} {
683726
foreach i $w_columns {
684-
$i tag conf g$highlight_commit -background $old_bgcolor
727+
$i tag conf g$highlight_commit -background {}
685728
$i tag lower g$highlight_commit
686729
}
687730
}
@@ -704,7 +747,6 @@ method _showcommit {cur_w lno} {
704747
set cmit [lindex $dat 0]
705748
set file [lindex $dat 1]
706749

707-
set old_bgcolor [$w_file tag cget g$cmit -background]
708750
foreach i $w_columns {
709751
$i tag conf g$cmit -background $active_color
710752
$i tag raise g$cmit

0 commit comments

Comments
 (0)