2 # Copyright (C) 2006, 2007 Shawn Pearce
6 field commit
; # input commit to blame
7 field path
; # input filename to view in $commit
16 field highlight_line
-1 ; # current line selected
17 field highlight_commit
{} ; # sha1 of commit selected
19 field total_lines
0 ; # total length of file
20 field blame_lines
0 ; # number of lines computed
21 field commit_count
0 ; # number of commits in $commit_list
22 field commit_list
{} ; # list of commit sha1 in receipt order
23 field order
; # array commit -> receipt order
24 field header
; # array commit,key -> header field
25 field line_commit
; # array line -> sha1 commit
26 field line_file
; # array line -> file name
28 field r_commit
; # commit currently being parsed
29 field r_orig_line
; # original line number
30 field r_final_line
; # final line number
31 field r_line_count
; # lines in this region
33 constructor new
{i_commit i_path
} {
38 wm title
$top "[appname] ([reponame]): File Viewer"
39 set status
"Loading $commit:$path..."
41 label $w.path
-text "$commit:$path" \
47 pack $w.path
-side top
-fill x
50 text $w.out.loaded_t
\
51 -background white
-borderwidth 0 \
57 $w.out.loaded_t tag conf annotated
-background grey
59 text $w.out.linenumber_t
\
60 -background white
-borderwidth 0 \
66 $w.out.linenumber_t tag conf linenumber
-justify right
69 -background white
-borderwidth 0 \
74 -xscrollcommand [list $w.out.sbx
set] \
77 scrollbar $w.out.sbx
-orient h
-command [list $w.out.file_t xview
]
78 scrollbar $w.out.sby
-orient v
\
79 -command [list scrollbar2many
[list \
90 grid conf
$w.out.sbx
-column 2 -sticky we
91 grid columnconfigure
$w.out
2 -weight 1
92 grid rowconfigure
$w.out
0 -weight 1
93 pack $w.out
-fill both
-expand 1
96 -textvariable @status
\
101 pack $w.status
-side bottom
-fill x
105 -background white
-borderwidth 0 \
110 -xscrollcommand [list $w.cm.sbx
set] \
111 -yscrollcommand [list $w.cm.sby
set] \
113 scrollbar $w.cm.sbx
-orient h
-command [list $w.cm.t xview
]
114 scrollbar $w.cm.sby
-orient v
-command [list $w.cm.t yview
]
115 pack $w.cm.sby
-side right
-fill y
116 pack $w.cm.sbx
-side bottom
-fill x
117 pack $w.cm.t
-expand 1 -fill both
118 pack $w.cm
-side bottom
-fill x
120 menu $w.ctxm
-tearoff 0
121 $w.ctxm add command
\
122 -label "Copy Commit" \
123 -command [cb _copycommit
]
125 set w_line
$w.out.linenumber_t
126 set w_load
$w.out.loaded_t
127 set w_file
$w.out.file_t
132 $w.out.linenumber_t
\
135 -background [$i cget
-foreground] \
136 -foreground [$i cget
-background]
137 $i conf
-yscrollcommand \
138 [list many2scrollbar
[list \
140 $w.out.linenumber_t
\
143 bind $i <Button-1
> "[cb _click $i @%x,%y]; focus $i"
148 tk_popup $w.ctxm %X %Y
154 $w.out.linenumber_t
\
157 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
158 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
159 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
160 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
161 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
162 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
163 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
164 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
165 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
166 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
169 bind $w.cm.t
<Button-1
> "focus $w.cm.t"
170 bind $top <Visibility
> "focus $top"
171 bind $top <Destroy
> [list delete_this
$this]
173 set cmd
[list git cat-file blob
"$commit:$path"]
174 set fd
[open "| $cmd" r
]
175 fconfigure $fd -blocking 0 -translation lf
-encoding binary
176 fileevent $fd readable
[cb _read_file
$fd]
179 method _read_file
{fd
} {
180 $w_load conf
-state normal
181 $w_line conf
-state normal
182 $w_file conf
-state normal
183 while {[gets $fd line
] >= 0} {
184 regsub "\r\$" $line {} line
186 $w_load insert end
"\n"
187 $w_line insert end
"$total_lines\n" linenumber
188 $w_file insert end
"$line\n"
190 $w_load conf
-state disabled
191 $w_line conf
-state disabled
192 $w_file conf
-state disabled
197 set cmd
[list git blame
-M -C --incremental $commit -- $path]
198 set fd
[open "| $cmd" r
]
199 fconfigure $fd -blocking 0 -translation lf
-encoding binary
200 fileevent $fd readable
[cb _read_blame
$fd]
202 } ifdeleted
{ catch {close $fd} }
204 method _read_blame
{fd
} {
205 while {[gets $fd line
] >= 0} {
206 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
207 cmit original_line final_line line_count
]} {
209 set r_orig_line
$original_line
210 set r_final_line
$final_line
211 set r_line_count
$line_count
213 if {[catch {set g
$order($cmit)}]} {
214 $w_line tag conf g
$cmit
215 $w_file tag conf g
$cmit
216 $w_line tag
raise in_sel
217 $w_file tag
raise in_sel
218 $w_file tag
raise sel
219 set order
($cmit) $commit_count
221 lappend commit_list
$cmit
223 } elseif
{[string match
{filename *} $line]} {
224 set file [string range
$line 9 end
]
226 set lno
$r_final_line
230 if {[catch {set g g
$line_commit($lno)}]} {
231 $w_load tag add annotated
$lno.0 "$lno.0 lineend + 1c"
233 $w_line tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
234 $w_file tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
237 set line_commit
($lno) $cmit
238 set line_file
($lno) $file
239 $w_line tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
240 $w_file tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
242 if {$highlight_line == -1} {
243 if {[lindex [$w_file yview
] 0] == 0} {
245 _showcommit
$this $lno
247 } elseif
{$highlight_line == $lno} {
248 _showcommit
$this $lno
256 set hc
$highlight_commit
258 && [expr {$order($hc) + 1}] == $order($cmit)} {
259 _showcommit
$this $highlight_line
261 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
262 set header
($r_commit,$key) $data
268 set status
{Annotation complete.
}
272 } ifdeleted
{ catch {close $fd} }
275 set have
$blame_lines
276 set total
$total_lines
278 if {$total} {set pdone
[expr {100 * $have / $total}]}
281 "Loading annotations... %i of %i lines annotated (%2i%%)" \
285 method _click
{cur_w pos
} {
286 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
287 if {$lno eq
{}} return
289 $w_line tag remove in_sel
0.0 end
290 $w_file tag remove in_sel
0.0 end
291 $w_line tag add in_sel
$lno.0 "$lno.0 + 1 line"
292 $w_file tag add in_sel
$lno.0 "$lno.0 + 1 line"
294 _showcommit
$this $lno
297 variable blame_colors
{
303 method _showcommit
{lno
} {
305 variable blame_colors
307 if {$highlight_commit ne
{}} {
308 set idx
$order($highlight_commit)
310 foreach c
$blame_colors {
311 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
312 $w_line tag conf g
$h -background white
313 $w_file tag conf g
$h -background white
318 $w_cmit conf
-state normal
319 $w_cmit delete
0.0 end
320 if {[catch {set cmit
$line_commit($lno)} myerr
]} {
321 puts "myerr = $myerr"
323 $w_cmit insert end
"Loading annotation..."
325 set idx
$order($cmit)
327 foreach c
$blame_colors {
328 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
329 $w_line tag conf g
$h -background $c
330 $w_file tag conf g
$h -background $c
337 catch {set author_name
$header($cmit,author
)}
338 catch {set author_email
$header($cmit,author-mail
)}
339 catch {set author_time
[clock format $header($cmit,author-time
)]}
341 set committer_name
{}
342 set committer_email
{}
343 set committer_time
{}
344 catch {set committer_name
$header($cmit,committer
)}
345 catch {set committer_email
$header($cmit,committer-mail
)}
346 catch {set committer_time
[clock format $header($cmit,committer-time
)]}
348 if {[catch {set msg
$header($cmit,message)}]} {
351 set fd
[open "| git cat-file commit $cmit" r
]
352 fconfigure $fd -encoding binary -translation lf
353 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
356 while {[gets $fd line
] > 0} {
357 if {[string match
{encoding *} $line]} {
358 set enc
[string tolower
[string range
$line 9 end
]]
361 set msg
[encoding convertfrom
$enc [read $fd]]
362 set msg
[string trim
$msg]
365 set author_name
[encoding convertfrom
$enc $author_name]
366 set committer_name
[encoding convertfrom
$enc $committer_name]
368 set header
($cmit,author
) $author_name
369 set header
($cmit,committer
) $committer_name
371 set header
($cmit,message) $msg
374 $w_cmit insert end
"commit $cmit\n"
375 $w_cmit insert end
"Author: $author_name $author_email $author_time\n"
376 $w_cmit insert end
"Committer: $committer_name $committer_email $committer_time\n"
377 $w_cmit insert end
"Original File: [escape_path $line_file($lno)]\n"
378 $w_cmit insert end
"\n"
379 $w_cmit insert end
$msg
381 $w_cmit conf
-state disabled
383 set highlight_line
$lno
384 set highlight_commit
$cmit
387 method _copycommit
{} {
388 set pos
@$::cursorX,$::cursorY
389 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
390 if {![catch {set commit
$line_commit($lno)}]} {