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 set w_load
$w.out.loaded_t
53 -background white
-borderwidth 0 \
59 $w_load tag conf annotated
-background grey
61 set w_line
$w.out.linenumber_t
63 -background white
-borderwidth 0 \
69 $w_line tag conf linenumber
-justify right
71 set w_cgrp
$w.out.commit_t
73 -background white
-borderwidth 0 \
80 set w_file
$w.out.file_t
82 -background white
-borderwidth 0 \
87 -xscrollcommand [list $w.out.sbx
set] \
90 scrollbar $w.out.sbx
-orient h
-command [list $w_file xview
]
91 scrollbar $w.out.sby
-orient v
\
92 -command [list scrollbar2many
[list \
105 grid conf
$w.out.sbx
-column 3 -sticky we
106 grid columnconfigure
$w.out
3 -weight 1
107 grid rowconfigure
$w.out
0 -weight 1
108 pack $w.out
-fill both
-expand 1
111 -textvariable @status
\
116 pack $w.status
-side bottom
-fill x
121 -background white
-borderwidth 0 \
126 -xscrollcommand [list $w.cm.sbx
set] \
127 -yscrollcommand [list $w.cm.sby
set] \
129 scrollbar $w.cm.sbx
-orient h
-command [list $w_cmit xview
]
130 scrollbar $w.cm.sby
-orient v
-command [list $w_cmit yview
]
131 pack $w.cm.sby
-side right
-fill y
132 pack $w.cm.sbx
-side bottom
-fill x
133 pack $w_cmit -expand 1 -fill both
134 pack $w.cm
-side bottom
-fill x
136 menu $w.ctxm
-tearoff 0
137 $w.ctxm add command
\
138 -label "Copy Commit" \
139 -command [cb _copycommit
]
147 -background [$i cget
-foreground] \
148 -foreground [$i cget
-background]
149 $i conf
-yscrollcommand \
150 [list many2scrollbar
[list \
156 bind $i <Button-1
> "[cb _click $i @%x,%y]; focus $i"
161 tk_popup $w.ctxm %X %Y
171 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
172 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
173 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
174 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
175 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
176 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
177 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
178 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
179 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
180 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
183 bind $w_cmit <Button-1
> [list focus $w_cmit]
184 bind $top <Visibility
> [list focus $top]
185 bind $top <Destroy
> [list delete_this
$this]
188 set fd
[open $path r
]
190 set cmd
[list git cat-file blob
"$commit:$path"]
191 set fd
[open "| $cmd" r
]
193 fconfigure $fd -blocking 0 -translation lf
-encoding binary
194 fileevent $fd readable
[cb _read_file
$fd]
197 method _read_file
{fd
} {
198 $w_load conf
-state normal
199 $w_cgrp conf
-state normal
200 $w_line conf
-state normal
201 $w_file conf
-state normal
202 while {[gets $fd line
] >= 0} {
203 regsub "\r\$" $line {} line
206 if {$total_lines > 1} {
207 $w_load insert end
"\n"
208 $w_cgrp insert end
"\n"
209 $w_line insert end
"\n"
210 $w_file insert end
"\n"
213 $w_line insert end
"$total_lines" linenumber
214 $w_file insert end
"$line"
216 $w_load conf
-state disabled
217 $w_cgrp conf
-state disabled
218 $w_line conf
-state disabled
219 $w_file conf
-state disabled
224 set cmd
[list git blame
-M -C --incremental]
226 lappend cmd
--contents $path
231 set fd
[open "| $cmd" r
]
232 fconfigure $fd -blocking 0 -translation lf
-encoding binary
233 fileevent $fd readable
[cb _read_blame
$fd]
235 } ifdeleted
{ catch {close $fd} }
237 method _read_blame
{fd
} {
238 $w_cgrp conf
-state normal
239 while {[gets $fd line
] >= 0} {
240 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
241 cmit original_line final_line line_count
]} {
243 set r_orig_line
$original_line
244 set r_final_line
$final_line
245 set r_line_count
$line_count
247 if {[catch {set g
$order($cmit)}]} {
248 $w_cgrp tag conf g
$cmit
249 $w_line tag conf g
$cmit
250 $w_file tag conf g
$cmit
252 $w_cgrp tag
raise in_sel
253 $w_line tag
raise in_sel
254 $w_file tag
raise in_sel
256 $w_file tag
raise sel
257 set order
($cmit) $commit_count
259 lappend commit_list
$cmit
261 } elseif
{[string match
{filename *} $line]} {
262 set file [string range
$line 9 end
]
264 set lno
$r_final_line
266 set abbr
[string range
$cmit 0 4]
269 set lno_e
"$lno.0 lineend + 1c"
270 if {[catch {set g g
$line_commit($lno)}]} {
271 $w_load tag add annotated
$lno.0 $lno_e
273 $w_cgrp tag remove g
$g $lno.0 $lno_e
274 $w_line tag remove g
$g $lno.0 $lno_e
275 $w_file tag remove g
$g $lno.0 $lno_e
278 set line_commit
($lno) $cmit
279 set line_file
($lno) $file
281 $w_cgrp delete
$lno.0 $lno_e
282 $w_cgrp insert
$lno.0 "$abbr\n"
284 $w_cgrp tag add g
$cmit $lno.0 $lno_e
285 $w_line tag add g
$cmit $lno.0 $lno_e
286 $w_file tag add g
$cmit $lno.0 $lno_e
288 if {$highlight_line == -1} {
289 if {[lindex [$w_file yview
] 0] == 0} {
291 _showcommit
$this $lno
293 } elseif
{$highlight_line == $lno} {
294 _showcommit
$this $lno
302 set hc
$highlight_commit
304 && [expr {$order($hc) + 1}] == $order($cmit)} {
305 _showcommit
$this $highlight_line
307 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
308 set header
($r_commit,$key) $data
311 $w_cgrp conf
-state disabled
315 set status
{Annotation complete.
}
319 } ifdeleted
{ catch {close $fd} }
322 set have
$blame_lines
323 set total
$total_lines
325 if {$total} {set pdone
[expr {100 * $have / $total}]}
328 "Loading annotations... %i of %i lines annotated (%2i%%)" \
332 method _click
{cur_w pos
} {
333 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
334 if {$lno eq
{}} return
336 set lno_e
"$lno.0 + 1 line"
338 $w_cgrp tag remove in_sel
0.0 end
339 $w_line tag remove in_sel
0.0 end
340 $w_file tag remove in_sel
0.0 end
342 $w_cgrp tag add in_sel
$lno.0 $lno_e
343 $w_line tag add in_sel
$lno.0 $lno_e
344 $w_file tag add in_sel
$lno.0 $lno_e
346 _showcommit
$this $lno
349 variable blame_colors
{
355 method _showcommit
{lno
} {
357 variable blame_colors
359 if {$highlight_commit ne
{}} {
360 set idx
$order($highlight_commit)
362 foreach c
$blame_colors {
363 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
364 $w_cgrp tag conf g
$h -background white
365 $w_line tag conf g
$h -background white
366 $w_file tag conf g
$h -background white
371 $w_cmit conf
-state normal
372 $w_cmit delete
0.0 end
373 if {[catch {set cmit
$line_commit($lno)}]} {
375 $w_cmit insert end
"Loading annotation..."
377 set idx
$order($cmit)
379 foreach c
$blame_colors {
380 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
381 $w_cgrp tag conf g
$h -background $c
382 $w_line tag conf g
$h -background $c
383 $w_file tag conf g
$h -background $c
390 catch {set author_name
$header($cmit,author
)}
391 catch {set author_email
$header($cmit,author-mail
)}
392 catch {set author_time
[clock format \
393 $header($cmit,author-time
) \
394 -format {%Y-
%m-
%d
%H
:%M
:%S
}
397 set committer_name
{}
398 set committer_email
{}
399 set committer_time
{}
400 catch {set committer_name
$header($cmit,committer
)}
401 catch {set committer_email
$header($cmit,committer-mail
)}
402 catch {set committer_time
[clock format \
403 $header($cmit,committer-time
) \
404 -format {%Y-
%m-
%d
%H
:%M
:%S
}
407 if {[catch {set msg
$header($cmit,message)}]} {
410 set fd
[open "| git cat-file commit $cmit" r
]
411 fconfigure $fd -encoding binary -translation lf
412 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
415 while {[gets $fd line
] > 0} {
416 if {[string match
{encoding *} $line]} {
417 set enc
[string tolower
[string range
$line 9 end
]]
420 set msg
[encoding convertfrom
$enc [read $fd]]
421 set msg
[string trim
$msg]
424 set author_name
[encoding convertfrom
$enc $author_name]
425 set committer_name
[encoding convertfrom
$enc $committer_name]
427 set header
($cmit,author
) $author_name
428 set header
($cmit,committer
) $committer_name
430 set header
($cmit,message) $msg
433 $w_cmit insert end
"commit $cmit
434 Author: $author_name $author_email $author_time
435 Committer: $committer_name $committer_email $committer_time
436 Original File: [escape_path $line_file($lno)]
440 $w_cmit conf
-state disabled
442 set highlight_line
$lno
443 set highlight_commit
$cmit
446 method _copycommit
{} {
447 set pos
@$::cursorX,$::cursorY
448 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
449 if {![catch {set commit
$line_commit($lno)}]} {