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 $w_load insert end
"\n"
207 $w_cgrp insert end
"\n"
208 $w_line insert end
"$total_lines\n" linenumber
209 $w_file insert end
"$line\n"
211 $w_load conf
-state disabled
212 $w_cgrp conf
-state disabled
213 $w_line conf
-state disabled
214 $w_file conf
-state disabled
219 set cmd
[list git blame
-M -C --incremental]
221 lappend cmd
--contents $path
226 set fd
[open "| $cmd" r
]
227 fconfigure $fd -blocking 0 -translation lf
-encoding binary
228 fileevent $fd readable
[cb _read_blame
$fd]
230 } ifdeleted
{ catch {close $fd} }
232 method _read_blame
{fd
} {
233 $w_cgrp conf
-state normal
234 while {[gets $fd line
] >= 0} {
235 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
236 cmit original_line final_line line_count
]} {
238 set r_orig_line
$original_line
239 set r_final_line
$final_line
240 set r_line_count
$line_count
242 if {[catch {set g
$order($cmit)}]} {
243 $w_cgrp tag conf g
$cmit
244 $w_line tag conf g
$cmit
245 $w_file tag conf g
$cmit
247 $w_cgrp tag
raise in_sel
248 $w_line tag
raise in_sel
249 $w_file tag
raise in_sel
251 $w_file tag
raise sel
252 set order
($cmit) $commit_count
254 lappend commit_list
$cmit
256 } elseif
{[string match
{filename *} $line]} {
257 set file [string range
$line 9 end
]
259 set lno
$r_final_line
261 set abbr
[string range
$cmit 0 4]
264 set lno_e
"$lno.0 lineend + 1c"
265 if {[catch {set g g
$line_commit($lno)}]} {
266 $w_load tag add annotated
$lno.0 $lno_e
268 $w_cgrp tag remove g
$g $lno.0 $lno_e
269 $w_line tag remove g
$g $lno.0 $lno_e
270 $w_file tag remove g
$g $lno.0 $lno_e
273 set line_commit
($lno) $cmit
274 set line_file
($lno) $file
276 $w_cgrp delete
$lno.0 $lno_e
277 $w_cgrp insert
$lno.0 "$abbr\n"
279 $w_cgrp tag add g
$cmit $lno.0 $lno_e
280 $w_line tag add g
$cmit $lno.0 $lno_e
281 $w_file tag add g
$cmit $lno.0 $lno_e
283 if {$highlight_line == -1} {
284 if {[lindex [$w_file yview
] 0] == 0} {
286 _showcommit
$this $lno
288 } elseif
{$highlight_line == $lno} {
289 _showcommit
$this $lno
297 set hc
$highlight_commit
299 && [expr {$order($hc) + 1}] == $order($cmit)} {
300 _showcommit
$this $highlight_line
302 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
303 set header
($r_commit,$key) $data
306 $w_cgrp conf
-state disabled
310 set status
{Annotation complete.
}
314 } ifdeleted
{ catch {close $fd} }
317 set have
$blame_lines
318 set total
$total_lines
320 if {$total} {set pdone
[expr {100 * $have / $total}]}
323 "Loading annotations... %i of %i lines annotated (%2i%%)" \
327 method _click
{cur_w pos
} {
328 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
329 if {$lno eq
{}} return
331 set lno_e
"$lno.0 + 1 line"
333 $w_cgrp tag remove in_sel
0.0 end
334 $w_line tag remove in_sel
0.0 end
335 $w_file tag remove in_sel
0.0 end
337 $w_cgrp tag add in_sel
$lno.0 $lno_e
338 $w_line tag add in_sel
$lno.0 $lno_e
339 $w_file tag add in_sel
$lno.0 $lno_e
341 _showcommit
$this $lno
344 variable blame_colors
{
350 method _showcommit
{lno
} {
352 variable blame_colors
354 if {$highlight_commit ne
{}} {
355 set idx
$order($highlight_commit)
357 foreach c
$blame_colors {
358 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
359 $w_cgrp tag conf g
$h -background white
360 $w_line tag conf g
$h -background white
361 $w_file tag conf g
$h -background white
366 $w_cmit conf
-state normal
367 $w_cmit delete
0.0 end
368 if {[catch {set cmit
$line_commit($lno)}]} {
370 $w_cmit insert end
"Loading annotation..."
372 set idx
$order($cmit)
374 foreach c
$blame_colors {
375 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
376 $w_cgrp tag conf g
$h -background $c
377 $w_line tag conf g
$h -background $c
378 $w_file tag conf g
$h -background $c
385 catch {set author_name
$header($cmit,author
)}
386 catch {set author_email
$header($cmit,author-mail
)}
387 catch {set author_time
[clock format \
388 $header($cmit,author-time
) \
389 -format {%Y-
%m-
%d
%H
:%M
:%S
}
392 set committer_name
{}
393 set committer_email
{}
394 set committer_time
{}
395 catch {set committer_name
$header($cmit,committer
)}
396 catch {set committer_email
$header($cmit,committer-mail
)}
397 catch {set committer_time
[clock format \
398 $header($cmit,committer-time
) \
399 -format {%Y-
%m-
%d
%H
:%M
:%S
}
402 if {[catch {set msg
$header($cmit,message)}]} {
405 set fd
[open "| git cat-file commit $cmit" r
]
406 fconfigure $fd -encoding binary -translation lf
407 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
410 while {[gets $fd line
] > 0} {
411 if {[string match
{encoding *} $line]} {
412 set enc
[string tolower
[string range
$line 9 end
]]
415 set msg
[encoding convertfrom
$enc [read $fd]]
416 set msg
[string trim
$msg]
419 set author_name
[encoding convertfrom
$enc $author_name]
420 set committer_name
[encoding convertfrom
$enc $committer_name]
422 set header
($cmit,author
) $author_name
423 set header
($cmit,committer
) $committer_name
425 set header
($cmit,message) $msg
428 $w_cmit insert end
"commit $cmit
429 Author: $author_name $author_email $author_time
430 Committer: $committer_name $committer_email $committer_time
431 Original File: [escape_path $line_file($lno)]
435 $w_cmit conf
-state disabled
437 set highlight_line
$lno
438 set highlight_commit
$cmit
441 method _copycommit
{} {
442 set pos
@$::cursorX,$::cursorY
443 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
444 if {![catch {set commit
$line_commit($lno)}]} {