git-gui: add build tab
[git-gui/bertw.git] / lib / blame.tcl
blobe1267af25882cc6c716e52e73a8d1b1f47a4778d
1 # git-gui blame viewer
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 class blame {
6 image create photo ::blame::img_back_arrow -data {R0lGODlhDgAPAMZkAAFRCAJTCgNTCgNVCgNVCwFYCQNYCwBaBQJZCgBaCARZCwZaDwhdEAheEAdfDwdfFghjEQdmFwdtExODIxaKJReQLxqVKx2ZLSKbMimfOCihOCeiOCmiOSakODGsQTKsQy6tRjGuRDOvQjOxQjSzQzeyRzm0ST25U0K7UT69TkK9UkG/UULAUULAUkPBU0XDVEfDV0jDWEfFV0rEWUjFWEbGV0XHVknHWUnIWUvJW07IXlHMYVLNYlPPZ1TRZFfTa1nTaVzSbFrValrYcGPXc17ZblzabF7bbmHccV3dc1/db2LccmDdcF/dd2HecWbddmTgdmTidGTidWbidmbldmzjfGnleWrlem7lgmjof2/of2rqe27qfnPqg3buh3juh3rwinzyjIn/n4//n////////////////////////////////////////////////////////////////////////////////////////////////////////////////yH5BAEKAH8ALAAAAAAOAA8AAAeHgH+Cg385hIeDSIaIhD9jOIyDSlJiM5F/S1pVTTcxKywqNQuCRlxTUUJhYF9YUFsQf0ddV09EPjopJh8aQw8eVFZTRUE7MCglIR1ODH8YRkxAPDIvJyQeGUkNghU9QDQjIBscFxY2DoMULi1eBwgBAwIABIQTIlkJl4ISLwX5ghEG/AlSgCgQADs=}
8 # Persistent 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 gotoline ; # line goto mini-dialog frame
26 field status ; # status mega-widget instance
27 field old_height ; # last known height of $w.file_pane
30 # Tk UI colors
32 variable active_color #c0edc5
33 variable group_colors {
34 #d6d6d6
35 #e1e1e1
36 #ececec
39 # Current blame data; cleared/reset on each load
41 field commit ; # input commit to blame
42 field path ; # input filename to view in $commit
44 field current_fd {} ; # background process running
45 field highlight_line -1 ; # current line selected
46 field highlight_column {} ; # current commit column selected
47 field highlight_commit {} ; # sha1 of commit selected
49 field total_lines 0 ; # total length of file
50 field blame_lines 0 ; # number of lines computed
51 field amov_data ; # list of {commit origfile origline}
52 field asim_data ; # list of {commit origfile origline}
54 field r_commit ; # commit currently being parsed
55 field r_orig_line ; # original line number
56 field r_final_line ; # final line number
57 field r_line_count ; # lines in this region
59 field tooltip_wm {} ; # Current tooltip toplevel, if open
60 field tooltip_t {} ; # Text widget in $tooltip_wm
61 field tooltip_timer {} ; # Current timer event for our tooltip
62 field tooltip_commit {} ; # Commit(s) in tooltip
64 constructor embed {i_w i_status i_commit i_path i_jump} {
66 set w $i_w
67 set status $i_status
69 _init $this $i_commit $i_path
71 foreach i [list $w $w_amov $w_asim $w_line $w_file $w_cviewer [$finder editor] [$gotoline editor]] {
72 bind $i <F7> [cb _show_finder]
73 bind $i <$::M1B-Key-f> [cb _show_finder]
74 bind $i <Escape> [list $finder hide]
75 bind $i <F3> [list $finder find_next]
76 bind $i <Shift-F3> [list $finder find_prev]
77 bind $i <F8> [cb _show_linebar]
78 bind $i <$::M1B-Key-g> [cb _show_linebar]
81 grid columnconfigure $w 0 -weight 1
82 grid rowconfigure $w 0 -weight 0
83 grid rowconfigure $w 1 -weight 1
84 grid rowconfigure $w 2 -weight 0
86 _load $this $i_jump
88 return $this
91 constructor new {i_commit i_path i_jump} {
92 global use_ttk
94 make_toplevel top w
95 wm title $top [append "[appname] ([reponame]): " [mc "File Viewer"]]
97 set status [::status_bar::new $w.status]
99 set font_w [font measure font_diff "0"]
101 _init $this $i_commit $i_path
103 grid configure $w.status -sticky ew
104 grid columnconfigure $top 0 -weight 1
105 grid rowconfigure $top 0 -weight 0
106 grid rowconfigure $top 1 -weight 1
107 grid rowconfigure $top 2 -weight 0
109 bind $top <F7> [cb _show_finder]
110 bind $top <Key-slash> [cb _show_finder]
111 bind $top <$::M1B-Key-s> [cb _show_finder]
112 bind $top <$::M1B-Key-f> [cb _show_finder]
113 bind $top <Escape> [list searchbar::hide $finder]
114 bind $top <F3> [list searchbar::find_next $finder]
115 bind $top <Shift-F3> [list searchbar::find_prev $finder]
116 bind $top <F8> [cb _show_linebar]
117 bind $top <$::M1B-Key-g> [cb _show_linebar]
118 catch { bind $top <Shift-Key-XF86_Switch_VT_3> [list searchbar::find_prev $finder] }
120 set req_w [winfo reqwidth $top]
121 set req_h [winfo reqheight $top]
122 set scr_w [expr {[winfo screenwidth $top] - 40}]
123 set scr_h [expr {[winfo screenheight $top] - 120}]
124 set opt_w [expr {$font_w * (80 + 5*3 + 3)}]
125 if {$req_w < $opt_w} {set req_w $opt_w}
126 if {$req_w > $scr_w} {set req_w $scr_w}
127 set opt_h [expr {$req_w*4/3}]
128 if {$req_h < $scr_h} {set req_h $scr_h}
129 if {$req_h > $opt_h} {set req_h $opt_h}
130 set g "${req_w}x${req_h}"
131 wm geometry $top $g
132 update
134 if {!$use_ttk} {
135 set old_height [winfo height $w.file_pane]
136 $w.file_pane sash place 0 \
137 [lindex [$w.file_pane sash coord 0] 0] \
138 [expr {int($old_height * 0.80)}]
139 bind $w.file_pane <Configure> \
140 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
143 wm protocol $top WM_DELETE_WINDOW "destroy $top"
144 bind $top <Destroy> [cb _handle_destroy %W]
146 _load $this $i_jump
149 method _init {i_commit i_path} {
150 global cursor_ptr M1B M1T have_tk85 use_ttk NS
151 variable active_color
152 variable group_colors
154 set commit $i_commit
155 set path $i_path
157 gold_frame $w.header
158 tlabel $w.header.commit_l \
159 -text [mc "Commit:"] \
160 -background gold \
161 -foreground black \
162 -anchor w \
163 -justify left
164 set w_back $w.header.commit_b
165 tlabel $w_back \
166 -image ::blame::img_back_arrow \
167 -borderwidth 0 \
168 -relief flat \
169 -state disabled \
170 -background gold \
171 -foreground black \
172 -activebackground gold
173 bind $w_back <Button-1> "
174 if {\[$w_back cget -state\] eq {normal}} {
175 [cb _history_menu]
178 tlabel $w.header.commit \
179 -textvariable @commit \
180 -background gold \
181 -foreground black \
182 -anchor w \
183 -justify left
184 tlabel $w.header.path_l \
185 -text [mc "File:"] \
186 -background gold \
187 -foreground black \
188 -anchor w \
189 -justify left
190 set w_path $w.header.path
191 tlabel $w_path \
192 -background gold \
193 -foreground black \
194 -anchor w \
195 -justify left
196 pack $w.header.commit_l -side left
197 pack $w_back -side left
198 pack $w.header.commit -side left
199 pack $w_path -fill x -side right
200 pack $w.header.path_l -side right
202 ${NS}::panedwindow $w.file_pane -orient vertical
203 frame $w.file_pane.out -relief flat -borderwidth 1
204 frame $w.file_pane.cm -relief sunken -borderwidth 1
205 if {$use_ttk} {
206 $w.file_pane add $w.file_pane.out \
207 -weight 3
208 $w.file_pane add $w.file_pane.cm \
209 -weight 1
210 } else {
211 $w.file_pane configure -borderwidth 0 -sashwidth 3
212 $w.file_pane add $w.file_pane.out \
213 -sticky nsew \
214 -minsize 100 \
215 -height 400 \
216 -width 100
217 $w.file_pane add $w.file_pane.cm \
218 -sticky nsew \
219 -minsize 25 \
220 -height 25 \
221 -width 100
224 set w_line $w.file_pane.out.linenumber_t
225 text $w_line \
226 -takefocus 0 \
227 -highlightthickness 0 \
228 -padx 0 -pady 0 \
229 -background white \
230 -foreground black \
231 -borderwidth 0 \
232 -state disabled \
233 -wrap none \
234 -height 40 \
235 -width 6 \
236 -font font_diff
237 $w_line tag conf linenumber -justify right -rmargin 5
239 set w_amov $w.file_pane.out.amove_t
240 text $w_amov \
241 -takefocus 0 \
242 -highlightthickness 0 \
243 -padx 0 -pady 0 \
244 -background white \
245 -foreground black \
246 -borderwidth 0 \
247 -state disabled \
248 -wrap none \
249 -height 40 \
250 -width 5 \
251 -font font_diff
252 $w_amov tag conf author_abbr -justify right -rmargin 5
253 $w_amov tag conf curr_commit
254 $w_amov tag conf prior_commit -foreground blue -underline 1
255 $w_amov tag bind prior_commit \
256 <Button-1> \
257 "[cb _load_commit $w_amov @amov_data @%x,%y];break"
259 set w_asim $w.file_pane.out.asimple_t
260 text $w_asim \
261 -takefocus 0 \
262 -highlightthickness 0 \
263 -padx 0 -pady 0 \
264 -background white \
265 -foreground black \
266 -borderwidth 0 \
267 -state disabled \
268 -wrap none \
269 -height 40 \
270 -width 4 \
271 -font font_diff
272 $w_asim tag conf author_abbr -justify right
273 $w_asim tag conf curr_commit
274 $w_asim tag conf prior_commit -foreground blue -underline 1
275 $w_asim tag bind prior_commit \
276 <Button-1> \
277 "[cb _load_commit $w_asim @asim_data @%x,%y];break"
279 set w_file $w.file_pane.out.file_t
280 text $w_file \
281 -takefocus 0 \
282 -highlightthickness 0 \
283 -padx 0 -pady 0 \
284 -background white \
285 -foreground black \
286 -borderwidth 0 \
287 -state disabled \
288 -wrap none \
289 -height 40 \
290 -width 80 \
291 -xscrollcommand [list $w.file_pane.out.sbx set] \
292 -font font_diff
293 if {$have_tk85} {
294 $w_file configure -inactiveselectbackground darkblue
296 $w_file tag conf found \
297 -background yellow
299 set w_columns [list $w_amov $w_asim $w_line $w_file]
301 delegate_sel_to $w_file [list $w_amov $w_asim $w_line]
303 ${NS}::scrollbar $w.file_pane.out.sbx \
304 -orient h \
305 -command [list $w_file xview]
306 ${NS}::scrollbar $w.file_pane.out.sby \
307 -orient v \
308 -command [list scrollbar2many $w_columns yview]
309 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
310 grid conf \
311 $w.file_pane.out.sbx \
312 -column 0 \
313 -columnspan [expr {[llength $w_columns] + 1}] \
314 -sticky we
315 grid columnconfigure \
316 $w.file_pane.out \
317 [expr {[llength $w_columns] - 1}] \
318 -weight 1
319 grid rowconfigure $w.file_pane.out 0 -weight 1
321 set finder [::searchbar::new \
322 $w.file_pane.out.ff $w_file $w_file \
323 -column 0 \
324 -columnspan [expr {[llength $w_columns] + 1}] \
327 set gotoline [::linebar::new \
328 $w.file_pane.out.lf $w_file \
329 -column 0 \
330 -columnspan [expr {[llength $w_columns] + 1}] \
333 set w_cviewer $w.file_pane.cm.t
334 text $w_cviewer \
335 -background white \
336 -foreground black \
337 -borderwidth 0 \
338 -state disabled \
339 -wrap none \
340 -height 10 \
341 -width 80 \
342 -xscrollcommand [list $w.file_pane.cm.sbx set] \
343 -yscrollcommand [list $w.file_pane.cm.sby set] \
344 -font font_diff
345 $w_cviewer tag conf still_loading \
346 -font font_uiitalic \
347 -justify center
348 $w_cviewer tag conf header_key \
349 -tabs {3c} \
350 -background $active_color \
351 -font font_uibold
352 $w_cviewer tag conf header_val \
353 -background $active_color \
354 -font font_ui
355 $w_cviewer tag raise sel
356 ${NS}::scrollbar $w.file_pane.cm.sbx \
357 -orient h \
358 -command [list $w_cviewer xview]
359 ${NS}::scrollbar $w.file_pane.cm.sby \
360 -orient v \
361 -command [list $w_cviewer yview]
362 pack $w.file_pane.cm.sby -side right -fill y
363 pack $w.file_pane.cm.sbx -side bottom -fill x
364 pack $w_cviewer -expand 1 -fill both
366 menu $w.ctxm -tearoff 0
367 $w.ctxm add command \
368 -label [mc "Copy Commit"] \
369 -command [cb _copycommit]
370 $w.ctxm add separator
371 $w.ctxm add command \
372 -label [mc "Find Text..."] \
373 -accelerator F7 \
374 -command [cb _show_finder]
375 $w.ctxm add command \
376 -label [mc "Goto Line..."] \
377 -accelerator "Ctrl-G" \
378 -command [cb _show_linebar]
379 menu $w.ctxm.enc
380 build_encoding_menu $w.ctxm.enc [cb _setencoding]
381 $w.ctxm add cascade \
382 -label [mc "Encoding"] \
383 -menu $w.ctxm.enc
384 $w.ctxm add command \
385 -label [mc "Do Full Copy Detection"] \
386 -command [cb _fullcopyblame]
387 $w.ctxm add separator
388 $w.ctxm add command \
389 -label [mc "Show History Context"] \
390 -command [cb _gitkcommit]
391 $w.ctxm add command \
392 -label [mc "Blame Parent Commit"] \
393 -command [cb _blameparent]
395 foreach i $w_columns {
396 for {set g 0} {$g < [llength $group_colors]} {incr g} {
397 $i tag conf color$g -background [lindex $group_colors $g]
400 if {$i eq $w_file} {
401 $w_file tag raise found
403 $i tag raise sel
405 $i conf -cursor $cursor_ptr
406 $i conf -yscrollcommand \
407 "[list $finder scrolled]
408 [list many2scrollbar $w_columns yview $w.file_pane.out.sby]"
410 set bind_cmd bind
411 if {$i ne $w_file} {
412 set bind_cmd delegator_bind
414 $bind_cmd $i <Button-1> "
415 [cb _hide_tooltip]
416 [cb _click $i @%x,%y]
417 focus $i
420 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
421 bind $i <Any-Enter> [cb _hide_tooltip]
422 bind $i <Any-Leave> [cb _hide_tooltip]
423 bind_button3 $i "
424 [cb _hide_tooltip]
425 set cursorX %x
426 set cursorY %y
427 set cursorW %W
428 tk_popup $w.ctxm %X %Y
430 bind $i <Shift-Tab> "[list focus $w_cviewer];break"
431 bind $i <Tab> "[cb _focus_search $w_cviewer];break"
434 foreach i [concat $w_columns $w_cviewer] {
435 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
436 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
437 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
438 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
439 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
440 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
441 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
442 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
443 bind $i <$::M1B-Key-b> {catch {%W yview scroll -1 pages};break}
444 bind $i <$::M1B-Key-f> {catch {%W yview scroll 1 pages};break}
447 bind $w_cviewer <Shift-Tab> "[cb _focus_search $w_file];break"
448 bind $w_cviewer <Tab> "[list focus $w_file];break"
449 bind $w_cviewer <Button-1> [list focus $w_cviewer]
450 bind $w_file <Visibility> [cb _focus_search $w_file]
452 grid configure $w.header -sticky ew
453 grid configure $w.file_pane -sticky nsew
456 method _focus_search {win} {
457 if {[searchbar::visible $finder]} {
458 focus [searchbar::editor $finder]
459 } else {
460 focus $win
464 method _handle_destroy {win} {
465 if {$win eq $w} {
466 _kill $this
467 delete_this
471 method _kill {} {
472 if {$current_fd ne {}} {
473 kill_file_process $current_fd
474 catch {close $current_fd}
475 set current_fd {}
479 method _load {jump} {
480 variable group_colors
482 _hide_tooltip $this
484 if {$total_lines != 0 || $current_fd ne {}} {
485 _kill $this
487 foreach i $w_columns {
488 $i conf -state normal
489 $i delete 0.0 end
490 foreach g [$i tag names] {
491 if {[regexp {^g[0-9a-f]{40}$} $g]} {
492 $i tag delete $g
495 $i conf -state disabled
498 $w_cviewer conf -state normal
499 $w_cviewer delete 0.0 end
500 $w_cviewer conf -state disabled
502 set highlight_line -1
503 set highlight_column {}
504 set highlight_commit {}
505 set total_lines 0
508 if {$history eq {}} {
509 $w_back conf -state disabled
510 } else {
511 $w_back conf -state normal
514 # Index 0 is always empty. There is never line 0 as
515 # we use only 1 based lines, as that matches both with
516 # git-blame output and with Tk's text widget.
518 set amov_data [list [list]]
519 set asim_data [list [list]]
521 $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
522 $w_path conf -text [escape_path $path]
524 set do_textconv 0
525 if {![is_config_false gui.textconv] && [git-version >= 1.7.2]} {
526 set filter [gitattr $path diff set]
527 set textconv [get_config [join [list diff $filter textconv] .]]
528 if {$filter ne {set} && $textconv ne {}} {
529 set do_textconv 1
532 if {$commit eq {}} {
533 if {$do_textconv ne 0} {
534 set fd [open_cmd_pipe $textconv $path]
535 } else {
536 set fd [open $path r]
538 fconfigure $fd -eofchar {}
539 } else {
540 if {$do_textconv ne 0} {
541 set fd [git_read cat-file --textconv "$commit:$path"]
542 } else {
543 set fd [git_read cat-file blob "$commit:$path"]
546 fconfigure $fd \
547 -blocking 0 \
548 -translation lf \
549 -encoding [get_path_encoding $path]
550 fileevent $fd readable [cb _read_file $fd $jump]
551 set current_fd $fd
554 method _history_menu {} {
555 set m $w.backmenu
556 if {[winfo exists $m]} {
557 $m delete 0 end
558 } else {
559 menu $m -tearoff 0
562 for {set i [expr {[llength $history] - 1}]
563 } {$i >= 0} {incr i -1} {
564 set e [lindex $history $i]
565 set c [lindex $e 0]
566 set f [lindex $e 1]
568 if {[regexp {^[0-9a-f]{40}$} $c]} {
569 set t [string range $c 0 8]...
570 } elseif {$c eq {}} {
571 set t {Working Directory}
572 } else {
573 set t $c
575 if {![catch {set summary $header($c,summary)}]} {
576 append t " $summary"
577 if {[string length $t] > 70} {
578 set t [string range $t 0 66]...
582 $m add command -label $t -command [cb _goback $i]
584 set X [winfo rootx $w_back]
585 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
586 tk_popup $m $X $Y
589 method _goback {i} {
590 set dat [lindex $history $i]
591 set history [lrange $history 0 [expr {$i - 1}]]
592 set commit [lindex $dat 0]
593 set path [lindex $dat 1]
594 _load $this [lrange $dat 2 5]
597 method _read_file {fd jump} {
598 if {$fd ne $current_fd} {
599 catch {close $fd}
600 return
603 foreach i $w_columns {$i conf -state normal}
604 while {[gets $fd line] >= 0} {
605 regsub "\r\$" $line {} line
606 incr total_lines
607 lappend amov_data {}
608 lappend asim_data {}
610 if {$total_lines > 1} {
611 foreach i $w_columns {$i insert end "\n"}
614 $w_line insert end "$total_lines" linenumber
615 $w_file insert end "$line"
618 set ln_wc [expr {[string length $total_lines] + 2}]
619 if {[$w_line cget -width] < $ln_wc} {
620 $w_line conf -width $ln_wc
623 foreach i $w_columns {$i conf -state disabled}
625 if {[eof $fd]} {
626 fconfigure $fd -blocking 1; # enable error reporting on close
627 if {[catch {close $fd} err]} {
628 tk_messageBox -icon error -title [mc Error] \
629 -message $err
632 # If we don't force Tk to update the widgets *right now*
633 # none of our jump commands will cause a change in the UI.
635 update
637 if {[llength $jump] == 1} {
638 set highlight_line [lindex $jump 0]
639 $w_file see "$highlight_line.0"
640 } elseif {[llength $jump] == 4} {
641 set highlight_column [lindex $jump 0]
642 set highlight_line [lindex $jump 1]
643 $w_file xview moveto [lindex $jump 2]
644 $w_file yview moveto [lindex $jump 3]
647 _exec_blame $this $w_asim @asim_data \
648 [list] \
649 [mc "Loading copy/move tracking annotations..."]
651 } ifdeleted { catch {close $fd} }
653 method _exec_blame {cur_w cur_d options cur_s} {
654 lappend options --incremental --encoding=utf-8
655 if {$commit eq {}} {
656 lappend options --contents $path
657 } else {
658 lappend options $commit
660 lappend options -- $path
661 set fd [eval git_read --nice blame $options]
662 fconfigure $fd -blocking 0 -translation lf -encoding utf-8
663 fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
664 set current_fd $fd
665 set blame_lines 0
667 $status start \
668 $cur_s \
669 [mc "lines annotated"]
672 method _read_blame {fd cur_w cur_d} {
673 upvar #0 $cur_d line_data
674 variable group_colors
676 if {$fd ne $current_fd} {
677 catch {close $fd}
678 return
681 $cur_w conf -state normal
682 while {[gets $fd line] >= 0} {
683 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
684 cmit original_line final_line line_count]} {
685 set r_commit $cmit
686 set r_orig_line $original_line
687 set r_final_line $final_line
688 set r_line_count $line_count
689 } elseif {[string match {filename *} $line]} {
690 set file [string range $line 9 end]
691 set n $r_line_count
692 set lno $r_final_line
693 set oln $r_orig_line
694 set cmit $r_commit
696 if {[regexp {^0{40}$} $cmit]} {
697 set commit_abbr work
698 set commit_type curr_commit
699 } elseif {$cmit eq $commit} {
700 set commit_abbr this
701 set commit_type curr_commit
702 } else {
703 set commit_type prior_commit
704 set commit_abbr [string range $cmit 0 3]
707 set author_abbr {}
708 set a_name {}
709 catch {set a_name $header($cmit,author)}
710 while {$a_name ne {}} {
711 if {$author_abbr ne {}
712 && [string index $a_name 0] eq {'}} {
713 regsub {^'[^']+'\s+} $a_name {} a_name
715 if {![regexp {^([[:upper:]])} $a_name _a]} break
716 append author_abbr $_a
717 unset _a
718 if {![regsub \
719 {^[[:upper:]][^\s]*\s+} \
720 $a_name {} a_name ]} break
722 if {$author_abbr eq {}} {
723 set author_abbr { |}
724 } else {
725 set author_abbr [string range $author_abbr 0 3]
727 unset a_name
729 set first_lno $lno
730 while {
731 $first_lno > 1
732 && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
733 && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
735 incr first_lno -1
738 set color {}
739 if {$first_lno < $lno} {
740 foreach g [$w_file tag names $first_lno.0] {
741 if {[regexp {^color[0-9]+$} $g]} {
742 set color $g
743 break
746 } else {
747 set i [lsort [concat \
748 [$w_file tag names "[expr {$first_lno - 1}].0"] \
749 [$w_file tag names "[expr {$lno + $n}].0"] \
751 for {set g 0} {$g < [llength $group_colors]} {incr g} {
752 if {[lsearch -sorted -exact $i color$g] == -1} {
753 set color color$g
754 break
758 if {$color eq {}} {
759 set color color0
762 while {$n > 0} {
763 set lno_e "$lno.0 lineend + 1c"
764 if {[lindex $line_data $lno] ne {}} {
765 set g [lindex $line_data $lno 0]
766 foreach i $w_columns {
767 $i tag remove g$g $lno.0 $lno_e
770 lset line_data $lno [list $cmit $file $oln]
772 $cur_w delete $lno.0 "$lno.0 lineend"
773 if {$lno == $first_lno} {
774 $cur_w insert $lno.0 $commit_abbr $commit_type
775 } elseif {$lno == [expr {$first_lno + 1}]} {
776 $cur_w insert $lno.0 $author_abbr author_abbr
777 } else {
778 $cur_w insert $lno.0 { |}
781 foreach i $w_columns {
782 if {$cur_w eq $w_amov} {
783 for {set g 0} \
784 {$g < [llength $group_colors]} \
785 {incr g} {
786 $i tag remove color$g $lno.0 $lno_e
788 $i tag add $color $lno.0 $lno_e
790 $i tag add g$cmit $lno.0 $lno_e
793 if {$highlight_column eq $cur_w} {
794 if {$highlight_line == -1
795 && [lindex [$w_file yview] 0] == 0} {
796 $w_file see $lno.0
797 set highlight_line $lno
799 if {$highlight_line == $lno} {
800 _showcommit $this $cur_w $lno
804 incr n -1
805 incr lno
806 incr oln
807 incr blame_lines
810 while {
811 $cmit eq [lindex $line_data $lno 0]
812 && $file eq [lindex $line_data $lno 1]
814 $cur_w delete $lno.0 "$lno.0 lineend"
816 if {$lno == $first_lno} {
817 $cur_w insert $lno.0 $commit_abbr $commit_type
818 } elseif {$lno == [expr {$first_lno + 1}]} {
819 $cur_w insert $lno.0 $author_abbr author_abbr
820 } else {
821 $cur_w insert $lno.0 { |}
824 if {$cur_w eq $w_amov} {
825 foreach i $w_columns {
826 for {set g 0} \
827 {$g < [llength $group_colors]} \
828 {incr g} {
829 $i tag remove color$g $lno.0 $lno_e
831 $i tag add $color $lno.0 $lno_e
835 incr lno
838 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
839 set header($r_commit,$key) $data
842 $cur_w conf -state disabled
844 if {[eof $fd]} {
845 close $fd
846 if {$cur_w eq $w_asim} {
847 # Switches for original location detection
848 set threshold [get_config gui.copyblamethreshold]
849 set original_options [list "-C$threshold"]
851 if {![is_config_true gui.fastcopyblame]} {
852 # thorough copy search; insert before the threshold
853 set original_options [linsert $original_options 0 -C]
855 if {[git-version >= 1.5.3]} {
856 lappend original_options -w ; # ignore indentation changes
859 _exec_blame $this $w_amov @amov_data \
860 $original_options \
861 [mc "Loading original location annotations..."]
862 } else {
863 set current_fd {}
864 $status stop [mc "Annotation complete."]
866 } else {
867 $status update $blame_lines $total_lines
869 } ifdeleted { catch {close $fd} }
871 method _find_commit_bound {data_list start_idx delta} {
872 upvar #0 $data_list line_data
873 set pos $start_idx
874 set limit [expr {[llength $line_data] - 1}]
875 set base_commit [lindex $line_data $pos 0]
877 while {$pos > 0 && $pos < $limit} {
878 set new_pos [expr {$pos + $delta}]
879 if {[lindex $line_data $new_pos 0] ne $base_commit} {
880 return $pos
883 set pos $new_pos
886 return $pos
889 method _fullcopyblame {} {
890 if {$current_fd ne {}} {
891 tk_messageBox \
892 -icon error \
893 -type ok \
894 -title [mc "Busy"] \
895 -message [mc "Annotation process is already running."]
897 return
900 # Switches for original location detection
901 set threshold [get_config gui.copyblamethreshold]
902 set original_options [list -C -C "-C$threshold"]
904 if {[git-version >= 1.5.3]} {
905 lappend original_options -w ; # ignore indentation changes
908 # Find the line range
909 set pos @$::cursorX,$::cursorY
910 set lno [lindex [split [$::cursorW index $pos] .] 0]
911 set min_amov_lno [_find_commit_bound $this @amov_data $lno -1]
912 set max_amov_lno [_find_commit_bound $this @amov_data $lno 1]
913 set min_asim_lno [_find_commit_bound $this @asim_data $lno -1]
914 set max_asim_lno [_find_commit_bound $this @asim_data $lno 1]
916 if {$min_asim_lno < $min_amov_lno} {
917 set min_amov_lno $min_asim_lno
920 if {$max_asim_lno > $max_amov_lno} {
921 set max_amov_lno $max_asim_lno
924 lappend original_options -L "$min_amov_lno,$max_amov_lno"
926 # Clear lines
927 for {set i $min_amov_lno} {$i <= $max_amov_lno} {incr i} {
928 lset amov_data $i [list ]
931 # Start the back-end process
932 _exec_blame $this $w_amov @amov_data \
933 $original_options \
934 [mc "Running thorough copy detection..."]
937 method _click {cur_w pos} {
938 set lno [lindex [split [$cur_w index $pos] .] 0]
939 _showcommit $this $cur_w $lno
942 method _setencoding {enc} {
943 force_path_encoding $path $enc
944 _load $this [list \
945 $highlight_column \
946 $highlight_line \
947 [lindex [$w_file xview] 0] \
948 [lindex [$w_file yview] 0] \
952 method _load_commit {cur_w cur_d pos} {
953 upvar #0 $cur_d line_data
954 set lno [lindex [split [$cur_w index $pos] .] 0]
955 set dat [lindex $line_data $lno]
956 if {$dat ne {}} {
957 _load_new_commit $this \
958 [lindex $dat 0] \
959 [lindex $dat 1] \
960 [list [lindex $dat 2]]
964 method _load_new_commit {new_commit new_path jump} {
965 lappend history [list \
966 $commit $path \
967 $highlight_column \
968 $highlight_line \
969 [lindex [$w_file xview] 0] \
970 [lindex [$w_file yview] 0] \
973 set commit $new_commit
974 set path $new_path
975 _load $this $jump
978 method _showcommit {cur_w lno} {
979 global repo_config
980 variable active_color
982 if {$highlight_commit ne {}} {
983 foreach i $w_columns {
984 $i tag conf g$highlight_commit -background {}
985 $i tag lower g$highlight_commit
989 if {$cur_w eq $w_asim} {
990 set dat [lindex $asim_data $lno]
991 set highlight_column $w_asim
992 } else {
993 set dat [lindex $amov_data $lno]
994 set highlight_column $w_amov
997 $w_cviewer conf -state normal
998 $w_cviewer delete 0.0 end
1000 if {$dat eq {}} {
1001 set cmit {}
1002 $w_cviewer insert end [mc "Loading annotation..."] still_loading
1003 } else {
1004 set cmit [lindex $dat 0]
1005 set file [lindex $dat 1]
1007 foreach i $w_columns {
1008 $i tag conf g$cmit -background $active_color
1009 $i tag raise g$cmit
1010 if {$i eq $w_file} {
1011 $w_file tag raise found
1013 $i tag raise sel
1016 set author_name {}
1017 set author_email {}
1018 set author_time {}
1019 catch {set author_name $header($cmit,author)}
1020 catch {set author_email $header($cmit,author-mail)}
1021 catch {set author_time [format_date $header($cmit,author-time)]}
1023 set committer_name {}
1024 set committer_email {}
1025 set committer_time {}
1026 catch {set committer_name $header($cmit,committer)}
1027 catch {set committer_email $header($cmit,committer-mail)}
1028 catch {set committer_time [format_date $header($cmit,committer-time)]}
1030 if {[catch {set msg $header($cmit,message)}]} {
1031 set msg {}
1032 catch {
1033 set fd [git_read cat-file commit $cmit]
1034 fconfigure $fd -encoding binary -translation lf
1035 # By default commits are assumed to be in utf-8
1036 set enc utf-8
1037 while {[gets $fd line] > 0} {
1038 if {[string match {encoding *} $line]} {
1039 set enc [string tolower [string range $line 9 end]]
1042 set msg [read $fd]
1043 close $fd
1045 set enc [tcl_encoding $enc]
1046 if {$enc ne {}} {
1047 set msg [encoding convertfrom $enc $msg]
1049 set msg [string trim $msg]
1051 set header($cmit,message) $msg
1054 $w_cviewer insert end "commit $cmit\n" header_key
1055 $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
1056 $w_cviewer insert end "$author_name $author_email" header_val
1057 $w_cviewer insert end " $author_time\n" header_val
1059 $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
1060 $w_cviewer insert end "$committer_name $committer_email" header_val
1061 $w_cviewer insert end " $committer_time\n" header_val
1063 if {$file ne $path} {
1064 $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
1065 $w_cviewer insert end "[escape_path $file]\n" header_val
1068 $w_cviewer insert end "\n$msg"
1070 $w_cviewer conf -state disabled
1072 set highlight_line $lno
1073 set highlight_commit $cmit
1075 if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
1076 _hide_tooltip $this
1080 method _get_click_amov_info {} {
1081 set pos @$::cursorX,$::cursorY
1082 set lno [lindex [split [$::cursorW index $pos] .] 0]
1083 return [lindex $amov_data $lno]
1086 method _copycommit {} {
1087 set dat [_get_click_amov_info $this]
1088 if {$dat ne {}} {
1089 clipboard clear
1090 clipboard append \
1091 -format STRING \
1092 -type STRING \
1093 -- [lindex $dat 0]
1097 method _format_offset_date {base offset} {
1098 set exval [expr {$base + $offset*24*60*60}]
1099 return [clock format $exval -format {%Y-%m-%d}]
1102 method _gitkcommit {} {
1103 global nullid
1105 set dat [_get_click_amov_info $this]
1106 if {$dat ne {}} {
1107 set cmit [lindex $dat 0]
1109 # If the line belongs to the working copy, use HEAD instead
1110 if {$cmit eq $nullid} {
1111 if {[catch {set cmit [git rev-parse --verify HEAD]} err]} {
1112 error_popup [strcat [mc "Cannot find HEAD commit:"] "\n\n$err"]
1113 return;
1117 set radius [get_config gui.blamehistoryctx]
1118 set cmdline [list --select-commit=$cmit]
1120 if {$radius > 0} {
1121 set author_time {}
1122 set committer_time {}
1124 catch {set author_time $header($cmit,author-time)}
1125 catch {set committer_time $header($cmit,committer-time)}
1127 if {$committer_time eq {}} {
1128 set committer_time $author_time
1131 set after_time [_format_offset_date $this $committer_time [expr {-$radius}]]
1132 set before_time [_format_offset_date $this $committer_time $radius]
1134 lappend cmdline --after=$after_time --before=$before_time
1137 lappend cmdline $cmit
1139 set base_rev "HEAD"
1140 if {$commit ne {}} {
1141 set base_rev $commit
1144 if {$base_rev ne $cmit} {
1145 lappend cmdline $base_rev
1148 do_gitk $cmdline
1152 method _blameparent {} {
1153 global nullid
1155 set dat [_get_click_amov_info $this]
1156 if {$dat ne {}} {
1157 set cmit [lindex $dat 0]
1158 set new_path [lindex $dat 1]
1160 # Allow using Blame Parent on lines modified in the working copy
1161 if {$cmit eq $nullid} {
1162 set parent_ref "HEAD"
1163 } else {
1164 set parent_ref "$cmit^"
1166 if {[catch {set cparent [git rev-parse --verify $parent_ref]} err]} {
1167 error_popup [strcat [mc "Cannot find parent commit:"] "\n\n$err"]
1168 return;
1171 _kill $this
1173 # Generate a diff between the commit and its parent,
1174 # and use the hunks to update the line number.
1175 # Request zero context to simplify calculations.
1176 if {$cmit eq $nullid} {
1177 set diffcmd [list diff-index --unified=0 $cparent -- $new_path]
1178 } else {
1179 set diffcmd [list diff-tree --unified=0 $cparent $cmit -- $new_path]
1181 if {[catch {set fd [eval git_read $diffcmd]} err]} {
1182 $status stop [mc "Unable to display parent"]
1183 error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
1184 return
1187 set r_orig_line [lindex $dat 2]
1189 fconfigure $fd \
1190 -blocking 0 \
1191 -encoding binary \
1192 -translation binary
1193 fileevent $fd readable [cb _read_diff_load_commit \
1194 $fd $cparent $new_path $r_orig_line]
1195 set current_fd $fd
1199 method _read_diff_load_commit {fd cparent new_path tline} {
1200 if {$fd ne $current_fd} {
1201 catch {close $fd}
1202 return
1205 while {[gets $fd line] >= 0} {
1206 if {[regexp {^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@} $line line \
1207 old_line osz old_size new_line nsz new_size]} {
1209 if {$osz eq {}} { set old_size 1 }
1210 if {$nsz eq {}} { set new_size 1 }
1212 if {$new_line <= $tline} {
1213 if {[expr {$new_line + $new_size}] > $tline} {
1214 # Target line within the hunk
1215 set line_shift [expr {
1216 ($new_size-$old_size)*($tline-$new_line)/$new_size
1218 } else {
1219 set line_shift [expr {$new_size-$old_size}]
1222 set r_orig_line [expr {$r_orig_line - $line_shift}]
1227 if {[eof $fd]} {
1228 close $fd
1229 set current_fd {}
1231 _load_new_commit $this \
1232 $cparent \
1233 $new_path \
1234 [list $r_orig_line]
1236 } ifdeleted { catch {close $fd} }
1238 method _show_tooltip {cur_w pos} {
1239 if {$tooltip_wm ne {}} {
1240 _open_tooltip $this $cur_w
1241 } elseif {$tooltip_timer eq {}} {
1242 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
1246 method _open_tooltip {cur_w} {
1247 set tooltip_timer {}
1248 set pos_x [winfo pointerx $cur_w]
1249 set pos_y [winfo pointery $cur_w]
1250 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
1251 _hide_tooltip $this
1252 return
1255 if {$tooltip_wm ne "$cur_w.tooltip"} {
1256 _hide_tooltip $this
1258 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
1259 catch {wm attributes $tooltip_wm -type tooltip}
1260 wm overrideredirect $tooltip_wm 1
1261 wm transient $tooltip_wm [winfo toplevel $cur_w]
1262 set tooltip_t $tooltip_wm.label
1263 text $tooltip_t \
1264 -takefocus 0 \
1265 -highlightthickness 0 \
1266 -relief flat \
1267 -borderwidth 0 \
1268 -wrap none \
1269 -background lightyellow \
1270 -foreground black
1271 $tooltip_t tag conf section_header -font font_uibold
1272 pack $tooltip_t
1273 } else {
1274 $tooltip_t conf -state normal
1275 $tooltip_t delete 0.0 end
1278 set pos @[join [list \
1279 [expr {$pos_x - [winfo rootx $cur_w]}] \
1280 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
1281 set lno [lindex [split [$cur_w index $pos] .] 0]
1282 if {$cur_w eq $w_amov} {
1283 set dat [lindex $amov_data $lno]
1284 set org {}
1285 } else {
1286 set dat [lindex $asim_data $lno]
1287 set org [lindex $amov_data $lno]
1290 if {$dat eq {}} {
1291 _hide_tooltip $this
1292 return
1295 set cmit [lindex $dat 0]
1296 set tooltip_commit [list $cmit]
1298 set author_name {}
1299 set summary {}
1300 set author_time {}
1301 catch {set author_name $header($cmit,author)}
1302 catch {set summary $header($cmit,summary)}
1303 catch {set author_time [format_date $header($cmit,author-time)]}
1305 $tooltip_t insert end "commit $cmit\n"
1306 $tooltip_t insert end "$author_name $author_time\n"
1307 $tooltip_t insert end "$summary"
1309 if {$org ne {} && [lindex $org 0] ne $cmit} {
1310 set save [$tooltip_t get 0.0 end]
1311 $tooltip_t delete 0.0 end
1313 set cmit [lindex $org 0]
1314 set file [lindex $org 1]
1315 lappend tooltip_commit $cmit
1317 set author_name {}
1318 set summary {}
1319 set author_time {}
1320 catch {set author_name $header($cmit,author)}
1321 catch {set summary $header($cmit,summary)}
1322 catch {set author_time [format_date $header($cmit,author-time)]}
1324 $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
1325 $tooltip_t insert end "commit $cmit\n"
1326 $tooltip_t insert end "$author_name $author_time\n"
1327 $tooltip_t insert end "$summary\n"
1329 if {$file ne $path} {
1330 $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
1331 $tooltip_t insert end "$file\n"
1334 $tooltip_t insert end "\n"
1335 $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
1336 $tooltip_t insert end $save
1339 $tooltip_t conf -state disabled
1340 _position_tooltip $this
1342 # On MacOS raising a window causes it to acquire focus.
1343 # Tk 8.5 on MacOS seems to properly support wm transient,
1344 # so we can safely counter the effect there.
1345 if {$::have_tk85 && [is_MacOSX]} {
1346 update
1347 if {$w eq {}} {
1348 raise .
1349 } else {
1350 raise $w
1355 method _position_tooltip {} {
1356 set max_h [lindex [split [$tooltip_t index end] .] 0]
1357 set max_w 0
1358 for {set i 1} {$i <= $max_h} {incr i} {
1359 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
1360 if {$c > $max_w} {set max_w $c}
1362 $tooltip_t conf -width $max_w -height $max_h
1364 set req_w [winfo reqwidth $tooltip_t]
1365 set req_h [winfo reqheight $tooltip_t]
1366 set pos_x [expr {[winfo pointerx .] + 5}]
1367 set pos_y [expr {[winfo pointery .] + 10}]
1369 set g "${req_w}x${req_h}"
1370 if {[tk windowingsystem] eq "win32" || $pos_x >= 0} {append g +}
1371 append g $pos_x
1372 if {[tk windowingsystem] eq "win32" || $pos_y >= 0} {append g +}
1373 append g $pos_y
1375 wm geometry $tooltip_wm $g
1376 if {![is_MacOSX]} {
1377 raise $tooltip_wm
1381 method _hide_tooltip {} {
1382 if {$tooltip_wm ne {}} {
1383 destroy $tooltip_wm
1384 set tooltip_wm {}
1385 set tooltip_commit {}
1387 if {$tooltip_timer ne {}} {
1388 after cancel $tooltip_timer
1389 set tooltip_timer {}
1393 method _resize {new_height} {
1394 set diff [expr {$new_height - $old_height}]
1395 if {$diff == 0} return
1397 set my [expr {[winfo height $w.file_pane] - 25}]
1398 set o [$w.file_pane sash coord 0]
1399 set ox [lindex $o 0]
1400 set oy [expr {[lindex $o 1] + $diff}]
1401 if {$oy < 0} {set oy 0}
1402 if {$oy > $my} {set oy $my}
1403 $w.file_pane sash place 0 $ox $oy
1405 set old_height $new_height
1408 method _show_finder {} {
1409 linebar::hide $gotoline
1410 searchbar::show $finder
1413 method _show_linebar {} {
1414 searchbar::hide $finder
1415 linebar::show $gotoline