Code

git-gui: Enable jumping to a specific line number in blame view.
[git.git] / lib / blame.tcl
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)
9 #
10 field history {}; # viewer history: {commit path}
11 field header    ; # array commit,key -> header field
13 # Tk UI control paths
14 #
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
31 #
32 variable active_color #c0edc5
33 variable group_colors {
34         #d6d6d6
35         #e1e1e1
36         #ececec
37 }
39 # Current blame data; cleared/reset on each load
40 #
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 new {i_commit i_path i_jump} {
65         global cursor_ptr M1B M1T have_tk85 use_ttk NS
66         variable active_color
67         variable group_colors
69         set commit $i_commit
70         set path   $i_path
72         make_toplevel top w
73         wm title $top [append "[appname] ([reponame]): " [mc "File Viewer"]]
75         set font_w [font measure font_diff "0"]
77         gold_frame $w.header
78         tlabel $w.header.commit_l \
79                 -text [mc "Commit:"] \
80                 -background gold \
81                 -foreground black \
82                 -anchor w \
83                 -justify left
84         set w_back $w.header.commit_b
85         tlabel $w_back \
86                 -image ::blame::img_back_arrow \
87                 -borderwidth 0 \
88                 -relief flat \
89                 -state disabled \
90                 -background gold \
91                 -foreground black \
92                 -activebackground gold
93         bind $w_back <Button-1> "
94                 if {\[$w_back cget -state\] eq {normal}} {
95                         [cb _history_menu]
96                 }
97                 "
98         tlabel $w.header.commit \
99                 -textvariable @commit \
100                 -background gold \
101                 -foreground black \
102                 -anchor w \
103                 -justify left
104         tlabel $w.header.path_l \
105                 -text [mc "File:"] \
106                 -background gold \
107                 -foreground black \
108                 -anchor w \
109                 -justify left
110         set w_path $w.header.path
111         tlabel $w_path \
112                 -background gold \
113                 -foreground black \
114                 -anchor w \
115                 -justify left
116         pack $w.header.commit_l -side left
117         pack $w_back -side left
118         pack $w.header.commit -side left
119         pack $w_path -fill x -side right
120         pack $w.header.path_l -side right
122         panedwindow $w.file_pane -orient vertical -borderwidth 0 -sashwidth 3
123         frame $w.file_pane.out -relief flat -borderwidth 1
124         frame $w.file_pane.cm -relief sunken -borderwidth 1
125         $w.file_pane add $w.file_pane.out \
126                 -sticky nsew \
127                 -minsize 100 \
128                 -height 100 \
129                 -width 100
130         $w.file_pane add $w.file_pane.cm \
131                 -sticky nsew \
132                 -minsize 25 \
133                 -height 25 \
134                 -width 100
136         set w_line $w.file_pane.out.linenumber_t
137         text $w_line \
138                 -takefocus 0 \
139                 -highlightthickness 0 \
140                 -padx 0 -pady 0 \
141                 -background white \
142                 -foreground black \
143                 -borderwidth 0 \
144                 -state disabled \
145                 -wrap none \
146                 -height 40 \
147                 -width 6 \
148                 -font font_diff
149         $w_line tag conf linenumber -justify right -rmargin 5
151         set w_amov $w.file_pane.out.amove_t
152         text $w_amov \
153                 -takefocus 0 \
154                 -highlightthickness 0 \
155                 -padx 0 -pady 0 \
156                 -background white \
157                 -foreground black \
158                 -borderwidth 0 \
159                 -state disabled \
160                 -wrap none \
161                 -height 40 \
162                 -width 5 \
163                 -font font_diff
164         $w_amov tag conf author_abbr -justify right -rmargin 5
165         $w_amov tag conf curr_commit
166         $w_amov tag conf prior_commit -foreground blue -underline 1
167         $w_amov tag bind prior_commit \
168                 <Button-1> \
169                 "[cb _load_commit $w_amov @amov_data @%x,%y];break"
171         set w_asim $w.file_pane.out.asimple_t
172         text $w_asim \
173                 -takefocus 0 \
174                 -highlightthickness 0 \
175                 -padx 0 -pady 0 \
176                 -background white \
177                 -foreground black \
178                 -borderwidth 0 \
179                 -state disabled \
180                 -wrap none \
181                 -height 40 \
182                 -width 4 \
183                 -font font_diff
184         $w_asim tag conf author_abbr -justify right
185         $w_asim tag conf curr_commit
186         $w_asim tag conf prior_commit -foreground blue -underline 1
187         $w_asim tag bind prior_commit \
188                 <Button-1> \
189                 "[cb _load_commit $w_asim @asim_data @%x,%y];break"
191         set w_file $w.file_pane.out.file_t
192         text $w_file \
193                 -takefocus 0 \
194                 -highlightthickness 0 \
195                 -padx 0 -pady 0 \
196                 -background white \
197                 -foreground black \
198                 -borderwidth 0 \
199                 -state disabled \
200                 -wrap none \
201                 -height 40 \
202                 -width 80 \
203                 -xscrollcommand [list $w.file_pane.out.sbx set] \
204                 -font font_diff
205         if {$have_tk85} {
206                 $w_file configure -inactiveselectbackground darkblue
207         }
208         $w_file tag conf found \
209                 -background yellow
211         set w_columns [list $w_amov $w_asim $w_line $w_file]
213         ${NS}::scrollbar $w.file_pane.out.sbx \
214                 -orient h \
215                 -command [list $w_file xview]
216         ${NS}::scrollbar $w.file_pane.out.sby \
217                 -orient v \
218                 -command [list scrollbar2many $w_columns yview]
219         eval grid $w_columns $w.file_pane.out.sby -sticky nsew
220         grid conf \
221                 $w.file_pane.out.sbx \
222                 -column [expr {[llength $w_columns] - 1}] \
223                 -sticky we
224         grid columnconfigure \
225                 $w.file_pane.out \
226                 [expr {[llength $w_columns] - 1}] \
227                 -weight 1
228         grid rowconfigure $w.file_pane.out 0 -weight 1
230         set finder [::searchbar::new \
231                 $w.file_pane.out.ff $w_file \
232                 -column [expr {[llength $w_columns] - 1}] \
233                 ]
235         set gotoline [::linebar::new \
236                 $w.file_pane.out.lf $w_file \
237                 -column [expr {[llength $w_columns] - 1}] \
238                 ]
240         set w_cviewer $w.file_pane.cm.t
241         text $w_cviewer \
242                 -background white \
243                 -foreground black \
244                 -borderwidth 0 \
245                 -state disabled \
246                 -wrap none \
247                 -height 10 \
248                 -width 80 \
249                 -xscrollcommand [list $w.file_pane.cm.sbx set] \
250                 -yscrollcommand [list $w.file_pane.cm.sby set] \
251                 -font font_diff
252         $w_cviewer tag conf still_loading \
253                 -font font_uiitalic \
254                 -justify center
255         $w_cviewer tag conf header_key \
256                 -tabs {3c} \
257                 -background $active_color \
258                 -font font_uibold
259         $w_cviewer tag conf header_val \
260                 -background $active_color \
261                 -font font_ui
262         $w_cviewer tag raise sel
263         ${NS}::scrollbar $w.file_pane.cm.sbx \
264                 -orient h \
265                 -command [list $w_cviewer xview]
266         ${NS}::scrollbar $w.file_pane.cm.sby \
267                 -orient v \
268                 -command [list $w_cviewer yview]
269         pack $w.file_pane.cm.sby -side right -fill y
270         pack $w.file_pane.cm.sbx -side bottom -fill x
271         pack $w_cviewer -expand 1 -fill both
273         set status [::status_bar::new $w.status]
275         menu $w.ctxm -tearoff 0
276         $w.ctxm add command \
277                 -label [mc "Copy Commit"] \
278                 -command [cb _copycommit]
279         $w.ctxm add separator
280         $w.ctxm add command \
281                 -label [mc "Find Text..."] \
282                 -accelerator F7 \
283                 -command [list searchbar::show $finder]
284         $w.ctxm add command \
285                 -label [mc "Goto Line..."] \
286                 -accelerator "Ctrl-G" \
287                 -command [list linebar::show $gotoline]
288         menu $w.ctxm.enc
289         build_encoding_menu $w.ctxm.enc [cb _setencoding]
290         $w.ctxm add cascade \
291                 -label [mc "Encoding"] \
292                 -menu $w.ctxm.enc
293         $w.ctxm add command \
294                 -label [mc "Do Full Copy Detection"] \
295                 -command [cb _fullcopyblame]
296         $w.ctxm add separator
297         $w.ctxm add command \
298                 -label [mc "Show History Context"] \
299                 -command [cb _gitkcommit]
300         $w.ctxm add command \
301                 -label [mc "Blame Parent Commit"] \
302                 -command [cb _blameparent]
304         foreach i $w_columns {
305                 for {set g 0} {$g < [llength $group_colors]} {incr g} {
306                         $i tag conf color$g -background [lindex $group_colors $g]
307                 }
309                 if {$i eq $w_file} {
310                         $w_file tag raise found
311                 }
312                 $i tag raise sel
314                 $i conf -cursor $cursor_ptr
315                 $i conf -yscrollcommand \
316                         "[list ::searchbar::scrolled $finder]
317                          [list many2scrollbar $w_columns yview $w.file_pane.out.sby]"
318                 bind $i <Button-1> "
319                         [cb _hide_tooltip]
320                         [cb _click $i @%x,%y]
321                         focus $i
322                 "
323                 bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
324                 bind $i <Any-Enter>   [cb _hide_tooltip]
325                 bind $i <Any-Leave>   [cb _hide_tooltip]
326                 bind_button3 $i "
327                         [cb _hide_tooltip]
328                         set cursorX %x
329                         set cursorY %y
330                         set cursorW %W
331                         tk_popup $w.ctxm %X %Y
332                 "
333                 bind $i <Shift-Tab> "[list focus $w_cviewer];break"
334                 bind $i <Tab>       "[cb _focus_search $w_cviewer];break"
335         }
337         foreach i [concat $w_columns $w_cviewer] {
338                 bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
339                 bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
340                 bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
341                 bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
342                 bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
343                 bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
344                 bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
345                 bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
346                 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
347                 bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
348         }
350         bind $w_cviewer <Shift-Tab> "[cb _focus_search $w_file];break"
351         bind $w_cviewer <Tab>       "[list focus $w_file];break"
352         bind $w_cviewer <Button-1>   [list focus $w_cviewer]
353         bind $w_file    <Visibility> [cb _focus_search $w_file]
354         bind $top       <F7>         [list searchbar::show $finder]
355         bind $top       <Escape>     [list searchbar::hide $finder]
356         bind $top       <F3>         [list searchbar::find_next $finder]
357         bind $top       <Shift-F3>   [list searchbar::find_prev $finder]
358         bind $top    <Control-Key-g> [list linebar::show $gotoline]
359         catch { bind $top <Shift-Key-XF86_Switch_VT_3> [list searchbar::find_prev $finder] }
361         grid configure $w.header -sticky ew
362         grid configure $w.file_pane -sticky nsew
363         grid configure $w.status -sticky ew
364         grid columnconfigure $top 0 -weight 1
365         grid rowconfigure $top 0 -weight 0
366         grid rowconfigure $top 1 -weight 1
367         grid rowconfigure $top 2 -weight 0
369         set req_w [winfo reqwidth  $top]
370         set req_h [winfo reqheight $top]
371         set scr_w [expr {[winfo screenwidth $top] - 40}]
372         set scr_h [expr {[winfo screenheight $top] - 120}]
373         set opt_w [expr {$font_w * (80 + 5*3 + 3)}]
374         if {$req_w < $opt_w} {set req_w $opt_w}
375         if {$req_w > $scr_w} {set req_w $scr_w}
376         set opt_h [expr {$req_w*4/3}]
377         if {$req_h < $scr_h} {set req_h $scr_h}
378         if {$req_h > $opt_h} {set req_h $opt_h}
379         set g "${req_w}x${req_h}"
380         wm geometry $top $g
381         update
383         set old_height [winfo height $w.file_pane]
384         $w.file_pane sash place 0 \
385                 [lindex [$w.file_pane sash coord 0] 0] \
386                 [expr {int($old_height * 0.80)}]
387         bind $w.file_pane <Configure> \
388         "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
390         wm protocol $top WM_DELETE_WINDOW "destroy $top"
391         bind $top <Destroy> [cb _handle_destroy %W]
393         _load $this $i_jump
396 method _focus_search {win} {
397         if {[searchbar::visible $finder]} {
398                 focus [searchbar::editor $finder]
399         } else {
400                 focus $win
401         }
404 method _handle_destroy {win} {
405         if {$win eq $w} {
406                 _kill $this
407                 delete_this
408         }
411 method _kill {} {
412         if {$current_fd ne {}} {
413                 kill_file_process $current_fd
414                 catch {close $current_fd}
415                 set current_fd {}
416         }
419 method _load {jump} {
420         variable group_colors
422         _hide_tooltip $this
424         if {$total_lines != 0 || $current_fd ne {}} {
425                 _kill $this
427                 foreach i $w_columns {
428                         $i conf -state normal
429                         $i delete 0.0 end
430                         foreach g [$i tag names] {
431                                 if {[regexp {^g[0-9a-f]{40}$} $g]} {
432                                         $i tag delete $g
433                                 }
434                         }
435                         $i conf -state disabled
436                 }
438                 $w_cviewer conf -state normal
439                 $w_cviewer delete 0.0 end
440                 $w_cviewer conf -state disabled
442                 set highlight_line -1
443                 set highlight_column {}
444                 set highlight_commit {}
445                 set total_lines 0
446         }
448         if {$history eq {}} {
449                 $w_back conf -state disabled
450         } else {
451                 $w_back conf -state normal
452         }
454         # Index 0 is always empty.  There is never line 0 as
455         # we use only 1 based lines, as that matches both with
456         # git-blame output and with Tk's text widget.
457         #
458         set amov_data [list [list]]
459         set asim_data [list [list]]
461         $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
462         $w_path conf -text [escape_path $path]
464         set do_textconv 0
465         if {![is_config_false gui.textconv] && [git-version >= 1.7.2]} {
466                 set filter [gitattr $path diff set]
467                 set textconv [get_config [join [list diff $filter textconv] .]]
468                 if {$filter ne {set} && $textconv ne {}} {
469                         set do_textconv 1
470                 }
471         }
472         if {$commit eq {}} {
473                 if {$do_textconv ne 0} {
474                         # Run textconv with sh -c "..." to allow it to
475                         # contain command + arguments. On windows, just
476                         # call the filter command.
477                         if {![file executable [shellpath]]} {
478                                 set fd [open |[linsert $textconv end $path] r]
479                         } else {
480                                 set fd [open |[list [shellpath] -c "$textconv \"\$0\"" $path] r]
481                         }
482                 } else {
483                         set fd [open $path r]
484                 }
485                 fconfigure $fd -eofchar {}
486         } else {
487                 if {$do_textconv ne 0} {
488                         set fd [git_read cat-file --textconv "$commit:$path"]
489                 } else {
490                         set fd [git_read cat-file blob "$commit:$path"]
491                 }
492         }
493         fconfigure $fd \
494                 -blocking 0 \
495                 -translation lf \
496                 -encoding [get_path_encoding $path]
497         fileevent $fd readable [cb _read_file $fd $jump]
498         set current_fd $fd
501 method _history_menu {} {
502         set m $w.backmenu
503         if {[winfo exists $m]} {
504                 $m delete 0 end
505         } else {
506                 menu $m -tearoff 0
507         }
509         for {set i [expr {[llength $history] - 1}]
510                 } {$i >= 0} {incr i -1} {
511                 set e [lindex $history $i]
512                 set c [lindex $e 0]
513                 set f [lindex $e 1]
515                 if {[regexp {^[0-9a-f]{40}$} $c]} {
516                         set t [string range $c 0 8]...
517                 } elseif {$c eq {}} {
518                         set t {Working Directory}
519                 } else {
520                         set t $c
521                 }
522                 if {![catch {set summary $header($c,summary)}]} {
523                         append t " $summary"
524                         if {[string length $t] > 70} {
525                                 set t [string range $t 0 66]...
526                         }
527                 }
529                 $m add command -label $t -command [cb _goback $i]
530         }
531         set X [winfo rootx $w_back]
532         set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
533         tk_popup $m $X $Y
536 method _goback {i} {
537         set dat [lindex $history $i]
538         set history [lrange $history 0 [expr {$i - 1}]]
539         set commit [lindex $dat 0]
540         set path [lindex $dat 1]
541         _load $this [lrange $dat 2 5]
544 method _read_file {fd jump} {
545         if {$fd ne $current_fd} {
546                 catch {close $fd}
547                 return
548         }
550         foreach i $w_columns {$i conf -state normal}
551         while {[gets $fd line] >= 0} {
552                 regsub "\r\$" $line {} line
553                 incr total_lines
554                 lappend amov_data {}
555                 lappend asim_data {}
557                 if {$total_lines > 1} {
558                         foreach i $w_columns {$i insert end "\n"}
559                 }
561                 $w_line insert end "$total_lines" linenumber
562                 $w_file insert end "$line"
563         }
565         set ln_wc [expr {[string length $total_lines] + 2}]
566         if {[$w_line cget -width] < $ln_wc} {
567                 $w_line conf -width $ln_wc
568         }
570         foreach i $w_columns {$i conf -state disabled}
572         if {[eof $fd]} {
573                 close $fd
575                 # If we don't force Tk to update the widgets *right now*
576                 # none of our jump commands will cause a change in the UI.
577                 #
578                 update
580                 if {[llength $jump] == 1} {
581                         set highlight_line [lindex $jump 0]
582                         $w_file see "$highlight_line.0"
583                 } elseif {[llength $jump] == 4} {
584                         set highlight_column [lindex $jump 0]
585                         set highlight_line [lindex $jump 1]
586                         $w_file xview moveto [lindex $jump 2]
587                         $w_file yview moveto [lindex $jump 3]
588                 }
590                 _exec_blame $this $w_asim @asim_data \
591                         [list] \
592                         [mc "Loading copy/move tracking annotations..."]
593         }
594 } ifdeleted { catch {close $fd} }
596 method _exec_blame {cur_w cur_d options cur_s} {
597         lappend options --incremental --encoding=utf-8
598         if {$commit eq {}} {
599                 lappend options --contents $path
600         } else {
601                 lappend options $commit
602         }
603         lappend options -- $path
604         set fd [eval git_read --nice blame $options]
605         fconfigure $fd -blocking 0 -translation lf -encoding utf-8
606         fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
607         set current_fd $fd
608         set blame_lines 0
610         $status start \
611                 $cur_s \
612                 [mc "lines annotated"]
615 method _read_blame {fd cur_w cur_d} {
616         upvar #0 $cur_d line_data
617         variable group_colors
619         if {$fd ne $current_fd} {
620                 catch {close $fd}
621                 return
622         }
624         $cur_w conf -state normal
625         while {[gets $fd line] >= 0} {
626                 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
627                         cmit original_line final_line line_count]} {
628                         set r_commit     $cmit
629                         set r_orig_line  $original_line
630                         set r_final_line $final_line
631                         set r_line_count $line_count
632                 } elseif {[string match {filename *} $line]} {
633                         set file [string range $line 9 end]
634                         set n    $r_line_count
635                         set lno  $r_final_line
636                         set oln  $r_orig_line
637                         set cmit $r_commit
639                         if {[regexp {^0{40}$} $cmit]} {
640                                 set commit_abbr work
641                                 set commit_type curr_commit
642                         } elseif {$cmit eq $commit} {
643                                 set commit_abbr this
644                                 set commit_type curr_commit
645                         } else {
646                                 set commit_type prior_commit
647                                 set commit_abbr [string range $cmit 0 3]
648                         }
650                         set author_abbr {}
651                         set a_name {}
652                         catch {set a_name $header($cmit,author)}
653                         while {$a_name ne {}} {
654                                 if {$author_abbr ne {}
655                                         && [string index $a_name 0] eq {'}} {
656                                         regsub {^'[^']+'\s+} $a_name {} a_name
657                                 }
658                                 if {![regexp {^([[:upper:]])} $a_name _a]} break
659                                 append author_abbr $_a
660                                 unset _a
661                                 if {![regsub \
662                                         {^[[:upper:]][^\s]*\s+} \
663                                         $a_name {} a_name ]} break
664                         }
665                         if {$author_abbr eq {}} {
666                                 set author_abbr { |}
667                         } else {
668                                 set author_abbr [string range $author_abbr 0 3]
669                         }
670                         unset a_name
672                         set first_lno $lno
673                         while {
674                            $first_lno > 1
675                         && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
676                         && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
677                         } {
678                                 incr first_lno -1
679                         }
681                         set color {}
682                         if {$first_lno < $lno} {
683                                 foreach g [$w_file tag names $first_lno.0] {
684                                         if {[regexp {^color[0-9]+$} $g]} {
685                                                 set color $g
686                                                 break
687                                         }
688                                 }
689                         } else {
690                                 set i [lsort [concat \
691                                         [$w_file tag names "[expr {$first_lno - 1}].0"] \
692                                         [$w_file tag names "[expr {$lno + $n}].0"] \
693                                         ]]
694                                 for {set g 0} {$g < [llength $group_colors]} {incr g} {
695                                         if {[lsearch -sorted -exact $i color$g] == -1} {
696                                                 set color color$g
697                                                 break
698                                         }
699                                 }
700                         }
701                         if {$color eq {}} {
702                                 set color color0
703                         }
705                         while {$n > 0} {
706                                 set lno_e "$lno.0 lineend + 1c"
707                                 if {[lindex $line_data $lno] ne {}} {
708                                         set g [lindex $line_data $lno 0]
709                                         foreach i $w_columns {
710                                                 $i tag remove g$g $lno.0 $lno_e
711                                         }
712                                 }
713                                 lset line_data $lno [list $cmit $file $oln]
715                                 $cur_w delete $lno.0 "$lno.0 lineend"
716                                 if {$lno == $first_lno} {
717                                         $cur_w insert $lno.0 $commit_abbr $commit_type
718                                 } elseif {$lno == [expr {$first_lno + 1}]} {
719                                         $cur_w insert $lno.0 $author_abbr author_abbr
720                                 } else {
721                                         $cur_w insert $lno.0 { |}
722                                 }
724                                 foreach i $w_columns {
725                                         if {$cur_w eq $w_amov} {
726                                                 for {set g 0} \
727                                                         {$g < [llength $group_colors]} \
728                                                         {incr g} {
729                                                         $i tag remove color$g $lno.0 $lno_e
730                                                 }
731                                                 $i tag add $color $lno.0 $lno_e
732                                         }
733                                         $i tag add g$cmit $lno.0 $lno_e
734                                 }
736                                 if {$highlight_column eq $cur_w} {
737                                         if {$highlight_line == -1
738                                          && [lindex [$w_file yview] 0] == 0} {
739                                                 $w_file see $lno.0
740                                                 set highlight_line $lno
741                                         }
742                                         if {$highlight_line == $lno} {
743                                                 _showcommit $this $cur_w $lno
744                                         }
745                                 }
747                                 incr n -1
748                                 incr lno
749                                 incr oln
750                                 incr blame_lines
751                         }
753                         while {
754                            $cmit eq [lindex $line_data $lno 0]
755                         && $file eq [lindex $line_data $lno 1]
756                         } {
757                                 $cur_w delete $lno.0 "$lno.0 lineend"
759                                 if {$lno == $first_lno} {
760                                         $cur_w insert $lno.0 $commit_abbr $commit_type
761                                 } elseif {$lno == [expr {$first_lno + 1}]} {
762                                         $cur_w insert $lno.0 $author_abbr author_abbr
763                                 } else {
764                                         $cur_w insert $lno.0 { |}
765                                 }
767                                 if {$cur_w eq $w_amov} {
768                                         foreach i $w_columns {
769                                                 for {set g 0} \
770                                                         {$g < [llength $group_colors]} \
771                                                         {incr g} {
772                                                         $i tag remove color$g $lno.0 $lno_e
773                                                 }
774                                                 $i tag add $color $lno.0 $lno_e
775                                         }
776                                 }
778                                 incr lno
779                         }
781                 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
782                         set header($r_commit,$key) $data
783                 }
784         }
785         $cur_w conf -state disabled
787         if {[eof $fd]} {
788                 close $fd
789                 if {$cur_w eq $w_asim} {
790                         # Switches for original location detection
791                         set threshold [get_config gui.copyblamethreshold]
792                         set original_options [list "-C$threshold"]
794                         if {![is_config_true gui.fastcopyblame]} {
795                                 # thorough copy search; insert before the threshold
796                                 set original_options [linsert $original_options 0 -C]
797                         }
798                         if {[git-version >= 1.5.3]} {
799                                 lappend original_options -w ; # ignore indentation changes
800                         }
802                         _exec_blame $this $w_amov @amov_data \
803                                 $original_options \
804                                 [mc "Loading original location annotations..."]
805                 } else {
806                         set current_fd {}
807                         $status stop [mc "Annotation complete."]
808                 }
809         } else {
810                 $status update $blame_lines $total_lines
811         }
812 } ifdeleted { catch {close $fd} }
814 method _find_commit_bound {data_list start_idx delta} {
815         upvar #0 $data_list line_data
816         set pos $start_idx
817         set limit       [expr {[llength $line_data] - 1}]
818         set base_commit [lindex $line_data $pos 0]
820         while {$pos > 0 && $pos < $limit} {
821                 set new_pos [expr {$pos + $delta}]
822                 if {[lindex $line_data $new_pos 0] ne $base_commit} {
823                         return $pos
824                 }
826                 set pos $new_pos
827         }
829         return $pos
832 method _fullcopyblame {} {
833         if {$current_fd ne {}} {
834                 tk_messageBox \
835                         -icon error \
836                         -type ok \
837                         -title [mc "Busy"] \
838                         -message [mc "Annotation process is already running."]
840                 return
841         }
843         # Switches for original location detection
844         set threshold [get_config gui.copyblamethreshold]
845         set original_options [list -C -C "-C$threshold"]
847         if {[git-version >= 1.5.3]} {
848                 lappend original_options -w ; # ignore indentation changes
849         }
851         # Find the line range
852         set pos @$::cursorX,$::cursorY
853         set lno [lindex [split [$::cursorW index $pos] .] 0]
854         set min_amov_lno [_find_commit_bound $this @amov_data $lno -1]
855         set max_amov_lno [_find_commit_bound $this @amov_data $lno 1]
856         set min_asim_lno [_find_commit_bound $this @asim_data $lno -1]
857         set max_asim_lno [_find_commit_bound $this @asim_data $lno 1]
859         if {$min_asim_lno < $min_amov_lno} {
860                 set min_amov_lno $min_asim_lno
861         }
863         if {$max_asim_lno > $max_amov_lno} {
864                 set max_amov_lno $max_asim_lno
865         }
867         lappend original_options -L "$min_amov_lno,$max_amov_lno"
869         # Clear lines
870         for {set i $min_amov_lno} {$i <= $max_amov_lno} {incr i} {
871                 lset amov_data $i [list ]
872         }
874         # Start the back-end process
875         _exec_blame $this $w_amov @amov_data \
876                 $original_options \
877                 [mc "Running thorough copy detection..."]
880 method _click {cur_w pos} {
881         set lno [lindex [split [$cur_w index $pos] .] 0]
882         _showcommit $this $cur_w $lno
885 method _setencoding {enc} {
886         force_path_encoding $path $enc
887         _load $this [list \
888                 $highlight_column \
889                 $highlight_line \
890                 [lindex [$w_file xview] 0] \
891                 [lindex [$w_file yview] 0] \
892                 ]
895 method _load_commit {cur_w cur_d pos} {
896         upvar #0 $cur_d line_data
897         set lno [lindex [split [$cur_w index $pos] .] 0]
898         set dat [lindex $line_data $lno]
899         if {$dat ne {}} {
900                 _load_new_commit $this  \
901                         [lindex $dat 0] \
902                         [lindex $dat 1] \
903                         [list [lindex $dat 2]]
904         }
907 method _load_new_commit {new_commit new_path jump} {
908         lappend history [list \
909                 $commit $path \
910                 $highlight_column \
911                 $highlight_line \
912                 [lindex [$w_file xview] 0] \
913                 [lindex [$w_file yview] 0] \
914                 ]
916         set commit $new_commit
917         set path   $new_path
918         _load $this $jump
921 method _showcommit {cur_w lno} {
922         global repo_config
923         variable active_color
925         if {$highlight_commit ne {}} {
926                 foreach i $w_columns {
927                         $i tag conf g$highlight_commit -background {}
928                         $i tag lower g$highlight_commit
929                 }
930         }
932         if {$cur_w eq $w_asim} {
933                 set dat [lindex $asim_data $lno]
934                 set highlight_column $w_asim
935         } else {
936                 set dat [lindex $amov_data $lno]
937                 set highlight_column $w_amov
938         }
940         $w_cviewer conf -state normal
941         $w_cviewer delete 0.0 end
943         if {$dat eq {}} {
944                 set cmit {}
945                 $w_cviewer insert end [mc "Loading annotation..."] still_loading
946         } else {
947                 set cmit [lindex $dat 0]
948                 set file [lindex $dat 1]
950                 foreach i $w_columns {
951                         $i tag conf g$cmit -background $active_color
952                         $i tag raise g$cmit
953                         if {$i eq $w_file} {
954                                 $w_file tag raise found
955                         }
956                         $i tag raise sel
957                 }
959                 set author_name {}
960                 set author_email {}
961                 set author_time {}
962                 catch {set author_name $header($cmit,author)}
963                 catch {set author_email $header($cmit,author-mail)}
964                 catch {set author_time [format_date $header($cmit,author-time)]}
966                 set committer_name {}
967                 set committer_email {}
968                 set committer_time {}
969                 catch {set committer_name $header($cmit,committer)}
970                 catch {set committer_email $header($cmit,committer-mail)}
971                 catch {set committer_time [format_date $header($cmit,committer-time)]}
973                 if {[catch {set msg $header($cmit,message)}]} {
974                         set msg {}
975                         catch {
976                                 set fd [git_read cat-file commit $cmit]
977                                 fconfigure $fd -encoding binary -translation lf
978                                 # By default commits are assumed to be in utf-8
979                                 set enc utf-8
980                                 while {[gets $fd line] > 0} {
981                                         if {[string match {encoding *} $line]} {
982                                                 set enc [string tolower [string range $line 9 end]]
983                                         }
984                                 }
985                                 set msg [read $fd]
986                                 close $fd
988                                 set enc [tcl_encoding $enc]
989                                 if {$enc ne {}} {
990                                         set msg [encoding convertfrom $enc $msg]
991                                 }
992                                 set msg [string trim $msg]
993                         }
994                         set header($cmit,message) $msg
995                 }
997                 $w_cviewer insert end "commit $cmit\n" header_key
998                 $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
999                 $w_cviewer insert end "$author_name $author_email" header_val
1000                 $w_cviewer insert end "  $author_time\n" header_val
1002                 $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
1003                 $w_cviewer insert end "$committer_name $committer_email" header_val
1004                 $w_cviewer insert end "  $committer_time\n" header_val
1006                 if {$file ne $path} {
1007                         $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
1008                         $w_cviewer insert end "[escape_path $file]\n" header_val
1009                 }
1011                 $w_cviewer insert end "\n$msg"
1012         }
1013         $w_cviewer conf -state disabled
1015         set highlight_line $lno
1016         set highlight_commit $cmit
1018         if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
1019                 _hide_tooltip $this
1020         }
1023 method _get_click_amov_info {} {
1024         set pos @$::cursorX,$::cursorY
1025         set lno [lindex [split [$::cursorW index $pos] .] 0]
1026         return [lindex $amov_data $lno]
1029 method _copycommit {} {
1030         set dat [_get_click_amov_info $this]
1031         if {$dat ne {}} {
1032                 clipboard clear
1033                 clipboard append \
1034                         -format STRING \
1035                         -type STRING \
1036                         -- [lindex $dat 0]
1037         }
1040 method _format_offset_date {base offset} {
1041         set exval [expr {$base + $offset*24*60*60}]
1042         return [clock format $exval -format {%Y-%m-%d}]
1045 method _gitkcommit {} {
1046         global nullid
1048         set dat [_get_click_amov_info $this]
1049         if {$dat ne {}} {
1050                 set cmit [lindex $dat 0]
1052                 # If the line belongs to the working copy, use HEAD instead
1053                 if {$cmit eq $nullid} {
1054                         if {[catch {set cmit [git rev-parse --verify HEAD]} err]} {
1055                                 error_popup [strcat [mc "Cannot find HEAD commit:"] "\n\n$err"]
1056                                 return;
1057                         }
1058                 }
1060                 set radius [get_config gui.blamehistoryctx]
1061                 set cmdline [list --select-commit=$cmit]
1063                 if {$radius > 0} {
1064                         set author_time {}
1065                         set committer_time {}
1067                         catch {set author_time $header($cmit,author-time)}
1068                         catch {set committer_time $header($cmit,committer-time)}
1070                         if {$committer_time eq {}} {
1071                                 set committer_time $author_time
1072                         }
1074                         set after_time [_format_offset_date $this $committer_time [expr {-$radius}]]
1075                         set before_time [_format_offset_date $this $committer_time $radius]
1077                         lappend cmdline --after=$after_time --before=$before_time
1078                 }
1080                 lappend cmdline $cmit
1082                 set base_rev "HEAD"
1083                 if {$commit ne {}} {
1084                         set base_rev $commit
1085                 }
1087                 if {$base_rev ne $cmit} {
1088                         lappend cmdline $base_rev
1089                 }
1091                 do_gitk $cmdline
1092         }
1095 method _blameparent {} {
1096         global nullid
1098         set dat [_get_click_amov_info $this]
1099         if {$dat ne {}} {
1100                 set cmit [lindex $dat 0]
1101                 set new_path [lindex $dat 1]
1103                 # Allow using Blame Parent on lines modified in the working copy
1104                 if {$cmit eq $nullid} {
1105                         set parent_ref "HEAD"
1106                 } else {
1107                         set parent_ref "$cmit^"
1108                 }
1109                 if {[catch {set cparent [git rev-parse --verify $parent_ref]} err]} {
1110                         error_popup [strcat [mc "Cannot find parent commit:"] "\n\n$err"]
1111                         return;
1112                 }
1114                 _kill $this
1116                 # Generate a diff between the commit and its parent,
1117                 # and use the hunks to update the line number.
1118                 # Request zero context to simplify calculations.
1119                 if {$cmit eq $nullid} {
1120                         set diffcmd [list diff-index --unified=0 $cparent -- $new_path]
1121                 } else {
1122                         set diffcmd [list diff-tree --unified=0 $cparent $cmit -- $new_path]
1123                 }
1124                 if {[catch {set fd [eval git_read $diffcmd]} err]} {
1125                         $status stop [mc "Unable to display parent"]
1126                         error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
1127                         return
1128                 }
1130                 set r_orig_line [lindex $dat 2]
1132                 fconfigure $fd \
1133                         -blocking 0 \
1134                         -encoding binary \
1135                         -translation binary
1136                 fileevent $fd readable [cb _read_diff_load_commit \
1137                         $fd $cparent $new_path $r_orig_line]
1138                 set current_fd $fd
1139         }
1142 method _read_diff_load_commit {fd cparent new_path tline} {
1143         if {$fd ne $current_fd} {
1144                 catch {close $fd}
1145                 return
1146         }
1148         while {[gets $fd line] >= 0} {
1149                 if {[regexp {^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@} $line line \
1150                         old_line osz old_size new_line nsz new_size]} {
1152                         if {$osz eq {}} { set old_size 1 }
1153                         if {$nsz eq {}} { set new_size 1 }
1155                         if {$new_line <= $tline} {
1156                                 if {[expr {$new_line + $new_size}] > $tline} {
1157                                         # Target line within the hunk
1158                                         set line_shift [expr {
1159                                                 ($new_size-$old_size)*($tline-$new_line)/$new_size
1160                                                 }]
1161                                 } else {
1162                                         set line_shift [expr {$new_size-$old_size}]
1163                                 }
1165                                 set r_orig_line [expr {$r_orig_line - $line_shift}]
1166                         }
1167                 }
1168         }
1170         if {[eof $fd]} {
1171                 close $fd;
1172                 set current_fd {}
1174                 _load_new_commit $this  \
1175                         $cparent        \
1176                         $new_path       \
1177                         [list $r_orig_line]
1178         }
1179 } ifdeleted { catch {close $fd} }
1181 method _show_tooltip {cur_w pos} {
1182         if {$tooltip_wm ne {}} {
1183                 _open_tooltip $this $cur_w
1184         } elseif {$tooltip_timer eq {}} {
1185                 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
1186         }
1189 method _open_tooltip {cur_w} {
1190         set tooltip_timer {}
1191         set pos_x [winfo pointerx $cur_w]
1192         set pos_y [winfo pointery $cur_w]
1193         if {[winfo containing $pos_x $pos_y] ne $cur_w} {
1194                 _hide_tooltip $this
1195                 return
1196         }
1198         if {$tooltip_wm ne "$cur_w.tooltip"} {
1199                 _hide_tooltip $this
1201                 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
1202                 wm overrideredirect $tooltip_wm 1
1203                 wm transient $tooltip_wm [winfo toplevel $cur_w]
1204                 set tooltip_t $tooltip_wm.label
1205                 text $tooltip_t \
1206                         -takefocus 0 \
1207                         -highlightthickness 0 \
1208                         -relief flat \
1209                         -borderwidth 0 \
1210                         -wrap none \
1211                         -background lightyellow \
1212                         -foreground black
1213                 $tooltip_t tag conf section_header -font font_uibold
1214                 pack $tooltip_t
1215         } else {
1216                 $tooltip_t conf -state normal
1217                 $tooltip_t delete 0.0 end
1218         }
1220         set pos @[join [list \
1221                 [expr {$pos_x - [winfo rootx $cur_w]}] \
1222                 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
1223         set lno [lindex [split [$cur_w index $pos] .] 0]
1224         if {$cur_w eq $w_amov} {
1225                 set dat [lindex $amov_data $lno]
1226                 set org {}
1227         } else {
1228                 set dat [lindex $asim_data $lno]
1229                 set org [lindex $amov_data $lno]
1230         }
1232         if {$dat eq {}} {
1233                 _hide_tooltip $this
1234                 return
1235         }
1237         set cmit [lindex $dat 0]
1238         set tooltip_commit [list $cmit]
1240         set author_name {}
1241         set summary     {}
1242         set author_time {}
1243         catch {set author_name $header($cmit,author)}
1244         catch {set summary     $header($cmit,summary)}
1245         catch {set author_time [format_date $header($cmit,author-time)]}
1247         $tooltip_t insert end "commit $cmit\n"
1248         $tooltip_t insert end "$author_name  $author_time\n"
1249         $tooltip_t insert end "$summary"
1251         if {$org ne {} && [lindex $org 0] ne $cmit} {
1252                 set save [$tooltip_t get 0.0 end]
1253                 $tooltip_t delete 0.0 end
1255                 set cmit [lindex $org 0]
1256                 set file [lindex $org 1]
1257                 lappend tooltip_commit $cmit
1259                 set author_name {}
1260                 set summary     {}
1261                 set author_time {}
1262                 catch {set author_name $header($cmit,author)}
1263                 catch {set summary     $header($cmit,summary)}
1264                 catch {set author_time [format_date $header($cmit,author-time)]}
1266                 $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
1267                 $tooltip_t insert end "commit $cmit\n"
1268                 $tooltip_t insert end "$author_name  $author_time\n"
1269                 $tooltip_t insert end "$summary\n"
1271                 if {$file ne $path} {
1272                         $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
1273                         $tooltip_t insert end "$file\n"
1274                 }
1276                 $tooltip_t insert end "\n"
1277                 $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
1278                 $tooltip_t insert end $save
1279         }
1281         $tooltip_t conf -state disabled
1282         _position_tooltip $this
1284         # On MacOS raising a window causes it to acquire focus.
1285         # Tk 8.5 on MacOS seems to properly support wm transient,
1286         # so we can safely counter the effect there.
1287         if {$::have_tk85 && [is_MacOSX]} {
1288                 update
1289                 if {$w eq {}} {
1290                         raise .
1291                 } else {
1292                         raise $w
1293                 }
1294         }
1297 method _position_tooltip {} {
1298         set max_h [lindex [split [$tooltip_t index end] .] 0]
1299         set max_w 0
1300         for {set i 1} {$i <= $max_h} {incr i} {
1301                 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
1302                 if {$c > $max_w} {set max_w $c}
1303         }
1304         $tooltip_t conf -width $max_w -height $max_h
1306         set req_w [winfo reqwidth  $tooltip_t]
1307         set req_h [winfo reqheight $tooltip_t]
1308         set pos_x [expr {[winfo pointerx .] +  5}]
1309         set pos_y [expr {[winfo pointery .] + 10}]
1311         set g "${req_w}x${req_h}"
1312         if {[tk windowingsystem] eq "win32" || $pos_x >= 0} {append g +}
1313         append g $pos_x
1314         if {[tk windowingsystem] eq "win32" || $pos_y >= 0} {append g +}
1315         append g $pos_y
1317         wm geometry $tooltip_wm $g
1318         if {![is_MacOSX]} {
1319                 raise $tooltip_wm
1320         }
1323 method _hide_tooltip {} {
1324         if {$tooltip_wm ne {}} {
1325                 destroy $tooltip_wm
1326                 set tooltip_wm {}
1327                 set tooltip_commit {}
1328         }
1329         if {$tooltip_timer ne {}} {
1330                 after cancel $tooltip_timer
1331                 set tooltip_timer {}
1332         }
1335 method _resize {new_height} {
1336         set diff [expr {$new_height - $old_height}]
1337         if {$diff == 0} return
1339         set my [expr {[winfo height $w.file_pane] - 25}]
1340         set o [$w.file_pane sash coord 0]
1341         set ox [lindex $o 0]
1342         set oy [expr {[lindex $o 1] + $diff}]
1343         if {$oy < 0}   {set oy 0}
1344         if {$oy > $my} {set oy $my}
1345         $w.file_pane sash place 0 $ox $oy
1347         set old_height $new_height