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 -image ::blame::img_back_arrow \
75 -activebackground orange
76 bind $w_back <Button-1
> "
77 if {\[$w_back cget -state\] eq {normal}} {
81 label $w.header.commit
\
82 -textvariable @commit
\
86 label $w.header.path_l
\
91 set w_path
$w.header.path
96 pack $w.header.commit_l
-side left
97 pack $w_back -side left
98 pack $w.header.commit
-side left
99 pack $w_path -fill x
-side right
100 pack $w.header.path_l
-side right
102 panedwindow $w.file_pane
-orient vertical
103 frame $w.file_pane.out
104 frame $w.file_pane.cm
105 $w.file_pane add
$w.file_pane.out
\
110 $w.file_pane add
$w.file_pane.cm
\
116 set w_load
$w.file_pane.out.loaded_t
118 -background white
-borderwidth 0 \
124 $w_load tag conf annotated
-background grey
126 set w_line
$w.file_pane.out.linenumber_t
128 -background white
-borderwidth 0 \
134 $w_line tag conf linenumber
-justify right
136 set w_cgrp
$w.file_pane.out.commit_t
138 -background white
-borderwidth 0 \
144 $w_cgrp tag conf curr_commit
145 $w_cgrp tag conf prior_commit
\
148 $w_cgrp tag
bind prior_commit
\
150 "[cb _load_commit @%x,%y];break"
152 set w_file
$w.file_pane.out.file_t
154 -background white
-borderwidth 0 \
159 -xscrollcommand [list $w.file_pane.out.sbx
set] \
162 scrollbar $w.file_pane.out.sbx
\
164 -command [list $w_file xview
]
165 scrollbar $w.file_pane.out.sby
\
167 -command [list scrollbar2many
[list \
178 $w.file_pane.out.sby
\
180 grid conf
$w.file_pane.out.sbx
-column 3 -sticky we
181 grid columnconfigure
$w.file_pane.out
3 -weight 1
182 grid rowconfigure
$w.file_pane.out
0 -weight 1
184 set w_cmit
$w.file_pane.cm.t
186 -background white
-borderwidth 0 \
191 -xscrollcommand [list $w.file_pane.cm.sbx
set] \
192 -yscrollcommand [list $w.file_pane.cm.sby
set] \
194 $w_cmit tag conf header_key
\
196 -background $active_color \
198 $w_cmit tag conf header_val
\
199 -background $active_color \
201 $w_cmit tag
raise sel
202 scrollbar $w.file_pane.cm.sbx
\
204 -command [list $w_cmit xview
]
205 scrollbar $w.file_pane.cm.sby
\
207 -command [list $w_cmit yview
]
208 pack $w.file_pane.cm.sby
-side right
-fill y
209 pack $w.file_pane.cm.sbx
-side bottom
-fill x
210 pack $w_cmit -expand 1 -fill both
216 -textvariable @status
\
219 pack $w.status.l
-side left
221 menu $w.ctxm
-tearoff 0
222 $w.ctxm add command
\
223 -label "Copy Commit" \
224 -command [cb _copycommit
]
231 $i conf
-cursor $cursor_ptr
232 $i conf
-yscrollcommand \
233 [list many2scrollbar
[list \
238 ] yview
$w.file_pane.out.sby
]
241 [cb _click $i @%x,%y]
244 bind $i <Any-Motion
> [cb _show_tooltip
$i @%x
,%y
]
245 bind $i <Any-Enter
> [cb _hide_tooltip
]
246 bind $i <Any-Leave
> [cb _hide_tooltip
]
252 tk_popup $w.ctxm %X %Y
262 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
263 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
264 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
265 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
266 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
267 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
268 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
269 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
270 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
271 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
274 bind $w_cmit <Button-1
> [list focus $w_cmit]
275 bind $top <Visibility
> [list focus $top]
276 bind $w_file <Destroy
> [list delete_this
$this]
278 grid configure
$w.header
-sticky ew
279 grid configure
$w.file_pane
-sticky nsew
280 grid configure
$w.status
-sticky ew
281 grid columnconfigure
$top 0 -weight 1
282 grid rowconfigure
$top 0 -weight 0
283 grid rowconfigure
$top 1 -weight 1
284 grid rowconfigure
$top 2 -weight 0
286 set req_w
[winfo reqwidth
$top]
287 set req_h
[winfo reqheight
$top]
288 if {$req_w < 600} {set req_w
600}
289 if {$req_h < 400} {set req_h
400}
290 set g
"${req_w}x${req_h}"
294 set old_height
[winfo height
$w.file_pane
]
295 $w.file_pane sash
place 0 \
296 [lindex [$w.file_pane sash coord
0] 0] \
297 [expr {int
($old_height * 0.70)}]
298 bind $w.file_pane
<Configure
> \
299 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
307 if {$total_lines != 0 ||
$current_fd ne
{}} {
308 if {$current_fd ne
{}} {
309 catch {close $current_fd}
313 set highlight_line
-1
314 set highlight_commit
{}
320 array unset line_commit
321 array unset line_file
323 $w_load conf
-state normal
324 $w_cgrp conf
-state normal
325 $w_line conf
-state normal
326 $w_file conf
-state normal
328 $w_load delete
0.0 end
329 $w_cgrp delete
0.0 end
330 $w_line delete
0.0 end
331 $w_file delete
0.0 end
333 $w_load conf
-state disabled
334 $w_cgrp conf
-state disabled
335 $w_line conf
-state disabled
336 $w_file conf
-state disabled
339 if {[winfo exists
$w.status.c
]} {
340 $w.status.c coords bar
0 0 0 20
344 -height [expr {int
([winfo reqheight
$w.status.l
] * 0.6)}] \
348 $w.status.c create rectangle
0 0 0 20 -tags bar
-fill navy
349 pack $w.status.c
-side right
352 if {$history eq
{}} {
353 $w_back conf
-state disabled
355 $w_back conf
-state normal
357 lappend history [list $commit $path]
359 set status
"Loading $commit:[escape_path $path]..."
360 $w_path conf
-text [escape_path
$path]
362 set fd
[open $path r
]
364 set cmd
[list git cat-file blob
"$commit:$path"]
365 set fd
[open "| $cmd" r
]
367 fconfigure $fd -blocking 0 -translation lf
-encoding binary
368 fileevent $fd readable
[cb _read_file
$fd]
372 method _history_menu
{} {
374 if {[winfo exists
$m]} {
380 for {set i
[expr {[llength $history] - 2}]
381 } {$i >= 0} {incr i
-1} {
382 set e
[lindex $history $i]
386 if {[regexp {^
[0-9a-f
]{40}$} $c]} {
387 set t
[string range
$c 0 8]...
391 if {![catch {set summary
$header($c,summary
)}]} {
395 $m add command
-label $t -command [cb _goback
$i $c $f]
397 set X
[winfo rootx
$w_back]
398 set Y
[expr {[winfo rooty
$w_back] + [winfo height
$w_back]}]
402 method _goback
{i c f
} {
403 set history [lrange $history 0 [expr {$i - 1}]]
409 method _read_file
{fd
} {
410 if {$fd ne
$current_fd} {
415 $w_load conf
-state normal
416 $w_cgrp conf
-state normal
417 $w_line conf
-state normal
418 $w_file conf
-state normal
419 while {[gets $fd line
] >= 0} {
420 regsub "\r\$" $line {} line
423 if {$total_lines > 1} {
424 $w_load insert end
"\n"
425 $w_cgrp insert end
"\n"
426 $w_line insert end
"\n"
427 $w_file insert end
"\n"
430 $w_line insert end
"$total_lines" linenumber
431 $w_file insert end
"$line"
433 $w_load conf
-state disabled
434 $w_cgrp conf
-state disabled
435 $w_line conf
-state disabled
436 $w_file conf
-state disabled
441 set cmd
{nice git blame
-M -C --incremental}
443 lappend cmd
--contents $path
448 set fd
[open "| $cmd" r
]
449 fconfigure $fd -blocking 0 -translation lf
-encoding binary
450 fileevent $fd readable
[cb _read_blame
$fd]
453 } ifdeleted
{ catch {close $fd} }
455 method _read_blame
{fd
} {
456 variable group_colors
458 if {$fd ne
$current_fd} {
463 $w_cgrp conf
-state normal
464 while {[gets $fd line
] >= 0} {
465 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
466 cmit original_line final_line line_count
]} {
468 set r_orig_line
$original_line
469 set r_final_line
$final_line
470 set r_line_count
$line_count
472 if {[catch {set g
$order($cmit)}]} {
473 set bg
[lindex $group_colors 0]
474 set group_colors
[lrange $group_colors 1 end
]
475 lappend group_colors
$bg
477 $w_cgrp tag conf g
$cmit -background $bg
478 $w_line tag conf g
$cmit -background $bg
479 $w_file tag conf g
$cmit -background $bg
481 set order
($cmit) $commit_count
483 lappend commit_list
$cmit
485 } elseif
{[string match
{filename *} $line]} {
486 set file [string range
$line 9 end
]
488 set lno
$r_final_line
491 if {[regexp {^
0{40}$} $cmit]} {
493 set commit_type curr_commit
494 } elseif
{$cmit eq
$commit} {
496 set commit_type curr_commit
498 set commit_type prior_commit
499 set commit_abbr
[string range
$cmit 0 4]
504 catch {set a_name
$header($cmit,author
)}
505 while {$a_name ne
{}} {
506 if {![regexp {^
([[:upper
:]])} $a_name _a
]} break
507 append author_abbr
$_a
510 {^
[[:upper
:]][^
\s
]*\s
+} \
511 $a_name {} a_name
]} break
513 if {$author_abbr eq
{}} {
516 set author_abbr
[string range
$author_abbr 0 3]
517 while {[string length
$author_abbr] < 4} {
518 set author_abbr
" $author_abbr"
525 ![catch {set ncmit
$line_commit([expr {$first_lno - 1}])}]
526 && ![catch {set nfile
$line_file([expr {$first_lno - 1}])}]
534 set lno_e
"$lno.0 lineend + 1c"
535 if {[catch {set g g
$line_commit($lno)}]} {
536 $w_load tag add annotated
$lno.0 $lno_e
538 $w_cgrp tag remove g
$g $lno.0 $lno_e
539 $w_line tag remove g
$g $lno.0 $lno_e
540 $w_file tag remove g
$g $lno.0 $lno_e
542 $w_cgrp tag remove a
$g $lno.0 $lno_e
543 $w_line tag remove a
$g $lno.0 $lno_e
544 $w_file tag remove a
$g $lno.0 $lno_e
547 set line_commit
($lno) $cmit
548 set line_file
($lno) $file
550 $w_cgrp delete
$lno.0 "$lno.0 lineend"
551 if {$lno == $first_lno} {
552 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
553 } elseif
{$lno == [expr {$first_lno + 1}]} {
554 $w_cgrp insert
$lno.0 $author_abbr
556 $w_cgrp insert
$lno.0 { |
}
559 $w_cgrp tag add g
$cmit $lno.0 $lno_e
560 $w_line tag add g
$cmit $lno.0 $lno_e
561 $w_file tag add g
$cmit $lno.0 $lno_e
563 $w_cgrp tag add a
$cmit $lno.0 $lno_e
564 $w_line tag add a
$cmit $lno.0 $lno_e
565 $w_file tag add a
$cmit $lno.0 $lno_e
567 if {$highlight_line == -1} {
568 if {[lindex [$w_file yview
] 0] == 0} {
570 _showcommit
$this $lno
572 } elseif
{$highlight_line == $lno} {
573 _showcommit
$this $lno
582 ![catch {set ncmit
$line_commit($lno)}]
583 && ![catch {set nfile
$line_file($lno)}]
587 $w_cgrp delete
$lno.0 "$lno.0 lineend"
589 if {$lno == $first_lno} {
590 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
591 } elseif
{$lno == [expr {$first_lno + 1}]} {
592 $w_cgrp insert
$lno.0 $author_abbr
594 $w_cgrp insert
$lno.0 { |
}
599 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
600 set header
($r_commit,$key) $data
603 $w_cgrp conf
-state disabled
608 set status
{Annotation complete.
}
613 } ifdeleted
{ catch {close $fd} }
616 set have
$blame_lines
617 set total
$total_lines
619 if {$total} {set pdone
[expr {100 * $have / $total}]}
622 "Loading annotations... %i of %i lines annotated (%2i%%)" \
624 $w.status.c coords bar
0 0 $pdone 20
627 method _click
{cur_w pos
} {
628 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
629 if {$lno eq
{}} return
630 _showcommit
$this $lno
633 method _load_commit
{pos
} {
634 set lno
[lindex [split [$w_cgrp index
$pos] .
] 0]
635 if {[catch {set cmit
$line_commit($lno)}]} return
636 if {[catch {set file $line_file($lno) }]} return
643 method _showcommit
{lno
} {
645 variable active_color
647 if {$highlight_commit ne
{}} {
648 set cmit
$highlight_commit
649 $w_cgrp tag conf a
$cmit -background {}
650 $w_line tag conf a
$cmit -background {}
651 $w_file tag conf a
$cmit -background {}
654 $w_cmit conf
-state normal
655 $w_cmit delete
0.0 end
656 if {[catch {set cmit
$line_commit($lno)}]} {
658 $w_cmit insert end
"Loading annotation..."
660 $w_cgrp tag conf a
$cmit -background $active_color
661 $w_line tag conf a
$cmit -background $active_color
662 $w_file tag conf a
$cmit -background $active_color
667 catch {set author_name
$header($cmit,author
)}
668 catch {set author_email
$header($cmit,author-mail
)}
669 catch {set author_time
[clock format \
670 $header($cmit,author-time
) \
671 -format {%Y-
%m-
%d
%H
:%M
:%S
}
674 set committer_name
{}
675 set committer_email
{}
676 set committer_time
{}
677 catch {set committer_name
$header($cmit,committer
)}
678 catch {set committer_email
$header($cmit,committer-mail
)}
679 catch {set committer_time
[clock format \
680 $header($cmit,committer-time
) \
681 -format {%Y-
%m-
%d
%H
:%M
:%S
}
684 if {[catch {set msg
$header($cmit,message)}]} {
687 set fd
[open "| git cat-file commit $cmit" r
]
688 fconfigure $fd -encoding binary -translation lf
689 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
692 while {[gets $fd line
] > 0} {
693 if {[string match
{encoding *} $line]} {
694 set enc
[string tolower
[string range
$line 9 end
]]
697 set msg
[encoding convertfrom
$enc [read $fd]]
698 set msg
[string trim
$msg]
701 set author_name
[encoding convertfrom
$enc $author_name]
702 set committer_name
[encoding convertfrom
$enc $committer_name]
704 set header
($cmit,author
) $author_name
705 set header
($cmit,committer
) $committer_name
707 set header
($cmit,message) $msg
710 $w_cmit insert end
"commit $cmit\n" header_key
711 $w_cmit insert end
"Author:\t" header_key
712 $w_cmit insert end
"$author_name $author_email" header_val
713 $w_cmit insert end
"$author_time\n" header_val
715 $w_cmit insert end
"Committer:\t" header_key
716 $w_cmit insert end
"$committer_name $committer_email" header_val
717 $w_cmit insert end
"$committer_time\n" header_val
719 if {$line_file($lno) ne
$path} {
720 $w_cmit insert end
"Original File:\t" header_key
721 $w_cmit insert end
"[escape_path $line_file($lno)]\n" header_val
724 $w_cmit insert end
"\n$msg"
726 $w_cmit conf
-state disabled
728 set highlight_line
$lno
729 set highlight_commit
$cmit
731 if {$highlight_commit eq
$tooltip_commit} {
736 method _copycommit
{} {
737 set pos
@$::cursorX,$::cursorY
738 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
739 if {![catch {set commit
$line_commit($lno)}]} {
748 method _show_tooltip
{cur_w pos
} {
749 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
750 if {[catch {set cmit
$line_commit($lno)}]} {
755 if {$cmit eq
$highlight_commit} {
760 if {$cmit eq
$tooltip_commit} {
761 _position_tooltip
$this
762 } elseif
{$tooltip_wm ne
{}} {
763 _open_tooltip
$this $cur_w
764 } elseif
{$tooltip_timer eq
{}} {
765 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
769 method _open_tooltip
{cur_w
} {
771 set pos_x
[winfo pointerx
$cur_w]
772 set pos_y
[winfo pointery
$cur_w]
773 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
778 set pos
@[join [list \
779 [expr {$pos_x - [winfo rootx
$cur_w]}] \
780 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
781 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
782 set cmit
$line_commit($lno)
787 catch {set author_name
$header($cmit,author
)}
788 catch {set author_email
$header($cmit,author-mail
)}
789 catch {set author_time
[clock format \
790 $header($cmit,author-time
) \
791 -format {%Y-
%m-
%d
%H
:%M
:%S
}
794 set committer_name
{}
795 set committer_email
{}
796 set committer_time
{}
797 catch {set committer_name
$header($cmit,committer
)}
798 catch {set committer_email
$header($cmit,committer-mail
)}
799 catch {set committer_time
[clock format \
800 $header($cmit,committer-time
) \
801 -format {%Y-
%m-
%d
%H
:%M
:%S
}
805 catch {set summary
$header($cmit,summary
)}
807 set tooltip_commit
$cmit
808 set tooltip_text
"commit $cmit
809 $author_name $author_email $author_time
812 set file $line_file($lno)
813 if {$file ne
$path} {
814 append tooltip_text
"
816 Original File: $file"
819 if {$tooltip_wm ne
"$cur_w.tooltip"} {
822 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
823 wm overrideredirect
$tooltip_wm 1
824 wm transient
$tooltip_wm [winfo toplevel $cur_w]
825 pack [label $tooltip_wm.
label \
826 -background lightyellow
\
828 -textvariable @tooltip_text
\
831 _position_tooltip
$this
834 method _position_tooltip
{} {
835 set req_w
[winfo reqwidth
$tooltip_wm.
label]
836 set req_h
[winfo reqheight
$tooltip_wm.
label]
837 set pos_x
[expr {[winfo pointerx .
] + 5}]
838 set pos_y
[expr {[winfo pointery .
] + 10}]
840 set g
"${req_w}x${req_h}"
841 if {$pos_x >= 0} {append g
+}
843 if {$pos_y >= 0} {append g
+}
846 wm geometry
$tooltip_wm $g
850 method _hide_tooltip
{} {
851 if {$tooltip_wm ne
{}} {
854 set tooltip_commit
{}
856 if {$tooltip_timer ne
{}} {
857 after cancel
$tooltip_timer
862 method _resize
{new_height
} {
863 set diff
[expr {$new_height - $old_height}]
864 if {$diff == 0} return
866 set my
[expr {[winfo height
$w.file_pane
] - 25}]
867 set o
[$w.file_pane sash coord
0]
869 set oy
[expr {[lindex $o 1] + $diff}]
870 if {$oy < 0} {set oy
0}
871 if {$oy > $my} {set oy
$my}
872 $w.file_pane sash
place 0 $ox $oy
874 set old_height
$new_height