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
> [list focus $w.cm.t
]
170 bind $top <Visibility
> [list focus $top]
171 bind $top <Destroy
> [list delete_this
$this]
174 set fd
[open $path r
]
176 set cmd
[list git cat-file blob
"$commit:$path"]
177 set fd
[open "| $cmd" r
]
179 fconfigure $fd -blocking 0 -translation lf
-encoding binary
180 fileevent $fd readable
[cb _read_file
$fd]
183 method _read_file
{fd
} {
184 $w_load conf
-state normal
185 $w_line conf
-state normal
186 $w_file conf
-state normal
187 while {[gets $fd line
] >= 0} {
188 regsub "\r\$" $line {} line
190 $w_load insert end
"\n"
191 $w_line insert end
"$total_lines\n" linenumber
192 $w_file insert end
"$line\n"
194 $w_load conf
-state disabled
195 $w_line conf
-state disabled
196 $w_file conf
-state disabled
201 set cmd
[list git blame
-M -C --incremental]
203 lappend cmd
--contents $path
208 set fd
[open "| $cmd" r
]
209 fconfigure $fd -blocking 0 -translation lf
-encoding binary
210 fileevent $fd readable
[cb _read_blame
$fd]
212 } ifdeleted
{ catch {close $fd} }
214 method _read_blame
{fd
} {
215 while {[gets $fd line
] >= 0} {
216 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
217 cmit original_line final_line line_count
]} {
219 set r_orig_line
$original_line
220 set r_final_line
$final_line
221 set r_line_count
$line_count
223 if {[catch {set g
$order($cmit)}]} {
224 $w_line tag conf g
$cmit
225 $w_file tag conf g
$cmit
226 $w_line tag
raise in_sel
227 $w_file tag
raise in_sel
228 $w_file tag
raise sel
229 set order
($cmit) $commit_count
231 lappend commit_list
$cmit
233 } elseif
{[string match
{filename *} $line]} {
234 set file [string range
$line 9 end
]
236 set lno
$r_final_line
240 set lno_e
"$lno.0 lineend + 1c"
241 if {[catch {set g g
$line_commit($lno)}]} {
242 $w_load tag add annotated
$lno.0 $lno_e
244 $w_line tag remove g
$g $lno.0 $lno_e
245 $w_file tag remove g
$g $lno.0 $lno_e
248 set line_commit
($lno) $cmit
249 set line_file
($lno) $file
250 $w_line tag add g
$cmit $lno.0 $lno_e
251 $w_file tag add g
$cmit $lno.0 $lno_e
253 if {$highlight_line == -1} {
254 if {[lindex [$w_file yview
] 0] == 0} {
256 _showcommit
$this $lno
258 } elseif
{$highlight_line == $lno} {
259 _showcommit
$this $lno
267 set hc
$highlight_commit
269 && [expr {$order($hc) + 1}] == $order($cmit)} {
270 _showcommit
$this $highlight_line
272 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line key data
]} {
273 set header
($r_commit,$key) $data
279 set status
{Annotation complete.
}
283 } ifdeleted
{ catch {close $fd} }
286 set have
$blame_lines
287 set total
$total_lines
289 if {$total} {set pdone
[expr {100 * $have / $total}]}
292 "Loading annotations... %i of %i lines annotated (%2i%%)" \
296 method _click
{cur_w pos
} {
297 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
298 if {$lno eq
{}} return
300 set lno_e
"$lno.0 + 1 line"
301 $w_line tag remove in_sel
0.0 end
302 $w_file tag remove in_sel
0.0 end
303 $w_line tag add in_sel
$lno.0 $lno_e
304 $w_file tag add in_sel
$lno.0 $lno_e
306 _showcommit
$this $lno
309 variable blame_colors
{
315 method _showcommit
{lno
} {
317 variable blame_colors
319 if {$highlight_commit ne
{}} {
320 set idx
$order($highlight_commit)
322 foreach c
$blame_colors {
323 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
324 $w_line tag conf g
$h -background white
325 $w_file tag conf g
$h -background white
330 $w_cmit conf
-state normal
331 $w_cmit delete
0.0 end
332 if {[catch {set cmit
$line_commit($lno)}]} {
334 $w_cmit insert end
"Loading annotation..."
336 set idx
$order($cmit)
338 foreach c
$blame_colors {
339 set h
[lindex $commit_list [expr {$idx - 1 + $i}]]
340 $w_line tag conf g
$h -background $c
341 $w_file tag conf g
$h -background $c
348 catch {set author_name
$header($cmit,author
)}
349 catch {set author_email
$header($cmit,author-mail
)}
350 catch {set author_time
[clock format \
351 $header($cmit,author-time
) \
352 -format {%Y-
%m-
%d
%H
:%M
:%S
}
355 set committer_name
{}
356 set committer_email
{}
357 set committer_time
{}
358 catch {set committer_name
$header($cmit,committer
)}
359 catch {set committer_email
$header($cmit,committer-mail
)}
360 catch {set committer_time
[clock format \
361 $header($cmit,committer-time
) \
362 -format {%Y-
%m-
%d
%H
:%M
:%S
}
365 if {[catch {set msg
$header($cmit,message)}]} {
368 set fd
[open "| git cat-file commit $cmit" r
]
369 fconfigure $fd -encoding binary -translation lf
370 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
373 while {[gets $fd line
] > 0} {
374 if {[string match
{encoding *} $line]} {
375 set enc
[string tolower
[string range
$line 9 end
]]
378 set msg
[encoding convertfrom
$enc [read $fd]]
379 set msg
[string trim
$msg]
382 set author_name
[encoding convertfrom
$enc $author_name]
383 set committer_name
[encoding convertfrom
$enc $committer_name]
385 set header
($cmit,author
) $author_name
386 set header
($cmit,committer
) $committer_name
388 set header
($cmit,message) $msg
391 $w_cmit insert end
"commit $cmit
392 Author: $author_name $author_email $author_time
393 Committer: $committer_name $committer_email $committer_time
394 Original File: [escape_path $line_file($lno)]
398 $w_cmit conf
-state disabled
400 set highlight_line
$lno
401 set highlight_commit
$cmit
404 method _copycommit
{} {
405 set pos
@$::cursorX,$::cursorY
406 set lno
[lindex [split [$::cursorW index
$pos] .
] 0]
407 if {![catch {set commit
$line_commit($lno)}]} {