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
} {
45 wm title
$top "[appname] ([reponame]): File Viewer"
46 set status
"Loading $commit:$path..."
48 label $w.path
-text "$commit:$path" \
54 pack $w.path
-side top
-fill x
57 set w_load
$w.out.loaded_t
59 -background white
-borderwidth 0 \
65 $w_load tag conf annotated
-background grey
67 set w_line
$w.out.linenumber_t
69 -background white
-borderwidth 0 \
75 $w_line tag conf linenumber
-justify right
77 set w_cgrp
$w.out.commit_t
79 -background white
-borderwidth 0 \
86 set w_file
$w.out.file_t
88 -background white
-borderwidth 0 \
93 -xscrollcommand [list $w.out.sbx
set] \
96 scrollbar $w.out.sbx
-orient h
-command [list $w_file xview
]
97 scrollbar $w.out.sby
-orient v
\
98 -command [list scrollbar2many
[list \
111 grid conf
$w.out.sbx
-column 3 -sticky we
112 grid columnconfigure
$w.out
3 -weight 1
113 grid rowconfigure
$w.out
0 -weight 1
114 pack $w.out
-fill both
-expand 1
117 -textvariable @status
\
122 pack $w.status
-side bottom
-fill x
127 -background white
-borderwidth 0 \
132 -xscrollcommand [list $w.cm.sbx
set] \
133 -yscrollcommand [list $w.cm.sby
set] \
135 scrollbar $w.cm.sbx
-orient h
-command [list $w_cmit xview
]
136 scrollbar $w.cm.sby
-orient v
-command [list $w_cmit yview
]
137 pack $w.cm.sby
-side right
-fill y
138 pack $w.cm.sbx
-side bottom
-fill x
139 pack $w_cmit -expand 1 -fill both
140 pack $w.cm
-side bottom
-fill x
142 menu $w.ctxm
-tearoff 0
143 $w.ctxm add command
\
144 -label "Copy Commit" \
145 -command [cb _copycommit
]
152 $i conf
-yscrollcommand \
153 [list many2scrollbar
[list \
159 bind $i <Button-1
> "[cb _click $i @%x,%y]; focus $i"
164 tk_popup $w.ctxm %X %Y
174 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
175 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
176 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
177 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
178 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
179 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
180 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
181 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
182 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
183 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
186 bind $w_cmit <Button-1
> [list focus $w_cmit]
187 bind $top <Visibility
> [list focus $top]
188 bind $top <Destroy
> [list delete_this
$this]
191 set fd
[open $path r
]
193 set cmd
[list git cat-file blob
"$commit:$path"]
194 set fd
[open "| $cmd" r
]
196 fconfigure $fd -blocking 0 -translation lf
-encoding binary
197 fileevent $fd readable
[cb _read_file
$fd]
200 method _read_file
{fd
} {
201 $w_load conf
-state normal
202 $w_cgrp conf
-state normal
203 $w_line conf
-state normal
204 $w_file conf
-state normal
205 while {[gets $fd line
] >= 0} {
206 regsub "\r\$" $line {} line
209 if {$total_lines > 1} {
210 $w_load insert end
"\n"
211 $w_cgrp insert end
"\n"
212 $w_line insert end
"\n"
213 $w_file insert end
"\n"
216 $w_line insert end
"$total_lines" linenumber
217 $w_file insert end
"$line"
219 $w_load conf
-state disabled
220 $w_cgrp conf
-state disabled
221 $w_line conf
-state disabled
222 $w_file conf
-state disabled
227 set cmd
[list git blame
-M -C --incremental]
229 lappend cmd
--contents $path
234 set fd
[open "| $cmd" r
]
235 fconfigure $fd -blocking 0 -translation lf
-encoding binary
236 fileevent $fd readable
[cb _read_blame
$fd]
238 } ifdeleted
{ catch {close $fd} }
240 method _read_blame
{fd
} {
241 variable group_colors
243 $w_cgrp conf
-state normal
244 while {[gets $fd line
] >= 0} {
245 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
246 cmit original_line final_line line_count
]} {
248 set r_orig_line
$original_line
249 set r_final_line
$final_line
250 set r_line_count
$line_count
252 if {[catch {set g
$order($cmit)}]} {
253 set bg
[lindex $group_colors 0]
254 set group_colors
[lrange $group_colors 1 end
]
255 lappend group_colors
$bg
257 $w_cgrp tag conf g
$cmit -background $bg
258 $w_line tag conf g
$cmit -background $bg
259 $w_file tag conf g
$cmit -background $bg
261 set order
($cmit) $commit_count
263 lappend commit_list
$cmit
265 } elseif
{[string match
{filename *} $line]} {
266 set file [string range
$line 9 end
]
268 set lno
$r_final_line
271 if {[regexp {^
0{40}$} $cmit]} {
274 set abbr
[string range
$cmit 0 4]
277 if {![catch {set ncmit
$line_commit([expr {$lno - 1}])}]} {
278 if {$ncmit eq
$cmit} {
284 set lno_e
"$lno.0 lineend + 1c"
285 if {[catch {set g g
$line_commit($lno)}]} {
286 $w_load tag add annotated
$lno.0 $lno_e
288 $w_cgrp tag remove g
$g $lno.0 $lno_e
289 $w_line tag remove g
$g $lno.0 $lno_e
290 $w_file tag remove g
$g $lno.0 $lno_e
292 $w_cgrp tag remove a
$g $lno.0 $lno_e
293 $w_line tag remove a
$g $lno.0 $lno_e
294 $w_file tag remove a
$g $lno.0 $lno_e
297 set line_commit
($lno) $cmit
298 set line_file
($lno) $file
300 $w_cgrp delete
$lno.0 "$lno.0 lineend"
301 $w_cgrp insert
$lno.0 $abbr
304 $w_cgrp tag add g
$cmit $lno.0 $lno_e
305 $w_line tag add g
$cmit $lno.0 $lno_e
306 $w_file tag add g
$cmit $lno.0 $lno_e
308 $w_cgrp tag add a
$cmit $lno.0 $lno_e
309 $w_line tag add a
$cmit $lno.0 $lno_e
310 $w_file tag add a
$cmit $lno.0 $lno_e
312 if {$highlight_line == -1} {
313 if {[lindex [$w_file yview
] 0] == 0} {
315 _showcommit
$this $lno
317 } elseif
{$highlight_line == $lno} {
318 _showcommit
$this $lno
326 if {![catch {set ncmit
$line_commit($lno)}]} {
327 if {$ncmit eq
$cmit} {
328 $w_cgrp delete
$lno.0 "$lno.0 lineend + 1c"
329 $w_cgrp insert
$lno.0 "|\n"
333 set hc
$highlight_commit
335 && [expr {$order($hc) + 1}] == $order($cmit)} {
336 _showcommit
$this $highlight_line
338 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
339 set header
($r_commit,$key) $data
342 $w_cgrp conf
-state disabled
346 set status
{Annotation complete.
}
350 } ifdeleted
{ catch {close $fd} }
353 set have
$blame_lines
354 set total
$total_lines
356 if {$total} {set pdone
[expr {100 * $have / $total}]}
359 "Loading annotations... %i of %i lines annotated (%2i%%)" \
363 method _click
{cur_w pos
} {
364 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
365 if {$lno eq
{}} return
366 _showcommit
$this $lno
369 method _showcommit
{lno
} {
371 variable active_color
373 if {$highlight_commit ne
{}} {
374 set cmit
$highlight_commit
375 $w_cgrp tag conf a
$cmit -background {}
376 $w_line tag conf a
$cmit -background {}
377 $w_file tag conf a
$cmit -background {}
380 $w_cmit conf
-state normal
381 $w_cmit delete
0.0 end
382 if {[catch {set cmit
$line_commit($lno)}]} {
384 $w_cmit insert end
"Loading annotation..."
386 $w_cgrp tag conf a
$cmit -background $active_color
387 $w_line tag conf a
$cmit -background $active_color
388 $w_file tag conf a
$cmit -background $active_color
393 catch {set author_name
$header($cmit,author
)}
394 catch {set author_email
$header($cmit,author-mail
)}
395 catch {set author_time
[clock format \
396 $header($cmit,author-time
) \
397 -format {%Y-
%m-
%d
%H
:%M
:%S
}
400 set committer_name
{}
401 set committer_email
{}
402 set committer_time
{}
403 catch {set committer_name
$header($cmit,committer
)}
404 catch {set committer_email
$header($cmit,committer-mail
)}
405 catch {set committer_time
[clock format \
406 $header($cmit,committer-time
) \
407 -format {%Y-
%m-
%d
%H
:%M
:%S
}
410 if {[catch {set msg
$header($cmit,message)}]} {
413 set fd
[open "| git cat-file commit $cmit" r
]
414 fconfigure $fd -encoding binary -translation lf
415 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
418 while {[gets $fd line
] > 0} {
419 if {[string match
{encoding *} $line]} {
420 set enc
[string tolower
[string range
$line 9 end
]]
423 set msg
[encoding convertfrom
$enc [read $fd]]
424 set msg
[string trim
$msg]
427 set author_name
[encoding convertfrom
$enc $author_name]
428 set committer_name
[encoding convertfrom
$enc $committer_name]
430 set header
($cmit,author
) $author_name
431 set header
($cmit,committer
) $committer_name
433 set header
($cmit,message) $msg
436 $w_cmit insert end
"commit $cmit
437 Author: $author_name $author_email $author_time
438 Committer: $committer_name $committer_email $committer_time
439 Original File: [escape_path $line_file($lno)]
443 $w_cmit conf
-state disabled
445 set highlight_line
$lno
446 set highlight_commit
$cmit
449 method _copycommit
{} {
450 set pos
@$::cursorX,$::cursorY
451 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
452 if {![catch {set commit
$line_commit($lno)}]} {