Code

f15e5a07237c70bb3c509af67b5f9672919e8fa2
[git.git] / lib / blame.tcl
1 # git-gui blame viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class blame {
6 field commit  ; # input commit to blame
7 field path    ; # input filename to view in $commit
9 field w
10 field w_line
11 field w_cgrp
12 field w_load
13 field w_file
14 field w_cmit
15 field status
16 field old_height
18 field highlight_line   -1 ; # current line selected
19 field highlight_commit {} ; # sha1 of commit selected
21 field total_lines       0  ; # total length of file
22 field blame_lines       0  ; # number of lines computed
23 field commit_count      0  ; # number of commits in $commit_list
24 field commit_list      {}  ; # list of commit sha1 in receipt order
25 field order                ; # array commit -> receipt order
26 field header               ; # array commit,key -> header field
27 field line_commit          ; # array line -> sha1 commit
28 field line_file            ; # array line -> file name
30 field r_commit      ; # commit currently being parsed
31 field r_orig_line   ; # original line number
32 field r_final_line  ; # final line number
33 field r_line_count  ; # lines in this region
35 field tooltip_wm     {} ; # Current tooltip toplevel, if open
36 field tooltip_timer  {} ; # Current timer event for our tooltip
37 field tooltip_commit {} ; # Commit in tooltip
38 field tooltip_text   {} ; # Text in current tooltip
40 variable active_color #98e1a0
41 variable group_colors {
42         #cbcbcb
43         #e1e1e1
44 }
46 constructor new {i_commit i_path} {
47         variable active_color
48         global cursor_ptr
50         set commit $i_commit
51         set path   $i_path
53         make_toplevel top w
54         wm title $top "[appname] ([reponame]): File Viewer"
55         set status "Loading $commit:$path..."
57         label $w.path -text "$commit:$path" \
58                 -anchor w \
59                 -justify left \
60                 -borderwidth 1 \
61                 -relief sunken \
62                 -font font_uibold
64         panedwindow $w.file_pane -orient vertical
65         frame $w.file_pane.out
66         frame $w.file_pane.cm
67         $w.file_pane add $w.file_pane.out \
68                 -sticky nsew \
69                 -minsize 100 \
70                 -height 100 \
71                 -width 100
72         $w.file_pane add $w.file_pane.cm \
73                 -sticky nsew \
74                 -minsize 25 \
75                 -height 25 \
76                 -width 100
78         set w_load $w.file_pane.out.loaded_t
79         text $w_load \
80                 -background white -borderwidth 0 \
81                 -state disabled \
82                 -wrap none \
83                 -height 40 \
84                 -width 1 \
85                 -font font_diff
86         $w_load tag conf annotated -background grey
88         set w_line $w.file_pane.out.linenumber_t
89         text $w_line \
90                 -background white -borderwidth 0 \
91                 -state disabled \
92                 -wrap none \
93                 -height 40 \
94                 -width 5 \
95                 -font font_diff
96         $w_line tag conf linenumber -justify right
98         set w_cgrp $w.file_pane.out.commit_t
99         text $w_cgrp \
100                 -background white -borderwidth 0 \
101                 -state disabled \
102                 -wrap none \
103                 -height 40 \
104                 -width 4 \
105                 -font font_diff
107         set w_file $w.file_pane.out.file_t
108         text $w_file \
109                 -background white -borderwidth 0 \
110                 -state disabled \
111                 -wrap none \
112                 -height 40 \
113                 -width 80 \
114                 -xscrollcommand [list $w.file_pane.out.sbx set] \
115                 -font font_diff
117         scrollbar $w.file_pane.out.sbx \
118                 -orient h \
119                 -command [list $w_file xview]
120         scrollbar $w.file_pane.out.sby \
121                 -orient v \
122                 -command [list scrollbar2many [list \
123                 $w_load \
124                 $w_line \
125                 $w_cgrp \
126                 $w_file \
127                 ] yview]
128         grid \
129                 $w_cgrp \
130                 $w_line \
131                 $w_load \
132                 $w_file \
133                 $w.file_pane.out.sby \
134                 -sticky nsew
135         grid conf $w.file_pane.out.sbx -column 3 -sticky we
136         grid columnconfigure $w.file_pane.out 3 -weight 1
137         grid rowconfigure $w.file_pane.out 0 -weight 1
139         label $w.status \
140                 -textvariable @status \
141                 -anchor w \
142                 -justify left \
143                 -borderwidth 1 \
144                 -relief sunken
146         set w_cmit $w.file_pane.cm.t
147         text $w_cmit \
148                 -background white -borderwidth 0 \
149                 -state disabled \
150                 -wrap none \
151                 -height 10 \
152                 -width 80 \
153                 -xscrollcommand [list $w.file_pane.cm.sbx set] \
154                 -yscrollcommand [list $w.file_pane.cm.sby set] \
155                 -font font_diff
156         $w_cmit tag conf header_key \
157                 -tabs {3c} \
158                 -background $active_color \
159                 -font font_uibold
160         $w_cmit tag conf header_val \
161                 -background $active_color \
162                 -font font_ui
163         $w_cmit tag raise sel
164         scrollbar $w.file_pane.cm.sbx \
165                 -orient h \
166                 -command [list $w_cmit xview]
167         scrollbar $w.file_pane.cm.sby \
168                 -orient v \
169                 -command [list $w_cmit yview]
170         pack $w.file_pane.cm.sby -side right -fill y
171         pack $w.file_pane.cm.sbx -side bottom -fill x
172         pack $w_cmit -expand 1 -fill both
174         menu $w.ctxm -tearoff 0
175         $w.ctxm add command \
176                 -label "Copy Commit" \
177                 -command [cb _copycommit]
179         foreach i [list \
180                 $w_cgrp \
181                 $w_load \
182                 $w_line \
183                 $w_file] {
184                 $i conf -cursor $cursor_ptr
185                 $i conf -yscrollcommand \
186                         [list many2scrollbar [list \
187                         $w_cgrp \
188                         $w_load \
189                         $w_line \
190                         $w_file \
191                         ] yview $w.file_pane.out.sby]
192                 bind $i <Button-1> "
193                         [cb _hide_tooltip]
194                         [cb _click $i @%x,%y]
195                         focus $i
196                 "
197                 bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
198                 bind $i <Any-Enter>   [cb _hide_tooltip]
199                 bind $i <Any-Leave>   [cb _hide_tooltip]
200                 bind_button3 $i "
201                         [cb _hide_tooltip]
202                         set cursorX %x
203                         set cursorY %y
204                         set cursorW %W
205                         tk_popup $w.ctxm %X %Y
206                 "
207         }
209         foreach i [list \
210                 $w_cgrp \
211                 $w_load \
212                 $w_line \
213                 $w_file \
214                 $w_cmit] {
215                 bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
216                 bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
217                 bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
218                 bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
219                 bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
220                 bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
221                 bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
222                 bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
223                 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
224                 bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
225         }
227         bind $w_cmit <Button-1> [list focus $w_cmit]
228         bind $top <Visibility> [list focus $top]
229         bind $top <Destroy> [list delete_this $this]
231         grid configure $w.path -sticky ew
232         grid configure $w.file_pane -sticky nsew
233         grid configure $w.status -sticky ew
234         grid columnconfigure $top 0 -weight 1
235         grid rowconfigure $top 0 -weight 0
236         grid rowconfigure $top 1 -weight 1
237         grid rowconfigure $top 2 -weight 0
239         set req_w [winfo reqwidth  $top]
240         set req_h [winfo reqheight $top]
241         if {$req_w < 600} {set req_w 600}
242         if {$req_h < 400} {set req_h 400}
243         set g "${req_w}x${req_h}"
244         wm geometry $top $g
245         update
247         set old_height [winfo height $w.file_pane]
248         $w.file_pane sash place 0 \
249                 [lindex [$w.file_pane sash coord 0] 0] \
250                 [expr {int($old_height * 0.70)}]
251         bind $w.file_pane <Configure> \
252         "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
254         if {$commit eq {}} {
255                 set fd [open $path r]
256         } else {
257                 set cmd [list git cat-file blob "$commit:$path"]
258                 set fd [open "| $cmd" r]
259         }
260         fconfigure $fd -blocking 0 -translation lf -encoding binary
261         fileevent $fd readable [cb _read_file $fd]
264 method _read_file {fd} {
265         $w_load conf -state normal
266         $w_cgrp conf -state normal
267         $w_line conf -state normal
268         $w_file conf -state normal
269         while {[gets $fd line] >= 0} {
270                 regsub "\r\$" $line {} line
271                 incr total_lines
273                 if {$total_lines > 1} {
274                         $w_load insert end "\n"
275                         $w_cgrp insert end "\n"
276                         $w_line insert end "\n"
277                         $w_file insert end "\n"
278                 }
280                 $w_line insert end "$total_lines" linenumber
281                 $w_file insert end "$line"
282         }
283         $w_load conf -state disabled
284         $w_cgrp conf -state disabled
285         $w_line conf -state disabled
286         $w_file conf -state disabled
288         if {[eof $fd]} {
289                 close $fd
290                 _status $this
291                 set cmd [list git blame -M -C --incremental]
292                 if {$commit eq {}} {
293                         lappend cmd --contents $path
294                 } else {
295                         lappend cmd $commit
296                 }
297                 lappend cmd -- $path
298                 set fd [open "| $cmd" r]
299                 fconfigure $fd -blocking 0 -translation lf -encoding binary
300                 fileevent $fd readable [cb _read_blame $fd]
301         }
302 } ifdeleted { catch {close $fd} }
304 method _read_blame {fd} {
305         variable group_colors
307         $w_cgrp conf -state normal
308         while {[gets $fd line] >= 0} {
309                 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
310                         cmit original_line final_line line_count]} {
311                         set r_commit     $cmit
312                         set r_orig_line  $original_line
313                         set r_final_line $final_line
314                         set r_line_count $line_count
316                         if {[catch {set g $order($cmit)}]} {
317                                 set bg [lindex $group_colors 0]
318                                 set group_colors [lrange $group_colors 1 end]
319                                 lappend group_colors $bg
321                                 $w_cgrp tag conf g$cmit -background $bg
322                                 $w_line tag conf g$cmit -background $bg
323                                 $w_file tag conf g$cmit -background $bg
325                                 set order($cmit) $commit_count
326                                 incr commit_count
327                                 lappend commit_list $cmit
328                         }
329                 } elseif {[string match {filename *} $line]} {
330                         set file [string range $line 9 end]
331                         set n    $r_line_count
332                         set lno  $r_final_line
333                         set cmit $r_commit
335                         if {[regexp {^0{40}$} $cmit]} {
336                                 set commit_abbr work
337                         } else {
338                                 set commit_abbr [string range $cmit 0 4]
339                         }
341                         set author_abbr {}
342                         set a_name {}
343                         catch {set a_name $header($cmit,author)}
344                         while {$a_name ne {}} {
345                                 if {![regexp {^([[:upper:]])} $a_name _a]} break
346                                 append author_abbr $_a
347                                 unset _a
348                                 if {![regsub \
349                                         {^[[:upper:]][^\s]*\s+} \
350                                         $a_name {} a_name ]} break
351                         }
352                         if {$author_abbr eq {}} {
353                                 set author_abbr { |}
354                         } else {
355                                 set author_abbr [string range $author_abbr 0 3]
356                                 while {[string length $author_abbr] < 4} {
357                                         set author_abbr " $author_abbr"
358                                 }
359                         }
360                         unset a_name
362                         set first_lno $lno
363                         while {
364                         ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
365                         && $ncmit eq $cmit
366                         } {
367                                 incr first_lno -1
368                         }
370                         while {$n > 0} {
371                                 set lno_e "$lno.0 lineend + 1c"
372                                 if {[catch {set g g$line_commit($lno)}]} {
373                                         $w_load tag add annotated $lno.0 $lno_e
374                                 } else {
375                                         $w_cgrp tag remove g$g $lno.0 $lno_e
376                                         $w_line tag remove g$g $lno.0 $lno_e
377                                         $w_file tag remove g$g $lno.0 $lno_e
379                                         $w_cgrp tag remove a$g $lno.0 $lno_e
380                                         $w_line tag remove a$g $lno.0 $lno_e
381                                         $w_file tag remove a$g $lno.0 $lno_e
382                                 }
384                                 set line_commit($lno) $cmit
385                                 set line_file($lno)   $file
387                                 $w_cgrp delete $lno.0 "$lno.0 lineend"
388                                 if {$lno == $first_lno} {
389                                         $w_cgrp insert $lno.0 $commit_abbr
390                                 } elseif {$lno == [expr {$first_lno + 1}]} {
391                                         $w_cgrp insert $lno.0 $author_abbr
392                                 } else {
393                                         $w_cgrp insert $lno.0 { |}
394                                 }
396                                 $w_cgrp tag add g$cmit $lno.0 $lno_e
397                                 $w_line tag add g$cmit $lno.0 $lno_e
398                                 $w_file tag add g$cmit $lno.0 $lno_e
400                                 $w_cgrp tag add a$cmit $lno.0 $lno_e
401                                 $w_line tag add a$cmit $lno.0 $lno_e
402                                 $w_file tag add a$cmit $lno.0 $lno_e
404                                 if {$highlight_line == -1} {
405                                         if {[lindex [$w_file yview] 0] == 0} {
406                                                 $w_file see $lno.0
407                                                 _showcommit $this $lno
408                                         }
409                                 } elseif {$highlight_line == $lno} {
410                                         _showcommit $this $lno
411                                 }
413                                 incr n -1
414                                 incr lno
415                                 incr blame_lines
416                         }
418                         while {![catch {set ncmit $line_commit($lno)}]
419                                 && $ncmit eq $cmit} {
420                                 $w_cgrp delete $lno.0 "$lno.0 lineend"
422                                 if {$lno == $first_lno} {
423                                         $w_cgrp insert $lno.0 $commit_abbr
424                                 } elseif {$lno == [expr {$first_lno + 1}]} {
425                                         $w_cgrp insert $lno.0 $author_abbr
426                                 } else {
427                                         $w_cgrp insert $lno.0 { |}
428                                 }
429                                 incr lno
430                         }
432                 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
433                         set header($r_commit,$key) $data
434                 }
435         }
436         $w_cgrp conf -state disabled
438         if {[eof $fd]} {
439                 close $fd
440                 set status {Annotation complete.}
441         } else {
442                 _status $this
443         }
444 } ifdeleted { catch {close $fd} }
446 method _status {} {
447         set have  $blame_lines
448         set total $total_lines
449         set pdone 0
450         if {$total} {set pdone [expr {100 * $have / $total}]}
452         set status [format \
453                 "Loading annotations... %i of %i lines annotated (%2i%%)" \
454                 $have $total $pdone]
457 method _click {cur_w pos} {
458         set lno [lindex [split [$cur_w index $pos] .] 0]
459         if {$lno eq {}} return
460         _showcommit $this $lno
463 method _showcommit {lno} {
464         global repo_config
465         variable active_color
467         if {$highlight_commit ne {}} {
468                 set cmit $highlight_commit
469                 $w_cgrp tag conf a$cmit -background {}
470                 $w_line tag conf a$cmit -background {}
471                 $w_file tag conf a$cmit -background {}
472         }
474         $w_cmit conf -state normal
475         $w_cmit delete 0.0 end
476         if {[catch {set cmit $line_commit($lno)}]} {
477                 set cmit {}
478                 $w_cmit insert end "Loading annotation..."
479         } else {
480                 $w_cgrp tag conf a$cmit -background $active_color
481                 $w_line tag conf a$cmit -background $active_color
482                 $w_file tag conf a$cmit -background $active_color
484                 set author_name {}
485                 set author_email {}
486                 set author_time {}
487                 catch {set author_name $header($cmit,author)}
488                 catch {set author_email $header($cmit,author-mail)}
489                 catch {set author_time [clock format \
490                         $header($cmit,author-time) \
491                         -format {%Y-%m-%d %H:%M:%S}
492                 ]}
494                 set committer_name {}
495                 set committer_email {}
496                 set committer_time {}
497                 catch {set committer_name $header($cmit,committer)}
498                 catch {set committer_email $header($cmit,committer-mail)}
499                 catch {set committer_time [clock format \
500                         $header($cmit,committer-time) \
501                         -format {%Y-%m-%d %H:%M:%S}
502                 ]}
504                 if {[catch {set msg $header($cmit,message)}]} {
505                         set msg {}
506                         catch {
507                                 set fd [open "| git cat-file commit $cmit" r]
508                                 fconfigure $fd -encoding binary -translation lf
509                                 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
510                                         set enc utf-8
511                                 }
512                                 while {[gets $fd line] > 0} {
513                                         if {[string match {encoding *} $line]} {
514                                                 set enc [string tolower [string range $line 9 end]]
515                                         }
516                                 }
517                                 set msg [encoding convertfrom $enc [read $fd]]
518                                 set msg [string trim $msg]
519                                 close $fd
521                                 set author_name [encoding convertfrom $enc $author_name]
522                                 set committer_name [encoding convertfrom $enc $committer_name]
524                                 set header($cmit,author) $author_name
525                                 set header($cmit,committer) $committer_name
526                         }
527                         set header($cmit,message) $msg
528                 }
530                 $w_cmit insert end "commit $cmit\n" header_key
531                 $w_cmit insert end "Author:\t" header_key
532                 $w_cmit insert end "$author_name $author_email" header_val
533                 $w_cmit insert end "$author_time\n" header_val
535                 $w_cmit insert end "Committer:\t" header_key
536                 $w_cmit insert end "$committer_name $committer_email" header_val
537                 $w_cmit insert end "$committer_time\n" header_val
539                 if {$line_file($lno) ne $path} {
540                         $w_cmit insert end "Original File:\t" header_key
541                         $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
542                 }
544                 $w_cmit insert end "\n$msg"
545         }
546         $w_cmit conf -state disabled
548         set highlight_line $lno
549         set highlight_commit $cmit
551         if {$highlight_commit eq $tooltip_commit} {
552                 _hide_tooltip $this
553         }
556 method _copycommit {} {
557         set pos @$::cursorX,$::cursorY
558         set lno [lindex [split [$::cursorW index $pos] .] 0]
559         if {![catch {set commit $line_commit($lno)}]} {
560                 clipboard clear
561                 clipboard append \
562                         -format STRING \
563                         -type STRING \
564                         -- $commit
565         }
568 method _show_tooltip {cur_w pos} {
569         set lno [lindex [split [$cur_w index $pos] .] 0]
570         if {[catch {set cmit $line_commit($lno)}]} {
571                 _hide_tooltip $this
572                 return
573         }
575         if {$cmit eq $highlight_commit} {
576                 _hide_tooltip $this
577                 return
578         }
580         if {$cmit eq $tooltip_commit} {
581                 _position_tooltip $this
582         } elseif {$tooltip_wm ne {}} {
583                 _open_tooltip $this $cur_w
584         } elseif {$tooltip_timer eq {}} {
585                 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
586         }
589 method _open_tooltip {cur_w} {
590         set tooltip_timer {}
591         set pos_x [winfo pointerx $cur_w]
592         set pos_y [winfo pointery $cur_w]
593         if {[winfo containing $pos_x $pos_y] ne $cur_w} {
594                 _hide_tooltip $this
595                 return
596         }
598         set pos @[join [list \
599                 [expr {$pos_x - [winfo rootx $cur_w]}] \
600                 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
601         set lno [lindex [split [$cur_w index $pos] .] 0]
602         set cmit $line_commit($lno)
604         set author_name {}
605         set author_email {}
606         set author_time {}
607         catch {set author_name $header($cmit,author)}
608         catch {set author_email $header($cmit,author-mail)}
609         catch {set author_time [clock format \
610                 $header($cmit,author-time) \
611                 -format {%Y-%m-%d %H:%M:%S}
612         ]}
614         set committer_name {}
615         set committer_email {}
616         set committer_time {}
617         catch {set committer_name $header($cmit,committer)}
618         catch {set committer_email $header($cmit,committer-mail)}
619         catch {set committer_time [clock format \
620                 $header($cmit,committer-time) \
621                 -format {%Y-%m-%d %H:%M:%S}
622         ]}
624         set summary {}
625         catch {set summary $header($cmit,summary)}
627         set tooltip_commit $cmit
628         set tooltip_text "commit $cmit
629 $author_name $author_email  $author_time
630 $summary"
632         if {$tooltip_wm ne "$cur_w.tooltip"} {
633                 _hide_tooltip $this
635                 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
636                 wm overrideredirect $tooltip_wm 1
637                 wm transient $tooltip_wm [winfo toplevel $cur_w]
638                 pack [label $tooltip_wm.label \
639                         -background lightyellow \
640                         -foreground black \
641                         -textvariable @tooltip_text \
642                         -justify left]
643         }
644         _position_tooltip $this
647 method _position_tooltip {} {
648         set req_w [winfo reqwidth  $tooltip_wm.label]
649         set req_h [winfo reqheight $tooltip_wm.label]
650         set pos_x [expr {[winfo pointerx .] +  5}]
651         set pos_y [expr {[winfo pointery .] + 10}]
653         set g "${req_w}x${req_h}"
654         if {$pos_x >= 0} {append g +}
655         append g $pos_x
656         if {$pos_y >= 0} {append g +}
657         append g $pos_y
659         wm geometry $tooltip_wm $g
660         raise $tooltip_wm
663 method _hide_tooltip {} {
664         if {$tooltip_wm ne {}} {
665                 destroy $tooltip_wm
666                 set tooltip_wm {}
667                 set tooltip_commit {}
668         }
669         if {$tooltip_timer ne {}} {
670                 after cancel $tooltip_timer
671                 set tooltip_timer {}
672         }
675 method _resize {new_height} {
676         set diff [expr {$new_height - $old_height}]
677         if {$diff == 0} return
679         set my [expr {[winfo height $w.file_pane] - 25}]
680         set o [$w.file_pane sash coord 0]
681         set ox [lindex $o 0]
682         set oy [expr {[lindex $o 1] + $diff}]
683         if {$oy < 0}   {set oy 0}
684         if {$oy > $my} {set oy $my}
685         $w.file_pane sash place 0 $ox $oy
687         set old_height $new_height