git-gui: Support starting gitk from Gui Blame
[git/git.git] / lib / blame.tcl
CommitLineData
f522c9b5
SP
1# git-gui blame viewer
2# Copyright (C) 2006, 2007 Shawn Pearce
3
685caf9a
SP
4class blame {
5
22c6769d
SP
6image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
7
c17c1751
SP
8# Persistant data (survives loads)
9#
22c6769d 10field history {}; # viewer history: {commit path}
c17c1751 11field header ; # array commit,key -> header field
22c6769d 12
c17c1751
SP
13# Tk UI control paths
14#
22c6769d
SP
15field w ; # top window in this viewer
16field w_back ; # our back button
17field w_path ; # label showing the current file path
14c4dfd3 18field w_columns ; # list of all column widgets in the viewer
22c6769d 19field w_line ; # text column: all line numbers
fc816d7b 20field w_amov ; # text column: annotations + move tracking
172c92b4 21field w_asim ; # text column: annotations (simple computation)
22c6769d 22field w_file ; # text column: actual file data
fc816d7b 23field w_cviewer ; # pane showing commit message
b29bd5ca 24field status ; # status mega-widget instance
22c6769d
SP
25field old_height ; # last known height of $w.file_pane
26
c17c1751
SP
27# Tk UI colors
28#
5d198d67
SP
29variable active_color #c0edc5
30variable group_colors {
c17c1751
SP
31 #d6d6d6
32 #e1e1e1
33 #ececec
34}
35
36# Current blame data; cleared/reset on each load
37#
38field commit ; # input commit to blame
39field path ; # input filename to view in $commit
40
41field current_fd {} ; # background process running
42field highlight_line -1 ; # current line selected
172c92b4 43field highlight_column {} ; # current commit column selected
c17c1751 44field highlight_commit {} ; # sha1 of commit selected
685caf9a
SP
45
46field total_lines 0 ; # total length of file
47field blame_lines 0 ; # number of lines computed
fc816d7b 48field amov_data ; # list of {commit origfile origline}
172c92b4 49field asim_data ; # list of {commit origfile origline}
685caf9a 50
c17c1751
SP
51field r_commit ; # commit currently being parsed
52field r_orig_line ; # original line number
53field r_final_line ; # final line number
54field r_line_count ; # lines in this region
685caf9a 55
c17c1751 56field tooltip_wm {} ; # Current tooltip toplevel, if open
383d4e0f 57field tooltip_t {} ; # Text widget in $tooltip_wm
c17c1751 58field tooltip_timer {} ; # Current timer event for our tooltip
383d4e0f 59field tooltip_commit {} ; # Commit(s) in tooltip
f96cd7b6 60
685caf9a 61constructor new {i_commit i_path} {
37ebc93f 62 global cursor_ptr
5d198d67
SP
63 variable active_color
64 variable group_colors
37ebc93f 65
685caf9a
SP
66 set commit $i_commit
67 set path $i_path
68
69 make_toplevel top w
1ac17950 70 wm title $top [append "[appname] ([reponame]): " [mc "File Viewer"]]
f522c9b5 71
d80ded01 72 frame $w.header -background gold
22c6769d 73 label $w.header.commit_l \
1ac17950 74 -text [mc "Commit:"] \
d80ded01 75 -background gold \
c382fdd7 76 -foreground black \
f522c9b5 77 -anchor w \
22c6769d
SP
78 -justify left
79 set w_back $w.header.commit_b
08dda17e 80 label $w_back \
22c6769d
SP
81 -image ::blame::img_back_arrow \
82 -borderwidth 0 \
83 -relief flat \
84 -state disabled \
d80ded01 85 -background gold \
c382fdd7 86 -foreground black \
d80ded01 87 -activebackground gold
08dda17e
SP
88 bind $w_back <Button-1> "
89 if {\[$w_back cget -state\] eq {normal}} {
90 [cb _history_menu]
91 }
92 "
22c6769d
SP
93 label $w.header.commit \
94 -textvariable @commit \
d80ded01 95 -background gold \
c382fdd7 96 -foreground black \
22c6769d
SP
97 -anchor w \
98 -justify left
99 label $w.header.path_l \
1ac17950 100 -text [mc "File:"] \
d80ded01 101 -background gold \
c382fdd7 102 -foreground black \
22c6769d
SP
103 -anchor w \
104 -justify left
105 set w_path $w.header.path
106 label $w_path \
d80ded01 107 -background gold \
c382fdd7 108 -foreground black \
22c6769d
SP
109 -anchor w \
110 -justify left
111 pack $w.header.commit_l -side left
112 pack $w_back -side left
113 pack $w.header.commit -side left
114 pack $w_path -fill x -side right
115 pack $w.header.path_l -side right
f522c9b5 116
d0b741dc
SP
117 panedwindow $w.file_pane -orient vertical
118 frame $w.file_pane.out
119 frame $w.file_pane.cm
120 $w.file_pane add $w.file_pane.out \
121 -sticky nsew \
122 -minsize 100 \
123 -height 100 \
124 -width 100
125 $w.file_pane add $w.file_pane.cm \
126 -sticky nsew \
127 -minsize 25 \
128 -height 25 \
129 -width 100
130
d0b741dc 131 set w_line $w.file_pane.out.linenumber_t
d89a494f 132 text $w_line \
06325707
SP
133 -takefocus 0 \
134 -highlightthickness 0 \
135 -padx 0 -pady 0 \
c382fdd7
PH
136 -background white \
137 -foreground black \
138 -borderwidth 0 \
f522c9b5
SP
139 -state disabled \
140 -wrap none \
141 -height 40 \
375e1365 142 -width 6 \
f522c9b5 143 -font font_diff
06325707 144 $w_line tag conf linenumber -justify right -rmargin 5
f522c9b5 145
fc816d7b
SP
146 set w_amov $w.file_pane.out.amove_t
147 text $w_amov \
06325707
SP
148 -takefocus 0 \
149 -highlightthickness 0 \
150 -padx 0 -pady 0 \
c382fdd7
PH
151 -background white \
152 -foreground black \
153 -borderwidth 0 \
a46fe1c1
SP
154 -state disabled \
155 -wrap none \
156 -height 40 \
172c92b4 157 -width 5 \
a46fe1c1 158 -font font_diff
172c92b4 159 $w_amov tag conf author_abbr -justify right -rmargin 5
fc816d7b 160 $w_amov tag conf curr_commit
172c92b4 161 $w_amov tag conf prior_commit -foreground blue -underline 1
fc816d7b 162 $w_amov tag bind prior_commit \
22c6769d 163 <Button-1> \
172c92b4
SP
164 "[cb _load_commit $w_amov @amov_data @%x,%y];break"
165
166 set w_asim $w.file_pane.out.asimple_t
167 text $w_asim \
168 -takefocus 0 \
169 -highlightthickness 0 \
170 -padx 0 -pady 0 \
c382fdd7
PH
171 -background white \
172 -foreground black \
173 -borderwidth 0 \
172c92b4
SP
174 -state disabled \
175 -wrap none \
176 -height 40 \
177 -width 4 \
178 -font font_diff
179 $w_asim tag conf author_abbr -justify right
180 $w_asim tag conf curr_commit
181 $w_asim tag conf prior_commit -foreground blue -underline 1
182 $w_asim tag bind prior_commit \
183 <Button-1> \
184 "[cb _load_commit $w_asim @asim_data @%x,%y];break"
a46fe1c1 185
d0b741dc 186 set w_file $w.file_pane.out.file_t
d89a494f 187 text $w_file \
06325707
SP
188 -takefocus 0 \
189 -highlightthickness 0 \
190 -padx 0 -pady 0 \
c382fdd7
PH
191 -background white \
192 -foreground black \
193 -borderwidth 0 \
f522c9b5
SP
194 -state disabled \
195 -wrap none \
196 -height 40 \
197 -width 80 \
d0b741dc 198 -xscrollcommand [list $w.file_pane.out.sbx set] \
f522c9b5
SP
199 -font font_diff
200
172c92b4 201 set w_columns [list $w_amov $w_asim $w_line $w_file]
14c4dfd3 202
d0b741dc
SP
203 scrollbar $w.file_pane.out.sbx \
204 -orient h \
205 -command [list $w_file xview]
206 scrollbar $w.file_pane.out.sby \
207 -orient v \
14c4dfd3
SP
208 -command [list scrollbar2many $w_columns yview]
209 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
210 grid conf \
211 $w.file_pane.out.sbx \
212 -column [expr {[llength $w_columns] - 1}] \
213 -sticky we
214 grid columnconfigure \
215 $w.file_pane.out \
216 [expr {[llength $w_columns] - 1}] \
217 -weight 1
d0b741dc 218 grid rowconfigure $w.file_pane.out 0 -weight 1
f522c9b5 219
fc816d7b
SP
220 set w_cviewer $w.file_pane.cm.t
221 text $w_cviewer \
c382fdd7
PH
222 -background white \
223 -foreground black \
224 -borderwidth 0 \
f522c9b5
SP
225 -state disabled \
226 -wrap none \
227 -height 10 \
228 -width 80 \
d0b741dc
SP
229 -xscrollcommand [list $w.file_pane.cm.sbx set] \
230 -yscrollcommand [list $w.file_pane.cm.sby set] \
f522c9b5 231 -font font_diff
debcd0fd
SP
232 $w_cviewer tag conf still_loading \
233 -font font_uiitalic \
234 -justify center
fc816d7b 235 $w_cviewer tag conf header_key \
74fe8985
SP
236 -tabs {3c} \
237 -background $active_color \
238 -font font_uibold
fc816d7b 239 $w_cviewer tag conf header_val \
74fe8985
SP
240 -background $active_color \
241 -font font_ui
fc816d7b 242 $w_cviewer tag raise sel
d0b741dc
SP
243 scrollbar $w.file_pane.cm.sbx \
244 -orient h \
fc816d7b 245 -command [list $w_cviewer xview]
d0b741dc
SP
246 scrollbar $w.file_pane.cm.sby \
247 -orient v \
fc816d7b 248 -command [list $w_cviewer yview]
d0b741dc
SP
249 pack $w.file_pane.cm.sby -side right -fill y
250 pack $w.file_pane.cm.sbx -side bottom -fill x
fc816d7b 251 pack $w_cviewer -expand 1 -fill both
f522c9b5 252
b29bd5ca 253 set status [::status_bar::new $w.status]
982cf98f 254
f522c9b5 255 menu $w.ctxm -tearoff 0
685caf9a 256 $w.ctxm add command \
1ac17950 257 -label [mc "Copy Commit"] \
685caf9a 258 -command [cb _copycommit]
a01fe996
AG
259 $w.ctxm add command \
260 -label [mc "Do Full Copy Detection"] \
261 -command [cb _fullcopyblame]
a9c80b83
AG
262 $w.ctxm add command \
263 -label [mc "Show History Context"] \
264 -command [cb _gitkcommit]
685caf9a 265
14c4dfd3 266 foreach i $w_columns {
5d198d67
SP
267 for {set g 0} {$g < [llength $group_colors]} {incr g} {
268 $i tag conf color$g -background [lindex $group_colors $g]
269 }
270
37ebc93f 271 $i conf -cursor $cursor_ptr
14c4dfd3
SP
272 $i conf -yscrollcommand [list many2scrollbar \
273 $w_columns yview $w.file_pane.out.sby]
b5a41224 274 bind $i <Button-1> "
41bf23d6
SP
275 [cb _hide_tooltip]
276 [cb _click $i @%x,%y]
277 focus $i
278 "
279 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
280 bind $i <Any-Enter> [cb _hide_tooltip]
281 bind $i <Any-Leave> [cb _hide_tooltip]
f522c9b5 282 bind_button3 $i "
41bf23d6 283 [cb _hide_tooltip]
f522c9b5
SP
284 set cursorX %x
285 set cursorY %y
286 set cursorW %W
287 tk_popup $w.ctxm %X %Y
288 "
fb626dc0
SP
289 bind $i <Shift-Tab> "[list focus $w_cviewer];break"
290 bind $i <Tab> "[list focus $w_cviewer];break"
f522c9b5
SP
291 }
292
fc816d7b 293 foreach i [concat $w_columns $w_cviewer] {
60aa065f
SP
294 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
295 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
296 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
297 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
298 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
299 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
300 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
301 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
302 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
303 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
304 }
305
fb626dc0
SP
306 bind $w_cviewer <Shift-Tab> "[list focus $w_file];break"
307 bind $w_cviewer <Tab> "[list focus $w_file];break"
fc816d7b 308 bind $w_cviewer <Button-1> [list focus $w_cviewer]
fb626dc0 309 bind $w_file <Visibility> [list focus $w_file]
f522c9b5 310
22c6769d 311 grid configure $w.header -sticky ew
d0b741dc
SP
312 grid configure $w.file_pane -sticky nsew
313 grid configure $w.status -sticky ew
314 grid columnconfigure $top 0 -weight 1
315 grid rowconfigure $top 0 -weight 0
316 grid rowconfigure $top 1 -weight 1
317 grid rowconfigure $top 2 -weight 0
318
319 set req_w [winfo reqwidth $top]
320 set req_h [winfo reqheight $top]
c1fd897a 321 set scr_h [expr {[winfo screenheight $top] - 100}]
d0b741dc 322 if {$req_w < 600} {set req_w 600}
c1fd897a 323 if {$req_h < $scr_h} {set req_h $scr_h}
d0b741dc
SP
324 set g "${req_w}x${req_h}"
325 wm geometry $top $g
326 update
327
328 set old_height [winfo height $w.file_pane]
329 $w.file_pane sash place 0 \
330 [lindex [$w.file_pane sash coord 0] 0] \
331 [expr {int($old_height * 0.70)}]
332 bind $w.file_pane <Configure> \
333 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
334
e6131d30
AG
335 wm protocol $top WM_DELETE_WINDOW "destroy $top"
336 bind $top <Destroy> [cb _kill]
337
0dfed77b 338 _load $this {}
22c6769d
SP
339}
340
e6131d30
AG
341method _kill {} {
342 if {$current_fd ne {}} {
343 kill_file_process $current_fd
344 catch {close $current_fd}
345 set current_fd {}
346 }
347}
348
0dfed77b 349method _load {jump} {
5d198d67
SP
350 variable group_colors
351
22c6769d
SP
352 _hide_tooltip $this
353
354 if {$total_lines != 0 || $current_fd ne {}} {
e6131d30 355 _kill $this
22c6769d 356
14c4dfd3
SP
357 foreach i $w_columns {
358 $i conf -state normal
359 $i delete 0.0 end
5d198d67
SP
360 foreach g [$i tag names] {
361 if {[regexp {^g[0-9a-f]{40}$} $g]} {
362 $i tag delete $g
363 }
14c4dfd3
SP
364 }
365 $i conf -state disabled
366 }
367
0dfed77b
SP
368 $w_cviewer conf -state normal
369 $w_cviewer delete 0.0 end
370 $w_cviewer conf -state disabled
371
22c6769d 372 set highlight_line -1
172c92b4 373 set highlight_column {}
22c6769d
SP
374 set highlight_commit {}
375 set total_lines 0
22c6769d
SP
376 }
377
22c6769d
SP
378 if {$history eq {}} {
379 $w_back conf -state disabled
380 } else {
381 $w_back conf -state normal
382 }
22c6769d 383
2f85b7e4
SP
384 # Index 0 is always empty. There is never line 0 as
385 # we use only 1 based lines, as that matches both with
386 # git-blame output and with Tk's text widget.
387 #
fc816d7b 388 set amov_data [list [list]]
172c92b4 389 set asim_data [list [list]]
2f85b7e4 390
1ac17950 391 $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
22c6769d 392 $w_path conf -text [escape_path $path]
a0db0d61
SP
393 if {$commit eq {}} {
394 set fd [open $path r]
6eb420ef 395 fconfigure $fd -eofchar {}
a0db0d61 396 } else {
0b812616 397 set fd [git_read cat-file blob "$commit:$path"]
a0db0d61 398 }
f522c9b5 399 fconfigure $fd -blocking 0 -translation lf -encoding binary
0dfed77b 400 fileevent $fd readable [cb _read_file $fd $jump]
22c6769d
SP
401 set current_fd $fd
402}
403
404method _history_menu {} {
405 set m $w.backmenu
406 if {[winfo exists $m]} {
407 $m delete 0 end
408 } else {
409 menu $m -tearoff 0
410 }
411
0dfed77b 412 for {set i [expr {[llength $history] - 1}]
22c6769d
SP
413 } {$i >= 0} {incr i -1} {
414 set e [lindex $history $i]
415 set c [lindex $e 0]
416 set f [lindex $e 1]
417
418 if {[regexp {^[0-9a-f]{40}$} $c]} {
419 set t [string range $c 0 8]...
c5db65ae
SP
420 } elseif {$c eq {}} {
421 set t {Working Directory}
22c6769d
SP
422 } else {
423 set t $c
424 }
425 if {![catch {set summary $header($c,summary)}]} {
426 append t " $summary"
b55a243d
SP
427 if {[string length $t] > 70} {
428 set t [string range $t 0 66]...
429 }
22c6769d
SP
430 }
431
0dfed77b 432 $m add command -label $t -command [cb _goback $i]
22c6769d
SP
433 }
434 set X [winfo rootx $w_back]
435 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
436 tk_popup $m $X $Y
437}
438
0dfed77b
SP
439method _goback {i} {
440 set dat [lindex $history $i]
22c6769d 441 set history [lrange $history 0 [expr {$i - 1}]]
0dfed77b
SP
442 set commit [lindex $dat 0]
443 set path [lindex $dat 1]
444 _load $this [lrange $dat 2 5]
f522c9b5
SP
445}
446
0dfed77b 447method _read_file {fd jump} {
22c6769d
SP
448 if {$fd ne $current_fd} {
449 catch {close $fd}
450 return
451 }
452
14c4dfd3 453 foreach i $w_columns {$i conf -state normal}
f522c9b5
SP
454 while {[gets $fd line] >= 0} {
455 regsub "\r\$" $line {} line
685caf9a 456 incr total_lines
fc816d7b 457 lappend amov_data {}
172c92b4 458 lappend asim_data {}
a46fe1c1 459
bea39c2d 460 if {$total_lines > 1} {
14c4dfd3 461 foreach i $w_columns {$i insert end "\n"}
bea39c2d
SP
462 }
463
464 $w_line insert end "$total_lines" linenumber
465 $w_file insert end "$line"
f522c9b5 466 }
81fb7efe
SP
467
468 set ln_wc [expr {[string length $total_lines] + 2}]
469 if {[$w_line cget -width] < $ln_wc} {
470 $w_line conf -width $ln_wc
471 }
472
14c4dfd3 473 foreach i $w_columns {$i conf -state disabled}
f522c9b5
SP
474
475 if {[eof $fd]} {
476 close $fd
0dfed77b
SP
477
478 # If we don't force Tk to update the widgets *right now*
479 # none of our jump commands will cause a change in the UI.
480 #
481 update
482
483 if {[llength $jump] == 1} {
484 set highlight_line [lindex $jump 0]
485 $w_file see "$highlight_line.0"
486 } elseif {[llength $jump] == 4} {
487 set highlight_column [lindex $jump 0]
488 set highlight_line [lindex $jump 1]
489 $w_file xview moveto [lindex $jump 2]
490 $w_file yview moveto [lindex $jump 3]
491 }
492
949da61b
SP
493 _exec_blame $this $w_asim @asim_data \
494 [list] \
c8c4854b 495 [mc "Loading copy/move tracking annotations..."]
f522c9b5 496 }
685caf9a 497} ifdeleted { catch {close $fd} }
f522c9b5 498
172c92b4 499method _exec_blame {cur_w cur_d options cur_s} {
0b812616 500 lappend options --incremental
172c92b4 501 if {$commit eq {}} {
0b812616 502 lappend options --contents $path
172c92b4 503 } else {
0b812616 504 lappend options $commit
172c92b4 505 }
0b812616
SP
506 lappend options -- $path
507 set fd [eval git_read --nice blame $options]
172c92b4 508 fconfigure $fd -blocking 0 -translation lf -encoding binary
b29bd5ca 509 fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
172c92b4
SP
510 set current_fd $fd
511 set blame_lines 0
b29bd5ca
SP
512
513 $status start \
c8c4854b
MB
514 $cur_s \
515 [mc "lines annotated"]
172c92b4
SP
516}
517
b29bd5ca 518method _read_blame {fd cur_w cur_d} {
172c92b4 519 upvar #0 $cur_d line_data
5d198d67 520 variable group_colors
172c92b4 521
22c6769d
SP
522 if {$fd ne $current_fd} {
523 catch {close $fd}
524 return
525 }
526
172c92b4 527 $cur_w conf -state normal
f522c9b5
SP
528 while {[gets $fd line] >= 0} {
529 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
530 cmit original_line final_line line_count]} {
685caf9a
SP
531 set r_commit $cmit
532 set r_orig_line $original_line
533 set r_final_line $final_line
534 set r_line_count $line_count
f522c9b5
SP
535 } elseif {[string match {filename *} $line]} {
536 set file [string range $line 9 end]
685caf9a
SP
537 set n $r_line_count
538 set lno $r_final_line
0dfed77b 539 set oln $r_orig_line
685caf9a 540 set cmit $r_commit
c9e6bfd8
SP
541
542 if {[regexp {^0{40}$} $cmit]} {
223475a7 543 set commit_abbr work
22c6769d
SP
544 set commit_type curr_commit
545 } elseif {$cmit eq $commit} {
546 set commit_abbr this
547 set commit_type curr_commit
c9e6bfd8 548 } else {
22c6769d 549 set commit_type prior_commit
172c92b4 550 set commit_abbr [string range $cmit 0 3]
c9e6bfd8
SP
551 }
552
223475a7
SP
553 set author_abbr {}
554 set a_name {}
555 catch {set a_name $header($cmit,author)}
556 while {$a_name ne {}} {
88dce86f
SP
557 if {$author_abbr ne {}
558 && [string index $a_name 0] eq {'}} {
559 regsub {^'[^']+'\s+} $a_name {} a_name
560 }
223475a7
SP
561 if {![regexp {^([[:upper:]])} $a_name _a]} break
562 append author_abbr $_a
563 unset _a
564 if {![regsub \
565 {^[[:upper:]][^\s]*\s+} \
566 $a_name {} a_name ]} break
567 }
568 if {$author_abbr eq {}} {
569 set author_abbr { |}
570 } else {
571 set author_abbr [string range $author_abbr 0 3]
c9e6bfd8 572 }
223475a7
SP
573 unset a_name
574
575 set first_lno $lno
576 while {
2f85b7e4 577 $first_lno > 1
172c92b4
SP
578 && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
579 && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
223475a7
SP
580 } {
581 incr first_lno -1
582 }
f522c9b5 583
5d198d67
SP
584 set color {}
585 if {$first_lno < $lno} {
586 foreach g [$w_file tag names $first_lno.0] {
587 if {[regexp {^color[0-9]+$} $g]} {
588 set color $g
589 break
590 }
591 }
592 } else {
593 set i [lsort [concat \
594 [$w_file tag names "[expr {$first_lno - 1}].0"] \
595 [$w_file tag names "[expr {$lno + $n}].0"] \
596 ]]
597 for {set g 0} {$g < [llength $group_colors]} {incr g} {
598 if {[lsearch -sorted -exact $i color$g] == -1} {
599 set color color$g
600 break
601 }
602 }
603 }
604 if {$color eq {}} {
605 set color color0
606 }
607
f522c9b5 608 while {$n > 0} {
0511798f 609 set lno_e "$lno.0 lineend + 1c"
172c92b4
SP
610 if {[lindex $line_data $lno] ne {}} {
611 set g [lindex $line_data $lno 0]
14c4dfd3
SP
612 foreach i $w_columns {
613 $i tag remove g$g $lno.0 $lno_e
614 }
f522c9b5 615 }
0dfed77b 616 lset line_data $lno [list $cmit $file $oln]
a46fe1c1 617
172c92b4 618 $cur_w delete $lno.0 "$lno.0 lineend"
223475a7 619 if {$lno == $first_lno} {
172c92b4 620 $cur_w insert $lno.0 $commit_abbr $commit_type
223475a7 621 } elseif {$lno == [expr {$first_lno + 1}]} {
172c92b4 622 $cur_w insert $lno.0 $author_abbr author_abbr
223475a7 623 } else {
172c92b4 624 $cur_w insert $lno.0 { |}
223475a7 625 }
a46fe1c1 626
14c4dfd3 627 foreach i $w_columns {
5d198d67
SP
628 if {$cur_w eq $w_amov} {
629 for {set g 0} \
630 {$g < [llength $group_colors]} \
631 {incr g} {
632 $i tag remove color$g $lno.0 $lno_e
633 }
634 $i tag add $color $lno.0 $lno_e
635 }
14c4dfd3
SP
636 $i tag add g$cmit $lno.0 $lno_e
637 }
f96cd7b6 638
172c92b4
SP
639 if {$highlight_column eq $cur_w} {
640 if {$highlight_line == -1
641 && [lindex [$w_file yview] 0] == 0} {
f522c9b5 642 $w_file see $lno.0
172c92b4
SP
643 set highlight_line $lno
644 }
645 if {$highlight_line == $lno} {
646 _showcommit $this $cur_w $lno
f522c9b5 647 }
f522c9b5
SP
648 }
649
650 incr n -1
651 incr lno
0dfed77b 652 incr oln
685caf9a 653 incr blame_lines
f522c9b5
SP
654 }
655
669fbc3d 656 while {
172c92b4
SP
657 $cmit eq [lindex $line_data $lno 0]
658 && $file eq [lindex $line_data $lno 1]
669fbc3d 659 } {
172c92b4 660 $cur_w delete $lno.0 "$lno.0 lineend"
223475a7
SP
661
662 if {$lno == $first_lno} {
172c92b4 663 $cur_w insert $lno.0 $commit_abbr $commit_type
223475a7 664 } elseif {$lno == [expr {$first_lno + 1}]} {
172c92b4 665 $cur_w insert $lno.0 $author_abbr author_abbr
223475a7 666 } else {
172c92b4 667 $cur_w insert $lno.0 { |}
c9e6bfd8 668 }
5d198d67
SP
669
670 if {$cur_w eq $w_amov} {
671 foreach i $w_columns {
672 for {set g 0} \
673 {$g < [llength $group_colors]} \
674 {incr g} {
675 $i tag remove color$g $lno.0 $lno_e
676 }
677 $i tag add $color $lno.0 $lno_e
678 }
679 }
680
223475a7 681 incr lno
c9e6bfd8 682 }
223475a7 683
685caf9a
SP
684 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
685 set header($r_commit,$key) $data
f522c9b5
SP
686 }
687 }
172c92b4 688 $cur_w conf -state disabled
f522c9b5
SP
689
690 if {[eof $fd]} {
691 close $fd
172c92b4 692 if {$cur_w eq $w_asim} {
57cae87b
AG
693 # Switches for original location detection
694 set threshold [get_config gui.copyblamethreshold]
695 set original_options [list "-C$threshold"]
696
697 if {![is_config_true gui.fastcopyblame]} {
698 # thorough copy search; insert before the threshold
699 set original_options [linsert $original_options 0 -C]
700 }
701 if {[git-version >= 1.5.3]} {
702 lappend original_options -w ; # ignore indentation changes
703 }
704
172c92b4 705 _exec_blame $this $w_amov @amov_data \
a8405667 706 $original_options \
c8c4854b 707 [mc "Loading original location annotations..."]
172c92b4
SP
708 } else {
709 set current_fd {}
c8c4854b 710 $status stop [mc "Annotation complete."]
172c92b4 711 }
f522c9b5 712 } else {
b29bd5ca 713 $status update $blame_lines $total_lines
f522c9b5 714 }
685caf9a 715} ifdeleted { catch {close $fd} }
f522c9b5 716
a01fe996
AG
717method _find_commit_bound {data_list start_idx delta} {
718 upvar #0 $data_list line_data
719 set pos $start_idx
720 set limit [expr {[llength $line_data] - 1}]
721 set base_commit [lindex $line_data $pos 0]
722
723 while {$pos > 0 && $pos < $limit} {
724 set new_pos [expr {$pos + $delta}]
725 if {[lindex $line_data $new_pos 0] ne $base_commit} {
726 return $pos
727 }
728
729 set pos $new_pos
730 }
731
732 return $pos
733}
734
735method _fullcopyblame {} {
736 if {$current_fd ne {}} {
737 tk_messageBox \
738 -icon error \
739 -type ok \
740 -title [mc "Busy"] \
741 -message [mc "Annotation process is already running."]
742
743 return
744 }
745
746 # Switches for original location detection
747 set threshold [get_config gui.copyblamethreshold]
748 set original_options [list -C -C "-C$threshold"]
749
750 if {[git-version >= 1.5.3]} {
751 lappend original_options -w ; # ignore indentation changes
752 }
753
754 # Find the line range
755 set pos @$::cursorX,$::cursorY
756 set lno [lindex [split [$::cursorW index $pos] .] 0]
757 set min_amov_lno [_find_commit_bound $this @amov_data $lno -1]
758 set max_amov_lno [_find_commit_bound $this @amov_data $lno 1]
759 set min_asim_lno [_find_commit_bound $this @asim_data $lno -1]
760 set max_asim_lno [_find_commit_bound $this @asim_data $lno 1]
761
762 if {$min_asim_lno < $min_amov_lno} {
763 set min_amov_lno $min_asim_lno
764 }
765
766 if {$max_asim_lno > $max_amov_lno} {
767 set max_amov_lno $max_asim_lno
768 }
769
770 lappend original_options -L "$min_amov_lno,$max_amov_lno"
771
772 # Clear lines
773 for {set i $min_amov_lno} {$i <= $max_amov_lno} {incr i} {
774 lset amov_data $i [list ]
775 }
776
777 # Start the back-end process
778 _exec_blame $this $w_amov @amov_data \
779 $original_options \
780 [mc "Running thorough copy detection..."]
781}
782
685caf9a 783method _click {cur_w pos} {
f522c9b5 784 set lno [lindex [split [$cur_w index $pos] .] 0]
172c92b4 785 _showcommit $this $cur_w $lno
f522c9b5
SP
786}
787
172c92b4
SP
788method _load_commit {cur_w cur_d pos} {
789 upvar #0 $cur_d line_data
790 set lno [lindex [split [$cur_w index $pos] .] 0]
791 set dat [lindex $line_data $lno]
2f85b7e4 792 if {$dat ne {}} {
0dfed77b
SP
793 lappend history [list \
794 $commit $path \
795 $highlight_column \
796 $highlight_line \
797 [lindex [$w_file xview] 0] \
798 [lindex [$w_file yview] 0] \
799 ]
2f85b7e4
SP
800 set commit [lindex $dat 0]
801 set path [lindex $dat 1]
0dfed77b 802 _load $this [list [lindex $dat 2]]
2f85b7e4 803 }
22c6769d
SP
804}
805
172c92b4 806method _showcommit {cur_w lno} {
685caf9a 807 global repo_config
5d198d67 808 variable active_color
f522c9b5 809
685caf9a 810 if {$highlight_commit ne {}} {
14c4dfd3 811 foreach i $w_columns {
5d198d67 812 $i tag conf g$highlight_commit -background {}
383d4e0f 813 $i tag lower g$highlight_commit
14c4dfd3 814 }
f522c9b5
SP
815 }
816
0f32da53 817 if {$cur_w eq $w_asim} {
172c92b4
SP
818 set dat [lindex $asim_data $lno]
819 set highlight_column $w_asim
0f32da53
SP
820 } else {
821 set dat [lindex $amov_data $lno]
822 set highlight_column $w_amov
172c92b4
SP
823 }
824
fc816d7b
SP
825 $w_cviewer conf -state normal
826 $w_cviewer delete 0.0 end
2f85b7e4 827
2f85b7e4 828 if {$dat eq {}} {
f522c9b5 829 set cmit {}
c8c4854b 830 $w_cviewer insert end [mc "Loading annotation..."] still_loading
f522c9b5 831 } else {
2f85b7e4
SP
832 set cmit [lindex $dat 0]
833 set file [lindex $dat 1]
834
14c4dfd3
SP
835 foreach i $w_columns {
836 $i tag conf g$cmit -background $active_color
383d4e0f 837 $i tag raise g$cmit
14c4dfd3 838 }
f522c9b5
SP
839
840 set author_name {}
841 set author_email {}
842 set author_time {}
685caf9a
SP
843 catch {set author_name $header($cmit,author)}
844 catch {set author_email $header($cmit,author-mail)}
66c75a5c 845 catch {set author_time [format_date $header($cmit,author-time)]}
f522c9b5
SP
846
847 set committer_name {}
848 set committer_email {}
849 set committer_time {}
685caf9a
SP
850 catch {set committer_name $header($cmit,committer)}
851 catch {set committer_email $header($cmit,committer-mail)}
66c75a5c 852 catch {set committer_time [format_date $header($cmit,committer-time)]}
f522c9b5 853
685caf9a 854 if {[catch {set msg $header($cmit,message)}]} {
f522c9b5
SP
855 set msg {}
856 catch {
0b812616 857 set fd [git_read cat-file commit $cmit]
f522c9b5
SP
858 fconfigure $fd -encoding binary -translation lf
859 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
860 set enc utf-8
861 }
862 while {[gets $fd line] > 0} {
863 if {[string match {encoding *} $line]} {
864 set enc [string tolower [string range $line 9 end]]
865 }
866 }
c4638f66 867 set msg [read $fd]
f522c9b5
SP
868 close $fd
869
c4638f66
SP
870 set enc [tcl_encoding $enc]
871 if {$enc ne {}} {
872 set msg [encoding convertfrom $enc $msg]
873 set author_name [encoding convertfrom $enc $author_name]
874 set committer_name [encoding convertfrom $enc $committer_name]
875 set header($cmit,author) $author_name
876 set header($cmit,committer) $committer_name
877 set header($cmit,summary) \
878 [encoding convertfrom $enc $header($cmit,summary)]
879 }
880 set msg [string trim $msg]
f522c9b5 881 }
685caf9a 882 set header($cmit,message) $msg
f522c9b5
SP
883 }
884
fc816d7b 885 $w_cviewer insert end "commit $cmit\n" header_key
31bb1d1b 886 $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
fc816d7b
SP
887 $w_cviewer insert end "$author_name $author_email" header_val
888 $w_cviewer insert end " $author_time\n" header_val
74fe8985 889
31bb1d1b 890 $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
fc816d7b
SP
891 $w_cviewer insert end "$committer_name $committer_email" header_val
892 $w_cviewer insert end " $committer_time\n" header_val
74fe8985 893
2f85b7e4 894 if {$file ne $path} {
31bb1d1b 895 $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
fc816d7b 896 $w_cviewer insert end "[escape_path $file]\n" header_val
74fe8985 897 }
0511798f 898
fc816d7b 899 $w_cviewer insert end "\n$msg"
f522c9b5 900 }
fc816d7b 901 $w_cviewer conf -state disabled
f522c9b5 902
685caf9a
SP
903 set highlight_line $lno
904 set highlight_commit $cmit
41bf23d6 905
383d4e0f 906 if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
41bf23d6
SP
907 _hide_tooltip $this
908 }
f522c9b5
SP
909}
910
a9c80b83 911method _get_click_amov_info {} {
685caf9a
SP
912 set pos @$::cursorX,$::cursorY
913 set lno [lindex [split [$::cursorW index $pos] .] 0]
a9c80b83
AG
914 return [lindex $amov_data $lno]
915}
916
917method _copycommit {} {
918 set dat [_get_click_amov_info $this]
2f85b7e4 919 if {$dat ne {}} {
f522c9b5
SP
920 clipboard clear
921 clipboard append \
922 -format STRING \
923 -type STRING \
2f85b7e4 924 -- [lindex $dat 0]
f522c9b5
SP
925 }
926}
685caf9a 927
a9c80b83
AG
928method _format_offset_date {base offset} {
929 set exval [expr {$base + $offset*24*60*60}]
930 return [clock format $exval -format {%Y-%m-%d}]
931}
932
933method _gitkcommit {} {
934 set dat [_get_click_amov_info $this]
935 if {$dat ne {}} {
936 set cmit [lindex $dat 0]
937 set radius [get_config gui.blamehistoryctx]
938 set cmdline [list --select-commit=$cmit]
939
940 if {$radius > 0} {
941 set author_time {}
942 set committer_time {}
943
944 catch {set author_time $header($cmit,author-time)}
945 catch {set committer_time $header($cmit,committer-time)}
946
947 if {$committer_time eq {}} {
948 set committer_time $author_time
949 }
950
951 set after_time [_format_offset_date $this $committer_time [expr {-$radius}]]
952 set before_time [_format_offset_date $this $committer_time $radius]
953
954 lappend cmdline --after=$after_time --before=$before_time
955 }
956
957 lappend cmdline $cmit
958
959 set base_rev "HEAD"
960 if {$commit ne {}} {
961 set base_rev $commit
962 }
963
964 if {$base_rev ne $cmit} {
965 lappend cmdline $base_rev
966 }
967
968 do_gitk $cmdline
969 }
970}
971
41bf23d6 972method _show_tooltip {cur_w pos} {
383d4e0f 973 if {$tooltip_wm ne {}} {
41bf23d6
SP
974 _open_tooltip $this $cur_w
975 } elseif {$tooltip_timer eq {}} {
976 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
977 }
978}
979
980method _open_tooltip {cur_w} {
981 set tooltip_timer {}
982 set pos_x [winfo pointerx $cur_w]
983 set pos_y [winfo pointery $cur_w]
984 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
985 _hide_tooltip $this
986 return
987 }
988
383d4e0f
SP
989 if {$tooltip_wm ne "$cur_w.tooltip"} {
990 _hide_tooltip $this
991
992 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
993 wm overrideredirect $tooltip_wm 1
994 wm transient $tooltip_wm [winfo toplevel $cur_w]
995 set tooltip_t $tooltip_wm.label
996 text $tooltip_t \
997 -takefocus 0 \
998 -highlightthickness 0 \
999 -relief flat \
1000 -borderwidth 0 \
1001 -wrap none \
1002 -background lightyellow \
1003 -foreground black
1004 $tooltip_t tag conf section_header -font font_uibold
1005 pack $tooltip_t
1006 } else {
1007 $tooltip_t conf -state normal
1008 $tooltip_t delete 0.0 end
1009 }
1010
41bf23d6
SP
1011 set pos @[join [list \
1012 [expr {$pos_x - [winfo rootx $cur_w]}] \
1013 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
1014 set lno [lindex [split [$cur_w index $pos] .] 0]
172c92b4
SP
1015 if {$cur_w eq $w_amov} {
1016 set dat [lindex $amov_data $lno]
383d4e0f 1017 set org {}
172c92b4
SP
1018 } else {
1019 set dat [lindex $asim_data $lno]
383d4e0f 1020 set org [lindex $amov_data $lno]
172c92b4 1021 }
383d4e0f 1022
a4228962
SP
1023 if {$dat eq {}} {
1024 _hide_tooltip $this
1025 return
1026 }
1027
2f85b7e4 1028 set cmit [lindex $dat 0]
383d4e0f 1029 set tooltip_commit [list $cmit]
41bf23d6
SP
1030
1031 set author_name {}
383d4e0f 1032 set summary {}
41bf23d6
SP
1033 set author_time {}
1034 catch {set author_name $header($cmit,author)}
383d4e0f 1035 catch {set summary $header($cmit,summary)}
66c75a5c 1036 catch {set author_time [format_date $header($cmit,author-time)]}
41bf23d6 1037
383d4e0f
SP
1038 $tooltip_t insert end "commit $cmit\n"
1039 $tooltip_t insert end "$author_name $author_time\n"
1040 $tooltip_t insert end "$summary"
41bf23d6 1041
383d4e0f 1042 if {$org ne {} && [lindex $org 0] ne $cmit} {
0f32da53
SP
1043 set save [$tooltip_t get 0.0 end]
1044 $tooltip_t delete 0.0 end
1045
383d4e0f
SP
1046 set cmit [lindex $org 0]
1047 set file [lindex $org 1]
1048 lappend tooltip_commit $cmit
41bf23d6 1049
383d4e0f
SP
1050 set author_name {}
1051 set summary {}
1052 set author_time {}
1053 catch {set author_name $header($cmit,author)}
1054 catch {set summary $header($cmit,summary)}
cbf13d9e 1055 catch {set author_time [format_date $header($cmit,author-time)]}
41bf23d6 1056
31bb1d1b 1057 $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
383d4e0f
SP
1058 $tooltip_t insert end "commit $cmit\n"
1059 $tooltip_t insert end "$author_name $author_time\n"
0f32da53 1060 $tooltip_t insert end "$summary\n"
79c50bf3 1061
383d4e0f 1062 if {$file ne $path} {
31bb1d1b 1063 $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
0f32da53 1064 $tooltip_t insert end "$file\n"
383d4e0f 1065 }
0f32da53
SP
1066
1067 $tooltip_t insert end "\n"
31bb1d1b 1068 $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
0f32da53 1069 $tooltip_t insert end $save
79c50bf3
SP
1070 }
1071
383d4e0f 1072 $tooltip_t conf -state disabled
41bf23d6
SP
1073 _position_tooltip $this
1074}
1075
1076method _position_tooltip {} {
383d4e0f
SP
1077 set max_h [lindex [split [$tooltip_t index end] .] 0]
1078 set max_w 0
1079 for {set i 1} {$i <= $max_h} {incr i} {
1080 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
1081 if {$c > $max_w} {set max_w $c}
1082 }
1083 $tooltip_t conf -width $max_w -height $max_h
1084
1085 set req_w [winfo reqwidth $tooltip_t]
1086 set req_h [winfo reqheight $tooltip_t]
41bf23d6
SP
1087 set pos_x [expr {[winfo pointerx .] + 5}]
1088 set pos_y [expr {[winfo pointery .] + 10}]
1089
1090 set g "${req_w}x${req_h}"
1091 if {$pos_x >= 0} {append g +}
1092 append g $pos_x
1093 if {$pos_y >= 0} {append g +}
1094 append g $pos_y
1095
1096 wm geometry $tooltip_wm $g
1097 raise $tooltip_wm
1098}
1099
1100method _hide_tooltip {} {
1101 if {$tooltip_wm ne {}} {
1102 destroy $tooltip_wm
1103 set tooltip_wm {}
1104 set tooltip_commit {}
1105 }
1106 if {$tooltip_timer ne {}} {
1107 after cancel $tooltip_timer
1108 set tooltip_timer {}
1109 }
1110}
1111
d0b741dc
SP
1112method _resize {new_height} {
1113 set diff [expr {$new_height - $old_height}]
1114 if {$diff == 0} return
1115
1116 set my [expr {[winfo height $w.file_pane] - 25}]
1117 set o [$w.file_pane sash coord 0]
1118 set ox [lindex $o 0]
1119 set oy [expr {[lindex $o 1] + $diff}]
1120 if {$oy < 0} {set oy 0}
1121 if {$oy > $my} {set oy $my}
1122 $w.file_pane sash place 0 $ox $oy
1123
1124 set old_height $new_height
1125}
1126
685caf9a 1127}