2 # Copyright (C) 2006, 2007 Shawn Pearce
6 field commit
; # input commit to blame
7 field path
; # input filename to view in $commit
17 field highlight_line
-1 ; # current line selected
18 field highlight_commit
{} ; # sha1 of commit selected
20 field total_lines
0 ; # total length of file
21 field blame_lines
0 ; # number of lines computed
22 field commit_count
0 ; # number of commits in $commit_list
23 field commit_list
{} ; # list of commit sha1 in receipt order
24 field order
; # array commit -> receipt order
25 field header
; # array commit,key -> header field
26 field line_commit
; # array line -> sha1 commit
27 field line_file
; # array line -> file name
29 field r_commit
; # commit currently being parsed
30 field r_orig_line
; # original line number
31 field r_final_line
; # final line number
32 field r_line_count
; # lines in this region
34 field tooltip_wm
{} ; # Current tooltip toplevel, if open
35 field tooltip_timer
{} ; # Current timer event for our tooltip
36 field tooltip_commit
{} ; # Commit in tooltip
37 field tooltip_text
{} ; # Text in current tooltip
39 variable active_color
#98e1a0
40 variable group_colors
{
45 constructor new
{i_commit i_path
} {
53 wm title
$top "[appname] ([reponame]): File Viewer"
54 set status
"Loading $commit:$path..."
56 label $w.path
-text "$commit:$path" \
62 pack $w.path
-side top
-fill x
65 set w_load
$w.out.loaded_t
67 -background white
-borderwidth 0 \
73 $w_load tag conf annotated
-background grey
75 set w_line
$w.out.linenumber_t
77 -background white
-borderwidth 0 \
83 $w_line tag conf linenumber
-justify right
85 set w_cgrp
$w.out.commit_t
87 -background white
-borderwidth 0 \
94 set w_file
$w.out.file_t
96 -background white
-borderwidth 0 \
101 -xscrollcommand [list $w.out.sbx
set] \
104 scrollbar $w.out.sbx
-orient h
-command [list $w_file xview
]
105 scrollbar $w.out.sby
-orient v
\
106 -command [list scrollbar2many
[list \
119 grid conf
$w.out.sbx
-column 3 -sticky we
120 grid columnconfigure
$w.out
3 -weight 1
121 grid rowconfigure
$w.out
0 -weight 1
122 pack $w.out
-fill both
-expand 1
125 -textvariable @status
\
130 pack $w.status
-side bottom
-fill x
135 -background white
-borderwidth 0 \
140 -xscrollcommand [list $w.cm.sbx
set] \
141 -yscrollcommand [list $w.cm.sby
set] \
143 $w_cmit tag conf header_key
\
145 -background $active_color \
147 $w_cmit tag conf header_val
\
148 -background $active_color \
150 $w_cmit tag
raise sel
151 scrollbar $w.cm.sbx
-orient h
-command [list $w_cmit xview
]
152 scrollbar $w.cm.sby
-orient v
-command [list $w_cmit yview
]
153 pack $w.cm.sby
-side right
-fill y
154 pack $w.cm.sbx
-side bottom
-fill x
155 pack $w_cmit -expand 1 -fill both
156 pack $w.cm
-side bottom
-fill x
158 menu $w.ctxm
-tearoff 0
159 $w.ctxm add command
\
160 -label "Copy Commit" \
161 -command [cb _copycommit
]
168 $i conf
-cursor $cursor_ptr
169 $i conf
-yscrollcommand \
170 [list many2scrollbar
[list \
178 [cb _click $i @%x,%y]
181 bind $i <Any-Motion
> [cb _show_tooltip
$i @%x
,%y
]
182 bind $i <Any-Enter
> [cb _hide_tooltip
]
183 bind $i <Any-Leave
> [cb _hide_tooltip
]
189 tk_popup $w.ctxm %X %Y
199 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
200 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
201 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
202 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
203 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
204 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
205 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
206 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
207 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
208 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
211 bind $w_cmit <Button-1
> [list focus $w_cmit]
212 bind $top <Visibility
> [list focus $top]
213 bind $top <Destroy
> [list delete_this
$this]
216 set fd
[open $path r
]
218 set cmd
[list git cat-file blob
"$commit:$path"]
219 set fd
[open "| $cmd" r
]
221 fconfigure $fd -blocking 0 -translation lf
-encoding binary
222 fileevent $fd readable
[cb _read_file
$fd]
225 method _read_file
{fd
} {
226 $w_load conf
-state normal
227 $w_cgrp conf
-state normal
228 $w_line conf
-state normal
229 $w_file conf
-state normal
230 while {[gets $fd line
] >= 0} {
231 regsub "\r\$" $line {} line
234 if {$total_lines > 1} {
235 $w_load insert end
"\n"
236 $w_cgrp insert end
"\n"
237 $w_line insert end
"\n"
238 $w_file insert end
"\n"
241 $w_line insert end
"$total_lines" linenumber
242 $w_file insert end
"$line"
244 $w_load conf
-state disabled
245 $w_cgrp conf
-state disabled
246 $w_line conf
-state disabled
247 $w_file conf
-state disabled
252 set cmd
[list git blame
-M -C --incremental]
254 lappend cmd
--contents $path
259 set fd
[open "| $cmd" r
]
260 fconfigure $fd -blocking 0 -translation lf
-encoding binary
261 fileevent $fd readable
[cb _read_blame
$fd]
263 } ifdeleted
{ catch {close $fd} }
265 method _read_blame
{fd
} {
266 variable group_colors
268 $w_cgrp conf
-state normal
269 while {[gets $fd line
] >= 0} {
270 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
271 cmit original_line final_line line_count
]} {
273 set r_orig_line
$original_line
274 set r_final_line
$final_line
275 set r_line_count
$line_count
277 if {[catch {set g
$order($cmit)}]} {
278 set bg
[lindex $group_colors 0]
279 set group_colors
[lrange $group_colors 1 end
]
280 lappend group_colors
$bg
282 $w_cgrp tag conf g
$cmit -background $bg
283 $w_line tag conf g
$cmit -background $bg
284 $w_file tag conf g
$cmit -background $bg
286 set order
($cmit) $commit_count
288 lappend commit_list
$cmit
290 } elseif
{[string match
{filename *} $line]} {
291 set file [string range
$line 9 end
]
293 set lno
$r_final_line
296 if {[regexp {^
0{40}$} $cmit]} {
299 set abbr
[string range
$cmit 0 4]
302 if {![catch {set ncmit
$line_commit([expr {$lno - 1}])}]} {
303 if {$ncmit eq
$cmit} {
309 set lno_e
"$lno.0 lineend + 1c"
310 if {[catch {set g g
$line_commit($lno)}]} {
311 $w_load tag add annotated
$lno.0 $lno_e
313 $w_cgrp tag remove g
$g $lno.0 $lno_e
314 $w_line tag remove g
$g $lno.0 $lno_e
315 $w_file tag remove g
$g $lno.0 $lno_e
317 $w_cgrp tag remove a
$g $lno.0 $lno_e
318 $w_line tag remove a
$g $lno.0 $lno_e
319 $w_file tag remove a
$g $lno.0 $lno_e
322 set line_commit
($lno) $cmit
323 set line_file
($lno) $file
325 $w_cgrp delete
$lno.0 "$lno.0 lineend"
326 $w_cgrp insert
$lno.0 $abbr
329 $w_cgrp tag add g
$cmit $lno.0 $lno_e
330 $w_line tag add g
$cmit $lno.0 $lno_e
331 $w_file tag add g
$cmit $lno.0 $lno_e
333 $w_cgrp tag add a
$cmit $lno.0 $lno_e
334 $w_line tag add a
$cmit $lno.0 $lno_e
335 $w_file tag add a
$cmit $lno.0 $lno_e
337 if {$highlight_line == -1} {
338 if {[lindex [$w_file yview
] 0] == 0} {
340 _showcommit
$this $lno
342 } elseif
{$highlight_line == $lno} {
343 _showcommit
$this $lno
351 if {![catch {set ncmit
$line_commit($lno)}]} {
352 if {$ncmit eq
$cmit} {
353 $w_cgrp delete
$lno.0 "$lno.0 lineend + 1c"
354 $w_cgrp insert
$lno.0 "|\n"
358 set hc
$highlight_commit
360 && [expr {$order($hc) + 1}] == $order($cmit)} {
361 _showcommit
$this $highlight_line
363 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
364 set header
($r_commit,$key) $data
367 $w_cgrp conf
-state disabled
371 set status
{Annotation complete.
}
375 } ifdeleted
{ catch {close $fd} }
378 set have
$blame_lines
379 set total
$total_lines
381 if {$total} {set pdone
[expr {100 * $have / $total}]}
384 "Loading annotations... %i of %i lines annotated (%2i%%)" \
388 method _click
{cur_w pos
} {
389 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
390 if {$lno eq
{}} return
391 _showcommit
$this $lno
394 method _showcommit
{lno
} {
396 variable active_color
398 if {$highlight_commit ne
{}} {
399 set cmit
$highlight_commit
400 $w_cgrp tag conf a
$cmit -background {}
401 $w_line tag conf a
$cmit -background {}
402 $w_file tag conf a
$cmit -background {}
405 $w_cmit conf
-state normal
406 $w_cmit delete
0.0 end
407 if {[catch {set cmit
$line_commit($lno)}]} {
409 $w_cmit insert end
"Loading annotation..."
411 $w_cgrp tag conf a
$cmit -background $active_color
412 $w_line tag conf a
$cmit -background $active_color
413 $w_file tag conf a
$cmit -background $active_color
418 catch {set author_name
$header($cmit,author
)}
419 catch {set author_email
$header($cmit,author-mail
)}
420 catch {set author_time
[clock format \
421 $header($cmit,author-time
) \
422 -format {%Y-
%m-
%d
%H
:%M
:%S
}
425 set committer_name
{}
426 set committer_email
{}
427 set committer_time
{}
428 catch {set committer_name
$header($cmit,committer
)}
429 catch {set committer_email
$header($cmit,committer-mail
)}
430 catch {set committer_time
[clock format \
431 $header($cmit,committer-time
) \
432 -format {%Y-
%m-
%d
%H
:%M
:%S
}
435 if {[catch {set msg
$header($cmit,message)}]} {
438 set fd
[open "| git cat-file commit $cmit" r
]
439 fconfigure $fd -encoding binary -translation lf
440 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
443 while {[gets $fd line
] > 0} {
444 if {[string match
{encoding *} $line]} {
445 set enc
[string tolower
[string range
$line 9 end
]]
448 set msg
[encoding convertfrom
$enc [read $fd]]
449 set msg
[string trim
$msg]
452 set author_name
[encoding convertfrom
$enc $author_name]
453 set committer_name
[encoding convertfrom
$enc $committer_name]
455 set header
($cmit,author
) $author_name
456 set header
($cmit,committer
) $committer_name
458 set header
($cmit,message) $msg
461 $w_cmit insert end
"commit $cmit\n" header_key
462 $w_cmit insert end
"Author:\t" header_key
463 $w_cmit insert end
"$author_name $author_email" header_val
464 $w_cmit insert end
"$author_time\n" header_val
466 $w_cmit insert end
"Committer:\t" header_key
467 $w_cmit insert end
"$committer_name $committer_email" header_val
468 $w_cmit insert end
"$committer_time\n" header_val
470 if {$line_file($lno) ne
$path} {
471 $w_cmit insert end
"Original File:\t" header_key
472 $w_cmit insert end
"[escape_path $line_file($lno)]\n" header_val
475 $w_cmit insert end
"\n$msg"
477 $w_cmit conf
-state disabled
479 set highlight_line
$lno
480 set highlight_commit
$cmit
482 if {$highlight_commit eq
$tooltip_commit} {
487 method _copycommit
{} {
488 set pos
@$::cursorX,$::cursorY
489 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
490 if {![catch {set commit
$line_commit($lno)}]} {
499 method _show_tooltip
{cur_w pos
} {
500 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
501 if {[catch {set cmit
$line_commit($lno)}]} {
506 if {$cmit eq
$highlight_commit} {
511 if {$cmit eq
$tooltip_commit} {
512 _position_tooltip
$this
513 } elseif
{$tooltip_wm ne
{}} {
514 _open_tooltip
$this $cur_w
515 } elseif
{$tooltip_timer eq
{}} {
516 set tooltip_timer
[after 1000 [cb _open_tooltip
$cur_w]]
520 method _open_tooltip
{cur_w
} {
522 set pos_x
[winfo pointerx
$cur_w]
523 set pos_y
[winfo pointery
$cur_w]
524 if {[winfo containing
$pos_x $pos_y] ne
$cur_w} {
529 set pos
@[join [list \
530 [expr {$pos_x - [winfo rootx
$cur_w]}] \
531 [expr {$pos_y - [winfo rooty
$cur_w]}]] ,]
532 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
533 set cmit
$line_commit($lno)
538 catch {set author_name
$header($cmit,author
)}
539 catch {set author_email
$header($cmit,author-mail
)}
540 catch {set author_time
[clock format \
541 $header($cmit,author-time
) \
542 -format {%Y-
%m-
%d
%H
:%M
:%S
}
545 set committer_name
{}
546 set committer_email
{}
547 set committer_time
{}
548 catch {set committer_name
$header($cmit,committer
)}
549 catch {set committer_email
$header($cmit,committer-mail
)}
550 catch {set committer_time
[clock format \
551 $header($cmit,committer-time
) \
552 -format {%Y-
%m-
%d
%H
:%M
:%S
}
556 catch {set summary
$header($cmit,summary
)}
558 set tooltip_commit
$cmit
559 set tooltip_text
"commit $cmit
560 $author_name $author_email $author_time
563 if {$tooltip_wm ne
"$cur_w.tooltip"} {
566 set tooltip_wm
[toplevel $cur_w.tooltip
-borderwidth 1]
567 wm overrideredirect
$tooltip_wm 1
568 wm transient
$tooltip_wm [winfo toplevel $cur_w]
569 pack [label $tooltip_wm.
label \
570 -background lightyellow
\
572 -textvariable @tooltip_text
\
575 _position_tooltip
$this
578 method _position_tooltip
{} {
579 set req_w
[winfo reqwidth
$tooltip_wm.
label]
580 set req_h
[winfo reqheight
$tooltip_wm.
label]
581 set pos_x
[expr {[winfo pointerx .
] + 5}]
582 set pos_y
[expr {[winfo pointery .
] + 10}]
584 set g
"${req_w}x${req_h}"
585 if {$pos_x >= 0} {append g
+}
587 if {$pos_y >= 0} {append g
+}
590 wm geometry
$tooltip_wm $g
594 method _hide_tooltip
{} {
595 if {$tooltip_wm ne
{}} {
598 set tooltip_commit
{}
600 if {$tooltip_timer ne
{}} {
601 after cancel
$tooltip_timer