Merge branch 'maint-1.6.1' into maint
[debian-git.git] / git-gui / lib / blame.tcl
blob1f3b08f9efff873631d73e128ee97c1b9dad1822
1 # git-gui blame viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class blame {
6 image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
8 # Persistant data (survives loads)
10 field history {}; # viewer history: {commit path}
11 field header ; # array commit,key -> header field
13 # Tk UI control paths
15 field w ; # top window in this viewer
16 field w_back ; # our back button
17 field w_path ; # label showing the current file path
18 field w_columns ; # list of all column widgets in the viewer
19 field w_line ; # text column: all line numbers
20 field w_amov ; # text column: annotations + move tracking
21 field w_asim ; # text column: annotations (simple computation)
22 field w_file ; # text column: actual file data
23 field w_cviewer ; # pane showing commit message
24 field finder ; # find mini-dialog frame
25 field status ; # status mega-widget instance
26 field old_height ; # last known height of $w.file_pane
29 # Tk UI colors
31 variable active_color #c0edc5
32 variable group_colors {
33 #d6d6d6
34 #e1e1e1
35 #ececec
38 # Current blame data; cleared/reset on each load
40 field commit ; # input commit to blame
41 field path ; # input filename to view in $commit
43 field current_fd {} ; # background process running
44 field highlight_line -1 ; # current line selected
45 field highlight_column {} ; # current commit column selected
46 field highlight_commit {} ; # sha1 of commit selected
48 field total_lines 0 ; # total length of file
49 field blame_lines 0 ; # number of lines computed
50 field amov_data ; # list of {commit origfile origline}
51 field asim_data ; # list of {commit origfile origline}
53 field r_commit ; # commit currently being parsed
54 field r_orig_line ; # original line number
55 field r_final_line ; # final line number
56 field r_line_count ; # lines in this region
58 field tooltip_wm {} ; # Current tooltip toplevel, if open
59 field tooltip_t {} ; # Text widget in $tooltip_wm
60 field tooltip_timer {} ; # Current timer event for our tooltip
61 field tooltip_commit {} ; # Commit(s) in tooltip
63 constructor new {i_commit i_path i_jump} {
64 global cursor_ptr M1B M1T have_tk85
65 variable active_color
66 variable group_colors
68 set commit $i_commit
69 set path $i_path
71 make_toplevel top w
72 wm title $top [append "[appname] ([reponame]): " [mc "File Viewer"]]
74 set font_w [font measure font_diff "0"]
76 frame $w.header -background gold
77 label $w.header.commit_l \
78 -text [mc "Commit:"] \
79 -background gold \
80 -foreground black \
81 -anchor w \
82 -justify left
83 set w_back $w.header.commit_b
84 label $w_back \
85 -image ::blame::img_back_arrow \
86 -borderwidth 0 \
87 -relief flat \
88 -state disabled \
89 -background gold \
90 -foreground black \
91 -activebackground gold
92 bind $w_back <Button-1> "
93 if {\[$w_back cget -state\] eq {normal}} {
94 [cb _history_menu]
97 label $w.header.commit \
98 -textvariable @commit \
99 -background gold \
100 -foreground black \
101 -anchor w \
102 -justify left
103 label $w.header.path_l \
104 -text [mc "File:"] \
105 -background gold \
106 -foreground black \
107 -anchor w \
108 -justify left
109 set w_path $w.header.path
110 label $w_path \
111 -background gold \
112 -foreground black \
113 -anchor w \
114 -justify left
115 pack $w.header.commit_l -side left
116 pack $w_back -side left
117 pack $w.header.commit -side left
118 pack $w_path -fill x -side right
119 pack $w.header.path_l -side right
121 panedwindow $w.file_pane -orient vertical -borderwidth 0 -sashwidth 3
122 frame $w.file_pane.out -relief flat -borderwidth 1
123 frame $w.file_pane.cm -relief sunken -borderwidth 1
124 $w.file_pane add $w.file_pane.out \
125 -sticky nsew \
126 -minsize 100 \
127 -height 100 \
128 -width 100
129 $w.file_pane add $w.file_pane.cm \
130 -sticky nsew \
131 -minsize 25 \
132 -height 25 \
133 -width 100
135 set w_line $w.file_pane.out.linenumber_t
136 text $w_line \
137 -takefocus 0 \
138 -highlightthickness 0 \
139 -padx 0 -pady 0 \
140 -background white \
141 -foreground black \
142 -borderwidth 0 \
143 -state disabled \
144 -wrap none \
145 -height 40 \
146 -width 6 \
147 -font font_diff
148 $w_line tag conf linenumber -justify right -rmargin 5
150 set w_amov $w.file_pane.out.amove_t
151 text $w_amov \
152 -takefocus 0 \
153 -highlightthickness 0 \
154 -padx 0 -pady 0 \
155 -background white \
156 -foreground black \
157 -borderwidth 0 \
158 -state disabled \
159 -wrap none \
160 -height 40 \
161 -width 5 \
162 -font font_diff
163 $w_amov tag conf author_abbr -justify right -rmargin 5
164 $w_amov tag conf curr_commit
165 $w_amov tag conf prior_commit -foreground blue -underline 1
166 $w_amov tag bind prior_commit \
167 <Button-1> \
168 "[cb _load_commit $w_amov @amov_data @%x,%y];break"
170 set w_asim $w.file_pane.out.asimple_t
171 text $w_asim \
172 -takefocus 0 \
173 -highlightthickness 0 \
174 -padx 0 -pady 0 \
175 -background white \
176 -foreground black \
177 -borderwidth 0 \
178 -state disabled \
179 -wrap none \
180 -height 40 \
181 -width 4 \
182 -font font_diff
183 $w_asim tag conf author_abbr -justify right
184 $w_asim tag conf curr_commit
185 $w_asim tag conf prior_commit -foreground blue -underline 1
186 $w_asim tag bind prior_commit \
187 <Button-1> \
188 "[cb _load_commit $w_asim @asim_data @%x,%y];break"
190 set w_file $w.file_pane.out.file_t
191 text $w_file \
192 -takefocus 0 \
193 -highlightthickness 0 \
194 -padx 0 -pady 0 \
195 -background white \
196 -foreground black \
197 -borderwidth 0 \
198 -state disabled \
199 -wrap none \
200 -height 40 \
201 -width 80 \
202 -xscrollcommand [list $w.file_pane.out.sbx set] \
203 -font font_diff
204 if {$have_tk85} {
205 $w_file configure -inactiveselectbackground darkblue
207 $w_file tag conf found \
208 -background yellow
210 set w_columns [list $w_amov $w_asim $w_line $w_file]
212 scrollbar $w.file_pane.out.sbx \
213 -orient h \
214 -command [list $w_file xview]
215 scrollbar $w.file_pane.out.sby \
216 -orient v \
217 -command [list scrollbar2many $w_columns yview]
218 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
219 grid conf \
220 $w.file_pane.out.sbx \
221 -column [expr {[llength $w_columns] - 1}] \
222 -sticky we
223 grid columnconfigure \
224 $w.file_pane.out \
225 [expr {[llength $w_columns] - 1}] \
226 -weight 1
227 grid rowconfigure $w.file_pane.out 0 -weight 1
229 set finder [::searchbar::new \
230 $w.file_pane.out.ff $w_file \
231 -column [expr {[llength $w_columns] - 1}] \
234 set w_cviewer $w.file_pane.cm.t
235 text $w_cviewer \
236 -background white \
237 -foreground black \
238 -borderwidth 0 \
239 -state disabled \
240 -wrap none \
241 -height 10 \
242 -width 80 \
243 -xscrollcommand [list $w.file_pane.cm.sbx set] \
244 -yscrollcommand [list $w.file_pane.cm.sby set] \
245 -font font_diff
246 $w_cviewer tag conf still_loading \
247 -font font_uiitalic \
248 -justify center
249 $w_cviewer tag conf header_key \
250 -tabs {3c} \
251 -background $active_color \
252 -font font_uibold
253 $w_cviewer tag conf header_val \
254 -background $active_color \
255 -font font_ui
256 $w_cviewer tag raise sel
257 scrollbar $w.file_pane.cm.sbx \
258 -orient h \
259 -command [list $w_cviewer xview]
260 scrollbar $w.file_pane.cm.sby \
261 -orient v \
262 -command [list $w_cviewer yview]
263 pack $w.file_pane.cm.sby -side right -fill y
264 pack $w.file_pane.cm.sbx -side bottom -fill x
265 pack $w_cviewer -expand 1 -fill both
267 set status [::status_bar::new $w.status]
269 menu $w.ctxm -tearoff 0
270 $w.ctxm add command \
271 -label [mc "Copy Commit"] \
272 -command [cb _copycommit]
273 $w.ctxm add separator
274 $w.ctxm add command \
275 -label [mc "Find Text..."] \
276 -accelerator F7 \
277 -command [list searchbar::show $finder]
278 menu $w.ctxm.enc
279 build_encoding_menu $w.ctxm.enc [cb _setencoding]
280 $w.ctxm add cascade \
281 -label [mc "Encoding"] \
282 -menu $w.ctxm.enc
283 $w.ctxm add command \
284 -label [mc "Do Full Copy Detection"] \
285 -command [cb _fullcopyblame]
286 $w.ctxm add separator
287 $w.ctxm add command \
288 -label [mc "Show History Context"] \
289 -command [cb _gitkcommit]
290 $w.ctxm add command \
291 -label [mc "Blame Parent Commit"] \
292 -command [cb _blameparent]
294 foreach i $w_columns {
295 for {set g 0} {$g < [llength $group_colors]} {incr g} {
296 $i tag conf color$g -background [lindex $group_colors $g]
299 if {$i eq $w_file} {
300 $w_file tag raise found
302 $i tag raise sel
304 $i conf -cursor $cursor_ptr
305 $i conf -yscrollcommand \
306 "[list ::searchbar::scrolled $finder]
307 [list many2scrollbar $w_columns yview $w.file_pane.out.sby]"
308 bind $i <Button-1> "
309 [cb _hide_tooltip]
310 [cb _click $i @%x,%y]
311 focus $i
313 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
314 bind $i <Any-Enter> [cb _hide_tooltip]
315 bind $i <Any-Leave> [cb _hide_tooltip]
316 bind_button3 $i "
317 [cb _hide_tooltip]
318 set cursorX %x
319 set cursorY %y
320 set cursorW %W
321 tk_popup $w.ctxm %X %Y
323 bind $i <Shift-Tab> "[list focus $w_cviewer];break"
324 bind $i <Tab> "[cb _focus_search $w_cviewer];break"
327 foreach i [concat $w_columns $w_cviewer] {
328 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
329 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
330 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
331 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
332 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
333 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
334 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
335 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
336 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
337 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
340 bind $w_cviewer <Shift-Tab> "[cb _focus_search $w_file];break"
341 bind $w_cviewer <Tab> "[list focus $w_file];break"
342 bind $w_cviewer <Button-1> [list focus $w_cviewer]
343 bind $w_file <Visibility> [cb _focus_search $w_file]
344 bind $top <F7> [list searchbar::show $finder]
345 bind $top <Escape> [list searchbar::hide $finder]
346 bind $top <F3> [list searchbar::find_next $finder]
347 bind $top <Shift-F3> [list searchbar::find_prev $finder]
348 catch { bind $top <Shift-Key-XF86_Switch_VT_3> [list searchbar::find_prev $finder] }
350 grid configure $w.header -sticky ew
351 grid configure $w.file_pane -sticky nsew
352 grid configure $w.status -sticky ew
353 grid columnconfigure $top 0 -weight 1
354 grid rowconfigure $top 0 -weight 0
355 grid rowconfigure $top 1 -weight 1
356 grid rowconfigure $top 2 -weight 0
358 set req_w [winfo reqwidth $top]
359 set req_h [winfo reqheight $top]
360 set scr_w [expr {[winfo screenwidth $top] - 40}]
361 set scr_h [expr {[winfo screenheight $top] - 120}]
362 set opt_w [expr {$font_w * (80 + 5*3 + 3)}]
363 if {$req_w < $opt_w} {set req_w $opt_w}
364 if {$req_w > $scr_w} {set req_w $scr_w}
365 set opt_h [expr {$req_w*4/3}]
366 if {$req_h < $scr_h} {set req_h $scr_h}
367 if {$req_h > $opt_h} {set req_h $opt_h}
368 set g "${req_w}x${req_h}"
369 wm geometry $top $g
370 update
372 set old_height [winfo height $w.file_pane]
373 $w.file_pane sash place 0 \
374 [lindex [$w.file_pane sash coord 0] 0] \
375 [expr {int($old_height * 0.80)}]
376 bind $w.file_pane <Configure> \
377 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
379 wm protocol $top WM_DELETE_WINDOW "destroy $top"
380 bind $top <Destroy> [cb _handle_destroy %W]
382 _load $this $i_jump
385 method _focus_search {win} {
386 if {[searchbar::visible $finder]} {
387 focus [searchbar::editor $finder]
388 } else {
389 focus $win
393 method _handle_destroy {win} {
394 if {$win eq $w} {
395 _kill $this
396 delete_this
400 method _kill {} {
401 if {$current_fd ne {}} {
402 kill_file_process $current_fd
403 catch {close $current_fd}
404 set current_fd {}
408 method _load {jump} {
409 variable group_colors
411 _hide_tooltip $this
413 if {$total_lines != 0 || $current_fd ne {}} {
414 _kill $this
416 foreach i $w_columns {
417 $i conf -state normal
418 $i delete 0.0 end
419 foreach g [$i tag names] {
420 if {[regexp {^g[0-9a-f]{40}$} $g]} {
421 $i tag delete $g
424 $i conf -state disabled
427 $w_cviewer conf -state normal
428 $w_cviewer delete 0.0 end
429 $w_cviewer conf -state disabled
431 set highlight_line -1
432 set highlight_column {}
433 set highlight_commit {}
434 set total_lines 0
437 if {$history eq {}} {
438 $w_back conf -state disabled
439 } else {
440 $w_back conf -state normal
443 # Index 0 is always empty. There is never line 0 as
444 # we use only 1 based lines, as that matches both with
445 # git-blame output and with Tk's text widget.
447 set amov_data [list [list]]
448 set asim_data [list [list]]
450 $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
451 $w_path conf -text [escape_path $path]
452 if {$commit eq {}} {
453 set fd [open $path r]
454 fconfigure $fd -eofchar {}
455 } else {
456 set fd [git_read cat-file blob "$commit:$path"]
458 fconfigure $fd \
459 -blocking 0 \
460 -translation lf \
461 -encoding [get_path_encoding $path]
462 fileevent $fd readable [cb _read_file $fd $jump]
463 set current_fd $fd
466 method _history_menu {} {
467 set m $w.backmenu
468 if {[winfo exists $m]} {
469 $m delete 0 end
470 } else {
471 menu $m -tearoff 0
474 for {set i [expr {[llength $history] - 1}]
475 } {$i >= 0} {incr i -1} {
476 set e [lindex $history $i]
477 set c [lindex $e 0]
478 set f [lindex $e 1]
480 if {[regexp {^[0-9a-f]{40}$} $c]} {
481 set t [string range $c 0 8]...
482 } elseif {$c eq {}} {
483 set t {Working Directory}
484 } else {
485 set t $c
487 if {![catch {set summary $header($c,summary)}]} {
488 append t " $summary"
489 if {[string length $t] > 70} {
490 set t [string range $t 0 66]...
494 $m add command -label $t -command [cb _goback $i]
496 set X [winfo rootx $w_back]
497 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
498 tk_popup $m $X $Y
501 method _goback {i} {
502 set dat [lindex $history $i]
503 set history [lrange $history 0 [expr {$i - 1}]]
504 set commit [lindex $dat 0]
505 set path [lindex $dat 1]
506 _load $this [lrange $dat 2 5]
509 method _read_file {fd jump} {
510 if {$fd ne $current_fd} {
511 catch {close $fd}
512 return
515 foreach i $w_columns {$i conf -state normal}
516 while {[gets $fd line] >= 0} {
517 regsub "\r\$" $line {} line
518 incr total_lines
519 lappend amov_data {}
520 lappend asim_data {}
522 if {$total_lines > 1} {
523 foreach i $w_columns {$i insert end "\n"}
526 $w_line insert end "$total_lines" linenumber
527 $w_file insert end "$line"
530 set ln_wc [expr {[string length $total_lines] + 2}]
531 if {[$w_line cget -width] < $ln_wc} {
532 $w_line conf -width $ln_wc
535 foreach i $w_columns {$i conf -state disabled}
537 if {[eof $fd]} {
538 close $fd
540 # If we don't force Tk to update the widgets *right now*
541 # none of our jump commands will cause a change in the UI.
543 update
545 if {[llength $jump] == 1} {
546 set highlight_line [lindex $jump 0]
547 $w_file see "$highlight_line.0"
548 } elseif {[llength $jump] == 4} {
549 set highlight_column [lindex $jump 0]
550 set highlight_line [lindex $jump 1]
551 $w_file xview moveto [lindex $jump 2]
552 $w_file yview moveto [lindex $jump 3]
555 _exec_blame $this $w_asim @asim_data \
556 [list] \
557 [mc "Loading copy/move tracking annotations..."]
559 } ifdeleted { catch {close $fd} }
561 method _exec_blame {cur_w cur_d options cur_s} {
562 lappend options --incremental --encoding=utf-8
563 if {$commit eq {}} {
564 lappend options --contents $path
565 } else {
566 lappend options $commit
568 lappend options -- $path
569 set fd [eval git_read --nice blame $options]
570 fconfigure $fd -blocking 0 -translation lf -encoding utf-8
571 fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
572 set current_fd $fd
573 set blame_lines 0
575 $status start \
576 $cur_s \
577 [mc "lines annotated"]
580 method _read_blame {fd cur_w cur_d} {
581 upvar #0 $cur_d line_data
582 variable group_colors
584 if {$fd ne $current_fd} {
585 catch {close $fd}
586 return
589 $cur_w conf -state normal
590 while {[gets $fd line] >= 0} {
591 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
592 cmit original_line final_line line_count]} {
593 set r_commit $cmit
594 set r_orig_line $original_line
595 set r_final_line $final_line
596 set r_line_count $line_count
597 } elseif {[string match {filename *} $line]} {
598 set file [string range $line 9 end]
599 set n $r_line_count
600 set lno $r_final_line
601 set oln $r_orig_line
602 set cmit $r_commit
604 if {[regexp {^0{40}$} $cmit]} {
605 set commit_abbr work
606 set commit_type curr_commit
607 } elseif {$cmit eq $commit} {
608 set commit_abbr this
609 set commit_type curr_commit
610 } else {
611 set commit_type prior_commit
612 set commit_abbr [string range $cmit 0 3]
615 set author_abbr {}
616 set a_name {}
617 catch {set a_name $header($cmit,author)}
618 while {$a_name ne {}} {
619 if {$author_abbr ne {}
620 && [string index $a_name 0] eq {'}} {
621 regsub {^'[^']+'\s+} $a_name {} a_name
623 if {![regexp {^([[:upper:]])} $a_name _a]} break
624 append author_abbr $_a
625 unset _a
626 if {![regsub \
627 {^[[:upper:]][^\s]*\s+} \
628 $a_name {} a_name ]} break
630 if {$author_abbr eq {}} {
631 set author_abbr { |}
632 } else {
633 set author_abbr [string range $author_abbr 0 3]
635 unset a_name
637 set first_lno $lno
638 while {
639 $first_lno > 1
640 && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
641 && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
643 incr first_lno -1
646 set color {}
647 if {$first_lno < $lno} {
648 foreach g [$w_file tag names $first_lno.0] {
649 if {[regexp {^color[0-9]+$} $g]} {
650 set color $g
651 break
654 } else {
655 set i [lsort [concat \
656 [$w_file tag names "[expr {$first_lno - 1}].0"] \
657 [$w_file tag names "[expr {$lno + $n}].0"] \
659 for {set g 0} {$g < [llength $group_colors]} {incr g} {
660 if {[lsearch -sorted -exact $i color$g] == -1} {
661 set color color$g
662 break
666 if {$color eq {}} {
667 set color color0
670 while {$n > 0} {
671 set lno_e "$lno.0 lineend + 1c"
672 if {[lindex $line_data $lno] ne {}} {
673 set g [lindex $line_data $lno 0]
674 foreach i $w_columns {
675 $i tag remove g$g $lno.0 $lno_e
678 lset line_data $lno [list $cmit $file $oln]
680 $cur_w delete $lno.0 "$lno.0 lineend"
681 if {$lno == $first_lno} {
682 $cur_w insert $lno.0 $commit_abbr $commit_type
683 } elseif {$lno == [expr {$first_lno + 1}]} {
684 $cur_w insert $lno.0 $author_abbr author_abbr
685 } else {
686 $cur_w insert $lno.0 { |}
689 foreach i $w_columns {
690 if {$cur_w eq $w_amov} {
691 for {set g 0} \
692 {$g < [llength $group_colors]} \
693 {incr g} {
694 $i tag remove color$g $lno.0 $lno_e
696 $i tag add $color $lno.0 $lno_e
698 $i tag add g$cmit $lno.0 $lno_e
701 if {$highlight_column eq $cur_w} {
702 if {$highlight_line == -1
703 && [lindex [$w_file yview] 0] == 0} {
704 $w_file see $lno.0
705 set highlight_line $lno
707 if {$highlight_line == $lno} {
708 _showcommit $this $cur_w $lno
712 incr n -1
713 incr lno
714 incr oln
715 incr blame_lines
718 while {
719 $cmit eq [lindex $line_data $lno 0]
720 && $file eq [lindex $line_data $lno 1]
722 $cur_w delete $lno.0 "$lno.0 lineend"
724 if {$lno == $first_lno} {
725 $cur_w insert $lno.0 $commit_abbr $commit_type
726 } elseif {$lno == [expr {$first_lno + 1}]} {
727 $cur_w insert $lno.0 $author_abbr author_abbr
728 } else {
729 $cur_w insert $lno.0 { |}
732 if {$cur_w eq $w_amov} {
733 foreach i $w_columns {
734 for {set g 0} \
735 {$g < [llength $group_colors]} \
736 {incr g} {
737 $i tag remove color$g $lno.0 $lno_e
739 $i tag add $color $lno.0 $lno_e
743 incr lno
746 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
747 set header($r_commit,$key) $data
750 $cur_w conf -state disabled
752 if {[eof $fd]} {
753 close $fd
754 if {$cur_w eq $w_asim} {
755 # Switches for original location detection
756 set threshold [get_config gui.copyblamethreshold]
757 set original_options [list "-C$threshold"]
759 if {![is_config_true gui.fastcopyblame]} {
760 # thorough copy search; insert before the threshold
761 set original_options [linsert $original_options 0 -C]
763 if {[git-version >= 1.5.3]} {
764 lappend original_options -w ; # ignore indentation changes
767 _exec_blame $this $w_amov @amov_data \
768 $original_options \
769 [mc "Loading original location annotations..."]
770 } else {
771 set current_fd {}
772 $status stop [mc "Annotation complete."]
774 } else {
775 $status update $blame_lines $total_lines
777 } ifdeleted { catch {close $fd} }
779 method _find_commit_bound {data_list start_idx delta} {
780 upvar #0 $data_list line_data
781 set pos $start_idx
782 set limit [expr {[llength $line_data] - 1}]
783 set base_commit [lindex $line_data $pos 0]
785 while {$pos > 0 && $pos < $limit} {
786 set new_pos [expr {$pos + $delta}]
787 if {[lindex $line_data $new_pos 0] ne $base_commit} {
788 return $pos
791 set pos $new_pos
794 return $pos
797 method _fullcopyblame {} {
798 if {$current_fd ne {}} {
799 tk_messageBox \
800 -icon error \
801 -type ok \
802 -title [mc "Busy"] \
803 -message [mc "Annotation process is already running."]
805 return
808 # Switches for original location detection
809 set threshold [get_config gui.copyblamethreshold]
810 set original_options [list -C -C "-C$threshold"]
812 if {[git-version >= 1.5.3]} {
813 lappend original_options -w ; # ignore indentation changes
816 # Find the line range
817 set pos @$::cursorX,$::cursorY
818 set lno [lindex [split [$::cursorW index $pos] .] 0]
819 set min_amov_lno [_find_commit_bound $this @amov_data $lno -1]
820 set max_amov_lno [_find_commit_bound $this @amov_data $lno 1]
821 set min_asim_lno [_find_commit_bound $this @asim_data $lno -1]
822 set max_asim_lno [_find_commit_bound $this @asim_data $lno 1]
824 if {$min_asim_lno < $min_amov_lno} {
825 set min_amov_lno $min_asim_lno
828 if {$max_asim_lno > $max_amov_lno} {
829 set max_amov_lno $max_asim_lno
832 lappend original_options -L "$min_amov_lno,$max_amov_lno"
834 # Clear lines
835 for {set i $min_amov_lno} {$i <= $max_amov_lno} {incr i} {
836 lset amov_data $i [list ]
839 # Start the back-end process
840 _exec_blame $this $w_amov @amov_data \
841 $original_options \
842 [mc "Running thorough copy detection..."]
845 method _click {cur_w pos} {
846 set lno [lindex [split [$cur_w index $pos] .] 0]
847 _showcommit $this $cur_w $lno
850 method _setencoding {enc} {
851 force_path_encoding $path $enc
852 _load $this [list \
853 $highlight_column \
854 $highlight_line \
855 [lindex [$w_file xview] 0] \
856 [lindex [$w_file yview] 0] \
860 method _load_commit {cur_w cur_d pos} {
861 upvar #0 $cur_d line_data
862 set lno [lindex [split [$cur_w index $pos] .] 0]
863 set dat [lindex $line_data $lno]
864 if {$dat ne {}} {
865 _load_new_commit $this \
866 [lindex $dat 0] \
867 [lindex $dat 1] \
868 [list [lindex $dat 2]]
872 method _load_new_commit {new_commit new_path jump} {
873 lappend history [list \
874 $commit $path \
875 $highlight_column \
876 $highlight_line \
877 [lindex [$w_file xview] 0] \
878 [lindex [$w_file yview] 0] \
881 set commit $new_commit
882 set path $new_path
883 _load $this $jump
886 method _showcommit {cur_w lno} {
887 global repo_config
888 variable active_color
890 if {$highlight_commit ne {}} {
891 foreach i $w_columns {
892 $i tag conf g$highlight_commit -background {}
893 $i tag lower g$highlight_commit
897 if {$cur_w eq $w_asim} {
898 set dat [lindex $asim_data $lno]
899 set highlight_column $w_asim
900 } else {
901 set dat [lindex $amov_data $lno]
902 set highlight_column $w_amov
905 $w_cviewer conf -state normal
906 $w_cviewer delete 0.0 end
908 if {$dat eq {}} {
909 set cmit {}
910 $w_cviewer insert end [mc "Loading annotation..."] still_loading
911 } else {
912 set cmit [lindex $dat 0]
913 set file [lindex $dat 1]
915 foreach i $w_columns {
916 $i tag conf g$cmit -background $active_color
917 $i tag raise g$cmit
918 if {$i eq $w_file} {
919 $w_file tag raise found
921 $i tag raise sel
924 set author_name {}
925 set author_email {}
926 set author_time {}
927 catch {set author_name $header($cmit,author)}
928 catch {set author_email $header($cmit,author-mail)}
929 catch {set author_time [format_date $header($cmit,author-time)]}
931 set committer_name {}
932 set committer_email {}
933 set committer_time {}
934 catch {set committer_name $header($cmit,committer)}
935 catch {set committer_email $header($cmit,committer-mail)}
936 catch {set committer_time [format_date $header($cmit,committer-time)]}
938 if {[catch {set msg $header($cmit,message)}]} {
939 set msg {}
940 catch {
941 set fd [git_read cat-file commit $cmit]
942 fconfigure $fd -encoding binary -translation lf
943 # By default commits are assumed to be in utf-8
944 set enc utf-8
945 while {[gets $fd line] > 0} {
946 if {[string match {encoding *} $line]} {
947 set enc [string tolower [string range $line 9 end]]
950 set msg [read $fd]
951 close $fd
953 set enc [tcl_encoding $enc]
954 if {$enc ne {}} {
955 set msg [encoding convertfrom $enc $msg]
957 set msg [string trim $msg]
959 set header($cmit,message) $msg
962 $w_cviewer insert end "commit $cmit\n" header_key
963 $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
964 $w_cviewer insert end "$author_name $author_email" header_val
965 $w_cviewer insert end " $author_time\n" header_val
967 $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
968 $w_cviewer insert end "$committer_name $committer_email" header_val
969 $w_cviewer insert end " $committer_time\n" header_val
971 if {$file ne $path} {
972 $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
973 $w_cviewer insert end "[escape_path $file]\n" header_val
976 $w_cviewer insert end "\n$msg"
978 $w_cviewer conf -state disabled
980 set highlight_line $lno
981 set highlight_commit $cmit
983 if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
984 _hide_tooltip $this
988 method _get_click_amov_info {} {
989 set pos @$::cursorX,$::cursorY
990 set lno [lindex [split [$::cursorW index $pos] .] 0]
991 return [lindex $amov_data $lno]
994 method _copycommit {} {
995 set dat [_get_click_amov_info $this]
996 if {$dat ne {}} {
997 clipboard clear
998 clipboard append \
999 -format STRING \
1000 -type STRING \
1001 -- [lindex $dat 0]
1005 method _format_offset_date {base offset} {
1006 set exval [expr {$base + $offset*24*60*60}]
1007 return [clock format $exval -format {%Y-%m-%d}]
1010 method _gitkcommit {} {
1011 global nullid
1013 set dat [_get_click_amov_info $this]
1014 if {$dat ne {}} {
1015 set cmit [lindex $dat 0]
1017 # If the line belongs to the working copy, use HEAD instead
1018 if {$cmit eq $nullid} {
1019 if {[catch {set cmit [git rev-parse --verify HEAD]} err]} {
1020 error_popup [strcat [mc "Cannot find HEAD commit:"] "\n\n$err"]
1021 return;
1025 set radius [get_config gui.blamehistoryctx]
1026 set cmdline [list --select-commit=$cmit]
1028 if {$radius > 0} {
1029 set author_time {}
1030 set committer_time {}
1032 catch {set author_time $header($cmit,author-time)}
1033 catch {set committer_time $header($cmit,committer-time)}
1035 if {$committer_time eq {}} {
1036 set committer_time $author_time
1039 set after_time [_format_offset_date $this $committer_time [expr {-$radius}]]
1040 set before_time [_format_offset_date $this $committer_time $radius]
1042 lappend cmdline --after=$after_time --before=$before_time
1045 lappend cmdline $cmit
1047 set base_rev "HEAD"
1048 if {$commit ne {}} {
1049 set base_rev $commit
1052 if {$base_rev ne $cmit} {
1053 lappend cmdline $base_rev
1056 do_gitk $cmdline
1060 method _blameparent {} {
1061 global nullid
1063 set dat [_get_click_amov_info $this]
1064 if {$dat ne {}} {
1065 set cmit [lindex $dat 0]
1066 set new_path [lindex $dat 1]
1068 # Allow using Blame Parent on lines modified in the working copy
1069 if {$cmit eq $nullid} {
1070 set parent_ref "HEAD"
1071 } else {
1072 set parent_ref "$cmit^"
1074 if {[catch {set cparent [git rev-parse --verify $parent_ref]} err]} {
1075 error_popup [strcat [mc "Cannot find parent commit:"] "\n\n$err"]
1076 return;
1079 _kill $this
1081 # Generate a diff between the commit and its parent,
1082 # and use the hunks to update the line number.
1083 # Request zero context to simplify calculations.
1084 if {$cmit eq $nullid} {
1085 set diffcmd [list diff-index --unified=0 $cparent -- $new_path]
1086 } else {
1087 set diffcmd [list diff-tree --unified=0 $cparent $cmit -- $new_path]
1089 if {[catch {set fd [eval git_read $diffcmd]} err]} {
1090 $status stop [mc "Unable to display parent"]
1091 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
1092 return
1095 set r_orig_line [lindex $dat 2]
1097 fconfigure $fd \
1098 -blocking 0 \
1099 -encoding binary \
1100 -translation binary
1101 fileevent $fd readable [cb _read_diff_load_commit \
1102 $fd $cparent $new_path $r_orig_line]
1103 set current_fd $fd
1107 method _read_diff_load_commit {fd cparent new_path tline} {
1108 if {$fd ne $current_fd} {
1109 catch {close $fd}
1110 return
1113 while {[gets $fd line] >= 0} {
1114 if {[regexp {^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@} $line line \
1115 old_line osz old_size new_line nsz new_size]} {
1117 if {$osz eq {}} { set old_size 1 }
1118 if {$nsz eq {}} { set new_size 1 }
1120 if {$new_line <= $tline} {
1121 if {[expr {$new_line + $new_size}] > $tline} {
1122 # Target line within the hunk
1123 set line_shift [expr {
1124 ($new_size-$old_size)*($tline-$new_line)/$new_size
1126 } else {
1127 set line_shift [expr {$new_size-$old_size}]
1130 set r_orig_line [expr {$r_orig_line - $line_shift}]
1135 if {[eof $fd]} {
1136 close $fd;
1137 set current_fd {}
1139 _load_new_commit $this \
1140 $cparent \
1141 $new_path \
1142 [list $r_orig_line]
1144 } ifdeleted { catch {close $fd} }
1146 method _show_tooltip {cur_w pos} {
1147 if {$tooltip_wm ne {}} {
1148 _open_tooltip $this $cur_w
1149 } elseif {$tooltip_timer eq {}} {
1150 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
1154 method _open_tooltip {cur_w} {
1155 set tooltip_timer {}
1156 set pos_x [winfo pointerx $cur_w]
1157 set pos_y [winfo pointery $cur_w]
1158 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
1159 _hide_tooltip $this
1160 return
1163 if {$tooltip_wm ne "$cur_w.tooltip"} {
1164 _hide_tooltip $this
1166 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
1167 wm overrideredirect $tooltip_wm 1
1168 wm transient $tooltip_wm [winfo toplevel $cur_w]
1169 set tooltip_t $tooltip_wm.label
1170 text $tooltip_t \
1171 -takefocus 0 \
1172 -highlightthickness 0 \
1173 -relief flat \
1174 -borderwidth 0 \
1175 -wrap none \
1176 -background lightyellow \
1177 -foreground black
1178 $tooltip_t tag conf section_header -font font_uibold
1179 pack $tooltip_t
1180 } else {
1181 $tooltip_t conf -state normal
1182 $tooltip_t delete 0.0 end
1185 set pos @[join [list \
1186 [expr {$pos_x - [winfo rootx $cur_w]}] \
1187 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
1188 set lno [lindex [split [$cur_w index $pos] .] 0]
1189 if {$cur_w eq $w_amov} {
1190 set dat [lindex $amov_data $lno]
1191 set org {}
1192 } else {
1193 set dat [lindex $asim_data $lno]
1194 set org [lindex $amov_data $lno]
1197 if {$dat eq {}} {
1198 _hide_tooltip $this
1199 return
1202 set cmit [lindex $dat 0]
1203 set tooltip_commit [list $cmit]
1205 set author_name {}
1206 set summary {}
1207 set author_time {}
1208 catch {set author_name $header($cmit,author)}
1209 catch {set summary $header($cmit,summary)}
1210 catch {set author_time [format_date $header($cmit,author-time)]}
1212 $tooltip_t insert end "commit $cmit\n"
1213 $tooltip_t insert end "$author_name $author_time\n"
1214 $tooltip_t insert end "$summary"
1216 if {$org ne {} && [lindex $org 0] ne $cmit} {
1217 set save [$tooltip_t get 0.0 end]
1218 $tooltip_t delete 0.0 end
1220 set cmit [lindex $org 0]
1221 set file [lindex $org 1]
1222 lappend tooltip_commit $cmit
1224 set author_name {}
1225 set summary {}
1226 set author_time {}
1227 catch {set author_name $header($cmit,author)}
1228 catch {set summary $header($cmit,summary)}
1229 catch {set author_time [format_date $header($cmit,author-time)]}
1231 $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
1232 $tooltip_t insert end "commit $cmit\n"
1233 $tooltip_t insert end "$author_name $author_time\n"
1234 $tooltip_t insert end "$summary\n"
1236 if {$file ne $path} {
1237 $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
1238 $tooltip_t insert end "$file\n"
1241 $tooltip_t insert end "\n"
1242 $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
1243 $tooltip_t insert end $save
1246 $tooltip_t conf -state disabled
1247 _position_tooltip $this
1250 method _position_tooltip {} {
1251 set max_h [lindex [split [$tooltip_t index end] .] 0]
1252 set max_w 0
1253 for {set i 1} {$i <= $max_h} {incr i} {
1254 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
1255 if {$c > $max_w} {set max_w $c}
1257 $tooltip_t conf -width $max_w -height $max_h
1259 set req_w [winfo reqwidth $tooltip_t]
1260 set req_h [winfo reqheight $tooltip_t]
1261 set pos_x [expr {[winfo pointerx .] + 5}]
1262 set pos_y [expr {[winfo pointery .] + 10}]
1264 set g "${req_w}x${req_h}"
1265 if {$pos_x >= 0} {append g +}
1266 append g $pos_x
1267 if {$pos_y >= 0} {append g +}
1268 append g $pos_y
1270 wm geometry $tooltip_wm $g
1271 raise $tooltip_wm
1274 method _hide_tooltip {} {
1275 if {$tooltip_wm ne {}} {
1276 destroy $tooltip_wm
1277 set tooltip_wm {}
1278 set tooltip_commit {}
1280 if {$tooltip_timer ne {}} {
1281 after cancel $tooltip_timer
1282 set tooltip_timer {}
1286 method _resize {new_height} {
1287 set diff [expr {$new_height - $old_height}]
1288 if {$diff == 0} return
1290 set my [expr {[winfo height $w.file_pane] - 25}]
1291 set o [$w.file_pane sash coord 0]
1292 set ox [lindex $o 0]
1293 set oy [expr {[lindex $o 1] + $diff}]
1294 if {$oy < 0} {set oy 0}
1295 if {$oy > $my} {set oy $my}
1296 $w.file_pane sash place 0 $ox $oy
1298 set old_height $new_height