git-gui: Convert blame to the "class" way of doing things
Our blame viewer code has historically been a mess simply because the data for multiple viewers was all crammed into a single pair of Tcl arrays. This made the code hard to read and even harder to maintain. Now that we have a slightly better way of tracking the data for our "meta-widgets" we can make use of it here in the blame viewer to cleanup the code and make it easier to work with long term. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
May 8, 2007 at 21:33 UTC
685caf9af672811b45ebeaaa299b8f9fbd743d82
3 files changed
+123
-133
git-gui.sh
+1
-1
@@ -1590,7 +1590,7 @@ blame {
1590
exit 1
1591
}
1592
set current_branch [lindex $argv 0]
1593
- show_blame $current_branch [lindex $argv 1]
1593
+ blame::new $current_branch [lindex $argv 1]
1594
return
1595
}
1596
citool -
lib/blame.tcl
+121
-131
@@ -1,20 +1,42 @@
1
# git-gui blame viewer
2
# Copyright (C) 2006, 2007 Shawn Pearce
3
4
-set next_browser_id 0
5
-
6
-proc show_blame {commit path} {
7
- global next_browser_id blame_status blame_data
8
-
9
- if {[winfo ismapped .]} {
10
- set w .browser[incr next_browser_id]
11
- set tl $w
12
- toplevel $w
13
- } else {
14
- set w {}
15
- set tl .
16
- }
17
- set blame_status($w) {Loading current file content...}
4
+class blame {
5
+
6
+field commit ; # input commit to blame
7
+field path ; # input filename to view in $commit
8
+
9
+field w
10
+field w_line
11
+field w_load
12
+field w_file
13
+field w_cmit
14
+field status
15
+
16
+field highlight_line -1 ; # current line selected
17
+field highlight_commit {} ; # sha1 of commit selected
18
+
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
27
+
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
32
+
33
+constructor new {i_commit i_path} {
34
+ set commit $i_commit
35
+ set path $i_path
36
+
37
+ make_toplevel top w
38
+ wm title $top "[appname] ([reponame]): File Viewer"
39
+ set status "Loading $commit:$path..."
40
41
label $w.path -text "$commit:$path" \
42
-anchor w \
@@ -70,7 +92,8 @@ proc show_blame {commit path} {
92
grid rowconfigure $w.out 0 -weight 1
93
pack $w.out -fill both -expand 1
94
73
- label $w.status -textvariable blame_status($w) \
95
+ label $w.status \
96
+ -textvariable @status \
97
-anchor w \
98
-justify left \
99
-borderwidth 1 \
@@ -95,8 +118,14 @@ proc show_blame {commit path} {
118
pack $w.cm -side bottom -fill x
119
120
menu $w.ctxm -tearoff 0
98
- $w.ctxm add command -label "Copy Commit" \
99
- -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
121
+ $w.ctxm add command \
122
+ -label "Copy Commit" \
123
+ -command [cb _copycommit]
124
+
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
129
130
foreach i [list \
131
$w.out.loaded_t \
@@ -111,14 +140,7 @@ proc show_blame {commit path} {
140
$w.out.linenumber_t \
141
$w.out.file_t \
142
] yview $w.out.sby]
114
- bind $i <Button-1> "
115
- blame_click {$w} \\
116
- $w.cm.t \\
117
- $w.out.linenumber_t \\
118
- $w.out.file_t \\
119
- $i @%x,%y
120
- focus $i
121
- "
143
+ bind $i <Button-1> "[cb _click $i @%x,%y]; focus $i"
144
bind_button3 $i "
145
set cursorX %x
146
set cursorY %y
@@ -145,157 +167,122 @@ proc show_blame {commit path} {
167
}
168
169
bind $w.cm.t <Button-1> "focus $w.cm.t"
148
- bind $tl <Visibility> "focus $tl"
149
- bind $tl <Destroy> "
150
- array unset blame_status {$w}
151
- array unset blame_data $w,*
152
- "
153
- wm title $tl "[appname] ([reponame]): File Viewer"
154
-
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
170
+ bind $top <Visibility> "focus $top"
171
+ bind $top <Destroy> [list delete_this $this]
172
173
set cmd [list git cat-file blob "$commit:$path"]
174
set fd [open "| $cmd" r]
175
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]
176
+ fileevent $fd readable [cb _read_file $fd]
177
}
178
170
-proc read_blame_catfile {fd w commit path w_cmit w_load w_line w_file} {
171
- global blame_status blame_data
172
-
173
- if {![winfo exists $w_file]} {
174
- catch {close $fd}
175
- return
176
- }
177
-
178
- set n $blame_data($w,total_lines)
179
+method _read_file {fd} {
180
$w_load conf -state normal
181
$w_line conf -state normal
182
$w_file conf -state normal
183
while {[gets $fd line] >= 0} {
184
regsub "\r\$" $line {} line
184
- incr n
185
+ incr total_lines
186
$w_load insert end "\n"
186
- $w_line insert end "$n\n" linenumber
187
+ $w_line insert end "$total_lines\n" linenumber
188
$w_file insert end "$line\n"
189
}
190
$w_load conf -state disabled
191
$w_line conf -state disabled
192
$w_file conf -state disabled
192
- set blame_data($w,total_lines) $n
193
194
if {[eof $fd]} {
195
close $fd
196
- blame_incremental_status $w
197
- set cmd [list git blame -M -C --incremental]
198
- lappend cmd $commit -- $path
196
+ _status $this
197
+ set cmd [list git blame -M -C --incremental $commit -- $path]
198
set fd [open "| $cmd" r]
199
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]
203
- }
204
-}
205
-
206
-proc read_blame_incremental {fd w w_load w_cmit w_line w_file} {
207
- global blame_status blame_data
208
-
209
- if {![winfo exists $w_file]} {
210
- catch {close $fd}
211
- return
200
+ fileevent $fd readable [cb _read_blame $fd]
201
}
202
+} ifdeleted { catch {close $fd} }
203
204
+method _read_blame {fd} {
205
while {[gets $fd line] >= 0} {
206
if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
207
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
208
+ set r_commit $cmit
209
+ set r_orig_line $original_line
210
+ set r_final_line $final_line
211
+ set r_line_count $line_count
212
222
- if {[catch {set g $blame_data($w,$cmit,order)}]} {
213
+ if {[catch {set g $order($cmit)}]} {
214
$w_line tag conf g$cmit
215
$w_file tag conf g$cmit
216
$w_line tag raise in_sel
217
$w_file tag raise in_sel
218
$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
219
+ set order($cmit) $commit_count
220
+ incr commit_count
221
+ lappend commit_list $cmit
222
}
223
} elseif {[string match {filename *} $line]} {
224
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)
225
+ set n $r_line_count
226
+ set lno $r_final_line
227
+ set cmit $r_commit
228
229
while {$n > 0} {
239
- if {[catch {set g g$blame_data($w,line$lno,commit)}]} {
230
+ if {[catch {set g g$line_commit($lno)}]} {
231
$w_load tag add annotated $lno.0 "$lno.0 lineend + 1c"
232
} else {
233
$w_line tag remove g$g $lno.0 "$lno.0 lineend + 1c"
234
$w_file tag remove g$g $lno.0 "$lno.0 lineend + 1c"
235
}
236
246
- set blame_data($w,line$lno,commit) $cmit
247
- set blame_data($w,line$lno,file) $file
237
+ set line_commit($lno) $cmit
238
+ set line_file($lno) $file
239
$w_line tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
240
$w_file tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
241
251
- if {$blame_data($w,highlight_line) == -1} {
242
+ if {$highlight_line == -1} {
243
if {[lindex [$w_file yview] 0] == 0} {
244
$w_file see $lno.0
254
- blame_showcommit $w $w_cmit $w_line $w_file $lno
245
+ _showcommit $this $lno
246
}
256
- } elseif {$blame_data($w,highlight_line) == $lno} {
257
- blame_showcommit $w $w_cmit $w_line $w_file $lno
247
+ } elseif {$highlight_line == $lno} {
248
+ _showcommit $this $lno
249
}
250
251
incr n -1
252
incr lno
262
- incr blame_data($w,blame_lines)
253
+ incr blame_lines
254
}
255
265
- set hc $blame_data($w,highlight_commit)
256
+ set hc $highlight_commit
257
if {$hc ne {}
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)
258
+ && [expr {$order($hc) + 1}] == $order($cmit)} {
259
+ _showcommit $this $highlight_line
260
}
272
- } elseif {[regexp {^([a-z-]+) (.*)$} $line line header data]} {
273
- set blame_data($w,$blame_data($w,commit),$header) $data
261
+ } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
262
+ set header($r_commit,$key) $data
263
}
264
}
265
266
if {[eof $fd]} {
267
close $fd
279
- set blame_status($w) {Annotation complete.}
268
+ set status {Annotation complete.}
269
} else {
281
- blame_incremental_status $w
270
+ _status $this
271
}
283
-}
284
-
285
-proc blame_incremental_status {w} {
286
- global blame_status blame_data
272
+} ifdeleted { catch {close $fd} }
273
288
- set have $blame_data($w,blame_lines)
289
- set total $blame_data($w,total_lines)
274
+method _status {} {
275
+ set have $blame_lines
276
+ set total $total_lines
277
set pdone 0
278
if {$total} {set pdone [expr {100 * $have / $total}]}
279
293
- set blame_status($w) [format \
280
+ set status [format \
281
"Loading annotations... %i of %i lines annotated (%2i%%)" \
282
$have $total $pdone]
283
}
284
298
-proc blame_click {w w_cmit w_line w_file cur_w pos} {
285
+method _click {cur_w pos} {
286
set lno [lindex [split [$cur_w index $pos] .] 0]
287
if {$lno eq {}} return
288
@@ -304,24 +291,24 @@ proc blame_click {w w_cmit w_line w_file cur_w pos} {
291
$w_line tag add in_sel $lno.0 "$lno.0 + 1 line"
292
$w_file tag add in_sel $lno.0 "$lno.0 + 1 line"
293
307
- blame_showcommit $w $w_cmit $w_line $w_file $lno
294
+ _showcommit $this $lno
295
}
296
310
-set blame_colors {
297
+variable blame_colors {
298
#ff4040
299
#ff40ff
300
#4040ff
301
}
302
316
-proc blame_showcommit {w w_cmit w_line w_file lno} {
317
- global blame_colors blame_data repo_config
303
+method _showcommit {lno} {
304
+ global repo_config
305
+ variable blame_colors
306
319
- set cmit $blame_data($w,highlight_commit)
320
- if {$cmit ne {}} {
321
- set idx $blame_data($w,$cmit,order)
307
+ if {$highlight_commit ne {}} {
308
+ set idx $order($highlight_commit)
309
set i 0
310
foreach c $blame_colors {
324
- set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
311
+ set h [lindex $commit_list [expr {$idx - 1 + $i}]]
312
$w_line tag conf g$h -background white
313
$w_file tag conf g$h -background white
314
incr i
@@ -330,14 +317,15 @@ proc blame_showcommit {w w_cmit w_line w_file lno} {
317
318
$w_cmit conf -state normal
319
$w_cmit delete 0.0 end
333
- if {[catch {set cmit $blame_data($w,line$lno,commit)}]} {
320
+ if {[catch {set cmit $line_commit($lno)} myerr]} {
321
+ puts "myerr = $myerr"
322
set cmit {}
323
$w_cmit insert end "Loading annotation..."
324
} else {
337
- set idx $blame_data($w,$cmit,order)
325
+ set idx $order($cmit)
326
set i 0
327
foreach c $blame_colors {
340
- set h [lindex $blame_data($w,commit_list) [expr {$idx - 1 + $i}]]
328
+ set h [lindex $commit_list [expr {$idx - 1 + $i}]]
329
$w_line tag conf g$h -background $c
330
$w_file tag conf g$h -background $c
331
incr i
@@ -346,18 +334,18 @@ proc blame_showcommit {w w_cmit w_line w_file lno} {
334
set author_name {}
335
set author_email {}
336
set author_time {}
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)]}
337
+ catch {set author_name $header($cmit,author)}
338
+ catch {set author_email $header($cmit,author-mail)}
339
+ catch {set author_time [clock format $header($cmit,author-time)]}
340
341
set committer_name {}
342
set committer_email {}
343
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)]}
344
+ catch {set committer_name $header($cmit,committer)}
345
+ catch {set committer_email $header($cmit,committer-mail)}
346
+ catch {set committer_time [clock format $header($cmit,committer-time)]}
347
360
- if {[catch {set msg $blame_data($w,$cmit,message)}]} {
348
+ if {[catch {set msg $header($cmit,message)}]} {
349
set msg {}
350
catch {
351
set fd [open "| git cat-file commit $cmit" r]
@@ -377,29 +365,29 @@ proc blame_showcommit {w w_cmit w_line w_file lno} {
365
set author_name [encoding convertfrom $enc $author_name]
366
set committer_name [encoding convertfrom $enc $committer_name]
367
380
- set blame_data($w,$cmit,author) $author_name
381
- set blame_data($w,$cmit,committer) $committer_name
368
+ set header($cmit,author) $author_name
369
+ set header($cmit,committer) $committer_name
370
}
383
- set blame_data($w,$cmit,message) $msg
371
+ set header($cmit,message) $msg
372
}
373
374
$w_cmit insert end "commit $cmit\n"
375
$w_cmit insert end "Author: $author_name $author_email $author_time\n"
376
$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"
377
+ $w_cmit insert end "Original File: [escape_path $line_file($lno)]\n"
378
$w_cmit insert end "\n"
379
$w_cmit insert end $msg
380
}
381
$w_cmit conf -state disabled
382
395
- set blame_data($w,highlight_line) $lno
396
- set blame_data($w,highlight_commit) $cmit
383
+ set highlight_line $lno
384
+ set highlight_commit $cmit
385
}
386
399
-proc blame_copycommit {w i pos} {
400
- global blame_data
401
- set lno [lindex [split [$i index $pos] .] 0]
402
- if {![catch {set commit $blame_data($w,line$lno,commit)}]} {
387
+method _copycommit {} {
388
+ set pos @$::cursorX,$::cursorY
389
+ set lno [lindex [split [$::cursorW index $pos] .] 0]
390
+ if {![catch {set commit $line_commit($lno)}]} {
391
clipboard clear
392
clipboard append \
393
-format STRING \
@@ -407,3 +395,5 @@ proc blame_copycommit {w i pos} {
395
-- $commit
396
}
397
}
398
+
399
+}
lib/browser.tcl
+1
-1
@@ -140,7 +140,7 @@ method _enter {} {
140
append p [lindex $n 1]
141
}
142
append p $name
143
- show_blame $browser_commit $p
143
+ blame::new $browser_commit $p
144
}
145
}
146
}