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