2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc show_blame
{commit path
} {
5 global next_browser_id blame_status blame_data
7 if {[winfo ismapped .
]} {
8 set w .browser
[incr next_browser_id
]
15 set blame_status
($w) {Loading current
file content...
}
17 label $w.path
-text "$commit:$path" \
23 pack $w.path
-side top
-fill x
26 text $w.out.loaded_t
\
27 -background white
-borderwidth 0 \
33 $w.out.loaded_t tag conf annotated
-background grey
35 text $w.out.linenumber_t
\
36 -background white
-borderwidth 0 \
42 $w.out.linenumber_t tag conf linenumber
-justify right
45 -background white
-borderwidth 0 \
50 -xscrollcommand [list $w.out.sbx
set] \
53 scrollbar $w.out.sbx
-orient h
-command [list $w.out.file_t xview
]
54 scrollbar $w.out.sby
-orient v
\
55 -command [list scrollbar2many
[list \
66 grid conf
$w.out.sbx
-column 2 -sticky we
67 grid columnconfigure
$w.out
2 -weight 1
68 grid rowconfigure
$w.out
0 -weight 1
69 pack $w.out
-fill both
-expand 1
71 label $w.status
-textvariable blame_status
($w) \
76 pack $w.status
-side bottom
-fill x
80 -background white
-borderwidth 0 \
85 -xscrollcommand [list $w.cm.sbx
set] \
86 -yscrollcommand [list $w.cm.sby
set] \
88 scrollbar $w.cm.sbx
-orient h
-command [list $w.cm.t xview
]
89 scrollbar $w.cm.sby
-orient v
-command [list $w.cm.t yview
]
90 pack $w.cm.sby
-side right
-fill y
91 pack $w.cm.sbx
-side bottom
-fill x
92 pack $w.cm.t
-expand 1 -fill both
93 pack $w.cm
-side bottom
-fill x
95 menu $w.ctxm
-tearoff 0
96 $w.ctxm add command
-label "Copy Commit" \
97 -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
101 $w.out.linenumber_t
\
104 -background [$i cget
-foreground] \
105 -foreground [$i cget
-background]
106 $i conf
-yscrollcommand \
107 [list many2scrollbar
[list \
109 $w.out.linenumber_t
\
115 $w.out.linenumber_t \\
124 tk_popup $w.ctxm %X %Y
128 bind $w.cm.t
<Button-1
> "focus $w.cm.t"
129 bind $tl <Visibility
> "focus $tl"
131 array unset blame_status {$w}
132 array unset blame_data $w,*
134 wm title
$tl "[appname] ([reponame]): File Viewer"
136 set blame_data
($w,commit_count
) 0
137 set blame_data
($w,commit_list
) {}
138 set blame_data
($w,total_lines
) 0
139 set blame_data
($w,blame_lines
) 0
140 set blame_data
($w,highlight_commit
) {}
141 set blame_data
($w,highlight_line
) -1
143 set cmd
[list git cat-file blob
"$commit:$path"]
144 set fd
[open "| $cmd" r
]
145 fconfigure $fd -blocking 0 -translation lf
-encoding binary
146 fileevent $fd readable
[list read_blame_catfile
\
147 $fd $w $commit $path \
148 $w.cm.t
$w.out.loaded_t
$w.out.linenumber_t
$w.out.file_t
]
151 proc read_blame_catfile
{fd w commit path w_cmit w_load w_line w_file
} {
152 global blame_status blame_data
154 if {![winfo exists
$w_file]} {
159 set n
$blame_data($w,total_lines
)
160 $w_load conf
-state normal
161 $w_line conf
-state normal
162 $w_file conf
-state normal
163 while {[gets $fd line
] >= 0} {
164 regsub "\r\$" $line {} line
166 $w_load insert end
"\n"
167 $w_line insert end
"$n\n" linenumber
168 $w_file insert end
"$line\n"
170 $w_load conf
-state disabled
171 $w_line conf
-state disabled
172 $w_file conf
-state disabled
173 set blame_data
($w,total_lines
) $n
177 blame_incremental_status
$w
178 set cmd
[list git blame
-M -C --incremental]
179 lappend cmd
$commit -- $path
180 set fd
[open "| $cmd" r
]
181 fconfigure $fd -blocking 0 -translation lf
-encoding binary
182 fileevent $fd readable
[list read_blame_incremental
$fd $w \
183 $w_load $w_cmit $w_line $w_file]
187 proc read_blame_incremental
{fd w w_load w_cmit w_line w_file
} {
188 global blame_status blame_data
190 if {![winfo exists
$w_file]} {
195 while {[gets $fd line
] >= 0} {
196 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
197 cmit original_line final_line line_count
]} {
198 set blame_data
($w,commit
) $cmit
199 set blame_data
($w,original_line
) $original_line
200 set blame_data
($w,final_line
) $final_line
201 set blame_data
($w,line_count
) $line_count
203 if {[catch {set g
$blame_data($w,$cmit,order
)}]} {
204 $w_line tag conf g
$cmit
205 $w_file tag conf g
$cmit
206 $w_line tag
raise in_sel
207 $w_file tag
raise in_sel
208 $w_file tag
raise sel
209 set blame_data
($w,$cmit,order
) $blame_data($w,commit_count
)
210 incr blame_data
($w,commit_count
)
211 lappend blame_data
($w,commit_list
) $cmit
213 } elseif
{[string match
{filename *} $line]} {
214 set file [string range
$line 9 end
]
215 set n
$blame_data($w,line_count
)
216 set lno
$blame_data($w,final_line
)
217 set cmit
$blame_data($w,commit
)
220 if {[catch {set g g
$blame_data($w,line
$lno,commit
)}]} {
221 $w_load tag add annotated
$lno.0 "$lno.0 lineend + 1c"
223 $w_line tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
224 $w_file tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
227 set blame_data
($w,line
$lno,commit
) $cmit
228 set blame_data
($w,line
$lno,file) $file
229 $w_line tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
230 $w_file tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
232 if {$blame_data($w,highlight_line
) == -1} {
233 if {[lindex [$w_file yview
] 0] == 0} {
235 blame_showcommit
$w $w_cmit $w_line $w_file $lno
237 } elseif
{$blame_data($w,highlight_line
) == $lno} {
238 blame_showcommit
$w $w_cmit $w_line $w_file $lno
243 incr blame_data
($w,blame_lines
)
246 set hc
$blame_data($w,highlight_commit
)
248 && [expr {$blame_data($w,$hc,order
) + 1}]
249 == $blame_data($w,$cmit,order
)} {
250 blame_showcommit
$w $w_cmit $w_line $w_file \
251 $blame_data($w,highlight_line
)
253 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line header data
]} {
254 set blame_data
($w,$blame_data($w,commit
),$header) $data
260 set blame_status
($w) {Annotation complete.
}
262 blame_incremental_status
$w
266 proc blame_incremental_status
{w
} {
267 global blame_status blame_data
269 set have
$blame_data($w,blame_lines
)
270 set total
$blame_data($w,total_lines
)
272 if {$total} {set pdone
[expr {100 * $have / $total}]}
274 set blame_status
($w) [format \
275 "Loading annotations... %i of %i lines annotated (%2i%%)" \
279 proc blame_click
{w w_cmit w_line w_file cur_w pos
} {
280 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
281 if {$lno eq
{}} return
283 $w_line tag remove in_sel
0.0 end
284 $w_file tag remove in_sel
0.0 end
285 $w_line tag add in_sel
$lno.0 "$lno.0 + 1 line"
286 $w_file tag add in_sel
$lno.0 "$lno.0 + 1 line"
288 blame_showcommit
$w $w_cmit $w_line $w_file $lno
297 proc blame_showcommit
{w w_cmit w_line w_file lno
} {
298 global blame_colors blame_data repo_config
300 set cmit
$blame_data($w,highlight_commit
)
302 set idx
$blame_data($w,$cmit,order
)
304 foreach c
$blame_colors {
305 set h
[lindex $blame_data($w,commit_list
) [expr {$idx - 1 + $i}]]
306 $w_line tag conf g
$h -background white
307 $w_file tag conf g
$h -background white
312 $w_cmit conf
-state normal
313 $w_cmit delete
0.0 end
314 if {[catch {set cmit
$blame_data($w,line
$lno,commit
)}]} {
316 $w_cmit insert end
"Loading annotation..."
318 set idx
$blame_data($w,$cmit,order
)
320 foreach c
$blame_colors {
321 set h
[lindex $blame_data($w,commit_list
) [expr {$idx - 1 + $i}]]
322 $w_line tag conf g
$h -background $c
323 $w_file tag conf g
$h -background $c
330 catch {set author_name
$blame_data($w,$cmit,author
)}
331 catch {set author_email
$blame_data($w,$cmit,author-mail
)}
332 catch {set author_time
[clock format $blame_data($w,$cmit,author-time
)]}
334 set committer_name
{}
335 set committer_email
{}
336 set committer_time
{}
337 catch {set committer_name
$blame_data($w,$cmit,committer
)}
338 catch {set committer_email
$blame_data($w,$cmit,committer-mail
)}
339 catch {set committer_time
[clock format $blame_data($w,$cmit,committer-time
)]}
341 if {[catch {set msg
$blame_data($w,$cmit,message)}]} {
344 set fd
[open "| git cat-file commit $cmit" r
]
345 fconfigure $fd -encoding binary -translation lf
346 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
349 while {[gets $fd line
] > 0} {
350 if {[string match
{encoding *} $line]} {
351 set enc
[string tolower
[string range
$line 9 end
]]
354 set msg
[encoding convertfrom
$enc [read $fd]]
355 set msg
[string trim
$msg]
358 set author_name
[encoding convertfrom
$enc $author_name]
359 set committer_name
[encoding convertfrom
$enc $committer_name]
361 set blame_data
($w,$cmit,author
) $author_name
362 set blame_data
($w,$cmit,committer
) $committer_name
364 set blame_data
($w,$cmit,message) $msg
367 $w_cmit insert end
"commit $cmit\n"
368 $w_cmit insert end
"Author: $author_name $author_email $author_time\n"
369 $w_cmit insert end
"Committer: $committer_name $committer_email $committer_time\n"
370 $w_cmit insert end
"Original File: [escape_path $blame_data($w,line$lno,file)]\n"
371 $w_cmit insert end
"\n"
372 $w_cmit insert end
$msg
374 $w_cmit conf
-state disabled
376 set blame_data
($w,highlight_line
) $lno
377 set blame_data
($w,highlight_commit
) $cmit
380 proc blame_copycommit
{w i pos
} {
382 set lno
[lindex [split [$i index
$pos] .
] 0]
383 if {![catch {set commit
$blame_data($w,line
$lno,commit
)}]} {