2 # Copyright (C) 2006, 2007 Shawn Pearce
6 image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
8 field commit ; # input commit to blame
9 field path ; # input filename to view in $commit
10 field history {}; # viewer history: {commit path}
12 field w ; # top window in this viewer
13 field w_back ; # our back button
14 field w_path ; # label showing the current file path
15 field w_line ; # text column: all line numbers
16 field w_cgrp ; # text column: abbreviated commit SHA-1s
17 field w_load ; # text column: loaded indicator
18 field w_file ; # text column: actual file data
19 field w_cmit ; # pane showing commit message
20 field status ; # text variable bound to status bar
21 field old_height ; # last known height of $w.file_pane
23 field current_fd {} ; # background process running
24 field highlight_line -1 ; # current line selected
25 field highlight_commit {} ; # sha1 of commit selected
27 field total_lines 0 ; # total length of file
28 field blame_lines 0 ; # number of lines computed
29 field commit_count 0 ; # number of commits in $commit_list
30 field commit_list {} ; # list of commit sha1 in receipt order
31 field order ; # array commit -> receipt order
32 field header ; # array commit,key -> header field
33 field line_commit ; # array line -> sha1 commit
34 field line_file ; # array line -> file name
36 field r_commit ; # commit currently being parsed
37 field r_orig_line ; # original line number
38 field r_final_line ; # final line number
39 field r_line_count ; # lines in this region
41 field tooltip_wm {} ; # Current tooltip toplevel, if open
42 field tooltip_timer {} ; # Current timer event for our tooltip
43 field tooltip_commit {} ; # Commit in tooltip
44 field tooltip_text {} ; # Text in current tooltip
46 variable active_color #98e1a0
47 variable group_colors {
52 constructor new {i_commit i_path} {
60 wm title $top "[appname] ([reponame]): File Viewer"
62 frame $w.header -background orange
63 label $w.header.commit_l \
68 set w_back $w.header.commit_b
70 -command [cb _history_menu] \
71 -image ::blame::img_back_arrow \
76 -activebackground orange
77 label $w.header.commit \
78 -textvariable @commit \
82 label $w.header.path_l \
87 set w_path $w.header.path
92 pack $w.header.commit_l -side left
93 pack $w_back -side left
94 pack $w.header.commit -side left
95 pack $w_path -fill x -side right
96 pack $w.header.path_l -side right
98 panedwindow $w.file_pane -orient vertical
99 frame $w.file_pane.out
100 frame $w.file_pane.cm
101 $w.file_pane add $w.file_pane.out \
106 $w.file_pane add $w.file_pane.cm \
112 set w_load $w.file_pane.out.loaded_t
114 -background white -borderwidth 0 \
120 $w_load tag conf annotated -background grey
122 set w_line $w.file_pane.out.linenumber_t
124 -background white -borderwidth 0 \
130 $w_line tag conf linenumber -justify right
132 set w_cgrp $w.file_pane.out.commit_t
134 -background white -borderwidth 0 \
140 $w_cgrp tag conf curr_commit
141 $w_cgrp tag conf prior_commit \
144 $w_cgrp tag bind prior_commit \
146 "[cb _load_commit @%x,%y];break"
148 set w_file $w.file_pane.out.file_t
150 -background white -borderwidth 0 \
155 -xscrollcommand [list $w.file_pane.out.sbx set] \
158 scrollbar $w.file_pane.out.sbx \
160 -command [list $w_file xview]
161 scrollbar $w.file_pane.out.sby \
163 -command [list scrollbar2many [list \
174 $w.file_pane.out.sby \
176 grid conf $w.file_pane.out.sbx -column 3 -sticky we
177 grid columnconfigure $w.file_pane.out 3 -weight 1
178 grid rowconfigure $w.file_pane.out 0 -weight 1
180 set w_cmit $w.file_pane.cm.t
182 -background white -borderwidth 0 \
187 -xscrollcommand [list $w.file_pane.cm.sbx set] \
188 -yscrollcommand [list $w.file_pane.cm.sby set] \
190 $w_cmit tag conf header_key \
192 -background $active_color \
194 $w_cmit tag conf header_val \
195 -background $active_color \
197 $w_cmit tag raise sel
198 scrollbar $w.file_pane.cm.sbx \
200 -command [list $w_cmit xview]
201 scrollbar $w.file_pane.cm.sby \
203 -command [list $w_cmit yview]
204 pack $w.file_pane.cm.sby -side right -fill y
205 pack $w.file_pane.cm.sbx -side bottom -fill x
206 pack $w_cmit -expand 1 -fill both
212 -textvariable @status \
215 pack $w.status.l -side left
217 menu $w.ctxm -tearoff 0
218 $w.ctxm add command \
219 -label "Copy Commit" \
220 -command [cb _copycommit]
227 $i conf -cursor $cursor_ptr
228 $i conf -yscrollcommand \
229 [list many2scrollbar [list \
234 ] yview $w.file_pane.out.sby]
237 [cb _click $i @%x,%y]
240 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
241 bind $i <Any-Enter> [cb _hide_tooltip]
242 bind $i <Any-Leave> [cb _hide_tooltip]
248 tk_popup $w.ctxm %X %Y
258 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
259 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
260 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
261 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
262 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
263 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
264 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
265 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
266 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
267 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
270 bind $w_cmit <Button-1> [list focus $w_cmit]
271 bind $top <Visibility> [list focus $top]
272 bind $w_file <Destroy> [list delete_this $this]
274 grid configure $w.header -sticky ew
275 grid configure $w.file_pane -sticky nsew
276 grid configure $w.status -sticky ew
277 grid columnconfigure $top 0 -weight 1
278 grid rowconfigure $top 0 -weight 0
279 grid rowconfigure $top 1 -weight 1
280 grid rowconfigure $top 2 -weight 0
282 set req_w [winfo reqwidth $top]
283 set req_h [winfo reqheight $top]
284 if {$req_w < 600} {set req_w 600}
285 if {$req_h < 400} {set req_h 400}
286 set g "${req_w}x${req_h}"
290 set old_height [winfo height $w.file_pane]
291 $w.file_pane sash place 0 \
292 [lindex [$w.file_pane sash coord 0] 0] \
293 [expr {int($old_height * 0.70)}]
294 bind $w.file_pane <Configure> \
295 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
303 if {$total_lines != 0 || $current_fd ne {}} {
304 if {$current_fd ne {}} {
305 catch {close $current_fd}
309 set highlight_line -1
310 set highlight_commit {}
316 array unset line_commit
317 array unset line_file
319 $w_load conf -state normal
320 $w_cgrp conf -state normal
321 $w_line conf -state normal
322 $w_file conf -state normal
324 $w_load delete 0.0 end
325 $w_cgrp delete 0.0 end
326 $w_line delete 0.0 end
327 $w_file delete 0.0 end
329 $w_load conf -state disabled
330 $w_cgrp conf -state disabled
331 $w_line conf -state disabled
332 $w_file conf -state disabled
335 if {[winfo exists $w.status.c]} {
336 $w.status.c coords bar 0 0 0 20
340 -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
344 $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
345 pack $w.status.c -side right
348 if {$history eq {}} {
349 $w_back conf -state disabled
351 $w_back conf -state normal
353 lappend history [list $commit $path]
355 set status "Loading $commit:[escape_path $path]..."
356 $w_path conf -text [escape_path $path]
358 set fd [open $path r]
360 set cmd [list git cat-file blob "$commit:$path"]
361 set fd [open "| $cmd" r]
363 fconfigure $fd -blocking 0 -translation lf -encoding binary
364 fileevent $fd readable [cb _read_file $fd]
368 method _history_menu {} {
370 if {[winfo exists $m]} {
376 for {set i [expr {[llength $history] - 2}]
377 } {$i >= 0} {incr i -1} {
378 set e [lindex $history $i]
382 if {[regexp {^[0-9a-f]{40}$} $c]} {
383 set t [string range $c 0 8]...
387 if {![catch {set summary $header($c,summary)}]} {
391 $m add command -label $t -command [cb _goback $i $c $f]
393 set X [winfo rootx $w_back]
394 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
398 method _goback {i c f} {
399 set history [lrange $history 0 [expr {$i - 1}]]
405 method _read_file {fd} {
406 if {$fd ne $current_fd} {
411 $w_load conf -state normal
412 $w_cgrp conf -state normal
413 $w_line conf -state normal
414 $w_file conf -state normal
415 while {[gets $fd line] >= 0} {
416 regsub "\r\$" $line {} line
419 if {$total_lines > 1} {
420 $w_load insert end "\n"
421 $w_cgrp insert end "\n"
422 $w_line insert end "\n"
423 $w_file insert end "\n"
426 $w_line insert end "$total_lines" linenumber
427 $w_file insert end "$line"
429 $w_load conf -state disabled
430 $w_cgrp conf -state disabled
431 $w_line conf -state disabled
432 $w_file conf -state disabled
437 set cmd {nice git blame -M -C --incremental}
439 lappend cmd --contents $path
444 set fd [open "| $cmd" r]
445 fconfigure $fd -blocking 0 -translation lf -encoding binary
446 fileevent $fd readable [cb _read_blame $fd]
449 } ifdeleted { catch {close $fd} }
451 method _read_blame {fd} {
452 variable group_colors
454 if {$fd ne $current_fd} {
459 $w_cgrp conf -state normal
460 while {[gets $fd line] >= 0} {
461 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
462 cmit original_line final_line line_count]} {
464 set r_orig_line $original_line
465 set r_final_line $final_line
466 set r_line_count $line_count
468 if {[catch {set g $order($cmit)}]} {
469 set bg [lindex $group_colors 0]
470 set group_colors [lrange $group_colors 1 end]
471 lappend group_colors $bg
473 $w_cgrp tag conf g$cmit -background $bg
474 $w_line tag conf g$cmit -background $bg
475 $w_file tag conf g$cmit -background $bg
477 set order($cmit) $commit_count
479 lappend commit_list $cmit
481 } elseif {[string match {filename *} $line]} {
482 set file [string range $line 9 end]
484 set lno $r_final_line
487 if {[regexp {^0{40}$} $cmit]} {
489 set commit_type curr_commit
490 } elseif {$cmit eq $commit} {
492 set commit_type curr_commit
494 set commit_type prior_commit
495 set commit_abbr [string range $cmit 0 4]
500 catch {set a_name $header($cmit,author)}
501 while {$a_name ne {}} {
502 if {![regexp {^([[:upper:]])} $a_name _a]} break
503 append author_abbr $_a
506 {^[[:upper:]][^\s]*\s+} \
507 $a_name {} a_name ]} break
509 if {$author_abbr eq {}} {
512 set author_abbr [string range $author_abbr 0 3]
513 while {[string length $author_abbr] < 4} {
514 set author_abbr " $author_abbr"
521 ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
522 && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
530 set lno_e "$lno.0 lineend + 1c"
531 if {[catch {set g g$line_commit($lno)}]} {
532 $w_load tag add annotated $lno.0 $lno_e
534 $w_cgrp tag remove g$g $lno.0 $lno_e
535 $w_line tag remove g$g $lno.0 $lno_e
536 $w_file tag remove g$g $lno.0 $lno_e
538 $w_cgrp tag remove a$g $lno.0 $lno_e
539 $w_line tag remove a$g $lno.0 $lno_e
540 $w_file tag remove a$g $lno.0 $lno_e
543 set line_commit($lno) $cmit
544 set line_file($lno) $file
546 $w_cgrp delete $lno.0 "$lno.0 lineend"
547 if {$lno == $first_lno} {
548 $w_cgrp insert $lno.0 $commit_abbr $commit_type
549 } elseif {$lno == [expr {$first_lno + 1}]} {
550 $w_cgrp insert $lno.0 $author_abbr
552 $w_cgrp insert $lno.0 { |}
555 $w_cgrp tag add g$cmit $lno.0 $lno_e
556 $w_line tag add g$cmit $lno.0 $lno_e
557 $w_file tag add g$cmit $lno.0 $lno_e
559 $w_cgrp tag add a$cmit $lno.0 $lno_e
560 $w_line tag add a$cmit $lno.0 $lno_e
561 $w_file tag add a$cmit $lno.0 $lno_e
563 if {$highlight_line == -1} {
564 if {[lindex [$w_file yview] 0] == 0} {
566 _showcommit $this $lno
568 } elseif {$highlight_line == $lno} {
569 _showcommit $this $lno
578 ![catch {set ncmit $line_commit($lno)}]
579 && ![catch {set nfile $line_file($lno)}]
583 $w_cgrp delete $lno.0 "$lno.0 lineend"
585 if {$lno == $first_lno} {
586 $w_cgrp insert $lno.0 $commit_abbr $commit_type
587 } elseif {$lno == [expr {$first_lno + 1}]} {
588 $w_cgrp insert $lno.0 $author_abbr
590 $w_cgrp insert $lno.0 { |}
595 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
596 set header($r_commit,$key) $data
599 $w_cgrp conf -state disabled
604 set status {Annotation complete.}
609 } ifdeleted { catch {close $fd} }
612 set have $blame_lines
613 set total $total_lines
615 if {$total} {set pdone [expr {100 * $have / $total}]}
618 "Loading annotations... %i of %i lines annotated (%2i%%)" \
620 $w.status.c coords bar 0 0 $pdone 20
623 method _click {cur_w pos} {
624 set lno [lindex [split [$cur_w index $pos] .] 0]
625 if {$lno eq {}} return
626 _showcommit $this $lno
629 method _load_commit {pos} {
630 set lno [lindex [split [$w_cgrp index $pos] .] 0]
631 if {[catch {set cmit $line_commit($lno)}]} return
632 if {[catch {set file $line_file($lno) }]} return
639 method _showcommit {lno} {
641 variable active_color
643 if {$highlight_commit ne {}} {
644 set cmit $highlight_commit
645 $w_cgrp tag conf a$cmit -background {}
646 $w_line tag conf a$cmit -background {}
647 $w_file tag conf a$cmit -background {}
650 $w_cmit conf -state normal
651 $w_cmit delete 0.0 end
652 if {[catch {set cmit $line_commit($lno)}]} {
654 $w_cmit insert end "Loading annotation..."
656 $w_cgrp tag conf a$cmit -background $active_color
657 $w_line tag conf a$cmit -background $active_color
658 $w_file tag conf a$cmit -background $active_color
663 catch {set author_name $header($cmit,author)}
664 catch {set author_email $header($cmit,author-mail)}
665 catch {set author_time [clock format \
666 $header($cmit,author-time) \
667 -format {%Y-%m-%d %H:%M:%S}
670 set committer_name {}
671 set committer_email {}
672 set committer_time {}
673 catch {set committer_name $header($cmit,committer)}
674 catch {set committer_email $header($cmit,committer-mail)}
675 catch {set committer_time [clock format \
676 $header($cmit,committer-time) \
677 -format {%Y-%m-%d %H:%M:%S}
680 if {[catch {set msg $header($cmit,message)}]} {
683 set fd [open "| git cat-file commit $cmit" r]
684 fconfigure $fd -encoding binary -translation lf
685 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
688 while {[gets $fd line] > 0} {
689 if {[string match {encoding *} $line]} {
690 set enc [string tolower [string range $line 9 end]]
693 set msg [encoding convertfrom $enc [read $fd]]
694 set msg [string trim $msg]
697 set author_name [encoding convertfrom $enc $author_name]
698 set committer_name [encoding convertfrom $enc $committer_name]
700 set header($cmit,author) $author_name
701 set header($cmit,committer) $committer_name
703 set header($cmit,message) $msg
706 $w_cmit insert end "commit $cmit\n" header_key
707 $w_cmit insert end "Author:\t" header_key
708 $w_cmit insert end "$author_name $author_email" header_val
709 $w_cmit insert end "$author_time\n" header_val
711 $w_cmit insert end "Committer:\t" header_key
712 $w_cmit insert end "$committer_name $committer_email" header_val
713 $w_cmit insert end "$committer_time\n" header_val
715 if {$line_file($lno) ne $path} {
716 $w_cmit insert end "Original File:\t" header_key
717 $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
720 $w_cmit insert end "\n$msg"
722 $w_cmit conf -state disabled
724 set highlight_line $lno
725 set highlight_commit $cmit
727 if {$highlight_commit eq $tooltip_commit} {
732 method _copycommit {} {
733 set pos @$::cursorX,$::cursorY
734 set lno [lindex [split [$::cursorW index $pos] .] 0]
735 if {![catch {set commit $line_commit($lno)}]} {
744 method _show_tooltip {cur_w pos} {
745 set lno [lindex [split [$cur_w index $pos] .] 0]
746 if {[catch {set cmit $line_commit($lno)}]} {
751 if {$cmit eq $highlight_commit} {
756 if {$cmit eq $tooltip_commit} {
757 _position_tooltip $this
758 } elseif {$tooltip_wm ne {}} {
759 _open_tooltip $this $cur_w
760 } elseif {$tooltip_timer eq {}} {
761 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
765 method _open_tooltip {cur_w} {
767 set pos_x [winfo pointerx $cur_w]
768 set pos_y [winfo pointery $cur_w]
769 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
774 set pos @[join [list \
775 [expr {$pos_x - [winfo rootx $cur_w]}] \
776 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
777 set lno [lindex [split [$cur_w index $pos] .] 0]
778 set cmit $line_commit($lno)
783 catch {set author_name $header($cmit,author)}
784 catch {set author_email $header($cmit,author-mail)}
785 catch {set author_time [clock format \
786 $header($cmit,author-time) \
787 -format {%Y-%m-%d %H:%M:%S}
790 set committer_name {}
791 set committer_email {}
792 set committer_time {}
793 catch {set committer_name $header($cmit,committer)}
794 catch {set committer_email $header($cmit,committer-mail)}
795 catch {set committer_time [clock format \
796 $header($cmit,committer-time) \
797 -format {%Y-%m-%d %H:%M:%S}
801 catch {set summary $header($cmit,summary)}
803 set tooltip_commit $cmit
804 set tooltip_text "commit $cmit
805 $author_name $author_email $author_time
808 if {$tooltip_wm ne "$cur_w.tooltip"} {
811 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
812 wm overrideredirect $tooltip_wm 1
813 wm transient $tooltip_wm [winfo toplevel $cur_w]
814 pack [label $tooltip_wm.label \
815 -background lightyellow \
817 -textvariable @tooltip_text \
820 _position_tooltip $this
823 method _position_tooltip {} {
824 set req_w [winfo reqwidth $tooltip_wm.label]
825 set req_h [winfo reqheight $tooltip_wm.label]
826 set pos_x [expr {[winfo pointerx .] + 5}]
827 set pos_y [expr {[winfo pointery .] + 10}]
829 set g "${req_w}x${req_h}"
830 if {$pos_x >= 0} {append g +}
832 if {$pos_y >= 0} {append g +}
835 wm geometry $tooltip_wm $g
839 method _hide_tooltip {} {
840 if {$tooltip_wm ne {}} {
843 set tooltip_commit {}
845 if {$tooltip_timer ne {}} {
846 after cancel $tooltip_timer
851 method _resize {new_height} {
852 set diff [expr {$new_height - $old_height}]
853 if {$diff == 0} return
855 set my [expr {[winfo height $w.file_pane] - 25}]
856 set o [$w.file_pane sash coord 0]
858 set oy [expr {[lindex $o 1] + $diff}]
859 if {$oy < 0} {set oy 0}
860 if {$oy > $my} {set oy $my}
861 $w.file_pane sash place 0 $ox $oy
863 set old_height $new_height