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]...
373 } elseif
{$c eq
{}} {
374 set t
{Working Directory
}
378 if {![catch {set summary
$header($c,summary
)}]} {
380 if {[string length
$t] > 70} {
381 set t
[string range
$t 0 66]...
385 $m add command
-label $t -command [cb _goback
$i $c $f]
387 set X
[winfo rootx
$w_back]
388 set Y
[expr {[winfo rooty
$w_back] + [winfo height
$w_back]}]
392 method _goback
{i c f
} {
393 set history [lrange $history 0 [expr {$i - 1}]]
399 method _read_file
{fd
} {
400 if {$fd ne
$current_fd} {
405 foreach i
$w_columns {$i conf
-state normal
}
406 while {[gets $fd line
] >= 0} {
407 regsub "\r\$" $line {} line
411 if {$total_lines > 1} {
412 foreach i
$w_columns {$i insert end
"\n"}
415 $w_line insert end
"$total_lines" linenumber
416 $w_file insert end
"$line"
419 set ln_wc
[expr {[string length
$total_lines] + 2}]
420 if {[$w_line cget
-width] < $ln_wc} {
421 $w_line conf
-width $ln_wc
424 foreach i
$w_columns {$i conf
-state disabled
}
430 set cmd
{nice git blame
-M -C --incremental}
432 lappend cmd
--contents $path
437 set fd
[open "| $cmd" r
]
438 fconfigure $fd -blocking 0 -translation lf
-encoding binary
439 fileevent $fd readable
[cb _read_blame
$fd]
442 } ifdeleted
{ catch {close $fd} }
444 method _read_blame
{fd
} {
445 if {$fd ne
$current_fd} {
450 $w_cgrp conf
-state normal
451 while {[gets $fd line
] >= 0} {
452 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
453 cmit original_line final_line line_count
]} {
455 set r_orig_line
$original_line
456 set r_final_line
$final_line
457 set r_line_count
$line_count
459 if {[catch {set g
$have_commit($cmit)}]} {
460 set bg
[lindex $group_colors 0]
461 set group_colors
[lrange $group_colors 1 end
]
462 lappend group_colors
$bg
463 foreach i
$w_columns {
464 $i tag conf g
$cmit -background $bg
466 set have_commit
($cmit) 1
468 } elseif
{[string match
{filename *} $line]} {
469 set file [string range
$line 9 end
]
471 set lno
$r_final_line
474 if {[regexp {^
0{40}$} $cmit]} {
476 set commit_type curr_commit
477 } elseif
{$cmit eq
$commit} {
479 set commit_type curr_commit
481 set commit_type prior_commit
482 set commit_abbr
[string range
$cmit 0 4]
487 catch {set a_name
$header($cmit,author
)}
488 while {$a_name ne
{}} {
489 if {![regexp {^
([[:upper
:]])} $a_name _a
]} break
490 append author_abbr
$_a
493 {^
[[:upper
:]][^
\s
]*\s
+} \
494 $a_name {} a_name
]} break
496 if {$author_abbr eq
{}} {
499 set author_abbr
[string range
$author_abbr 0 3]
500 while {[string length
$author_abbr] < 4} {
501 set author_abbr
" $author_abbr"
509 && $cmit eq
[lindex $line_data [expr {$first_lno - 1}] 0]
510 && $file eq
[lindex $line_data [expr {$first_lno - 1}] 1]
516 set lno_e
"$lno.0 lineend + 1c"
517 if {[lindex $line_data $lno] ne
{}} {
518 set g
[lindex $line_data $lno 0]
519 foreach i
$w_columns {
520 $i tag remove g
$g $lno.0 $lno_e
523 lset line_data
$lno [list $cmit $file]
525 $w_cgrp delete
$lno.0 "$lno.0 lineend"
526 if {$lno == $first_lno} {
527 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
528 } elseif
{$lno == [expr {$first_lno + 1}]} {
529 $w_cgrp insert
$lno.0 $author_abbr
531 $w_cgrp insert
$lno.0 { |
}
534 foreach i
$w_columns {
535 $i tag add g
$cmit $lno.0 $lno_e
538 if {$highlight_line == -1} {
539 if {[lindex [$w_file yview
] 0] == 0} {
541 _showcommit
$this $lno
543 } elseif
{$highlight_line == $lno} {
544 _showcommit
$this $lno
553 $cmit eq
[lindex $line_data $lno 0]
554 && $file eq
[lindex $line_data $lno 1]
556 $w_cgrp delete
$lno.0 "$lno.0 lineend"
558 if {$lno == $first_lno} {
559 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
560 } elseif
{$lno == [expr {$first_lno + 1}]} {
561 $w_cgrp insert
$lno.0 $author_abbr
563 $w_cgrp insert
$lno.0 { |
}
568 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
569 set header
($r_commit,$key) $data
572 $w_cgrp conf
-state disabled
577 set status
{Annotation complete.
}
582 } ifdeleted
{ catch {close $fd} }
585 set have
$blame_lines
586 set total
$total_lines
588 if {$total} {set pdone
[expr {100 * $have / $total}]}
591 "Loading annotations... %i of %i lines annotated (%2i%%)" \
593 $w.status.c coords bar
0 0 $pdone 20
596 method _click
{cur_w pos
} {
597 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
598 if {$lno eq
{}} return
599 _showcommit
$this $lno
602 method _load_commit
{pos
} {
603 set lno
[lindex [split [$w_cgrp index
$pos] .
] 0]
604 set dat
[lindex $line_data $lno]
606 set commit
[lindex $dat 0]
607 set path
[lindex $dat 1]
612 method _showcommit
{lno
} {
615 if {$highlight_commit ne
{}} {
616 foreach i
$w_columns {
617 $i tag conf g
$highlight_commit -background $old_bgcolor
621 $w_cmit conf
-state normal
622 $w_cmit delete
0.0 end
624 set dat
[lindex $line_data $lno]
627 $w_cmit insert end
"Loading annotation..."
629 set cmit
[lindex $dat 0]
630 set file [lindex $dat 1]
632 set old_bgcolor
[$w_file tag cget g
$cmit -background]
633 foreach i
$w_columns {
634 $i tag conf g
$cmit -background $active_color
640 catch {set author_name
$header($cmit,author
)}
641 catch {set author_email
$header($cmit,author-mail
)}
642 catch {set author_time
[clock format \
643 $header($cmit,author-time
) \
644 -format {%Y-
%m-
%d
%H
:%M
:%S
}
647 set committer_name
{}
648 set committer_email
{}
649 set committer_time
{}
650 catch {set committer_name
$header($cmit,committer
)}
651 catch {set committer_email
$header($cmit,committer-mail
)}
652 catch {set committer_time
[clock format \
653 $header($cmit,committer-time
) \
654 -format {%Y-
%m-
%d
%H
:%M
:%S
}
657 if {[catch {set msg
$header($cmit,message)}]} {
660 set fd
[open "| git cat-file commit $cmit" r
]
661 fconfigure $fd -encoding binary -translation lf
662 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
665 while {[gets $fd line
] > 0} {
666 if {[string match
{encoding *} $line]} {
667 set enc
[string tolower
[string range
$line 9 end
]]
670 set msg
[encoding convertfrom
$enc [read $fd]]
671 set msg
[string trim
$msg]
674 set author_name
[encoding convertfrom
$enc $author_name]
675 set committer_name
[encoding convertfrom
$enc $committer_name]
677 set header
($cmit,author
) $author_name
678 set header
($cmit,committer
) $committer_name
680 set header
($cmit,message) $msg
683 $w_cmit insert end
"commit $cmit\n" header_key
684 $w_cmit insert end
"Author:\t" header_key
685 $w_cmit insert end
"$author_name $author_email" header_val
686 $w_cmit insert end
"$author_time\n" header_val
688 $w_cmit insert end
"Committer:\t" header_key
689 $w_cmit insert end
"$committer_name $committer_email" header_val
690 $w_cmit insert end
"$committer_time\n" header_val
692 if {$file ne
$path} {
693 $w_cmit insert end
"Original File:\t" header_key
694 $w_cmit insert end
"[escape_path $file]\n" header_val
697 $w_cmit insert end
"\n$msg"
699 $w_cmit conf
-state disabled
701 set highlight_line
$lno
702 set highlight_commit
$cmit
704 if {$highlight_commit eq
$tooltip_commit} {
709 method _copycommit
{} {
710 set pos
@$::cursorX,$::cursorY
711 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
712 set dat
[lindex $line_data $lno]
722 method _show_tooltip
{cur_w pos
} {
723 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
724 set dat
[lindex $line_data $lno]
729 set cmit
[lindex $dat 0]
731 if {$cmit eq
$highlight_commit} {
736 if {$cmit eq
$tooltip_commit} {
737 _position_tooltip
$this
738 } elseif
{$tooltip_wm ne
{}} {
739 _open_tooltip
$this $cur_w
740 } elseif
{$tooltip_timer eq
{}} {
741 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
745 method _open_tooltip
{cur_w
} {
747 set pos_x
[winfo pointerx
$cur_w]
748 set pos_y
[winfo pointery
$cur_w]
749 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
754 set pos
@[join [list \
755 [expr {$pos_x - [winfo rootx
$cur_w]}] \
756 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
757 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
758 set dat
[lindex $line_data $lno]
759 set cmit
[lindex $dat 0]
760 set file [lindex $dat 1]
765 catch {set author_name
$header($cmit,author
)}
766 catch {set author_email
$header($cmit,author-mail
)}
767 catch {set author_time
[clock format \
768 $header($cmit,author-time
) \
769 -format {%Y-
%m-
%d
%H
:%M
:%S
}
772 set committer_name
{}
773 set committer_email
{}
774 set committer_time
{}
775 catch {set committer_name
$header($cmit,committer
)}
776 catch {set committer_email
$header($cmit,committer-mail
)}
777 catch {set committer_time
[clock format \
778 $header($cmit,committer-time
) \
779 -format {%Y-
%m-
%d
%H
:%M
:%S
}
783 catch {set summary
$header($cmit,summary
)}
785 set tooltip_commit
$cmit
786 set tooltip_text
"commit $cmit
787 $author_name $author_email $author_time
790 if {$file ne
$path} {
791 append tooltip_text
"
793 Original File: $file"
796 if {$tooltip_wm ne
"$cur_w.tooltip"} {
799 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
800 wm overrideredirect
$tooltip_wm 1
801 wm transient
$tooltip_wm [winfo toplevel $cur_w]
802 pack [label $tooltip_wm.
label \
803 -background lightyellow
\
805 -textvariable @tooltip_text
\
808 _position_tooltip
$this
811 method _position_tooltip
{} {
812 set req_w
[winfo reqwidth
$tooltip_wm.
label]
813 set req_h
[winfo reqheight
$tooltip_wm.
label]
814 set pos_x
[expr {[winfo pointerx .
] + 5}]
815 set pos_y
[expr {[winfo pointery .
] + 10}]
817 set g
"${req_w}x${req_h}"
818 if {$pos_x >= 0} {append g
+}
820 if {$pos_y >= 0} {append g
+}
823 wm geometry
$tooltip_wm $g
827 method _hide_tooltip
{} {
828 if {$tooltip_wm ne
{}} {
831 set tooltip_commit
{}
833 if {$tooltip_timer ne
{}} {
834 after cancel
$tooltip_timer
839 method _resize
{new_height
} {
840 set diff
[expr {$new_height - $old_height}]
841 if {$diff == 0} return
843 set my
[expr {[winfo height
$w.file_pane
] - 25}]
844 set o
[$w.file_pane sash coord
0]
846 set oy
[expr {[lindex $o 1] + $diff}]
847 if {$oy < 0} {set oy
0}
848 if {$oy > $my} {set oy
$my}
849 $w.file_pane sash
place 0 $ox $oy
851 set old_height
$new_height