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
)}]} {
393 if {[string length
$t] > 70} {
394 set t
[string range
$t 0 66]...
398 $m add command
-label $t -command [cb _goback
$i $c $f]
400 set X
[winfo rootx
$w_back]
401 set Y
[expr {[winfo rooty
$w_back] + [winfo height
$w_back]}]
405 method _goback
{i c f
} {
406 set history [lrange $history 0 [expr {$i - 1}]]
412 method _read_file
{fd
} {
413 if {$fd ne
$current_fd} {
418 $w_load conf
-state normal
419 $w_cgrp conf
-state normal
420 $w_line conf
-state normal
421 $w_file conf
-state normal
422 while {[gets $fd line
] >= 0} {
423 regsub "\r\$" $line {} line
426 if {$total_lines > 1} {
427 $w_load insert end
"\n"
428 $w_cgrp insert end
"\n"
429 $w_line insert end
"\n"
430 $w_file insert end
"\n"
433 $w_line insert end
"$total_lines" linenumber
434 $w_file insert end
"$line"
436 $w_load conf
-state disabled
437 $w_cgrp conf
-state disabled
438 $w_line conf
-state disabled
439 $w_file conf
-state disabled
444 set cmd
{nice git blame
-M -C --incremental}
446 lappend cmd
--contents $path
451 set fd
[open "| $cmd" r
]
452 fconfigure $fd -blocking 0 -translation lf
-encoding binary
453 fileevent $fd readable
[cb _read_blame
$fd]
456 } ifdeleted
{ catch {close $fd} }
458 method _read_blame
{fd
} {
459 variable group_colors
461 if {$fd ne
$current_fd} {
466 $w_cgrp conf
-state normal
467 while {[gets $fd line
] >= 0} {
468 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
469 cmit original_line final_line line_count
]} {
471 set r_orig_line
$original_line
472 set r_final_line
$final_line
473 set r_line_count
$line_count
475 if {[catch {set g
$order($cmit)}]} {
476 set bg
[lindex $group_colors 0]
477 set group_colors
[lrange $group_colors 1 end
]
478 lappend group_colors
$bg
480 $w_cgrp tag conf g
$cmit -background $bg
481 $w_line tag conf g
$cmit -background $bg
482 $w_file tag conf g
$cmit -background $bg
484 set order
($cmit) $commit_count
486 lappend commit_list
$cmit
488 } elseif
{[string match
{filename *} $line]} {
489 set file [string range
$line 9 end
]
491 set lno
$r_final_line
494 if {[regexp {^
0{40}$} $cmit]} {
496 set commit_type curr_commit
497 } elseif
{$cmit eq
$commit} {
499 set commit_type curr_commit
501 set commit_type prior_commit
502 set commit_abbr
[string range
$cmit 0 4]
507 catch {set a_name
$header($cmit,author
)}
508 while {$a_name ne
{}} {
509 if {![regexp {^
([[:upper
:]])} $a_name _a
]} break
510 append author_abbr
$_a
513 {^
[[:upper
:]][^
\s
]*\s
+} \
514 $a_name {} a_name
]} break
516 if {$author_abbr eq
{}} {
519 set author_abbr
[string range
$author_abbr 0 3]
520 while {[string length
$author_abbr] < 4} {
521 set author_abbr
" $author_abbr"
528 ![catch {set ncmit
$line_commit([expr {$first_lno - 1}])}]
529 && ![catch {set nfile
$line_file([expr {$first_lno - 1}])}]
537 set lno_e
"$lno.0 lineend + 1c"
538 if {[catch {set g g
$line_commit($lno)}]} {
539 $w_load tag add annotated
$lno.0 $lno_e
541 $w_cgrp tag remove g
$g $lno.0 $lno_e
542 $w_line tag remove g
$g $lno.0 $lno_e
543 $w_file tag remove g
$g $lno.0 $lno_e
545 $w_cgrp tag remove a
$g $lno.0 $lno_e
546 $w_line tag remove a
$g $lno.0 $lno_e
547 $w_file tag remove a
$g $lno.0 $lno_e
550 set line_commit
($lno) $cmit
551 set line_file
($lno) $file
553 $w_cgrp delete
$lno.0 "$lno.0 lineend"
554 if {$lno == $first_lno} {
555 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
556 } elseif
{$lno == [expr {$first_lno + 1}]} {
557 $w_cgrp insert
$lno.0 $author_abbr
559 $w_cgrp insert
$lno.0 { |
}
562 $w_cgrp tag add g
$cmit $lno.0 $lno_e
563 $w_line tag add g
$cmit $lno.0 $lno_e
564 $w_file tag add g
$cmit $lno.0 $lno_e
566 $w_cgrp tag add a
$cmit $lno.0 $lno_e
567 $w_line tag add a
$cmit $lno.0 $lno_e
568 $w_file tag add a
$cmit $lno.0 $lno_e
570 if {$highlight_line == -1} {
571 if {[lindex [$w_file yview
] 0] == 0} {
573 _showcommit
$this $lno
575 } elseif
{$highlight_line == $lno} {
576 _showcommit
$this $lno
585 ![catch {set ncmit
$line_commit($lno)}]
586 && ![catch {set nfile
$line_file($lno)}]
590 $w_cgrp delete
$lno.0 "$lno.0 lineend"
592 if {$lno == $first_lno} {
593 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
594 } elseif
{$lno == [expr {$first_lno + 1}]} {
595 $w_cgrp insert
$lno.0 $author_abbr
597 $w_cgrp insert
$lno.0 { |
}
602 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
603 set header
($r_commit,$key) $data
606 $w_cgrp conf
-state disabled
611 set status
{Annotation complete.
}
616 } ifdeleted
{ catch {close $fd} }
619 set have
$blame_lines
620 set total
$total_lines
622 if {$total} {set pdone
[expr {100 * $have / $total}]}
625 "Loading annotations... %i of %i lines annotated (%2i%%)" \
627 $w.status.c coords bar
0 0 $pdone 20
630 method _click
{cur_w pos
} {
631 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
632 if {$lno eq
{}} return
633 _showcommit
$this $lno
636 method _load_commit
{pos
} {
637 set lno
[lindex [split [$w_cgrp index
$pos] .
] 0]
638 if {[catch {set cmit
$line_commit($lno)}]} return
639 if {[catch {set file $line_file($lno) }]} return
646 method _showcommit
{lno
} {
648 variable active_color
650 if {$highlight_commit ne
{}} {
651 set cmit
$highlight_commit
652 $w_cgrp tag conf a
$cmit -background {}
653 $w_line tag conf a
$cmit -background {}
654 $w_file tag conf a
$cmit -background {}
657 $w_cmit conf
-state normal
658 $w_cmit delete
0.0 end
659 if {[catch {set cmit
$line_commit($lno)}]} {
661 $w_cmit insert end
"Loading annotation..."
663 $w_cgrp tag conf a
$cmit -background $active_color
664 $w_line tag conf a
$cmit -background $active_color
665 $w_file tag conf a
$cmit -background $active_color
670 catch {set author_name
$header($cmit,author
)}
671 catch {set author_email
$header($cmit,author-mail
)}
672 catch {set author_time
[clock format \
673 $header($cmit,author-time
) \
674 -format {%Y-
%m-
%d
%H
:%M
:%S
}
677 set committer_name
{}
678 set committer_email
{}
679 set committer_time
{}
680 catch {set committer_name
$header($cmit,committer
)}
681 catch {set committer_email
$header($cmit,committer-mail
)}
682 catch {set committer_time
[clock format \
683 $header($cmit,committer-time
) \
684 -format {%Y-
%m-
%d
%H
:%M
:%S
}
687 if {[catch {set msg
$header($cmit,message)}]} {
690 set fd
[open "| git cat-file commit $cmit" r
]
691 fconfigure $fd -encoding binary -translation lf
692 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
695 while {[gets $fd line
] > 0} {
696 if {[string match
{encoding *} $line]} {
697 set enc
[string tolower
[string range
$line 9 end
]]
700 set msg
[encoding convertfrom
$enc [read $fd]]
701 set msg
[string trim
$msg]
704 set author_name
[encoding convertfrom
$enc $author_name]
705 set committer_name
[encoding convertfrom
$enc $committer_name]
707 set header
($cmit,author
) $author_name
708 set header
($cmit,committer
) $committer_name
710 set header
($cmit,message) $msg
713 $w_cmit insert end
"commit $cmit\n" header_key
714 $w_cmit insert end
"Author:\t" header_key
715 $w_cmit insert end
"$author_name $author_email" header_val
716 $w_cmit insert end
"$author_time\n" header_val
718 $w_cmit insert end
"Committer:\t" header_key
719 $w_cmit insert end
"$committer_name $committer_email" header_val
720 $w_cmit insert end
"$committer_time\n" header_val
722 if {$line_file($lno) ne
$path} {
723 $w_cmit insert end
"Original File:\t" header_key
724 $w_cmit insert end
"[escape_path $line_file($lno)]\n" header_val
727 $w_cmit insert end
"\n$msg"
729 $w_cmit conf
-state disabled
731 set highlight_line
$lno
732 set highlight_commit
$cmit
734 if {$highlight_commit eq
$tooltip_commit} {
739 method _copycommit
{} {
740 set pos
@$::cursorX,$::cursorY
741 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
742 if {![catch {set commit
$line_commit($lno)}]} {
751 method _show_tooltip
{cur_w pos
} {
752 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
753 if {[catch {set cmit
$line_commit($lno)}]} {
758 if {$cmit eq
$highlight_commit} {
763 if {$cmit eq
$tooltip_commit} {
764 _position_tooltip
$this
765 } elseif
{$tooltip_wm ne
{}} {
766 _open_tooltip
$this $cur_w
767 } elseif
{$tooltip_timer eq
{}} {
768 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
772 method _open_tooltip
{cur_w
} {
774 set pos_x
[winfo pointerx
$cur_w]
775 set pos_y
[winfo pointery
$cur_w]
776 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
781 set pos
@[join [list \
782 [expr {$pos_x - [winfo rootx
$cur_w]}] \
783 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
784 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
785 set cmit
$line_commit($lno)
790 catch {set author_name
$header($cmit,author
)}
791 catch {set author_email
$header($cmit,author-mail
)}
792 catch {set author_time
[clock format \
793 $header($cmit,author-time
) \
794 -format {%Y-
%m-
%d
%H
:%M
:%S
}
797 set committer_name
{}
798 set committer_email
{}
799 set committer_time
{}
800 catch {set committer_name
$header($cmit,committer
)}
801 catch {set committer_email
$header($cmit,committer-mail
)}
802 catch {set committer_time
[clock format \
803 $header($cmit,committer-time
) \
804 -format {%Y-
%m-
%d
%H
:%M
:%S
}
808 catch {set summary
$header($cmit,summary
)}
810 set tooltip_commit
$cmit
811 set tooltip_text
"commit $cmit
812 $author_name $author_email $author_time
815 set file $line_file($lno)
816 if {$file ne
$path} {
817 append tooltip_text
"
819 Original File: $file"
822 if {$tooltip_wm ne
"$cur_w.tooltip"} {
825 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
826 wm overrideredirect
$tooltip_wm 1
827 wm transient
$tooltip_wm [winfo toplevel $cur_w]
828 pack [label $tooltip_wm.
label \
829 -background lightyellow
\
831 -textvariable @tooltip_text
\
834 _position_tooltip
$this
837 method _position_tooltip
{} {
838 set req_w
[winfo reqwidth
$tooltip_wm.
label]
839 set req_h
[winfo reqheight
$tooltip_wm.
label]
840 set pos_x
[expr {[winfo pointerx .
] + 5}]
841 set pos_y
[expr {[winfo pointery .
] + 10}]
843 set g
"${req_w}x${req_h}"
844 if {$pos_x >= 0} {append g
+}
846 if {$pos_y >= 0} {append g
+}
849 wm geometry
$tooltip_wm $g
853 method _hide_tooltip
{} {
854 if {$tooltip_wm ne
{}} {
857 set tooltip_commit
{}
859 if {$tooltip_timer ne
{}} {
860 after cancel
$tooltip_timer
865 method _resize
{new_height
} {
866 set diff
[expr {$new_height - $old_height}]
867 if {$diff == 0} return
869 set my
[expr {[winfo height
$w.file_pane
] - 25}]
870 set o
[$w.file_pane sash coord
0]
872 set oy
[expr {[lindex $o 1] + $diff}]
873 if {$oy < 0} {set oy
0}
874 if {$oy > $my} {set oy
$my}
875 $w.file_pane sash
place 0 $ox $oy
877 set old_height
$new_height