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
130 $w.out.linenumber_t
\
133 bind $i <Key-Up
> {catch {%W yview scroll
-1 units
};break}
134 bind $i <Key-Down
> {catch {%W yview scroll
1 units
};break}
135 bind $i <Key-Left
> {catch {%W xview scroll
-1 units
};break}
136 bind $i <Key-Right
> {catch {%W xview scroll
1 units
};break}
137 bind $i <Key-k
> {catch {%W yview scroll
-1 units
};break}
138 bind $i <Key-j
> {catch {%W yview scroll
1 units
};break}
139 bind $i <Key-h
> {catch {%W xview scroll
-1 units
};break}
140 bind $i <Key-l
> {catch {%W xview scroll
1 units
};break}
141 bind $i <Control-Key-b
> {catch {%W yview scroll
-1 pages
};break}
142 bind $i <Control-Key-f
> {catch {%W yview scroll
1 pages
};break}
145 bind $w.cm.t
<Button-1
> "focus $w.cm.t"
146 bind $tl <Visibility
> "focus $tl"
148 array unset blame_status {$w}
149 array unset blame_data $w,*
151 wm title
$tl "[appname] ([reponame]): File Viewer"
153 set blame_data
($w,commit_count
) 0
154 set blame_data
($w,commit_list
) {}
155 set blame_data
($w,total_lines
) 0
156 set blame_data
($w,blame_lines
) 0
157 set blame_data
($w,highlight_commit
) {}
158 set blame_data
($w,highlight_line
) -1
160 set cmd
[list git cat-file blob
"$commit:$path"]
161 set fd
[open "| $cmd" r
]
162 fconfigure $fd -blocking 0 -translation lf
-encoding binary
163 fileevent $fd readable
[list read_blame_catfile
\
164 $fd $w $commit $path \
165 $w.cm.t
$w.out.loaded_t
$w.out.linenumber_t
$w.out.file_t
]
168 proc read_blame_catfile
{fd w commit path w_cmit w_load w_line w_file
} {
169 global blame_status blame_data
171 if {![winfo exists
$w_file]} {
176 set n
$blame_data($w,total_lines
)
177 $w_load conf
-state normal
178 $w_line conf
-state normal
179 $w_file conf
-state normal
180 while {[gets $fd line
] >= 0} {
181 regsub "\r\$" $line {} line
183 $w_load insert end
"\n"
184 $w_line insert end
"$n\n" linenumber
185 $w_file insert end
"$line\n"
187 $w_load conf
-state disabled
188 $w_line conf
-state disabled
189 $w_file conf
-state disabled
190 set blame_data
($w,total_lines
) $n
194 blame_incremental_status
$w
195 set cmd
[list git blame
-M -C --incremental]
196 lappend cmd
$commit -- $path
197 set fd
[open "| $cmd" r
]
198 fconfigure $fd -blocking 0 -translation lf
-encoding binary
199 fileevent $fd readable
[list read_blame_incremental
$fd $w \
200 $w_load $w_cmit $w_line $w_file]
204 proc read_blame_incremental
{fd w w_load w_cmit w_line w_file
} {
205 global blame_status blame_data
207 if {![winfo exists
$w_file]} {
212 while {[gets $fd line
] >= 0} {
213 if {[regexp {^
([a-z0-9
]{40}) (\d
+) (\d
+) (\d
+)$} $line line
\
214 cmit original_line final_line line_count
]} {
215 set blame_data
($w,commit
) $cmit
216 set blame_data
($w,original_line
) $original_line
217 set blame_data
($w,final_line
) $final_line
218 set blame_data
($w,line_count
) $line_count
220 if {[catch {set g
$blame_data($w,$cmit,order
)}]} {
221 $w_line tag conf g
$cmit
222 $w_file tag conf g
$cmit
223 $w_line tag
raise in_sel
224 $w_file tag
raise in_sel
225 $w_file tag
raise sel
226 set blame_data
($w,$cmit,order
) $blame_data($w,commit_count
)
227 incr blame_data
($w,commit_count
)
228 lappend blame_data
($w,commit_list
) $cmit
230 } elseif
{[string match
{filename *} $line]} {
231 set file [string range
$line 9 end
]
232 set n
$blame_data($w,line_count
)
233 set lno
$blame_data($w,final_line
)
234 set cmit
$blame_data($w,commit
)
237 if {[catch {set g g
$blame_data($w,line
$lno,commit
)}]} {
238 $w_load tag add annotated
$lno.0 "$lno.0 lineend + 1c"
240 $w_line tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
241 $w_file tag remove g
$g $lno.0 "$lno.0 lineend + 1c"
244 set blame_data
($w,line
$lno,commit
) $cmit
245 set blame_data
($w,line
$lno,file) $file
246 $w_line tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
247 $w_file tag add g
$cmit $lno.0 "$lno.0 lineend + 1c"
249 if {$blame_data($w,highlight_line
) == -1} {
250 if {[lindex [$w_file yview
] 0] == 0} {
252 blame_showcommit
$w $w_cmit $w_line $w_file $lno
254 } elseif
{$blame_data($w,highlight_line
) == $lno} {
255 blame_showcommit
$w $w_cmit $w_line $w_file $lno
260 incr blame_data
($w,blame_lines
)
263 set hc
$blame_data($w,highlight_commit
)
265 && [expr {$blame_data($w,$hc,order
) + 1}]
266 == $blame_data($w,$cmit,order
)} {
267 blame_showcommit
$w $w_cmit $w_line $w_file \
268 $blame_data($w,highlight_line
)
270 } elseif
{[regexp {^
([a-z-
]+) (.
*)$} $line line header data
]} {
271 set blame_data
($w,$blame_data($w,commit
),$header) $data
277 set blame_status
($w) {Annotation complete.
}
279 blame_incremental_status
$w
283 proc blame_incremental_status
{w
} {
284 global blame_status blame_data
286 set have
$blame_data($w,blame_lines
)
287 set total
$blame_data($w,total_lines
)
289 if {$total} {set pdone
[expr {100 * $have / $total}]}
291 set blame_status
($w) [format \
292 "Loading annotations... %i of %i lines annotated (%2i%%)" \
296 proc blame_click
{w w_cmit w_line w_file cur_w pos
} {
297 set lno
[lindex [split [$cur_w index
$pos] .
] 0]
298 if {$lno eq
{}} return
300 $w_line tag remove in_sel
0.0 end
301 $w_file tag remove in_sel
0.0 end
302 $w_line tag add in_sel
$lno.0 "$lno.0 + 1 line"
303 $w_file tag add in_sel
$lno.0 "$lno.0 + 1 line"
305 blame_showcommit
$w $w_cmit $w_line $w_file $lno
314 proc blame_showcommit
{w w_cmit w_line w_file lno
} {
315 global blame_colors blame_data repo_config
317 set cmit
$blame_data($w,highlight_commit
)
319 set idx
$blame_data($w,$cmit,order
)
321 foreach c
$blame_colors {
322 set h
[lindex $blame_data($w,commit_list
) [expr {$idx - 1 + $i}]]
323 $w_line tag conf g
$h -background white
324 $w_file tag conf g
$h -background white
329 $w_cmit conf
-state normal
330 $w_cmit delete
0.0 end
331 if {[catch {set cmit
$blame_data($w,line
$lno,commit
)}]} {
333 $w_cmit insert end
"Loading annotation..."
335 set idx
$blame_data($w,$cmit,order
)
337 foreach c
$blame_colors {
338 set h
[lindex $blame_data($w,commit_list
) [expr {$idx - 1 + $i}]]
339 $w_line tag conf g
$h -background $c
340 $w_file tag conf g
$h -background $c
347 catch {set author_name
$blame_data($w,$cmit,author
)}
348 catch {set author_email
$blame_data($w,$cmit,author-mail
)}
349 catch {set author_time
[clock format $blame_data($w,$cmit,author-time
)]}
351 set committer_name
{}
352 set committer_email
{}
353 set committer_time
{}
354 catch {set committer_name
$blame_data($w,$cmit,committer
)}
355 catch {set committer_email
$blame_data($w,$cmit,committer-mail
)}
356 catch {set committer_time
[clock format $blame_data($w,$cmit,committer-time
)]}
358 if {[catch {set msg
$blame_data($w,$cmit,message)}]} {
361 set fd
[open "| git cat-file commit $cmit" r
]
362 fconfigure $fd -encoding binary -translation lf
363 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
366 while {[gets $fd line
] > 0} {
367 if {[string match
{encoding *} $line]} {
368 set enc
[string tolower
[string range
$line 9 end
]]
371 set msg
[encoding convertfrom
$enc [read $fd]]
372 set msg
[string trim
$msg]
375 set author_name
[encoding convertfrom
$enc $author_name]
376 set committer_name
[encoding convertfrom
$enc $committer_name]
378 set blame_data
($w,$cmit,author
) $author_name
379 set blame_data
($w,$cmit,committer
) $committer_name
381 set blame_data
($w,$cmit,message) $msg
384 $w_cmit insert end
"commit $cmit\n"
385 $w_cmit insert end
"Author: $author_name $author_email $author_time\n"
386 $w_cmit insert end
"Committer: $committer_name $committer_email $committer_time\n"
387 $w_cmit insert end
"Original File: [escape_path $blame_data($w,line$lno,file)]\n"
388 $w_cmit insert end
"\n"
389 $w_cmit insert end
$msg
391 $w_cmit conf
-state disabled
393 set blame_data
($w,highlight_line
) $lno
394 set blame_data
($w,highlight_commit
) $cmit
397 proc blame_copycommit
{w i pos
} {
399 set lno
[lindex [split [$i index
$pos] .
] 0]
400 if {![catch {set commit
$blame_data($w,line
$lno,commit
)}]} {