2 # Copyright (C) 2006, 2007 Shawn Pearce
6 image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
8 # Persistant data (survives loads)
10 field history {}; # viewer history: {commit path}
11 field header ; # array commit,key -> header field
15 field w ; # top window in this viewer
16 field w_back ; # our back button
17 field w_path ; # label showing the current file path
18 field w_columns ; # list of all column widgets in the viewer
19 field w_line ; # text column: all line numbers
20 field w_cgrp ; # text column: abbreviated commit SHA-1s
21 field w_file ; # text column: actual file data
22 field w_cmit ; # pane showing commit message
23 field status ; # text variable bound to status bar
24 field old_height ; # last known height of $w.file_pane
28 field active_color #c0edc5
35 # Current blame data; cleared/reset on each load
37 field commit ; # input commit to blame
38 field path ; # input filename to view in $commit
40 field current_fd {} ; # background process running
41 field highlight_line -1 ; # current line selected
42 field highlight_commit {} ; # sha1 of commit selected
43 field old_bgcolor {} ; # background of current selection
45 field total_lines 0 ; # total length of file
46 field blame_lines 0 ; # number of lines computed
47 field have_commit ; # array commit -> 1
48 field line_commit ; # array line -> sha1 commit
49 field line_file ; # array line -> file name
51 field r_commit ; # commit currently being parsed
52 field r_orig_line ; # original line number
53 field r_final_line ; # final line number
54 field r_line_count ; # lines in this region
56 field tooltip_wm {} ; # Current tooltip toplevel, if open
57 field tooltip_timer {} ; # Current timer event for our tooltip
58 field tooltip_commit {} ; # Commit in tooltip
59 field tooltip_text {} ; # Text in current tooltip
61 constructor new {i_commit i_path} {
68 wm title $top "[appname] ([reponame]): File Viewer"
70 frame $w.header -background orange
71 label $w.header.commit_l \
76 set w_back $w.header.commit_b
78 -image ::blame::img_back_arrow \
83 -activebackground orange
84 bind $w_back <Button-1> "
85 if {\[$w_back cget -state\] eq {normal}} {
89 label $w.header.commit \
90 -textvariable @commit \
94 label $w.header.path_l \
99 set w_path $w.header.path
104 pack $w.header.commit_l -side left
105 pack $w_back -side left
106 pack $w.header.commit -side left
107 pack $w_path -fill x -side right
108 pack $w.header.path_l -side right
110 panedwindow $w.file_pane -orient vertical
111 frame $w.file_pane.out
112 frame $w.file_pane.cm
113 $w.file_pane add $w.file_pane.out \
118 $w.file_pane add $w.file_pane.cm \
124 set w_line $w.file_pane.out.linenumber_t
127 -highlightthickness 0 \
129 -background white -borderwidth 0 \
135 $w_line tag conf linenumber -justify right -rmargin 5
137 set w_cgrp $w.file_pane.out.commit_t
140 -highlightthickness 0 \
142 -background white -borderwidth 0 \
148 $w_cgrp tag conf curr_commit
149 $w_cgrp tag conf prior_commit \
152 $w_cgrp tag bind prior_commit \
154 "[cb _load_commit @%x,%y];break"
156 set w_file $w.file_pane.out.file_t
159 -highlightthickness 0 \
161 -background white -borderwidth 0 \
166 -xscrollcommand [list $w.file_pane.out.sbx set] \
169 set w_columns [list $w_cgrp $w_line $w_file]
171 scrollbar $w.file_pane.out.sbx \
173 -command [list $w_file xview]
174 scrollbar $w.file_pane.out.sby \
176 -command [list scrollbar2many $w_columns yview]
177 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
179 $w.file_pane.out.sbx \
180 -column [expr {[llength $w_columns] - 1}] \
182 grid columnconfigure \
184 [expr {[llength $w_columns] - 1}] \
186 grid rowconfigure $w.file_pane.out 0 -weight 1
188 set w_cmit $w.file_pane.cm.t
190 -background white -borderwidth 0 \
195 -xscrollcommand [list $w.file_pane.cm.sbx set] \
196 -yscrollcommand [list $w.file_pane.cm.sby set] \
198 $w_cmit tag conf header_key \
200 -background $active_color \
202 $w_cmit tag conf header_val \
203 -background $active_color \
205 $w_cmit tag raise sel
206 scrollbar $w.file_pane.cm.sbx \
208 -command [list $w_cmit xview]
209 scrollbar $w.file_pane.cm.sby \
211 -command [list $w_cmit yview]
212 pack $w.file_pane.cm.sby -side right -fill y
213 pack $w.file_pane.cm.sbx -side bottom -fill x
214 pack $w_cmit -expand 1 -fill both
220 -textvariable @status \
223 pack $w.status.l -side left
225 menu $w.ctxm -tearoff 0
226 $w.ctxm add command \
227 -label "Copy Commit" \
228 -command [cb _copycommit]
230 foreach i $w_columns {
231 $i conf -cursor $cursor_ptr
232 $i conf -yscrollcommand [list many2scrollbar \
233 $w_columns yview $w.file_pane.out.sby]
236 [cb _click $i @%x,%y]
239 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
240 bind $i <Any-Enter> [cb _hide_tooltip]
241 bind $i <Any-Leave> [cb _hide_tooltip]
247 tk_popup $w.ctxm %X %Y
251 foreach i [concat $w_columns $w_cmit] {
252 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
253 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
254 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
255 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
256 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
257 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
258 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
259 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
260 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
261 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
264 bind $w_cmit <Button-1> [list focus $w_cmit]
265 bind $top <Visibility> [list focus $top]
266 bind $w_file <Destroy> [list delete_this $this]
268 grid configure $w.header -sticky ew
269 grid configure $w.file_pane -sticky nsew
270 grid configure $w.status -sticky ew
271 grid columnconfigure $top 0 -weight 1
272 grid rowconfigure $top 0 -weight 0
273 grid rowconfigure $top 1 -weight 1
274 grid rowconfigure $top 2 -weight 0
276 set req_w [winfo reqwidth $top]
277 set req_h [winfo reqheight $top]
278 if {$req_w < 600} {set req_w 600}
279 if {$req_h < 400} {set req_h 400}
280 set g "${req_w}x${req_h}"
284 set old_height [winfo height $w.file_pane]
285 $w.file_pane sash place 0 \
286 [lindex [$w.file_pane sash coord 0] 0] \
287 [expr {int($old_height * 0.70)}]
288 bind $w.file_pane <Configure> \
289 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
297 if {$total_lines != 0 || $current_fd ne {}} {
298 if {$current_fd ne {}} {
299 catch {close $current_fd}
303 foreach i $w_columns {
304 $i conf -state normal
306 foreach cmit [array names have_commit] {
309 $i conf -state disabled
312 set highlight_line -1
313 set highlight_commit {}
316 array unset have_commit
317 array unset line_commit
318 array unset line_file
321 if {[winfo exists $w.status.c]} {
322 $w.status.c coords bar 0 0 0 20
326 -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
330 $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
331 pack $w.status.c -side right
334 if {$history eq {}} {
335 $w_back conf -state disabled
337 $w_back conf -state normal
339 lappend history [list $commit $path]
341 set status "Loading $commit:[escape_path $path]..."
342 $w_path conf -text [escape_path $path]
344 set fd [open $path r]
346 set cmd [list git cat-file blob "$commit:$path"]
347 set fd [open "| $cmd" r]
349 fconfigure $fd -blocking 0 -translation lf -encoding binary
350 fileevent $fd readable [cb _read_file $fd]
354 method _history_menu {} {
356 if {[winfo exists $m]} {
362 for {set i [expr {[llength $history] - 2}]
363 } {$i >= 0} {incr i -1} {
364 set e [lindex $history $i]
368 if {[regexp {^[0-9a-f]{40}$} $c]} {
369 set t [string range $c 0 8]...
373 if {![catch {set summary $header($c,summary)}]} {
375 if {[string length $t] > 70} {
376 set t [string range $t 0 66]...
380 $m add command -label $t -command [cb _goback $i $c $f]
382 set X [winfo rootx $w_back]
383 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
387 method _goback {i c f} {
388 set history [lrange $history 0 [expr {$i - 1}]]
394 method _read_file {fd} {
395 if {$fd ne $current_fd} {
400 foreach i $w_columns {$i conf -state normal}
401 while {[gets $fd line] >= 0} {
402 regsub "\r\$" $line {} line
405 if {$total_lines > 1} {
406 foreach i $w_columns {$i insert end "\n"}
409 $w_line insert end "$total_lines" linenumber
410 $w_file insert end "$line"
413 set ln_wc [expr {[string length $total_lines] + 2}]
414 if {[$w_line cget -width] < $ln_wc} {
415 $w_line conf -width $ln_wc
418 foreach i $w_columns {$i conf -state disabled}
424 set cmd {nice git blame -M -C --incremental}
426 lappend cmd --contents $path
431 set fd [open "| $cmd" r]
432 fconfigure $fd -blocking 0 -translation lf -encoding binary
433 fileevent $fd readable [cb _read_blame $fd]
436 } ifdeleted { catch {close $fd} }
438 method _read_blame {fd} {
439 if {$fd ne $current_fd} {
444 $w_cgrp conf -state normal
445 while {[gets $fd line] >= 0} {
446 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
447 cmit original_line final_line line_count]} {
449 set r_orig_line $original_line
450 set r_final_line $final_line
451 set r_line_count $line_count
453 if {[catch {set g $have_commit($cmit)}]} {
454 set bg [lindex $group_colors 0]
455 set group_colors [lrange $group_colors 1 end]
456 lappend group_colors $bg
457 foreach i $w_columns {
458 $i tag conf g$cmit -background $bg
460 set have_commit($cmit) 1
462 } elseif {[string match {filename *} $line]} {
463 set file [string range $line 9 end]
465 set lno $r_final_line
468 if {[regexp {^0{40}$} $cmit]} {
470 set commit_type curr_commit
471 } elseif {$cmit eq $commit} {
473 set commit_type curr_commit
475 set commit_type prior_commit
476 set commit_abbr [string range $cmit 0 4]
481 catch {set a_name $header($cmit,author)}
482 while {$a_name ne {}} {
483 if {![regexp {^([[:upper:]])} $a_name _a]} break
484 append author_abbr $_a
487 {^[[:upper:]][^\s]*\s+} \
488 $a_name {} a_name ]} break
490 if {$author_abbr eq {}} {
493 set author_abbr [string range $author_abbr 0 3]
494 while {[string length $author_abbr] < 4} {
495 set author_abbr " $author_abbr"
502 ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
503 && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
511 set lno_e "$lno.0 lineend + 1c"
512 if {![catch {set g g$line_commit($lno)}]} {
513 foreach i $w_columns {
514 $i tag remove g$g $lno.0 $lno_e
518 set line_commit($lno) $cmit
519 set line_file($lno) $file
521 $w_cgrp delete $lno.0 "$lno.0 lineend"
522 if {$lno == $first_lno} {
523 $w_cgrp insert $lno.0 $commit_abbr $commit_type
524 } elseif {$lno == [expr {$first_lno + 1}]} {
525 $w_cgrp insert $lno.0 $author_abbr
527 $w_cgrp insert $lno.0 { |}
530 foreach i $w_columns {
531 $i tag add g$cmit $lno.0 $lno_e
534 if {$highlight_line == -1} {
535 if {[lindex [$w_file yview] 0] == 0} {
537 _showcommit $this $lno
539 } elseif {$highlight_line == $lno} {
540 _showcommit $this $lno
549 ![catch {set ncmit $line_commit($lno)}]
550 && ![catch {set nfile $line_file($lno)}]
554 $w_cgrp delete $lno.0 "$lno.0 lineend"
556 if {$lno == $first_lno} {
557 $w_cgrp insert $lno.0 $commit_abbr $commit_type
558 } elseif {$lno == [expr {$first_lno + 1}]} {
559 $w_cgrp insert $lno.0 $author_abbr
561 $w_cgrp insert $lno.0 { |}
566 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
567 set header($r_commit,$key) $data
570 $w_cgrp conf -state disabled
575 set status {Annotation complete.}
580 } ifdeleted { catch {close $fd} }
583 set have $blame_lines
584 set total $total_lines
586 if {$total} {set pdone [expr {100 * $have / $total}]}
589 "Loading annotations... %i of %i lines annotated (%2i%%)" \
591 $w.status.c coords bar 0 0 $pdone 20
594 method _click {cur_w pos} {
595 set lno [lindex [split [$cur_w index $pos] .] 0]
596 if {$lno eq {}} return
597 _showcommit $this $lno
600 method _load_commit {pos} {
601 set lno [lindex [split [$w_cgrp index $pos] .] 0]
602 if {[catch {set cmit $line_commit($lno)}]} return
603 if {[catch {set file $line_file($lno) }]} return
610 method _showcommit {lno} {
613 if {$highlight_commit ne {}} {
614 foreach i $w_columns {
615 $i tag conf g$highlight_commit -background $old_bgcolor
619 $w_cmit conf -state normal
620 $w_cmit delete 0.0 end
621 if {[catch {set cmit $line_commit($lno)}]} {
623 $w_cmit insert end "Loading annotation..."
625 set old_bgcolor [$w_file tag cget g$cmit -background]
626 foreach i $w_columns {
627 $i tag conf g$cmit -background $active_color
633 catch {set author_name $header($cmit,author)}
634 catch {set author_email $header($cmit,author-mail)}
635 catch {set author_time [clock format \
636 $header($cmit,author-time) \
637 -format {%Y-%m-%d %H:%M:%S}
640 set committer_name {}
641 set committer_email {}
642 set committer_time {}
643 catch {set committer_name $header($cmit,committer)}
644 catch {set committer_email $header($cmit,committer-mail)}
645 catch {set committer_time [clock format \
646 $header($cmit,committer-time) \
647 -format {%Y-%m-%d %H:%M:%S}
650 if {[catch {set msg $header($cmit,message)}]} {
653 set fd [open "| git cat-file commit $cmit" r]
654 fconfigure $fd -encoding binary -translation lf
655 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
658 while {[gets $fd line] > 0} {
659 if {[string match {encoding *} $line]} {
660 set enc [string tolower [string range $line 9 end]]
663 set msg [encoding convertfrom $enc [read $fd]]
664 set msg [string trim $msg]
667 set author_name [encoding convertfrom $enc $author_name]
668 set committer_name [encoding convertfrom $enc $committer_name]
670 set header($cmit,author) $author_name
671 set header($cmit,committer) $committer_name
673 set header($cmit,message) $msg
676 $w_cmit insert end "commit $cmit\n" header_key
677 $w_cmit insert end "Author:\t" header_key
678 $w_cmit insert end "$author_name $author_email" header_val
679 $w_cmit insert end "$author_time\n" header_val
681 $w_cmit insert end "Committer:\t" header_key
682 $w_cmit insert end "$committer_name $committer_email" header_val
683 $w_cmit insert end "$committer_time\n" header_val
685 if {$line_file($lno) ne $path} {
686 $w_cmit insert end "Original File:\t" header_key
687 $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
690 $w_cmit insert end "\n$msg"
692 $w_cmit conf -state disabled
694 set highlight_line $lno
695 set highlight_commit $cmit
697 if {$highlight_commit eq $tooltip_commit} {
702 method _copycommit {} {
703 set pos @$::cursorX,$::cursorY
704 set lno [lindex [split [$::cursorW index $pos] .] 0]
705 if {![catch {set commit $line_commit($lno)}]} {
714 method _show_tooltip {cur_w pos} {
715 set lno [lindex [split [$cur_w index $pos] .] 0]
716 if {[catch {set cmit $line_commit($lno)}]} {
721 if {$cmit eq $highlight_commit} {
726 if {$cmit eq $tooltip_commit} {
727 _position_tooltip $this
728 } elseif {$tooltip_wm ne {}} {
729 _open_tooltip $this $cur_w
730 } elseif {$tooltip_timer eq {}} {
731 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
735 method _open_tooltip {cur_w} {
737 set pos_x [winfo pointerx $cur_w]
738 set pos_y [winfo pointery $cur_w]
739 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
744 set pos @[join [list \
745 [expr {$pos_x - [winfo rootx $cur_w]}] \
746 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
747 set lno [lindex [split [$cur_w index $pos] .] 0]
748 set cmit $line_commit($lno)
753 catch {set author_name $header($cmit,author)}
754 catch {set author_email $header($cmit,author-mail)}
755 catch {set author_time [clock format \
756 $header($cmit,author-time) \
757 -format {%Y-%m-%d %H:%M:%S}
760 set committer_name {}
761 set committer_email {}
762 set committer_time {}
763 catch {set committer_name $header($cmit,committer)}
764 catch {set committer_email $header($cmit,committer-mail)}
765 catch {set committer_time [clock format \
766 $header($cmit,committer-time) \
767 -format {%Y-%m-%d %H:%M:%S}
771 catch {set summary $header($cmit,summary)}
773 set tooltip_commit $cmit
774 set tooltip_text "commit $cmit
775 $author_name $author_email $author_time
778 set file $line_file($lno)
779 if {$file ne $path} {
780 append tooltip_text "
782 Original File: $file"
785 if {$tooltip_wm ne "$cur_w.tooltip"} {
788 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
789 wm overrideredirect $tooltip_wm 1
790 wm transient $tooltip_wm [winfo toplevel $cur_w]
791 pack [label $tooltip_wm.label \
792 -background lightyellow \
794 -textvariable @tooltip_text \
797 _position_tooltip $this
800 method _position_tooltip {} {
801 set req_w [winfo reqwidth $tooltip_wm.label]
802 set req_h [winfo reqheight $tooltip_wm.label]
803 set pos_x [expr {[winfo pointerx .] + 5}]
804 set pos_y [expr {[winfo pointery .] + 10}]
806 set g "${req_w}x${req_h}"
807 if {$pos_x >= 0} {append g +}
809 if {$pos_y >= 0} {append g +}
812 wm geometry $tooltip_wm $g
816 method _hide_tooltip {} {
817 if {$tooltip_wm ne {}} {
820 set tooltip_commit {}
822 if {$tooltip_timer ne {}} {
823 after cancel $tooltip_timer
828 method _resize {new_height} {
829 set diff [expr {$new_height - $old_height}]
830 if {$diff == 0} return
832 set my [expr {[winfo height $w.file_pane] - 25}]
833 set o [$w.file_pane sash coord 0]
835 set oy [expr {[lindex $o 1] + $diff}]
836 if {$oy < 0} {set oy 0}
837 if {$oy > $my} {set oy $my}
838 $w.file_pane sash place 0 $ox $oy
840 set old_height $new_height