2 # Copyright (C) 2006, 2007 Shawn Pearce
6 proc show_blame {commit path} {
7 global next_browser_id blame_status blame_data
9 if {[winfo ismapped .]} {
10 set w .browser[incr next_browser_id]
17 set blame_status($w) {Loading current file content...}
19 label $w.path -text "$commit:$path" \
25 pack $w.path -side top -fill x
28 text $w.out.loaded_t \
29 -background white -borderwidth 0 \
35 $w.out.loaded_t tag conf annotated -background grey
37 text $w.out.linenumber_t \
38 -background white -borderwidth 0 \
44 $w.out.linenumber_t tag conf linenumber -justify right
47 -background white -borderwidth 0 \
52 -xscrollcommand [list $w.out.sbx set] \
55 scrollbar $w.out.sbx -orient h -command [list $w.out.file_t xview]
56 scrollbar $w.out.sby -orient v \
57 -command [list scrollbar2many [list \
68 grid conf $w.out.sbx -column 2 -sticky we
69 grid columnconfigure $w.out 2 -weight 1
70 grid rowconfigure $w.out 0 -weight 1
71 pack $w.out -fill both -expand 1
73 label $w.status -textvariable blame_status($w) \
78 pack $w.status -side bottom -fill x
82 -background white -borderwidth 0 \
87 -xscrollcommand [list $w.cm.sbx set] \
88 -yscrollcommand [list $w.cm.sby set] \
90 scrollbar $w.cm.sbx -orient h -command [list $w.cm.t xview]
91 scrollbar $w.cm.sby -orient v -command [list $w.cm.t yview]
92 pack $w.cm.sby -side right -fill y
93 pack $w.cm.sbx -side bottom -fill x
94 pack $w.cm.t -expand 1 -fill both
95 pack $w.cm -side bottom -fill x
97 menu $w.ctxm -tearoff 0
98 $w.ctxm add command -label "Copy Commit" \
99 -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
103 $w.out.linenumber_t \
106 -background [$i cget -foreground] \
107 -foreground [$i cget -background]
108 $i conf -yscrollcommand \
109 [list many2scrollbar [list \
111 $w.out.linenumber_t \
117 $w.out.linenumber_t \\
126 tk_popup $w.ctxm %X %Y
132 $w.out.linenumber_t \
135 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
136 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
137 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
138 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
139 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
140 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
141 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
142 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
143 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
144 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
147 bind $w.cm.t <Button-1> "focus $w.cm.t"
148 bind $tl <Visibility> "focus $tl"
150 array unset blame_status {$w}
151 array unset blame_data $w,*
153 wm title $tl "[appname] ([reponame]): File Viewer"
155 set blame_data($w,commit_count) 0
156 set blame_data($w,commit_list) {}
157 set blame_data($w,total_lines) 0
158 set blame_data($w,blame_lines) 0
159 set blame_data($w,highlight_commit) {}
160 set blame_data($w,highlight_line) -1
162 set cmd [list git cat-file blob "$commit:$path"]
163 set fd [open "| $cmd" r]
164 fconfigure $fd -blocking 0 -translation lf -encoding binary
165 fileevent $fd readable [list read_blame_catfile \
166 $fd $w $commit $path \
167 $w.cm.t $w.out.loaded_t $w.out.linenumber_t $w.out.file_t]
170 proc read_blame_catfile {fd w commit path w_cmit w_load w_line w_file} {
171 global blame_status blame_data
173 if {![winfo exists $w_file]} {
178 set n $blame_data($w,total_lines)
179 $w_load conf -state normal
180 $w_line conf -state normal
181 $w_file conf -state normal
182 while {[gets $fd line] >= 0} {
183 regsub "\r\$" $line {} line
185 $w_load insert end "\n"
186 $w_line insert end "$n\n" linenumber
187 $w_file insert end "$line\n"
189 $w_load conf -state disabled
190 $w_line conf -state disabled
191 $w_file conf -state disabled
192 set blame_data($w,total_lines) $n
196 blame_incremental_status $w
197 set cmd [list git blame -M -C --incremental]
198 lappend cmd $commit -- $path
199 set fd [open "| $cmd" r]
200 fconfigure $fd -blocking 0 -translation lf -encoding binary
201 fileevent $fd readable [list read_blame_incremental $fd $w \
202 $w_load $w_cmit $w_line $w_file]
206 proc read_blame_incremental {fd w w_load w_cmit w_line w_file} {
207 global blame_status blame_data
209 if {![winfo exists $w_file]} {
214 while {[gets $fd line] >= 0} {
215 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
216 cmit original_line final_line line_count]} {
217 set blame_data($w,commit) $cmit
218 set blame_data($w,original_line) $original_line
219 set blame_data($w,final_line) $final_line
220 set blame_data($w,line_count) $line_count
222 if {[catch {set g $blame_data($w,$cmit,order)}]} {
223 $w_line tag conf g$cmit
224 $w_file tag conf g$cmit
225 $w_line tag raise in_sel
226 $w_file tag raise in_sel
227 $w_file tag raise sel
228 set blame_data($w,$cmit,order) $blame_data($w,commit_count)
229 incr blame_data($w,commit_count)
230 lappend blame_data($w,commit_list) $cmit
232 } elseif {[string match {filename *} $line]} {
233 set file [string range $line 9 end]
234 set n $blame_data($w,line_count)
235 set lno $blame_data($w,final_line)
236 set cmit $blame_data($w,commit)
239 if {[catch {set g g$blame_data($w,line$lno,commit)}]} {
240 $w_load tag add annotated $lno.0 "$lno.0 lineend + 1c"
242 $w_line tag remove g$g $lno.0 "$lno.0 lineend + 1c"
243 $w_file tag remove g$g $lno.0 "$lno.0 lineend + 1c"
246 set blame_data($w,line$lno,commit) $cmit
247 set blame_data($w,line$lno,file) $file
248 $w_line tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
249 $w_file tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
251 if {$blame_data($w,highlight_line) == -1} {
252 if {[lindex [$w_file yview] 0] == 0} {
254 blame_showcommit $w $w_cmit $w_line $w_file $lno
256 } elseif {$blame_data($w,highlight_line) == $lno} {
257 blame_showcommit $w $w_cmit $w_line $w_file $lno
262 incr blame_data($w,blame_lines)
265 set hc $blame_data($w,highlight_commit)
267 && [expr {$blame_data($w,$hc,order) + 1}]
268 == $blame_data($w,$cmit,order)} {
269 blame_showcommit $w $w_cmit $w_line $w_file \
270 $blame_data($w,highlight_line)
272 } elseif {[regexp {^([a-z-]+) (.*)$} $line line header data]} {
273 set blame_data($w,$blame_data($w,commit),$header) $data
279 set blame_status($w) {Annotation complete.}
281 blame_incremental_status $w
285 proc blame_incremental_status {w} {
286 global blame_status blame_data
288 set have $blame_data($w,blame_lines)
289 set total $blame_data($w,total_lines)
291 if {$total} {set pdone [expr {100 * $have / $total}]}
293 set blame_status($w) [format \
294 "Loading annotations... %i of %i lines annotated (%2i%%)" \
298 proc blame_click {w w_cmit w_line w_file cur_w pos} {
299 set lno [lindex [split [$cur_w index $pos] .] 0]
300 if {$lno eq {}} return
302 $w_line tag remove in_sel 0.0 end
303 $w_file tag remove in_sel 0.0 end
304 $w_line tag add in_sel $lno.0 "$lno.0 + 1 line"
305 $w_file tag add in_sel $lno.0 "$lno.0 + 1 line"
307 blame_showcommit $w $w_cmit $w_line $w_file $lno
316 proc blame_showcommit {w w_cmit w_line w_file lno} {
317 global blame_colors blame_data repo_config
319 set cmit $blame_data($w,highlight_commit)
321 set idx $blame_data($w,$cmit,order)
323 foreach c $blame_colors {
324 set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
325 $w_line tag conf g$h -background white
326 $w_file tag conf g$h -background white
331 $w_cmit conf -state normal
332 $w_cmit delete 0.0 end
333 if {[catch {set cmit $blame_data($w,line$lno,commit)}]} {
335 $w_cmit insert end "Loading annotation..."
337 set idx $blame_data($w,$cmit,order)
339 foreach c $blame_colors {
340 set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
341 $w_line tag conf g$h -background $c
342 $w_file tag conf g$h -background $c
349 catch {set author_name $blame_data($w,$cmit,author)}
350 catch {set author_email $blame_data($w,$cmit,author-mail)}
351 catch {set author_time [clock format $blame_data($w,$cmit,author-time)]}
353 set committer_name {}
354 set committer_email {}
355 set committer_time {}
356 catch {set committer_name $blame_data($w,$cmit,committer)}
357 catch {set committer_email $blame_data($w,$cmit,committer-mail)}
358 catch {set committer_time [clock format $blame_data($w,$cmit,committer-time)]}
360 if {[catch {set msg $blame_data($w,$cmit,message)}]} {
363 set fd [open "| git cat-file commit $cmit" r]
364 fconfigure $fd -encoding binary -translation lf
365 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
368 while {[gets $fd line] > 0} {
369 if {[string match {encoding *} $line]} {
370 set enc [string tolower [string range $line 9 end]]
373 set msg [encoding convertfrom $enc [read $fd]]
374 set msg [string trim $msg]
377 set author_name [encoding convertfrom $enc $author_name]
378 set committer_name [encoding convertfrom $enc $committer_name]
380 set blame_data($w,$cmit,author) $author_name
381 set blame_data($w,$cmit,committer) $committer_name
383 set blame_data($w,$cmit,message) $msg
386 $w_cmit insert end "commit $cmit\n"
387 $w_cmit insert end "Author: $author_name $author_email $author_time\n"
388 $w_cmit insert end "Committer: $committer_name $committer_email $committer_time\n"
389 $w_cmit insert end "Original File: [escape_path $blame_data($w,line$lno,file)]\n"
390 $w_cmit insert end "\n"
391 $w_cmit insert end $msg
393 $w_cmit conf -state disabled
395 set blame_data($w,highlight_line) $lno
396 set blame_data($w,highlight_commit) $cmit
399 proc blame_copycommit {w i pos} {
401 set lno [lindex [split [$i index $pos] .] 0]
402 if {![catch {set commit $blame_data($w,line$lno,commit)}]} {