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 variable active_color #98e1a0
35 variable group_colors {
40 constructor new {i_commit i_path} {
47 wm title $top "[appname] ([reponame]): File Viewer"
48 set status "Loading $commit:$path..."
50 label $w.path -text "$commit:$path" \
56 pack $w.path -side top -fill x
59 set w_load $w.out.loaded_t
61 -background white -borderwidth 0 \
67 $w_load tag conf annotated -background grey
69 set w_line $w.out.linenumber_t
71 -background white -borderwidth 0 \
77 $w_line tag conf linenumber -justify right
79 set w_cgrp $w.out.commit_t
81 -background white -borderwidth 0 \
88 set w_file $w.out.file_t
90 -background white -borderwidth 0 \
95 -xscrollcommand [list $w.out.sbx set] \
98 scrollbar $w.out.sbx -orient h -command [list $w_file xview]
99 scrollbar $w.out.sby -orient v \
100 -command [list scrollbar2many [list \
113 grid conf $w.out.sbx -column 3 -sticky we
114 grid columnconfigure $w.out 3 -weight 1
115 grid rowconfigure $w.out 0 -weight 1
116 pack $w.out -fill both -expand 1
119 -textvariable @status \
124 pack $w.status -side bottom -fill x
129 -background white -borderwidth 0 \
134 -xscrollcommand [list $w.cm.sbx set] \
135 -yscrollcommand [list $w.cm.sby set] \
137 scrollbar $w.cm.sbx -orient h -command [list $w_cmit xview]
138 scrollbar $w.cm.sby -orient v -command [list $w_cmit yview]
139 pack $w.cm.sby -side right -fill y
140 pack $w.cm.sbx -side bottom -fill x
141 pack $w_cmit -expand 1 -fill both
142 pack $w.cm -side bottom -fill x
144 menu $w.ctxm -tearoff 0
145 $w.ctxm add command \
146 -label "Copy Commit" \
147 -command [cb _copycommit]
154 $i conf -cursor $cursor_ptr
155 $i conf -yscrollcommand \
156 [list many2scrollbar [list \
162 bind $i <Button-1> "[cb _click $i @%x,%y]; focus $i"
167 tk_popup $w.ctxm %X %Y
177 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
178 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
179 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
180 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
181 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
182 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
183 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
184 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
185 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
186 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
189 bind $w_cmit <Button-1> [list focus $w_cmit]
190 bind $top <Visibility> [list focus $top]
191 bind $top <Destroy> [list delete_this $this]
194 set fd [open $path r]
196 set cmd [list git cat-file blob "$commit:$path"]
197 set fd [open "| $cmd" r]
199 fconfigure $fd -blocking 0 -translation lf -encoding binary
200 fileevent $fd readable [cb _read_file $fd]
203 method _read_file {fd} {
204 $w_load conf -state normal
205 $w_cgrp conf -state normal
206 $w_line conf -state normal
207 $w_file conf -state normal
208 while {[gets $fd line] >= 0} {
209 regsub "\r\$" $line {} line
212 if {$total_lines > 1} {
213 $w_load insert end "\n"
214 $w_cgrp insert end "\n"
215 $w_line insert end "\n"
216 $w_file insert end "\n"
219 $w_line insert end "$total_lines" linenumber
220 $w_file insert end "$line"
222 $w_load conf -state disabled
223 $w_cgrp conf -state disabled
224 $w_line conf -state disabled
225 $w_file conf -state disabled
230 set cmd [list git blame -M -C --incremental]
232 lappend cmd --contents $path
237 set fd [open "| $cmd" r]
238 fconfigure $fd -blocking 0 -translation lf -encoding binary
239 fileevent $fd readable [cb _read_blame $fd]
241 } ifdeleted { catch {close $fd} }
243 method _read_blame {fd} {
244 variable group_colors
246 $w_cgrp conf -state normal
247 while {[gets $fd line] >= 0} {
248 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
249 cmit original_line final_line line_count]} {
251 set r_orig_line $original_line
252 set r_final_line $final_line
253 set r_line_count $line_count
255 if {[catch {set g $order($cmit)}]} {
256 set bg [lindex $group_colors 0]
257 set group_colors [lrange $group_colors 1 end]
258 lappend group_colors $bg
260 $w_cgrp tag conf g$cmit -background $bg
261 $w_line tag conf g$cmit -background $bg
262 $w_file tag conf g$cmit -background $bg
264 set order($cmit) $commit_count
266 lappend commit_list $cmit
268 } elseif {[string match {filename *} $line]} {
269 set file [string range $line 9 end]
271 set lno $r_final_line
274 if {[regexp {^0{40}$} $cmit]} {
277 set abbr [string range $cmit 0 4]
280 if {![catch {set ncmit $line_commit([expr {$lno - 1}])}]} {
281 if {$ncmit eq $cmit} {
287 set lno_e "$lno.0 lineend + 1c"
288 if {[catch {set g g$line_commit($lno)}]} {
289 $w_load tag add annotated $lno.0 $lno_e
291 $w_cgrp tag remove g$g $lno.0 $lno_e
292 $w_line tag remove g$g $lno.0 $lno_e
293 $w_file tag remove g$g $lno.0 $lno_e
295 $w_cgrp tag remove a$g $lno.0 $lno_e
296 $w_line tag remove a$g $lno.0 $lno_e
297 $w_file tag remove a$g $lno.0 $lno_e
300 set line_commit($lno) $cmit
301 set line_file($lno) $file
303 $w_cgrp delete $lno.0 "$lno.0 lineend"
304 $w_cgrp insert $lno.0 $abbr
307 $w_cgrp tag add g$cmit $lno.0 $lno_e
308 $w_line tag add g$cmit $lno.0 $lno_e
309 $w_file tag add g$cmit $lno.0 $lno_e
311 $w_cgrp tag add a$cmit $lno.0 $lno_e
312 $w_line tag add a$cmit $lno.0 $lno_e
313 $w_file tag add a$cmit $lno.0 $lno_e
315 if {$highlight_line == -1} {
316 if {[lindex [$w_file yview] 0] == 0} {
318 _showcommit $this $lno
320 } elseif {$highlight_line == $lno} {
321 _showcommit $this $lno
329 if {![catch {set ncmit $line_commit($lno)}]} {
330 if {$ncmit eq $cmit} {
331 $w_cgrp delete $lno.0 "$lno.0 lineend + 1c"
332 $w_cgrp insert $lno.0 "|\n"
336 set hc $highlight_commit
338 && [expr {$order($hc) + 1}] == $order($cmit)} {
339 _showcommit $this $highlight_line
341 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
342 set header($r_commit,$key) $data
345 $w_cgrp conf -state disabled
349 set status {Annotation complete.}
353 } ifdeleted { catch {close $fd} }
356 set have $blame_lines
357 set total $total_lines
359 if {$total} {set pdone [expr {100 * $have / $total}]}
362 "Loading annotations... %i of %i lines annotated (%2i%%)" \
366 method _click {cur_w pos} {
367 set lno [lindex [split [$cur_w index $pos] .] 0]
368 if {$lno eq {}} return
369 _showcommit $this $lno
372 method _showcommit {lno} {
374 variable active_color
376 if {$highlight_commit ne {}} {
377 set cmit $highlight_commit
378 $w_cgrp tag conf a$cmit -background {}
379 $w_line tag conf a$cmit -background {}
380 $w_file tag conf a$cmit -background {}
383 $w_cmit conf -state normal
384 $w_cmit delete 0.0 end
385 if {[catch {set cmit $line_commit($lno)}]} {
387 $w_cmit insert end "Loading annotation..."
389 $w_cgrp tag conf a$cmit -background $active_color
390 $w_line tag conf a$cmit -background $active_color
391 $w_file tag conf a$cmit -background $active_color
396 catch {set author_name $header($cmit,author)}
397 catch {set author_email $header($cmit,author-mail)}
398 catch {set author_time [clock format \
399 $header($cmit,author-time) \
400 -format {%Y-%m-%d %H:%M:%S}
403 set committer_name {}
404 set committer_email {}
405 set committer_time {}
406 catch {set committer_name $header($cmit,committer)}
407 catch {set committer_email $header($cmit,committer-mail)}
408 catch {set committer_time [clock format \
409 $header($cmit,committer-time) \
410 -format {%Y-%m-%d %H:%M:%S}
413 if {[catch {set msg $header($cmit,message)}]} {
416 set fd [open "| git cat-file commit $cmit" r]
417 fconfigure $fd -encoding binary -translation lf
418 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
421 while {[gets $fd line] > 0} {
422 if {[string match {encoding *} $line]} {
423 set enc [string tolower [string range $line 9 end]]
426 set msg [encoding convertfrom $enc [read $fd]]
427 set msg [string trim $msg]
430 set author_name [encoding convertfrom $enc $author_name]
431 set committer_name [encoding convertfrom $enc $committer_name]
433 set header($cmit,author) $author_name
434 set header($cmit,committer) $committer_name
436 set header($cmit,message) $msg
439 $w_cmit insert end "commit $cmit
440 Author: $author_name $author_email $author_time
441 Committer: $committer_name $committer_email $committer_time
442 Original File: [escape_path $line_file($lno)]
446 $w_cmit conf -state disabled
448 set highlight_line $lno
449 set highlight_commit $cmit
452 method _copycommit {} {
453 set pos @$::cursorX,$::cursorY
454 set lno [lindex [split [$::cursorW index $pos] .] 0]
455 if {![catch {set commit $line_commit($lno)}]} {