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_amov ; # text column: annotations + move tracking
21 field w_file ; # text column: actual file data
22 field w_cviewer ; # 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 amov_data ; # list of {commit origfile origline}
50 field r_commit ; # commit currently being parsed
51 field r_orig_line ; # original line number
52 field r_final_line ; # final line number
53 field r_line_count ; # lines in this region
55 field tooltip_wm {} ; # Current tooltip toplevel, if open
56 field tooltip_timer {} ; # Current timer event for our tooltip
57 field tooltip_commit {} ; # Commit in tooltip
58 field tooltip_text {} ; # Text in current tooltip
60 constructor new {i_commit i_path} {
67 wm title $top "[appname] ([reponame]): File Viewer"
69 frame $w.header -background orange
70 label $w.header.commit_l \
75 set w_back $w.header.commit_b
77 -image ::blame::img_back_arrow \
82 -activebackground orange
83 bind $w_back <Button-1> "
84 if {\[$w_back cget -state\] eq {normal}} {
88 label $w.header.commit \
89 -textvariable @commit \
93 label $w.header.path_l \
98 set w_path $w.header.path
103 pack $w.header.commit_l -side left
104 pack $w_back -side left
105 pack $w.header.commit -side left
106 pack $w_path -fill x -side right
107 pack $w.header.path_l -side right
109 panedwindow $w.file_pane -orient vertical
110 frame $w.file_pane.out
111 frame $w.file_pane.cm
112 $w.file_pane add $w.file_pane.out \
117 $w.file_pane add $w.file_pane.cm \
123 set w_line $w.file_pane.out.linenumber_t
126 -highlightthickness 0 \
128 -background white -borderwidth 0 \
134 $w_line tag conf linenumber -justify right -rmargin 5
136 set w_amov $w.file_pane.out.amove_t
139 -highlightthickness 0 \
141 -background white -borderwidth 0 \
147 $w_amov tag conf curr_commit
148 $w_amov tag conf prior_commit \
151 $w_amov tag bind prior_commit \
153 "[cb _load_commit @%x,%y];break"
155 set w_file $w.file_pane.out.file_t
158 -highlightthickness 0 \
160 -background white -borderwidth 0 \
165 -xscrollcommand [list $w.file_pane.out.sbx set] \
168 set w_columns [list $w_amov $w_line $w_file]
170 scrollbar $w.file_pane.out.sbx \
172 -command [list $w_file xview]
173 scrollbar $w.file_pane.out.sby \
175 -command [list scrollbar2many $w_columns yview]
176 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
178 $w.file_pane.out.sbx \
179 -column [expr {[llength $w_columns] - 1}] \
181 grid columnconfigure \
183 [expr {[llength $w_columns] - 1}] \
185 grid rowconfigure $w.file_pane.out 0 -weight 1
187 set w_cviewer $w.file_pane.cm.t
189 -background white -borderwidth 0 \
194 -xscrollcommand [list $w.file_pane.cm.sbx set] \
195 -yscrollcommand [list $w.file_pane.cm.sby set] \
197 $w_cviewer tag conf still_loading \
198 -font font_uiitalic \
200 $w_cviewer tag conf header_key \
202 -background $active_color \
204 $w_cviewer tag conf header_val \
205 -background $active_color \
207 $w_cviewer tag raise sel
208 scrollbar $w.file_pane.cm.sbx \
210 -command [list $w_cviewer xview]
211 scrollbar $w.file_pane.cm.sby \
213 -command [list $w_cviewer yview]
214 pack $w.file_pane.cm.sby -side right -fill y
215 pack $w.file_pane.cm.sbx -side bottom -fill x
216 pack $w_cviewer -expand 1 -fill both
222 -textvariable @status \
225 pack $w.status.l -side left
227 menu $w.ctxm -tearoff 0
228 $w.ctxm add command \
229 -label "Copy Commit" \
230 -command [cb _copycommit]
232 foreach i $w_columns {
233 $i conf -cursor $cursor_ptr
234 $i conf -yscrollcommand [list many2scrollbar \
235 $w_columns yview $w.file_pane.out.sby]
238 [cb _click $i @%x,%y]
241 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
242 bind $i <Any-Enter> [cb _hide_tooltip]
243 bind $i <Any-Leave> [cb _hide_tooltip]
249 tk_popup $w.ctxm %X %Y
253 foreach i [concat $w_columns $w_cviewer] {
254 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
255 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
256 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
257 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
258 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
259 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
260 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
261 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
262 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
263 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
266 bind $w_cviewer <Button-1> [list focus $w_cviewer]
267 bind $top <Visibility> [list focus $top]
268 bind $w_file <Destroy> [list delete_this $this]
270 grid configure $w.header -sticky ew
271 grid configure $w.file_pane -sticky nsew
272 grid configure $w.status -sticky ew
273 grid columnconfigure $top 0 -weight 1
274 grid rowconfigure $top 0 -weight 0
275 grid rowconfigure $top 1 -weight 1
276 grid rowconfigure $top 2 -weight 0
278 set req_w [winfo reqwidth $top]
279 set req_h [winfo reqheight $top]
280 if {$req_w < 600} {set req_w 600}
281 if {$req_h < 400} {set req_h 400}
282 set g "${req_w}x${req_h}"
286 set old_height [winfo height $w.file_pane]
287 $w.file_pane sash place 0 \
288 [lindex [$w.file_pane sash coord 0] 0] \
289 [expr {int($old_height * 0.70)}]
290 bind $w.file_pane <Configure> \
291 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
299 if {$total_lines != 0 || $current_fd ne {}} {
300 if {$current_fd ne {}} {
301 catch {close $current_fd}
305 foreach i $w_columns {
306 $i conf -state normal
308 foreach cmit [array names have_commit] {
311 $i conf -state disabled
314 set highlight_line -1
315 set highlight_commit {}
318 array unset have_commit
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 # Index 0 is always empty. There is never line 0 as
342 # we use only 1 based lines, as that matches both with
343 # git-blame output and with Tk's text widget.
345 set amov_data [list [list]]
347 set status "Loading $commit:[escape_path $path]..."
348 $w_path conf -text [escape_path $path]
350 set fd [open $path r]
352 set cmd [list git cat-file blob "$commit:$path"]
353 set fd [open "| $cmd" r]
355 fconfigure $fd -blocking 0 -translation lf -encoding binary
356 fileevent $fd readable [cb _read_file $fd]
360 method _history_menu {} {
362 if {[winfo exists $m]} {
368 for {set i [expr {[llength $history] - 2}]
369 } {$i >= 0} {incr i -1} {
370 set e [lindex $history $i]
374 if {[regexp {^[0-9a-f]{40}$} $c]} {
375 set t [string range $c 0 8]...
376 } elseif {$c eq {}} {
377 set t {Working Directory}
381 if {![catch {set summary $header($c,summary)}]} {
383 if {[string length $t] > 70} {
384 set t [string range $t 0 66]...
388 $m add command -label $t -command [cb _goback $i $c $f]
390 set X [winfo rootx $w_back]
391 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
395 method _goback {i c f} {
396 set history [lrange $history 0 [expr {$i - 1}]]
402 method _read_file {fd} {
403 if {$fd ne $current_fd} {
408 foreach i $w_columns {$i conf -state normal}
409 while {[gets $fd line] >= 0} {
410 regsub "\r\$" $line {} line
414 if {$total_lines > 1} {
415 foreach i $w_columns {$i insert end "\n"}
418 $w_line insert end "$total_lines" linenumber
419 $w_file insert end "$line"
422 set ln_wc [expr {[string length $total_lines] + 2}]
423 if {[$w_line cget -width] < $ln_wc} {
424 $w_line conf -width $ln_wc
427 foreach i $w_columns {$i conf -state disabled}
433 set cmd {nice git blame -M -C --incremental}
435 lappend cmd --contents $path
440 set fd [open "| $cmd" r]
441 fconfigure $fd -blocking 0 -translation lf -encoding binary
442 fileevent $fd readable [cb _read_blame $fd]
445 } ifdeleted { catch {close $fd} }
447 method _read_blame {fd} {
448 if {$fd ne $current_fd} {
453 $w_amov conf -state normal
454 while {[gets $fd line] >= 0} {
455 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
456 cmit original_line final_line line_count]} {
458 set r_orig_line $original_line
459 set r_final_line $final_line
460 set r_line_count $line_count
462 if {[catch {set g $have_commit($cmit)}]} {
463 set bg [lindex $group_colors 0]
464 set group_colors [lrange $group_colors 1 end]
465 lappend group_colors $bg
466 foreach i $w_columns {
467 $i tag conf g$cmit -background $bg
469 set have_commit($cmit) 1
471 } elseif {[string match {filename *} $line]} {
472 set file [string range $line 9 end]
474 set lno $r_final_line
477 if {[regexp {^0{40}$} $cmit]} {
479 set commit_type curr_commit
480 } elseif {$cmit eq $commit} {
482 set commit_type curr_commit
484 set commit_type prior_commit
485 set commit_abbr [string range $cmit 0 4]
490 catch {set a_name $header($cmit,author)}
491 while {$a_name ne {}} {
492 if {![regexp {^([[:upper:]])} $a_name _a]} break
493 append author_abbr $_a
496 {^[[:upper:]][^\s]*\s+} \
497 $a_name {} a_name ]} break
499 if {$author_abbr eq {}} {
502 set author_abbr [string range $author_abbr 0 3]
503 while {[string length $author_abbr] < 4} {
504 set author_abbr " $author_abbr"
512 && $cmit eq [lindex $amov_data [expr {$first_lno - 1}] 0]
513 && $file eq [lindex $amov_data [expr {$first_lno - 1}] 1]
519 set lno_e "$lno.0 lineend + 1c"
520 if {[lindex $amov_data $lno] ne {}} {
521 set g [lindex $amov_data $lno 0]
522 foreach i $w_columns {
523 $i tag remove g$g $lno.0 $lno_e
526 lset amov_data $lno [list $cmit $file]
528 $w_amov delete $lno.0 "$lno.0 lineend"
529 if {$lno == $first_lno} {
530 $w_amov insert $lno.0 $commit_abbr $commit_type
531 } elseif {$lno == [expr {$first_lno + 1}]} {
532 $w_amov insert $lno.0 $author_abbr
534 $w_amov insert $lno.0 { |}
537 foreach i $w_columns {
538 $i tag add g$cmit $lno.0 $lno_e
541 if {$highlight_line == -1} {
542 if {[lindex [$w_file yview] 0] == 0} {
544 _showcommit $this $lno
546 } elseif {$highlight_line == $lno} {
547 _showcommit $this $lno
556 $cmit eq [lindex $amov_data $lno 0]
557 && $file eq [lindex $amov_data $lno 1]
559 $w_amov delete $lno.0 "$lno.0 lineend"
561 if {$lno == $first_lno} {
562 $w_amov insert $lno.0 $commit_abbr $commit_type
563 } elseif {$lno == [expr {$first_lno + 1}]} {
564 $w_amov insert $lno.0 $author_abbr
566 $w_amov insert $lno.0 { |}
571 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
572 set header($r_commit,$key) $data
575 $w_amov conf -state disabled
580 set status {Annotation complete.}
585 } ifdeleted { catch {close $fd} }
588 set have $blame_lines
589 set total $total_lines
591 if {$total} {set pdone [expr {100 * $have / $total}]}
594 "Loading annotations... %i of %i lines annotated (%2i%%)" \
596 $w.status.c coords bar 0 0 $pdone 20
599 method _click {cur_w pos} {
600 set lno [lindex [split [$cur_w index $pos] .] 0]
601 if {$lno eq {}} return
602 _showcommit $this $lno
605 method _load_commit {pos} {
606 set lno [lindex [split [$w_amov index $pos] .] 0]
607 set dat [lindex $amov_data $lno]
609 set commit [lindex $dat 0]
610 set path [lindex $dat 1]
615 method _showcommit {lno} {
618 if {$highlight_commit ne {}} {
619 foreach i $w_columns {
620 $i tag conf g$highlight_commit -background $old_bgcolor
624 $w_cviewer conf -state normal
625 $w_cviewer delete 0.0 end
627 set dat [lindex $amov_data $lno]
630 $w_cviewer insert end "Loading annotation..." still_loading
632 set cmit [lindex $dat 0]
633 set file [lindex $dat 1]
635 set old_bgcolor [$w_file tag cget g$cmit -background]
636 foreach i $w_columns {
637 $i tag conf g$cmit -background $active_color
643 catch {set author_name $header($cmit,author)}
644 catch {set author_email $header($cmit,author-mail)}
645 catch {set author_time [clock format \
646 $header($cmit,author-time) \
647 -format {%Y-%m-%d %H:%M:%S}
650 set committer_name {}
651 set committer_email {}
652 set committer_time {}
653 catch {set committer_name $header($cmit,committer)}
654 catch {set committer_email $header($cmit,committer-mail)}
655 catch {set committer_time [clock format \
656 $header($cmit,committer-time) \
657 -format {%Y-%m-%d %H:%M:%S}
660 if {[catch {set msg $header($cmit,message)}]} {
663 set fd [open "| git cat-file commit $cmit" r]
664 fconfigure $fd -encoding binary -translation lf
665 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
668 while {[gets $fd line] > 0} {
669 if {[string match {encoding *} $line]} {
670 set enc [string tolower [string range $line 9 end]]
673 set msg [encoding convertfrom $enc [read $fd]]
674 set msg [string trim $msg]
677 set author_name [encoding convertfrom $enc $author_name]
678 set committer_name [encoding convertfrom $enc $committer_name]
680 set header($cmit,author) $author_name
681 set header($cmit,committer) $committer_name
683 set header($cmit,message) $msg
686 $w_cviewer insert end "commit $cmit\n" header_key
687 $w_cviewer insert end "Author:\t" header_key
688 $w_cviewer insert end "$author_name $author_email" header_val
689 $w_cviewer insert end " $author_time\n" header_val
691 $w_cviewer insert end "Committer:\t" header_key
692 $w_cviewer insert end "$committer_name $committer_email" header_val
693 $w_cviewer insert end " $committer_time\n" header_val
695 if {$file ne $path} {
696 $w_cviewer insert end "Original File:\t" header_key
697 $w_cviewer insert end "[escape_path $file]\n" header_val
700 $w_cviewer insert end "\n$msg"
702 $w_cviewer conf -state disabled
704 set highlight_line $lno
705 set highlight_commit $cmit
707 if {$highlight_commit eq $tooltip_commit} {
712 method _copycommit {} {
713 set pos @$::cursorX,$::cursorY
714 set lno [lindex [split [$::cursorW index $pos] .] 0]
715 set dat [lindex $amov_data $lno]
725 method _show_tooltip {cur_w pos} {
726 set lno [lindex [split [$cur_w index $pos] .] 0]
727 set dat [lindex $amov_data $lno]
732 set cmit [lindex $dat 0]
734 if {$cmit eq $highlight_commit} {
739 if {$cmit eq $tooltip_commit} {
740 _position_tooltip $this
741 } elseif {$tooltip_wm ne {}} {
742 _open_tooltip $this $cur_w
743 } elseif {$tooltip_timer eq {}} {
744 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
748 method _open_tooltip {cur_w} {
750 set pos_x [winfo pointerx $cur_w]
751 set pos_y [winfo pointery $cur_w]
752 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
757 set pos @[join [list \
758 [expr {$pos_x - [winfo rootx $cur_w]}] \
759 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
760 set lno [lindex [split [$cur_w index $pos] .] 0]
761 set dat [lindex $amov_data $lno]
762 set cmit [lindex $dat 0]
763 set file [lindex $dat 1]
768 catch {set author_name $header($cmit,author)}
769 catch {set author_email $header($cmit,author-mail)}
770 catch {set author_time [clock format \
771 $header($cmit,author-time) \
772 -format {%Y-%m-%d %H:%M:%S}
775 set committer_name {}
776 set committer_email {}
777 set committer_time {}
778 catch {set committer_name $header($cmit,committer)}
779 catch {set committer_email $header($cmit,committer-mail)}
780 catch {set committer_time [clock format \
781 $header($cmit,committer-time) \
782 -format {%Y-%m-%d %H:%M:%S}
786 catch {set summary $header($cmit,summary)}
788 set tooltip_commit $cmit
789 set tooltip_text "commit $cmit
790 $author_name $author_email $author_time
793 if {$file ne $path} {
794 append tooltip_text "
796 Original File: $file"
799 if {$tooltip_wm ne "$cur_w.tooltip"} {
802 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
803 wm overrideredirect $tooltip_wm 1
804 wm transient $tooltip_wm [winfo toplevel $cur_w]
805 pack [label $tooltip_wm.label \
806 -background lightyellow \
808 -textvariable @tooltip_text \
811 _position_tooltip $this
814 method _position_tooltip {} {
815 set req_w [winfo reqwidth $tooltip_wm.label]
816 set req_h [winfo reqheight $tooltip_wm.label]
817 set pos_x [expr {[winfo pointerx .] + 5}]
818 set pos_y [expr {[winfo pointery .] + 10}]
820 set g "${req_w}x${req_h}"
821 if {$pos_x >= 0} {append g +}
823 if {$pos_y >= 0} {append g +}
826 wm geometry $tooltip_wm $g
830 method _hide_tooltip {} {
831 if {$tooltip_wm ne {}} {
834 set tooltip_commit {}
836 if {$tooltip_timer ne {}} {
837 after cancel $tooltip_timer
842 method _resize {new_height} {
843 set diff [expr {$new_height - $old_height}]
844 if {$diff == 0} return
846 set my [expr {[winfo height $w.file_pane] - 25}]
847 set o [$w.file_pane sash coord 0]
849 set oy [expr {[lindex $o 1] + $diff}]
850 if {$oy < 0} {set oy 0}
851 if {$oy > $my} {set oy $my}
852 $w.file_pane sash place 0 $ox $oy
854 set old_height $new_height