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_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_cgrp
$w.file_pane.out.commit_t
139 -highlightthickness 0 \
141 -background white
-borderwidth 0 \
147 $w_cgrp tag conf curr_commit
148 $w_cgrp tag conf prior_commit
\
151 $w_cgrp 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_cgrp $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_cmit
$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_cmit tag conf header_key
\
199 -background $active_color \
201 $w_cmit tag conf header_val
\
202 -background $active_color \
204 $w_cmit tag
raise sel
205 scrollbar $w.file_pane.cm.sbx
\
207 -command [list $w_cmit xview
]
208 scrollbar $w.file_pane.cm.sby
\
210 -command [list $w_cmit yview
]
211 pack $w.file_pane.cm.sby
-side right
-fill y
212 pack $w.file_pane.cm.sbx
-side bottom
-fill x
213 pack $w_cmit -expand 1 -fill both
219 -textvariable @status
\
222 pack $w.status.l
-side left
224 menu $w.ctxm
-tearoff 0
225 $w.ctxm add command
\
226 -label "Copy Commit" \
227 -command [cb _copycommit
]
229 foreach i
$w_columns {
230 $i conf
-cursor $cursor_ptr
231 $i conf
-yscrollcommand [list many2scrollbar
\
232 $w_columns yview
$w.file_pane.out.sby
]
235 [cb _click $i @%x,%y]
238 bind $i <Any-Motion
> [cb _show_tooltip
$i @%x
,%y
]
239 bind $i <Any-Enter
> [cb _hide_tooltip
]
240 bind $i <Any-Leave
> [cb _hide_tooltip
]
246 tk_popup $w.ctxm %X %Y
250 foreach i
[concat $w_columns $w_cmit] {
251 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
252 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
253 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
254 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
255 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
256 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
257 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
258 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
259 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
260 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
263 bind $w_cmit <Button-1
> [list focus $w_cmit]
264 bind $top <Visibility
> [list focus $top]
265 bind $w_file <Destroy
> [list delete_this
$this]
267 grid configure
$w.header
-sticky ew
268 grid configure
$w.file_pane
-sticky nsew
269 grid configure
$w.status
-sticky ew
270 grid columnconfigure
$top 0 -weight 1
271 grid rowconfigure
$top 0 -weight 0
272 grid rowconfigure
$top 1 -weight 1
273 grid rowconfigure
$top 2 -weight 0
275 set req_w
[winfo reqwidth
$top]
276 set req_h
[winfo reqheight
$top]
277 if {$req_w < 600} {set req_w
600}
278 if {$req_h < 400} {set req_h
400}
279 set g
"${req_w}x${req_h}"
283 set old_height
[winfo height
$w.file_pane
]
284 $w.file_pane sash
place 0 \
285 [lindex [$w.file_pane sash coord
0] 0] \
286 [expr {int
($old_height * 0.70)}]
287 bind $w.file_pane
<Configure
> \
288 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
296 if {$total_lines != 0 ||
$current_fd ne
{}} {
297 if {$current_fd ne
{}} {
298 catch {close $current_fd}
302 foreach i
$w_columns {
303 $i conf
-state normal
305 foreach cmit
[array names have_commit
] {
308 $i conf
-state disabled
311 set highlight_line
-1
312 set highlight_commit
{}
315 array unset have_commit
318 if {[winfo exists
$w.status.c
]} {
319 $w.status.c coords bar
0 0 0 20
323 -height [expr {int
([winfo reqheight
$w.status.l
] * 0.6)}] \
327 $w.status.c create rectangle
0 0 0 20 -tags bar
-fill navy
328 pack $w.status.c
-side right
331 if {$history eq
{}} {
332 $w_back conf
-state disabled
334 $w_back conf
-state normal
336 lappend history [list $commit $path]
338 # Index 0 is always empty. There is never line 0 as
339 # we use only 1 based lines, as that matches both with
340 # git-blame output and with Tk's text widget.
342 set line_data
[list [list]]
344 set status
"Loading $commit:[escape_path $path]..."
345 $w_path conf
-text [escape_path
$path]
347 set fd
[open $path r
]
349 set cmd
[list git cat-file blob
"$commit:$path"]
350 set fd
[open "| $cmd" r
]
352 fconfigure $fd -blocking 0 -translation lf
-encoding binary
353 fileevent $fd readable
[cb _read_file
$fd]
357 method _history_menu
{} {
359 if {[winfo exists
$m]} {
365 for {set i
[expr {[llength $history] - 2}]
366 } {$i >= 0} {incr i
-1} {
367 set e
[lindex $history $i]
371 if {[regexp {^
[0-9a-f
]{40}$} $c]} {
372 set t
[string range
$c 0 8]...
376 if {![catch {set summary
$header($c,summary
)}]} {
378 if {[string length
$t] > 70} {
379 set t
[string range
$t 0 66]...
383 $m add command
-label $t -command [cb _goback
$i $c $f]
385 set X
[winfo rootx
$w_back]
386 set Y
[expr {[winfo rooty
$w_back] + [winfo height
$w_back]}]
390 method _goback
{i c f
} {
391 set history [lrange $history 0 [expr {$i - 1}]]
397 method _read_file
{fd
} {
398 if {$fd ne
$current_fd} {
403 foreach i
$w_columns {$i conf
-state normal
}
404 while {[gets $fd line
] >= 0} {
405 regsub "\r\$" $line {} line
409 if {$total_lines > 1} {
410 foreach i
$w_columns {$i insert end
"\n"}
413 $w_line insert end
"$total_lines" linenumber
414 $w_file insert end
"$line"
417 set ln_wc
[expr {[string length
$total_lines] + 2}]
418 if {[$w_line cget
-width] < $ln_wc} {
419 $w_line conf
-width $ln_wc
422 foreach i
$w_columns {$i conf
-state disabled
}
428 set cmd
{nice git blame
-M -C --incremental}
430 lappend cmd
--contents $path
435 set fd
[open "| $cmd" r
]
436 fconfigure $fd -blocking 0 -translation lf
-encoding binary
437 fileevent $fd readable
[cb _read_blame
$fd]
440 } ifdeleted
{ catch {close $fd} }
442 method _read_blame
{fd
} {
443 if {$fd ne
$current_fd} {
448 $w_cgrp conf
-state normal
449 while {[gets $fd line
] >= 0} {
450 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
451 cmit original_line final_line line_count
]} {
453 set r_orig_line
$original_line
454 set r_final_line
$final_line
455 set r_line_count
$line_count
457 if {[catch {set g
$have_commit($cmit)}]} {
458 set bg
[lindex $group_colors 0]
459 set group_colors
[lrange $group_colors 1 end
]
460 lappend group_colors
$bg
461 foreach i
$w_columns {
462 $i tag conf g
$cmit -background $bg
464 set have_commit
($cmit) 1
466 } elseif
{[string match
{filename *} $line]} {
467 set file [string range
$line 9 end
]
469 set lno
$r_final_line
472 if {[regexp {^
0{40}$} $cmit]} {
474 set commit_type curr_commit
475 } elseif
{$cmit eq
$commit} {
477 set commit_type curr_commit
479 set commit_type prior_commit
480 set commit_abbr
[string range
$cmit 0 4]
485 catch {set a_name
$header($cmit,author
)}
486 while {$a_name ne
{}} {
487 if {![regexp {^
([[:upper
:]])} $a_name _a
]} break
488 append author_abbr
$_a
491 {^
[[:upper
:]][^
\s
]*\s
+} \
492 $a_name {} a_name
]} break
494 if {$author_abbr eq
{}} {
497 set author_abbr
[string range
$author_abbr 0 3]
498 while {[string length
$author_abbr] < 4} {
499 set author_abbr
" $author_abbr"
507 && $cmit eq
[lindex $line_data [expr {$first_lno - 1}] 0]
508 && $file eq
[lindex $line_data [expr {$first_lno - 1}] 1]
514 set lno_e
"$lno.0 lineend + 1c"
515 if {[lindex $line_data $lno] ne
{}} {
516 set g
[lindex $line_data $lno 0]
517 foreach i
$w_columns {
518 $i tag remove g
$g $lno.0 $lno_e
521 lset line_data
$lno [list $cmit $file]
523 $w_cgrp delete
$lno.0 "$lno.0 lineend"
524 if {$lno == $first_lno} {
525 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
526 } elseif
{$lno == [expr {$first_lno + 1}]} {
527 $w_cgrp insert
$lno.0 $author_abbr
529 $w_cgrp insert
$lno.0 { |
}
532 foreach i
$w_columns {
533 $i tag add g
$cmit $lno.0 $lno_e
536 if {$highlight_line == -1} {
537 if {[lindex [$w_file yview
] 0] == 0} {
539 _showcommit
$this $lno
541 } elseif
{$highlight_line == $lno} {
542 _showcommit
$this $lno
551 $cmit eq
[lindex $line_data $lno 0]
552 && $file eq
[lindex $line_data $lno 1]
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 set dat
[lindex $line_data $lno]
604 set commit
[lindex $dat 0]
605 set path
[lindex $dat 1]
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
622 set dat
[lindex $line_data $lno]
625 $w_cmit insert end
"Loading annotation..."
627 set cmit
[lindex $dat 0]
628 set file [lindex $dat 1]
630 set old_bgcolor
[$w_file tag cget g
$cmit -background]
631 foreach i
$w_columns {
632 $i tag conf g
$cmit -background $active_color
638 catch {set author_name
$header($cmit,author
)}
639 catch {set author_email
$header($cmit,author-mail
)}
640 catch {set author_time
[clock format \
641 $header($cmit,author-time
) \
642 -format {%Y-
%m-
%d
%H
:%M
:%S
}
645 set committer_name
{}
646 set committer_email
{}
647 set committer_time
{}
648 catch {set committer_name
$header($cmit,committer
)}
649 catch {set committer_email
$header($cmit,committer-mail
)}
650 catch {set committer_time
[clock format \
651 $header($cmit,committer-time
) \
652 -format {%Y-
%m-
%d
%H
:%M
:%S
}
655 if {[catch {set msg
$header($cmit,message)}]} {
658 set fd
[open "| git cat-file commit $cmit" r
]
659 fconfigure $fd -encoding binary -translation lf
660 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
663 while {[gets $fd line
] > 0} {
664 if {[string match
{encoding *} $line]} {
665 set enc
[string tolower
[string range
$line 9 end
]]
668 set msg
[encoding convertfrom
$enc [read $fd]]
669 set msg
[string trim
$msg]
672 set author_name
[encoding convertfrom
$enc $author_name]
673 set committer_name
[encoding convertfrom
$enc $committer_name]
675 set header
($cmit,author
) $author_name
676 set header
($cmit,committer
) $committer_name
678 set header
($cmit,message) $msg
681 $w_cmit insert end
"commit $cmit\n" header_key
682 $w_cmit insert end
"Author:\t" header_key
683 $w_cmit insert end
"$author_name $author_email" header_val
684 $w_cmit insert end
"$author_time\n" header_val
686 $w_cmit insert end
"Committer:\t" header_key
687 $w_cmit insert end
"$committer_name $committer_email" header_val
688 $w_cmit insert end
"$committer_time\n" header_val
690 if {$file ne
$path} {
691 $w_cmit insert end
"Original File:\t" header_key
692 $w_cmit insert end
"[escape_path $file]\n" header_val
695 $w_cmit insert end
"\n$msg"
697 $w_cmit conf
-state disabled
699 set highlight_line
$lno
700 set highlight_commit
$cmit
702 if {$highlight_commit eq
$tooltip_commit} {
707 method _copycommit
{} {
708 set pos
@$::cursorX,$::cursorY
709 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
710 set dat
[lindex $line_data $lno]
720 method _show_tooltip
{cur_w pos
} {
721 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
722 set dat
[lindex $line_data $lno]
727 set cmit
[lindex $dat 0]
729 if {$cmit eq
$highlight_commit} {
734 if {$cmit eq
$tooltip_commit} {
735 _position_tooltip
$this
736 } elseif
{$tooltip_wm ne
{}} {
737 _open_tooltip
$this $cur_w
738 } elseif
{$tooltip_timer eq
{}} {
739 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
743 method _open_tooltip
{cur_w
} {
745 set pos_x
[winfo pointerx
$cur_w]
746 set pos_y
[winfo pointery
$cur_w]
747 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
752 set pos
@[join [list \
753 [expr {$pos_x - [winfo rootx
$cur_w]}] \
754 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
755 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
756 set dat
[lindex $line_data $lno]
757 set cmit
[lindex $dat 0]
758 set file [lindex $dat 1]
763 catch {set author_name
$header($cmit,author
)}
764 catch {set author_email
$header($cmit,author-mail
)}
765 catch {set author_time
[clock format \
766 $header($cmit,author-time
) \
767 -format {%Y-
%m-
%d
%H
:%M
:%S
}
770 set committer_name
{}
771 set committer_email
{}
772 set committer_time
{}
773 catch {set committer_name
$header($cmit,committer
)}
774 catch {set committer_email
$header($cmit,committer-mail
)}
775 catch {set committer_time
[clock format \
776 $header($cmit,committer-time
) \
777 -format {%Y-
%m-
%d
%H
:%M
:%S
}
781 catch {set summary
$header($cmit,summary
)}
783 set tooltip_commit
$cmit
784 set tooltip_text
"commit $cmit
785 $author_name $author_email $author_time
788 if {$file ne
$path} {
789 append tooltip_text
"
791 Original File: $file"
794 if {$tooltip_wm ne
"$cur_w.tooltip"} {
797 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
798 wm overrideredirect
$tooltip_wm 1
799 wm transient
$tooltip_wm [winfo toplevel $cur_w]
800 pack [label $tooltip_wm.
label \
801 -background lightyellow
\
803 -textvariable @tooltip_text
\
806 _position_tooltip
$this
809 method _position_tooltip
{} {
810 set req_w
[winfo reqwidth
$tooltip_wm.
label]
811 set req_h
[winfo reqheight
$tooltip_wm.
label]
812 set pos_x
[expr {[winfo pointerx .
] + 5}]
813 set pos_y
[expr {[winfo pointery .
] + 10}]
815 set g
"${req_w}x${req_h}"
816 if {$pos_x >= 0} {append g
+}
818 if {$pos_y >= 0} {append g
+}
821 wm geometry
$tooltip_wm $g
825 method _hide_tooltip
{} {
826 if {$tooltip_wm ne
{}} {
829 set tooltip_commit
{}
831 if {$tooltip_timer ne
{}} {
832 after cancel
$tooltip_timer
837 method _resize
{new_height
} {
838 set diff
[expr {$new_height - $old_height}]
839 if {$diff == 0} return
841 set my
[expr {[winfo height
$w.file_pane
] - 25}]
842 set o
[$w.file_pane sash coord
0]
844 set oy
[expr {[lindex $o 1] + $diff}]
845 if {$oy < 0} {set oy
0}
846 if {$oy > $my} {set oy
$my}
847 $w.file_pane sash
place 0 $ox $oy
849 set old_height
$new_height