Revert "Make the installation target of git-gui a little less chatty"
[alt-git.git] / lib / blame.tcl
blob8b032d959011f111c052fb4eee8be02aa6d45dd9
1 # git-gui blame viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class blame {
6 field commit ; # input commit to blame
7 field path ; # input filename to view in $commit
9 field w
10 field w_line
11 field w_load
12 field w_file
13 field w_cmit
14 field status
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} {
34 set commit $i_commit
35 set path $i_path
37 make_toplevel top w
38 wm title $top "[appname] ([reponame]): File Viewer"
39 set status "Loading $commit:$path..."
41 label $w.path -text "$commit:$path" \
42 -anchor w \
43 -justify left \
44 -borderwidth 1 \
45 -relief sunken \
46 -font font_uibold
47 pack $w.path -side top -fill x
49 frame $w.out
50 text $w.out.loaded_t \
51 -background white -borderwidth 0 \
52 -state disabled \
53 -wrap none \
54 -height 40 \
55 -width 1 \
56 -font font_diff
57 $w.out.loaded_t tag conf annotated -background grey
59 text $w.out.linenumber_t \
60 -background white -borderwidth 0 \
61 -state disabled \
62 -wrap none \
63 -height 40 \
64 -width 5 \
65 -font font_diff
66 $w.out.linenumber_t tag conf linenumber -justify right
68 text $w.out.file_t \
69 -background white -borderwidth 0 \
70 -state disabled \
71 -wrap none \
72 -height 40 \
73 -width 80 \
74 -xscrollcommand [list $w.out.sbx set] \
75 -font font_diff
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 \
80 $w.out.loaded_t \
81 $w.out.linenumber_t \
82 $w.out.file_t \
83 ] yview]
84 grid \
85 $w.out.linenumber_t \
86 $w.out.loaded_t \
87 $w.out.file_t \
88 $w.out.sby \
89 -sticky nsew
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
95 label $w.status \
96 -textvariable @status \
97 -anchor w \
98 -justify left \
99 -borderwidth 1 \
100 -relief sunken
101 pack $w.status -side bottom -fill x
103 frame $w.cm
104 text $w.cm.t \
105 -background white -borderwidth 0 \
106 -state disabled \
107 -wrap none \
108 -height 10 \
109 -width 80 \
110 -xscrollcommand [list $w.cm.sbx set] \
111 -yscrollcommand [list $w.cm.sby set] \
112 -font font_diff
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
128 set w_cmit $w.cm.t
130 foreach i [list \
131 $w.out.loaded_t \
132 $w.out.linenumber_t \
133 $w.out.file_t] {
134 $i tag conf in_sel \
135 -background [$i cget -foreground] \
136 -foreground [$i cget -background]
137 $i conf -yscrollcommand \
138 [list many2scrollbar [list \
139 $w.out.loaded_t \
140 $w.out.linenumber_t \
141 $w.out.file_t \
142 ] yview $w.out.sby]
143 bind $i <Button-1> "[cb _click $i @%x,%y]; focus $i"
144 bind_button3 $i "
145 set cursorX %x
146 set cursorY %y
147 set cursorW %W
148 tk_popup $w.ctxm %X %Y
152 foreach i [list \
153 $w.out.loaded_t \
154 $w.out.linenumber_t \
155 $w.out.file_t \
156 $w.cm.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]
173 if {$commit eq {}} {
174 set fd [open $path r]
175 } else {
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
189 incr total_lines
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
198 if {[eof $fd]} {
199 close $fd
200 _status $this
201 set cmd [list git blame -M -C --incremental]
202 if {$commit eq {}} {
203 lappend cmd --contents $path
204 } else {
205 lappend cmd $commit
207 lappend cmd -- $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]} {
218 set r_commit $cmit
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
230 incr commit_count
231 lappend commit_list $cmit
233 } elseif {[string match {filename *} $line]} {
234 set file [string range $line 9 end]
235 set n $r_line_count
236 set lno $r_final_line
237 set cmit $r_commit
239 while {$n > 0} {
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
243 } else {
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} {
255 $w_file see $lno.0
256 _showcommit $this $lno
258 } elseif {$highlight_line == $lno} {
259 _showcommit $this $lno
262 incr n -1
263 incr lno
264 incr blame_lines
267 set hc $highlight_commit
268 if {$hc ne {}
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
277 if {[eof $fd]} {
278 close $fd
279 set status {Annotation complete.}
280 } else {
281 _status $this
283 } ifdeleted { catch {close $fd} }
285 method _status {} {
286 set have $blame_lines
287 set total $total_lines
288 set pdone 0
289 if {$total} {set pdone [expr {100 * $have / $total}]}
291 set status [format \
292 "Loading annotations... %i of %i lines annotated (%2i%%)" \
293 $have $total $pdone]
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 {
310 #ff4040
311 #ff40ff
312 #4040ff
315 method _showcommit {lno} {
316 global repo_config
317 variable blame_colors
319 if {$highlight_commit ne {}} {
320 set idx $order($highlight_commit)
321 set i 0
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
326 incr i
330 $w_cmit conf -state normal
331 $w_cmit delete 0.0 end
332 if {[catch {set cmit $line_commit($lno)}]} {
333 set cmit {}
334 $w_cmit insert end "Loading annotation..."
335 } else {
336 set idx $order($cmit)
337 set i 0
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
342 incr i
345 set author_name {}
346 set author_email {}
347 set author_time {}
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)}]} {
366 set msg {}
367 catch {
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)}]} {
371 set enc utf-8
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]
380 close $fd
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)]
396 $msg"
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)}]} {
408 clipboard clear
409 clipboard append \
410 -format STRING \
411 -type STRING \
412 -- $commit