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_file
; # text column: actual file data
18 field w_cmit
; # pane showing commit message
19 field status
; # text variable bound to status bar
20 field old_height
; # last known height of $w.file_pane
22 field current_fd
{} ; # background process running
23 field highlight_line
-1 ; # current line selected
24 field highlight_commit
{} ; # sha1 of commit selected
26 field total_lines
0 ; # total length of file
27 field blame_lines
0 ; # number of lines computed
28 field commit_count
0 ; # number of commits in $commit_list
29 field commit_list
{} ; # list of commit sha1 in receipt order
30 field order
; # array commit -> receipt order
31 field header
; # array commit,key -> header field
32 field line_commit
; # array line -> sha1 commit
33 field line_file
; # array line -> file name
35 field r_commit
; # commit currently being parsed
36 field r_orig_line
; # original line number
37 field r_final_line
; # final line number
38 field r_line_count
; # lines in this region
40 field tooltip_wm
{} ; # Current tooltip toplevel, if open
41 field tooltip_timer
{} ; # Current timer event for our tooltip
42 field tooltip_commit
{} ; # Commit in tooltip
43 field tooltip_text
{} ; # Text in current tooltip
45 variable active_color
#c0edc5
46 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_line
$w.file_pane.out.linenumber_t
119 -highlightthickness 0 \
121 -background white
-borderwidth 0 \
127 $w_line tag conf linenumber
-justify right
-rmargin 5
129 set w_cgrp
$w.file_pane.out.commit_t
132 -highlightthickness 0 \
134 -background white
-borderwidth 0 \
140 $w_cgrp tag conf curr_commit
141 $w_cgrp tag conf prior_commit
\
144 $w_cgrp tag
bind prior_commit
\
146 "[cb _load_commit @%x,%y];break"
148 set w_file
$w.file_pane.out.file_t
151 -highlightthickness 0 \
153 -background white
-borderwidth 0 \
158 -xscrollcommand [list $w.file_pane.out.sbx
set] \
161 scrollbar $w.file_pane.out.sbx
\
163 -command [list $w_file xview
]
164 scrollbar $w.file_pane.out.sby
\
166 -command [list scrollbar2many
[list \
175 $w.file_pane.out.sby
\
177 grid conf
$w.file_pane.out.sbx
-column 2 -sticky we
178 grid columnconfigure
$w.file_pane.out
2 -weight 1
179 grid rowconfigure
$w.file_pane.out
0 -weight 1
181 set w_cmit
$w.file_pane.cm.t
183 -background white
-borderwidth 0 \
188 -xscrollcommand [list $w.file_pane.cm.sbx
set] \
189 -yscrollcommand [list $w.file_pane.cm.sby
set] \
191 $w_cmit tag conf header_key
\
193 -background $active_color \
195 $w_cmit tag conf header_val
\
196 -background $active_color \
198 $w_cmit tag
raise sel
199 scrollbar $w.file_pane.cm.sbx
\
201 -command [list $w_cmit xview
]
202 scrollbar $w.file_pane.cm.sby
\
204 -command [list $w_cmit yview
]
205 pack $w.file_pane.cm.sby
-side right
-fill y
206 pack $w.file_pane.cm.sbx
-side bottom
-fill x
207 pack $w_cmit -expand 1 -fill both
213 -textvariable @status
\
216 pack $w.status.l
-side left
218 menu $w.ctxm
-tearoff 0
219 $w.ctxm add command
\
220 -label "Copy Commit" \
221 -command [cb _copycommit
]
227 $i conf
-cursor $cursor_ptr
228 $i conf
-yscrollcommand \
229 [list many2scrollbar
[list \
233 ] yview
$w.file_pane.out.sby
]
236 [cb _click $i @%x,%y]
239 bind $i <Any-Motion
> [cb _show_tooltip
$i @%x
,%y
]
240 bind $i <Any-Enter
> [cb _hide_tooltip
]
241 bind $i <Any-Leave
> [cb _hide_tooltip
]
247 tk_popup $w.ctxm %X %Y
256 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
257 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
258 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
259 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
260 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
261 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
262 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
263 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
264 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
265 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
268 bind $w_cmit <Button-1
> [list focus $w_cmit]
269 bind $top <Visibility
> [list focus $top]
270 bind $w_file <Destroy
> [list delete_this
$this]
272 grid configure
$w.header
-sticky ew
273 grid configure
$w.file_pane
-sticky nsew
274 grid configure
$w.status
-sticky ew
275 grid columnconfigure
$top 0 -weight 1
276 grid rowconfigure
$top 0 -weight 0
277 grid rowconfigure
$top 1 -weight 1
278 grid rowconfigure
$top 2 -weight 0
280 set req_w
[winfo reqwidth
$top]
281 set req_h
[winfo reqheight
$top]
282 if {$req_w < 600} {set req_w
600}
283 if {$req_h < 400} {set req_h
400}
284 set g
"${req_w}x${req_h}"
288 set old_height
[winfo height
$w.file_pane
]
289 $w.file_pane sash
place 0 \
290 [lindex [$w.file_pane sash coord
0] 0] \
291 [expr {int
($old_height * 0.70)}]
292 bind $w.file_pane
<Configure
> \
293 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
301 if {$total_lines != 0 ||
$current_fd ne
{}} {
302 if {$current_fd ne
{}} {
303 catch {close $current_fd}
307 set highlight_line
-1
308 set highlight_commit
{}
314 array unset line_commit
315 array unset line_file
317 $w_cgrp conf
-state normal
318 $w_line conf
-state normal
319 $w_file conf
-state normal
321 $w_cgrp delete
0.0 end
322 $w_line delete
0.0 end
323 $w_file delete
0.0 end
325 $w_cgrp conf
-state disabled
326 $w_line conf
-state disabled
327 $w_file conf
-state disabled
330 if {[winfo exists
$w.status.c
]} {
331 $w.status.c coords bar
0 0 0 20
335 -height [expr {int
([winfo reqheight
$w.status.l
] * 0.6)}] \
339 $w.status.c create rectangle
0 0 0 20 -tags bar
-fill navy
340 pack $w.status.c
-side right
343 if {$history eq
{}} {
344 $w_back conf
-state disabled
346 $w_back conf
-state normal
348 lappend history [list $commit $path]
350 set status
"Loading $commit:[escape_path $path]..."
351 $w_path conf
-text [escape_path
$path]
353 set fd
[open $path r
]
355 set cmd
[list git cat-file blob
"$commit:$path"]
356 set fd
[open "| $cmd" r
]
358 fconfigure $fd -blocking 0 -translation lf
-encoding binary
359 fileevent $fd readable
[cb _read_file
$fd]
363 method _history_menu
{} {
365 if {[winfo exists
$m]} {
371 for {set i
[expr {[llength $history] - 2}]
372 } {$i >= 0} {incr i
-1} {
373 set e
[lindex $history $i]
377 if {[regexp {^
[0-9a-f
]{40}$} $c]} {
378 set t
[string range
$c 0 8]...
382 if {![catch {set summary
$header($c,summary
)}]} {
384 if {[string length
$t] > 70} {
385 set t
[string range
$t 0 66]...
389 $m add command
-label $t -command [cb _goback
$i $c $f]
391 set X
[winfo rootx
$w_back]
392 set Y
[expr {[winfo rooty
$w_back] + [winfo height
$w_back]}]
396 method _goback
{i c f
} {
397 set history [lrange $history 0 [expr {$i - 1}]]
403 method _read_file
{fd
} {
404 if {$fd ne
$current_fd} {
409 $w_cgrp conf
-state normal
410 $w_line conf
-state normal
411 $w_file conf
-state normal
412 while {[gets $fd line
] >= 0} {
413 regsub "\r\$" $line {} line
416 if {$total_lines > 1} {
417 $w_cgrp insert end
"\n"
418 $w_line insert end
"\n"
419 $w_file insert end
"\n"
422 $w_line insert end
"$total_lines" linenumber
423 $w_file insert end
"$line"
425 $w_cgrp conf
-state disabled
426 $w_line conf
-state disabled
427 $w_file conf
-state disabled
432 set cmd
{nice git blame
-M -C --incremental}
434 lappend cmd
--contents $path
439 set fd
[open "| $cmd" r
]
440 fconfigure $fd -blocking 0 -translation lf
-encoding binary
441 fileevent $fd readable
[cb _read_blame
$fd]
444 } ifdeleted
{ catch {close $fd} }
446 method _read_blame
{fd
} {
447 variable group_colors
449 if {$fd ne
$current_fd} {
454 $w_cgrp conf
-state normal
455 while {[gets $fd line
] >= 0} {
456 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
457 cmit original_line final_line line_count
]} {
459 set r_orig_line
$original_line
460 set r_final_line
$final_line
461 set r_line_count
$line_count
463 if {[catch {set g
$order($cmit)}]} {
464 set bg
[lindex $group_colors 0]
465 set group_colors
[lrange $group_colors 1 end
]
466 lappend group_colors
$bg
468 $w_cgrp tag conf g
$cmit -background $bg
469 $w_line tag conf g
$cmit -background $bg
470 $w_file tag conf g
$cmit -background $bg
472 set order
($cmit) $commit_count
474 lappend commit_list
$cmit
476 } elseif
{[string match
{filename *} $line]} {
477 set file [string range
$line 9 end
]
479 set lno
$r_final_line
482 if {[regexp {^
0{40}$} $cmit]} {
484 set commit_type curr_commit
485 } elseif
{$cmit eq
$commit} {
487 set commit_type curr_commit
489 set commit_type prior_commit
490 set commit_abbr
[string range
$cmit 0 4]
495 catch {set a_name
$header($cmit,author
)}
496 while {$a_name ne
{}} {
497 if {![regexp {^
([[:upper
:]])} $a_name _a
]} break
498 append author_abbr
$_a
501 {^
[[:upper
:]][^
\s
]*\s
+} \
502 $a_name {} a_name
]} break
504 if {$author_abbr eq
{}} {
507 set author_abbr
[string range
$author_abbr 0 3]
508 while {[string length
$author_abbr] < 4} {
509 set author_abbr
" $author_abbr"
516 ![catch {set ncmit
$line_commit([expr {$first_lno - 1}])}]
517 && ![catch {set nfile
$line_file([expr {$first_lno - 1}])}]
525 set lno_e
"$lno.0 lineend + 1c"
526 if {![catch {set g g
$line_commit($lno)}]} {
527 $w_cgrp tag remove g
$g $lno.0 $lno_e
528 $w_line tag remove g
$g $lno.0 $lno_e
529 $w_file tag remove g
$g $lno.0 $lno_e
531 $w_cgrp tag remove a
$g $lno.0 $lno_e
532 $w_line tag remove a
$g $lno.0 $lno_e
533 $w_file tag remove a
$g $lno.0 $lno_e
536 set line_commit
($lno) $cmit
537 set line_file
($lno) $file
539 $w_cgrp delete
$lno.0 "$lno.0 lineend"
540 if {$lno == $first_lno} {
541 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
542 } elseif
{$lno == [expr {$first_lno + 1}]} {
543 $w_cgrp insert
$lno.0 $author_abbr
545 $w_cgrp insert
$lno.0 { |
}
548 $w_cgrp tag add g
$cmit $lno.0 $lno_e
549 $w_line tag add g
$cmit $lno.0 $lno_e
550 $w_file tag add g
$cmit $lno.0 $lno_e
552 $w_cgrp tag add a
$cmit $lno.0 $lno_e
553 $w_line tag add a
$cmit $lno.0 $lno_e
554 $w_file tag add a
$cmit $lno.0 $lno_e
556 if {$highlight_line == -1} {
557 if {[lindex [$w_file yview
] 0] == 0} {
559 _showcommit
$this $lno
561 } elseif
{$highlight_line == $lno} {
562 _showcommit
$this $lno
571 ![catch {set ncmit
$line_commit($lno)}]
572 && ![catch {set nfile
$line_file($lno)}]
576 $w_cgrp delete
$lno.0 "$lno.0 lineend"
578 if {$lno == $first_lno} {
579 $w_cgrp insert
$lno.0 $commit_abbr $commit_type
580 } elseif
{$lno == [expr {$first_lno + 1}]} {
581 $w_cgrp insert
$lno.0 $author_abbr
583 $w_cgrp insert
$lno.0 { |
}
588 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
589 set header
($r_commit,$key) $data
592 $w_cgrp conf
-state disabled
597 set status
{Annotation complete.
}
602 } ifdeleted
{ catch {close $fd} }
605 set have
$blame_lines
606 set total
$total_lines
608 if {$total} {set pdone
[expr {100 * $have / $total}]}
611 "Loading annotations... %i of %i lines annotated (%2i%%)" \
613 $w.status.c coords bar
0 0 $pdone 20
616 method _click
{cur_w pos
} {
617 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
618 if {$lno eq
{}} return
619 _showcommit
$this $lno
622 method _load_commit
{pos
} {
623 set lno
[lindex [split [$w_cgrp index
$pos] .
] 0]
624 if {[catch {set cmit
$line_commit($lno)}]} return
625 if {[catch {set file $line_file($lno) }]} return
632 method _showcommit
{lno
} {
634 variable active_color
636 if {$highlight_commit ne
{}} {
637 set cmit
$highlight_commit
638 $w_cgrp tag conf a
$cmit -background {}
639 $w_line tag conf a
$cmit -background {}
640 $w_file tag conf a
$cmit -background {}
643 $w_cmit conf
-state normal
644 $w_cmit delete
0.0 end
645 if {[catch {set cmit
$line_commit($lno)}]} {
647 $w_cmit insert end
"Loading annotation..."
649 $w_cgrp tag conf a
$cmit -background $active_color
650 $w_line tag conf a
$cmit -background $active_color
651 $w_file tag conf a
$cmit -background $active_color
656 catch {set author_name
$header($cmit,author
)}
657 catch {set author_email
$header($cmit,author-mail
)}
658 catch {set author_time
[clock format \
659 $header($cmit,author-time
) \
660 -format {%Y-
%m-
%d
%H
:%M
:%S
}
663 set committer_name
{}
664 set committer_email
{}
665 set committer_time
{}
666 catch {set committer_name
$header($cmit,committer
)}
667 catch {set committer_email
$header($cmit,committer-mail
)}
668 catch {set committer_time
[clock format \
669 $header($cmit,committer-time
) \
670 -format {%Y-
%m-
%d
%H
:%M
:%S
}
673 if {[catch {set msg
$header($cmit,message)}]} {
676 set fd
[open "| git cat-file commit $cmit" r
]
677 fconfigure $fd -encoding binary -translation lf
678 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
681 while {[gets $fd line
] > 0} {
682 if {[string match
{encoding *} $line]} {
683 set enc
[string tolower
[string range
$line 9 end
]]
686 set msg
[encoding convertfrom
$enc [read $fd]]
687 set msg
[string trim
$msg]
690 set author_name
[encoding convertfrom
$enc $author_name]
691 set committer_name
[encoding convertfrom
$enc $committer_name]
693 set header
($cmit,author
) $author_name
694 set header
($cmit,committer
) $committer_name
696 set header
($cmit,message) $msg
699 $w_cmit insert end
"commit $cmit\n" header_key
700 $w_cmit insert end
"Author:\t" header_key
701 $w_cmit insert end
"$author_name $author_email" header_val
702 $w_cmit insert end
"$author_time\n" header_val
704 $w_cmit insert end
"Committer:\t" header_key
705 $w_cmit insert end
"$committer_name $committer_email" header_val
706 $w_cmit insert end
"$committer_time\n" header_val
708 if {$line_file($lno) ne
$path} {
709 $w_cmit insert end
"Original File:\t" header_key
710 $w_cmit insert end
"[escape_path $line_file($lno)]\n" header_val
713 $w_cmit insert end
"\n$msg"
715 $w_cmit conf
-state disabled
717 set highlight_line
$lno
718 set highlight_commit
$cmit
720 if {$highlight_commit eq
$tooltip_commit} {
725 method _copycommit
{} {
726 set pos
@$::cursorX,$::cursorY
727 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
728 if {![catch {set commit
$line_commit($lno)}]} {
737 method _show_tooltip
{cur_w pos
} {
738 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
739 if {[catch {set cmit
$line_commit($lno)}]} {
744 if {$cmit eq
$highlight_commit} {
749 if {$cmit eq
$tooltip_commit} {
750 _position_tooltip
$this
751 } elseif
{$tooltip_wm ne
{}} {
752 _open_tooltip
$this $cur_w
753 } elseif
{$tooltip_timer eq
{}} {
754 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
758 method _open_tooltip
{cur_w
} {
760 set pos_x
[winfo pointerx
$cur_w]
761 set pos_y
[winfo pointery
$cur_w]
762 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
767 set pos
@[join [list \
768 [expr {$pos_x - [winfo rootx
$cur_w]}] \
769 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
770 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
771 set cmit
$line_commit($lno)
776 catch {set author_name
$header($cmit,author
)}
777 catch {set author_email
$header($cmit,author-mail
)}
778 catch {set author_time
[clock format \
779 $header($cmit,author-time
) \
780 -format {%Y-
%m-
%d
%H
:%M
:%S
}
783 set committer_name
{}
784 set committer_email
{}
785 set committer_time
{}
786 catch {set committer_name
$header($cmit,committer
)}
787 catch {set committer_email
$header($cmit,committer-mail
)}
788 catch {set committer_time
[clock format \
789 $header($cmit,committer-time
) \
790 -format {%Y-
%m-
%d
%H
:%M
:%S
}
794 catch {set summary
$header($cmit,summary
)}
796 set tooltip_commit
$cmit
797 set tooltip_text
"commit $cmit
798 $author_name $author_email $author_time
801 set file $line_file($lno)
802 if {$file ne
$path} {
803 append tooltip_text
"
805 Original File: $file"
808 if {$tooltip_wm ne
"$cur_w.tooltip"} {
811 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
812 wm overrideredirect
$tooltip_wm 1
813 wm transient
$tooltip_wm [winfo toplevel $cur_w]
814 pack [label $tooltip_wm.
label \
815 -background lightyellow
\
817 -textvariable @tooltip_text
\
820 _position_tooltip
$this
823 method _position_tooltip
{} {
824 set req_w
[winfo reqwidth
$tooltip_wm.
label]
825 set req_h
[winfo reqheight
$tooltip_wm.
label]
826 set pos_x
[expr {[winfo pointerx .
] + 5}]
827 set pos_y
[expr {[winfo pointery .
] + 10}]
829 set g
"${req_w}x${req_h}"
830 if {$pos_x >= 0} {append g
+}
832 if {$pos_y >= 0} {append g
+}
835 wm geometry
$tooltip_wm $g
839 method _hide_tooltip
{} {
840 if {$tooltip_wm ne
{}} {
843 set tooltip_commit
{}
845 if {$tooltip_timer ne
{}} {
846 after cancel
$tooltip_timer
851 method _resize
{new_height
} {
852 set diff
[expr {$new_height - $old_height}]
853 if {$diff == 0} return
855 set my
[expr {[winfo height
$w.file_pane
] - 25}]
856 set o
[$w.file_pane sash coord
0]
858 set oy
[expr {[lindex $o 1] + $diff}]
859 if {$oy < 0} {set oy
0}
860 if {$oy > $my} {set oy
$my}
861 $w.file_pane sash
place 0 $ox $oy
863 set old_height
$new_height