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