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 constructor new {i_commit i_path} {
39 wm title $top "[appname] ([reponame]): File Viewer"
40 set status "Loading $commit:$path..."
42 label $w.path -text "$commit:$path" \
48 pack $w.path -side top -fill x
51 text $w.out.loaded_t \
52 -background white -borderwidth 0 \
58 $w.out.loaded_t tag conf annotated -background grey
60 text $w.out.linenumber_t \
61 -background white -borderwidth 0 \
67 $w.out.linenumber_t tag conf linenumber -justify right
69 text $w.out.commit_t \
70 -background white -borderwidth 0 \
78 -background white -borderwidth 0 \
83 -xscrollcommand [list $w.out.sbx set] \
86 scrollbar $w.out.sbx -orient h -command [list $w.out.file_t xview]
87 scrollbar $w.out.sby -orient v \
88 -command [list scrollbar2many [list \
101 grid conf $w.out.sbx -column 3 -sticky we
102 grid columnconfigure $w.out 3 -weight 1
103 grid rowconfigure $w.out 0 -weight 1
104 pack $w.out -fill both -expand 1
107 -textvariable @status \
112 pack $w.status -side bottom -fill x
116 -background white -borderwidth 0 \
121 -xscrollcommand [list $w.cm.sbx set] \
122 -yscrollcommand [list $w.cm.sby set] \
124 scrollbar $w.cm.sbx -orient h -command [list $w.cm.t xview]
125 scrollbar $w.cm.sby -orient v -command [list $w.cm.t yview]
126 pack $w.cm.sby -side right -fill y
127 pack $w.cm.sbx -side bottom -fill x
128 pack $w.cm.t -expand 1 -fill both
129 pack $w.cm -side bottom -fill x
131 menu $w.ctxm -tearoff 0
132 $w.ctxm add command \
133 -label "Copy Commit" \
134 -command [cb _copycommit]
136 set w_line $w.out.linenumber_t
137 set w_cgrp $w.out.commit_t
138 set w_load $w.out.loaded_t
139 set w_file $w.out.file_t
145 $w.out.linenumber_t \
148 -background [$i cget -foreground] \
149 -foreground [$i cget -background]
150 $i conf -yscrollcommand \
151 [list many2scrollbar [list \
154 $w.out.linenumber_t \
157 bind $i <Button-1> "[cb _click $i @%x,%y]; focus $i"
162 tk_popup $w.ctxm %X %Y
169 $w.out.linenumber_t \
172 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
173 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
174 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
175 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
176 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
177 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
178 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
179 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
180 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
181 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
184 bind $w.cm.t <Button-1> [list focus $w.cm.t]
185 bind $top <Visibility> [list focus $top]
186 bind $top <Destroy> [list delete_this $this]
189 set fd [open $path r]
191 set cmd [list git cat-file blob "$commit:$path"]
192 set fd [open "| $cmd" r]
194 fconfigure $fd -blocking 0 -translation lf -encoding binary
195 fileevent $fd readable [cb _read_file $fd]
198 method _read_file {fd} {
199 $w_load conf -state normal
200 $w_cgrp conf -state normal
201 $w_line conf -state normal
202 $w_file conf -state normal
203 while {[gets $fd line] >= 0} {
204 regsub "\r\$" $line {} line
207 $w_load insert end "\n"
208 $w_cgrp insert end "\n"
209 $w_line insert end "$total_lines\n" linenumber
210 $w_file insert end "$line\n"
212 $w_load conf -state disabled
213 $w_cgrp conf -state disabled
214 $w_line conf -state disabled
215 $w_file conf -state disabled
220 set cmd [list git blame -M -C --incremental]
222 lappend cmd --contents $path
227 set fd [open "| $cmd" r]
228 fconfigure $fd -blocking 0 -translation lf -encoding binary
229 fileevent $fd readable [cb _read_blame $fd]
231 } ifdeleted { catch {close $fd} }
233 method _read_blame {fd} {
234 $w_cgrp conf -state normal
235 while {[gets $fd line] >= 0} {
236 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
237 cmit original_line final_line line_count]} {
239 set r_orig_line $original_line
240 set r_final_line $final_line
241 set r_line_count $line_count
243 if {[catch {set g $order($cmit)}]} {
244 $w_cgrp tag conf g$cmit
245 $w_line tag conf g$cmit
246 $w_file tag conf g$cmit
248 $w_cgrp tag raise in_sel
249 $w_line tag raise in_sel
250 $w_file tag raise in_sel
252 $w_file tag raise sel
253 set order($cmit) $commit_count
255 lappend commit_list $cmit
257 } elseif {[string match {filename *} $line]} {
258 set file [string range $line 9 end]
260 set lno $r_final_line
262 set abbr [string range $cmit 0 4]
265 set lno_e "$lno.0 lineend + 1c"
266 if {[catch {set g g$line_commit($lno)}]} {
267 $w_load tag add annotated $lno.0 $lno_e
269 $w_cgrp tag remove g$g $lno.0 $lno_e
270 $w_line tag remove g$g $lno.0 $lno_e
271 $w_file tag remove g$g $lno.0 $lno_e
274 set line_commit($lno) $cmit
275 set line_file($lno) $file
277 $w_cgrp delete $lno.0 $lno_e
278 $w_cgrp insert $lno.0 "$abbr\n"
280 $w_cgrp tag add g$cmit $lno.0 $lno_e
281 $w_line tag add g$cmit $lno.0 $lno_e
282 $w_file tag add g$cmit $lno.0 $lno_e
284 if {$highlight_line == -1} {
285 if {[lindex [$w_file yview] 0] == 0} {
287 _showcommit $this $lno
289 } elseif {$highlight_line == $lno} {
290 _showcommit $this $lno
298 set hc $highlight_commit
300 && [expr {$order($hc) + 1}] == $order($cmit)} {
301 _showcommit $this $highlight_line
303 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
304 set header($r_commit,$key) $data
307 $w_cgrp conf -state disabled
311 set status {Annotation complete.}
315 } ifdeleted { catch {close $fd} }
318 set have $blame_lines
319 set total $total_lines
321 if {$total} {set pdone [expr {100 * $have / $total}]}
324 "Loading annotations... %i of %i lines annotated (%2i%%)" \
328 method _click {cur_w pos} {
329 set lno [lindex [split [$cur_w index $pos] .] 0]
330 if {$lno eq {}} return
332 set lno_e "$lno.0 + 1 line"
334 $w_cgrp tag remove in_sel 0.0 end
335 $w_line tag remove in_sel 0.0 end
336 $w_file tag remove in_sel 0.0 end
338 $w_cgrp tag add in_sel $lno.0 $lno_e
339 $w_line tag add in_sel $lno.0 $lno_e
340 $w_file tag add in_sel $lno.0 $lno_e
342 _showcommit $this $lno
345 variable blame_colors {
351 method _showcommit {lno} {
353 variable blame_colors
355 if {$highlight_commit ne {}} {
356 set idx $order($highlight_commit)
358 foreach c $blame_colors {
359 set h [lindex $commit_list [expr {$idx - 1 + $i}]]
360 $w_cgrp tag conf g$h -background white
361 $w_line tag conf g$h -background white
362 $w_file tag conf g$h -background white
367 $w_cmit conf -state normal
368 $w_cmit delete 0.0 end
369 if {[catch {set cmit $line_commit($lno)}]} {
371 $w_cmit insert end "Loading annotation..."
373 set idx $order($cmit)
375 foreach c $blame_colors {
376 set h [lindex $commit_list [expr {$idx - 1 + $i}]]
377 $w_cgrp tag conf g$h -background $c
378 $w_line tag conf g$h -background $c
379 $w_file tag conf g$h -background $c
386 catch {set author_name $header($cmit,author)}
387 catch {set author_email $header($cmit,author-mail)}
388 catch {set author_time [clock format \
389 $header($cmit,author-time) \
390 -format {%Y-%m-%d %H:%M:%S}
393 set committer_name {}
394 set committer_email {}
395 set committer_time {}
396 catch {set committer_name $header($cmit,committer)}
397 catch {set committer_email $header($cmit,committer-mail)}
398 catch {set committer_time [clock format \
399 $header($cmit,committer-time) \
400 -format {%Y-%m-%d %H:%M:%S}
403 if {[catch {set msg $header($cmit,message)}]} {
406 set fd [open "| git cat-file commit $cmit" r]
407 fconfigure $fd -encoding binary -translation lf
408 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
411 while {[gets $fd line] > 0} {
412 if {[string match {encoding *} $line]} {
413 set enc [string tolower [string range $line 9 end]]
416 set msg [encoding convertfrom $enc [read $fd]]
417 set msg [string trim $msg]
420 set author_name [encoding convertfrom $enc $author_name]
421 set committer_name [encoding convertfrom $enc $committer_name]
423 set header($cmit,author) $author_name
424 set header($cmit,committer) $committer_name
426 set header($cmit,message) $msg
429 $w_cmit insert end "commit $cmit
430 Author: $author_name $author_email $author_time
431 Committer: $committer_name $committer_email $committer_time
432 Original File: [escape_path $line_file($lno)]
436 $w_cmit conf -state disabled
438 set highlight_line $lno
439 set highlight_commit $cmit
442 method _copycommit {} {
443 set pos @$::cursorX,$::cursorY
444 set lno [lindex [split [$::cursorW index $pos] .] 0]
445 if {![catch {set commit $line_commit($lno)}]} {