lib / blame.tclon commit git-gui: use "blame -w -C -C" for "where did it come from, originally?" (a840566)
   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     ; # text variable bound to status bar
  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# Switches for original location detection
  37#
  38variable original_options [list -C -C]
  39if {[git-version >= 1.5.3]} {
  40        lappend original_options -w ; # ignore indentation changes
  41}
  42
  43# Current blame data; cleared/reset on each load
  44#
  45field commit               ; # input commit to blame
  46field path                 ; # input filename to view in $commit
  47
  48field current_fd        {} ; # background process running
  49field highlight_line    -1 ; # current line selected
  50field highlight_column  {} ; # current commit column selected
  51field highlight_commit  {} ; # sha1 of commit selected
  52
  53field total_lines       0  ; # total length of file
  54field blame_lines       0  ; # number of lines computed
  55field amov_data            ; # list of {commit origfile origline}
  56field asim_data            ; # list of {commit origfile origline}
  57
  58field r_commit             ; # commit currently being parsed
  59field r_orig_line          ; # original line number
  60field r_final_line         ; # final line number
  61field r_line_count         ; # lines in this region
  62
  63field tooltip_wm        {} ; # Current tooltip toplevel, if open
  64field tooltip_t         {} ; # Text widget in $tooltip_wm
  65field tooltip_timer     {} ; # Current timer event for our tooltip
  66field tooltip_commit    {} ; # Commit(s) in tooltip
  67
  68constructor new {i_commit i_path} {
  69        global cursor_ptr
  70        variable active_color
  71        variable group_colors
  72
  73        set commit $i_commit
  74        set path   $i_path
  75
  76        make_toplevel top w
  77        wm title $top "[appname] ([reponame]): File Viewer"
  78
  79        frame $w.header -background gold
  80        label $w.header.commit_l \
  81                -text {Commit:} \
  82                -background gold \
  83                -anchor w \
  84                -justify left
  85        set w_back $w.header.commit_b
  86        label $w_back \
  87                -image ::blame::img_back_arrow \
  88                -borderwidth 0 \
  89                -relief flat \
  90                -state disabled \
  91                -background gold \
  92                -activebackground gold
  93        bind $w_back <Button-1> "
  94                if {\[$w_back cget -state\] eq {normal}} {
  95                        [cb _history_menu]
  96                }
  97                "
  98        label $w.header.commit \
  99                -textvariable @commit \
 100                -background gold \
 101                -anchor w \
 102                -justify left
 103        label $w.header.path_l \
 104                -text {File:} \
 105                -background gold \
 106                -anchor w \
 107                -justify left
 108        set w_path $w.header.path
 109        label $w_path \
 110                -background gold \
 111                -anchor w \
 112                -justify left
 113        pack $w.header.commit_l -side left
 114        pack $w_back -side left
 115        pack $w.header.commit -side left
 116        pack $w_path -fill x -side right
 117        pack $w.header.path_l -side right
 118
 119        panedwindow $w.file_pane -orient vertical
 120        frame $w.file_pane.out
 121        frame $w.file_pane.cm
 122        $w.file_pane add $w.file_pane.out \
 123                -sticky nsew \
 124                -minsize 100 \
 125                -height 100 \
 126                -width 100
 127        $w.file_pane add $w.file_pane.cm \
 128                -sticky nsew \
 129                -minsize 25 \
 130                -height 25 \
 131                -width 100
 132
 133        set w_line $w.file_pane.out.linenumber_t
 134        text $w_line \
 135                -takefocus 0 \
 136                -highlightthickness 0 \
 137                -padx 0 -pady 0 \
 138                -background white -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 -borderwidth 0 \
 152                -state disabled \
 153                -wrap none \
 154                -height 40 \
 155                -width 5 \
 156                -font font_diff
 157        $w_amov tag conf author_abbr -justify right -rmargin 5
 158        $w_amov tag conf curr_commit
 159        $w_amov tag conf prior_commit -foreground blue -underline 1
 160        $w_amov tag bind prior_commit \
 161                <Button-1> \
 162                "[cb _load_commit $w_amov @amov_data @%x,%y];break"
 163
 164        set w_asim $w.file_pane.out.asimple_t
 165        text $w_asim \
 166                -takefocus 0 \
 167                -highlightthickness 0 \
 168                -padx 0 -pady 0 \
 169                -background white -borderwidth 0 \
 170                -state disabled \
 171                -wrap none \
 172                -height 40 \
 173                -width 4 \
 174                -font font_diff
 175        $w_asim tag conf author_abbr -justify right
 176        $w_asim tag conf curr_commit
 177        $w_asim tag conf prior_commit -foreground blue -underline 1
 178        $w_asim tag bind prior_commit \
 179                <Button-1> \
 180                "[cb _load_commit $w_asim @asim_data @%x,%y];break"
 181
 182        set w_file $w.file_pane.out.file_t
 183        text $w_file \
 184                -takefocus 0 \
 185                -highlightthickness 0 \
 186                -padx 0 -pady 0 \
 187                -background white -borderwidth 0 \
 188                -state disabled \
 189                -wrap none \
 190                -height 40 \
 191                -width 80 \
 192                -xscrollcommand [list $w.file_pane.out.sbx set] \
 193                -font font_diff
 194
 195        set w_columns [list $w_amov $w_asim $w_line $w_file]
 196
 197        scrollbar $w.file_pane.out.sbx \
 198                -orient h \
 199                -command [list $w_file xview]
 200        scrollbar $w.file_pane.out.sby \
 201                -orient v \
 202                -command [list scrollbar2many $w_columns yview]
 203        eval grid $w_columns $w.file_pane.out.sby -sticky nsew
 204        grid conf \
 205                $w.file_pane.out.sbx \
 206                -column [expr {[llength $w_columns] - 1}] \
 207                -sticky we
 208        grid columnconfigure \
 209                $w.file_pane.out \
 210                [expr {[llength $w_columns] - 1}] \
 211                -weight 1
 212        grid rowconfigure $w.file_pane.out 0 -weight 1
 213
 214        set w_cviewer $w.file_pane.cm.t
 215        text $w_cviewer \
 216                -background white -borderwidth 0 \
 217                -state disabled \
 218                -wrap none \
 219                -height 10 \
 220                -width 80 \
 221                -xscrollcommand [list $w.file_pane.cm.sbx set] \
 222                -yscrollcommand [list $w.file_pane.cm.sby set] \
 223                -font font_diff
 224        $w_cviewer tag conf still_loading \
 225                -font font_uiitalic \
 226                -justify center
 227        $w_cviewer tag conf header_key \
 228                -tabs {3c} \
 229                -background $active_color \
 230                -font font_uibold
 231        $w_cviewer tag conf header_val \
 232                -background $active_color \
 233                -font font_ui
 234        $w_cviewer tag raise sel
 235        scrollbar $w.file_pane.cm.sbx \
 236                -orient h \
 237                -command [list $w_cviewer xview]
 238        scrollbar $w.file_pane.cm.sby \
 239                -orient v \
 240                -command [list $w_cviewer yview]
 241        pack $w.file_pane.cm.sby -side right -fill y
 242        pack $w.file_pane.cm.sbx -side bottom -fill x
 243        pack $w_cviewer -expand 1 -fill both
 244
 245        frame $w.status \
 246                -borderwidth 1 \
 247                -relief sunken
 248        label $w.status.l \
 249                -textvariable @status \
 250                -anchor w \
 251                -justify left
 252        pack $w.status.l -side left
 253
 254        menu $w.ctxm -tearoff 0
 255        $w.ctxm add command \
 256                -label "Copy Commit" \
 257                -command [cb _copycommit]
 258
 259        foreach i $w_columns {
 260                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 261                        $i tag conf color$g -background [lindex $group_colors $g]
 262                }
 263
 264                $i conf -cursor $cursor_ptr
 265                $i conf -yscrollcommand [list many2scrollbar \
 266                        $w_columns yview $w.file_pane.out.sby]
 267                bind $i <Button-1> "
 268                        [cb _hide_tooltip]
 269                        [cb _click $i @%x,%y]
 270                        focus $i
 271                "
 272                bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
 273                bind $i <Any-Enter>   [cb _hide_tooltip]
 274                bind $i <Any-Leave>   [cb _hide_tooltip]
 275                bind_button3 $i "
 276                        [cb _hide_tooltip]
 277                        set cursorX %x
 278                        set cursorY %y
 279                        set cursorW %W
 280                        tk_popup $w.ctxm %X %Y
 281                "
 282                bind $i <Shift-Tab> "[list focus $w_cviewer];break"
 283                bind $i <Tab>       "[list focus $w_cviewer];break"
 284        }
 285
 286        foreach i [concat $w_columns $w_cviewer] {
 287                bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
 288                bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
 289                bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
 290                bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
 291                bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
 292                bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
 293                bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
 294                bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
 295                bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
 296                bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
 297        }
 298
 299        bind $w_cviewer <Shift-Tab> "[list focus $w_file];break"
 300        bind $w_cviewer <Tab>       "[list focus $w_file];break"
 301        bind $w_cviewer <Button-1> [list focus $w_cviewer]
 302        bind $w_file    <Visibility> [list focus $w_file]
 303
 304        grid configure $w.header -sticky ew
 305        grid configure $w.file_pane -sticky nsew
 306        grid configure $w.status -sticky ew
 307        grid columnconfigure $top 0 -weight 1
 308        grid rowconfigure $top 0 -weight 0
 309        grid rowconfigure $top 1 -weight 1
 310        grid rowconfigure $top 2 -weight 0
 311
 312        set req_w [winfo reqwidth  $top]
 313        set req_h [winfo reqheight $top]
 314        set scr_h [expr {[winfo screenheight $top] - 100}]
 315        if {$req_w < 600} {set req_w 600}
 316        if {$req_h < $scr_h} {set req_h $scr_h}
 317        set g "${req_w}x${req_h}"
 318        wm geometry $top $g
 319        update
 320
 321        set old_height [winfo height $w.file_pane]
 322        $w.file_pane sash place 0 \
 323                [lindex [$w.file_pane sash coord 0] 0] \
 324                [expr {int($old_height * 0.70)}]
 325        bind $w.file_pane <Configure> \
 326        "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
 327
 328        _load $this {}
 329}
 330
 331method _load {jump} {
 332        variable group_colors
 333
 334        _hide_tooltip $this
 335
 336        if {$total_lines != 0 || $current_fd ne {}} {
 337                if {$current_fd ne {}} {
 338                        catch {close $current_fd}
 339                        set current_fd {}
 340                }
 341
 342                foreach i $w_columns {
 343                        $i conf -state normal
 344                        $i delete 0.0 end
 345                        foreach g [$i tag names] {
 346                                if {[regexp {^g[0-9a-f]{40}$} $g]} {
 347                                        $i tag delete $g
 348                                }
 349                        }
 350                        $i conf -state disabled
 351                }
 352
 353                $w_cviewer conf -state normal
 354                $w_cviewer delete 0.0 end
 355                $w_cviewer conf -state disabled
 356
 357                set highlight_line -1
 358                set highlight_column {}
 359                set highlight_commit {}
 360                set total_lines 0
 361        }
 362
 363        if {[winfo exists $w.status.c]} {
 364                $w.status.c coords bar 0 0 0 20
 365        } else {
 366                canvas $w.status.c \
 367                        -width 100 \
 368                        -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
 369                        -borderwidth 1 \
 370                        -relief groove \
 371                        -highlightt 0
 372                $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
 373                pack $w.status.c -side right
 374        }
 375
 376        if {$history eq {}} {
 377                $w_back conf -state disabled
 378        } else {
 379                $w_back conf -state normal
 380        }
 381
 382        # Index 0 is always empty.  There is never line 0 as
 383        # we use only 1 based lines, as that matches both with
 384        # git-blame output and with Tk's text widget.
 385        #
 386        set amov_data [list [list]]
 387        set asim_data [list [list]]
 388
 389        set status "Loading $commit:[escape_path $path]..."
 390        $w_path conf -text [escape_path $path]
 391        if {$commit eq {}} {
 392                set fd [open $path r]
 393        } else {
 394                set cmd [list git cat-file blob "$commit:$path"]
 395                set fd [open "| $cmd" r]
 396        }
 397        fconfigure $fd -blocking 0 -translation lf -encoding binary
 398        fileevent $fd readable [cb _read_file $fd $jump]
 399        set current_fd $fd
 400}
 401
 402method _history_menu {} {
 403        set m $w.backmenu
 404        if {[winfo exists $m]} {
 405                $m delete 0 end
 406        } else {
 407                menu $m -tearoff 0
 408        }
 409
 410        for {set i [expr {[llength $history] - 1}]
 411                } {$i >= 0} {incr i -1} {
 412                set e [lindex $history $i]
 413                set c [lindex $e 0]
 414                set f [lindex $e 1]
 415
 416                if {[regexp {^[0-9a-f]{40}$} $c]} {
 417                        set t [string range $c 0 8]...
 418                } elseif {$c eq {}} {
 419                        set t {Working Directory}
 420                } else {
 421                        set t $c
 422                }
 423                if {![catch {set summary $header($c,summary)}]} {
 424                        append t " $summary"
 425                        if {[string length $t] > 70} {
 426                                set t [string range $t 0 66]...
 427                        }
 428                }
 429
 430                $m add command -label $t -command [cb _goback $i]
 431        }
 432        set X [winfo rootx $w_back]
 433        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 434        tk_popup $m $X $Y
 435}
 436
 437method _goback {i} {
 438        set dat [lindex $history $i]
 439        set history [lrange $history 0 [expr {$i - 1}]]
 440        set commit [lindex $dat 0]
 441        set path [lindex $dat 1]
 442        _load $this [lrange $dat 2 5]
 443}
 444
 445method _read_file {fd jump} {
 446        if {$fd ne $current_fd} {
 447                catch {close $fd}
 448                return
 449        }
 450
 451        foreach i $w_columns {$i conf -state normal}
 452        while {[gets $fd line] >= 0} {
 453                regsub "\r\$" $line {} line
 454                incr total_lines
 455                lappend amov_data {}
 456                lappend asim_data {}
 457
 458                if {$total_lines > 1} {
 459                        foreach i $w_columns {$i insert end "\n"}
 460                }
 461
 462                $w_line insert end "$total_lines" linenumber
 463                $w_file insert end "$line"
 464        }
 465
 466        set ln_wc [expr {[string length $total_lines] + 2}]
 467        if {[$w_line cget -width] < $ln_wc} {
 468                $w_line conf -width $ln_wc
 469        }
 470
 471        foreach i $w_columns {$i conf -state disabled}
 472
 473        if {[eof $fd]} {
 474                close $fd
 475
 476                # If we don't force Tk to update the widgets *right now*
 477                # none of our jump commands will cause a change in the UI.
 478                #
 479                update
 480
 481                if {[llength $jump] == 1} {
 482                        set highlight_line [lindex $jump 0]
 483                        $w_file see "$highlight_line.0"
 484                } elseif {[llength $jump] == 4} {
 485                        set highlight_column [lindex $jump 0]
 486                        set highlight_line [lindex $jump 1]
 487                        $w_file xview moveto [lindex $jump 2]
 488                        $w_file yview moveto [lindex $jump 3]
 489                }
 490
 491                _exec_blame $this $w_asim @asim_data \
 492                        [list] \
 493                        { copy/move tracking}
 494        }
 495} ifdeleted { catch {close $fd} }
 496
 497method _exec_blame {cur_w cur_d options cur_s} {
 498        set cmd [list]
 499        if {![is_Windows] || [is_Cygwin]} {
 500                lappend cmd nice
 501        }
 502        lappend cmd git blame
 503        set cmd [concat $cmd $options]
 504        lappend cmd --incremental
 505        if {$commit eq {}} {
 506                lappend cmd --contents $path
 507        } else {
 508                lappend cmd $commit
 509        }
 510        lappend cmd -- $path
 511        set fd [open "| $cmd" r]
 512        fconfigure $fd -blocking 0 -translation lf -encoding binary
 513        fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d $cur_s]
 514        set current_fd $fd
 515        set blame_lines 0
 516        _status $this $cur_s
 517}
 518
 519method _read_blame {fd cur_w cur_d cur_s} {
 520        upvar #0 $cur_d line_data
 521        variable group_colors
 522        variable original_options
 523
 524        if {$fd ne $current_fd} {
 525                catch {close $fd}
 526                return
 527        }
 528
 529        $cur_w conf -state normal
 530        while {[gets $fd line] >= 0} {
 531                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 532                        cmit original_line final_line line_count]} {
 533                        set r_commit     $cmit
 534                        set r_orig_line  $original_line
 535                        set r_final_line $final_line
 536                        set r_line_count $line_count
 537                } elseif {[string match {filename *} $line]} {
 538                        set file [string range $line 9 end]
 539                        set n    $r_line_count
 540                        set lno  $r_final_line
 541                        set oln  $r_orig_line
 542                        set cmit $r_commit
 543
 544                        if {[regexp {^0{40}$} $cmit]} {
 545                                set commit_abbr work
 546                                set commit_type curr_commit
 547                        } elseif {$cmit eq $commit} {
 548                                set commit_abbr this
 549                                set commit_type curr_commit
 550                        } else {
 551                                set commit_type prior_commit
 552                                set commit_abbr [string range $cmit 0 3]
 553                        }
 554
 555                        set author_abbr {}
 556                        set a_name {}
 557                        catch {set a_name $header($cmit,author)}
 558                        while {$a_name ne {}} {
 559                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 560                                append author_abbr $_a
 561                                unset _a
 562                                if {![regsub \
 563                                        {^[[:upper:]][^\s]*\s+} \
 564                                        $a_name {} a_name ]} break
 565                        }
 566                        if {$author_abbr eq {}} {
 567                                set author_abbr { |}
 568                        } else {
 569                                set author_abbr [string range $author_abbr 0 3]
 570                        }
 571                        unset a_name
 572
 573                        set first_lno $lno
 574                        while {
 575                           $first_lno > 1
 576                        && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
 577                        && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
 578                        } {
 579                                incr first_lno -1
 580                        }
 581
 582                        set color {}
 583                        if {$first_lno < $lno} {
 584                                foreach g [$w_file tag names $first_lno.0] {
 585                                        if {[regexp {^color[0-9]+$} $g]} {
 586                                                set color $g
 587                                                break
 588                                        }
 589                                }
 590                        } else {
 591                                set i [lsort [concat \
 592                                        [$w_file tag names "[expr {$first_lno - 1}].0"] \
 593                                        [$w_file tag names "[expr {$lno + $n}].0"] \
 594                                        ]]
 595                                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 596                                        if {[lsearch -sorted -exact $i color$g] == -1} {
 597                                                set color color$g
 598                                                break
 599                                        }
 600                                }
 601                        }
 602                        if {$color eq {}} {
 603                                set color color0
 604                        }
 605
 606                        while {$n > 0} {
 607                                set lno_e "$lno.0 lineend + 1c"
 608                                if {[lindex $line_data $lno] ne {}} {
 609                                        set g [lindex $line_data $lno 0]
 610                                        foreach i $w_columns {
 611                                                $i tag remove g$g $lno.0 $lno_e
 612                                        }
 613                                }
 614                                lset line_data $lno [list $cmit $file $oln]
 615
 616                                $cur_w delete $lno.0 "$lno.0 lineend"
 617                                if {$lno == $first_lno} {
 618                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 619                                } elseif {$lno == [expr {$first_lno + 1}]} {
 620                                        $cur_w insert $lno.0 $author_abbr author_abbr
 621                                } else {
 622                                        $cur_w insert $lno.0 { |}
 623                                }
 624
 625                                foreach i $w_columns {
 626                                        if {$cur_w eq $w_amov} {
 627                                                for {set g 0} \
 628                                                        {$g < [llength $group_colors]} \
 629                                                        {incr g} {
 630                                                        $i tag remove color$g $lno.0 $lno_e
 631                                                }
 632                                                $i tag add $color $lno.0 $lno_e
 633                                        }
 634                                        $i tag add g$cmit $lno.0 $lno_e
 635                                }
 636
 637                                if {$highlight_column eq $cur_w} {
 638                                        if {$highlight_line == -1
 639                                         && [lindex [$w_file yview] 0] == 0} {
 640                                                $w_file see $lno.0
 641                                                set highlight_line $lno
 642                                        }
 643                                        if {$highlight_line == $lno} {
 644                                                _showcommit $this $cur_w $lno
 645                                        }
 646                                }
 647
 648                                incr n -1
 649                                incr lno
 650                                incr oln
 651                                incr blame_lines
 652                        }
 653
 654                        while {
 655                           $cmit eq [lindex $line_data $lno 0]
 656                        && $file eq [lindex $line_data $lno 1]
 657                        } {
 658                                $cur_w delete $lno.0 "$lno.0 lineend"
 659
 660                                if {$lno == $first_lno} {
 661                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 662                                } elseif {$lno == [expr {$first_lno + 1}]} {
 663                                        $cur_w insert $lno.0 $author_abbr author_abbr
 664                                } else {
 665                                        $cur_w insert $lno.0 { |}
 666                                }
 667
 668                                if {$cur_w eq $w_amov} {
 669                                        foreach i $w_columns {
 670                                                for {set g 0} \
 671                                                        {$g < [llength $group_colors]} \
 672                                                        {incr g} {
 673                                                        $i tag remove color$g $lno.0 $lno_e
 674                                                }
 675                                                $i tag add $color $lno.0 $lno_e
 676                                        }
 677                                }
 678
 679                                incr lno
 680                        }
 681
 682                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 683                        set header($r_commit,$key) $data
 684                }
 685        }
 686        $cur_w conf -state disabled
 687
 688        if {[eof $fd]} {
 689                close $fd
 690                if {$cur_w eq $w_asim} {
 691                        _exec_blame $this $w_amov @amov_data \
 692                                $original_options \
 693                                { original location}
 694                } else {
 695                        set current_fd {}
 696                        set status {Annotation complete.}
 697                        destroy $w.status.c
 698                }
 699        } else {
 700                _status $this $cur_s
 701        }
 702} ifdeleted { catch {close $fd} }
 703
 704method _status {cur_s} {
 705        set have  $blame_lines
 706        set total $total_lines
 707        set pdone 0
 708        if {$total} {set pdone [expr {100 * $have / $total}]}
 709
 710        set status [format \
 711                "Loading%s annotations... %i of %i lines annotated (%2i%%)" \
 712                $cur_s $have $total $pdone]
 713        $w.status.c coords bar 0 0 $pdone 20
 714}
 715
 716method _click {cur_w pos} {
 717        set lno [lindex [split [$cur_w index $pos] .] 0]
 718        _showcommit $this $cur_w $lno
 719}
 720
 721method _load_commit {cur_w cur_d pos} {
 722        upvar #0 $cur_d line_data
 723        set lno [lindex [split [$cur_w index $pos] .] 0]
 724        set dat [lindex $line_data $lno]
 725        if {$dat ne {}} {
 726                lappend history [list \
 727                        $commit $path \
 728                        $highlight_column \
 729                        $highlight_line \
 730                        [lindex [$w_file xview] 0] \
 731                        [lindex [$w_file yview] 0] \
 732                        ]
 733                set commit [lindex $dat 0]
 734                set path   [lindex $dat 1]
 735                _load $this [list [lindex $dat 2]]
 736        }
 737}
 738
 739method _showcommit {cur_w lno} {
 740        global repo_config
 741        variable active_color
 742
 743        if {$highlight_commit ne {}} {
 744                foreach i $w_columns {
 745                        $i tag conf g$highlight_commit -background {}
 746                        $i tag lower g$highlight_commit
 747                }
 748        }
 749
 750        if {$cur_w eq $w_asim} {
 751                set dat [lindex $asim_data $lno]
 752                set highlight_column $w_asim
 753        } else {
 754                set dat [lindex $amov_data $lno]
 755                set highlight_column $w_amov
 756        }
 757
 758        $w_cviewer conf -state normal
 759        $w_cviewer delete 0.0 end
 760
 761        if {$dat eq {}} {
 762                set cmit {}
 763                $w_cviewer insert end "Loading annotation..." still_loading
 764        } else {
 765                set cmit [lindex $dat 0]
 766                set file [lindex $dat 1]
 767
 768                foreach i $w_columns {
 769                        $i tag conf g$cmit -background $active_color
 770                        $i tag raise g$cmit
 771                }
 772
 773                set author_name {}
 774                set author_email {}
 775                set author_time {}
 776                catch {set author_name $header($cmit,author)}
 777                catch {set author_email $header($cmit,author-mail)}
 778                catch {set author_time [clock format \
 779                        $header($cmit,author-time) \
 780                        -format {%Y-%m-%d %H:%M:%S}
 781                ]}
 782
 783                set committer_name {}
 784                set committer_email {}
 785                set committer_time {}
 786                catch {set committer_name $header($cmit,committer)}
 787                catch {set committer_email $header($cmit,committer-mail)}
 788                catch {set committer_time [clock format \
 789                        $header($cmit,committer-time) \
 790                        -format {%Y-%m-%d %H:%M:%S}
 791                ]}
 792
 793                if {[catch {set msg $header($cmit,message)}]} {
 794                        set msg {}
 795                        catch {
 796                                set fd [open "| git cat-file commit $cmit" r]
 797                                fconfigure $fd -encoding binary -translation lf
 798                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 799                                        set enc utf-8
 800                                }
 801                                while {[gets $fd line] > 0} {
 802                                        if {[string match {encoding *} $line]} {
 803                                                set enc [string tolower [string range $line 9 end]]
 804                                        }
 805                                }
 806                                set msg [encoding convertfrom $enc [read $fd]]
 807                                set msg [string trim $msg]
 808                                close $fd
 809
 810                                set author_name [encoding convertfrom $enc $author_name]
 811                                set committer_name [encoding convertfrom $enc $committer_name]
 812
 813                                set header($cmit,author) $author_name
 814                                set header($cmit,committer) $committer_name
 815                        }
 816                        set header($cmit,message) $msg
 817                }
 818
 819                $w_cviewer insert end "commit $cmit\n" header_key
 820                $w_cviewer insert end "Author:\t" header_key
 821                $w_cviewer insert end "$author_name $author_email" header_val
 822                $w_cviewer insert end "  $author_time\n" header_val
 823
 824                $w_cviewer insert end "Committer:\t" header_key
 825                $w_cviewer insert end "$committer_name $committer_email" header_val
 826                $w_cviewer insert end "  $committer_time\n" header_val
 827
 828                if {$file ne $path} {
 829                        $w_cviewer insert end "Original File:\t" header_key
 830                        $w_cviewer insert end "[escape_path $file]\n" header_val
 831                }
 832
 833                $w_cviewer insert end "\n$msg"
 834        }
 835        $w_cviewer conf -state disabled
 836
 837        set highlight_line $lno
 838        set highlight_commit $cmit
 839
 840        if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
 841                _hide_tooltip $this
 842        }
 843}
 844
 845method _copycommit {} {
 846        set pos @$::cursorX,$::cursorY
 847        set lno [lindex [split [$::cursorW index $pos] .] 0]
 848        set dat [lindex $amov_data $lno]
 849        if {$dat ne {}} {
 850                clipboard clear
 851                clipboard append \
 852                        -format STRING \
 853                        -type STRING \
 854                        -- [lindex $dat 0]
 855        }
 856}
 857
 858method _show_tooltip {cur_w pos} {
 859        if {$tooltip_wm ne {}} {
 860                _open_tooltip $this $cur_w
 861        } elseif {$tooltip_timer eq {}} {
 862                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 863        }
 864}
 865
 866method _open_tooltip {cur_w} {
 867        set tooltip_timer {}
 868        set pos_x [winfo pointerx $cur_w]
 869        set pos_y [winfo pointery $cur_w]
 870        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 871                _hide_tooltip $this
 872                return
 873        }
 874
 875        if {$tooltip_wm ne "$cur_w.tooltip"} {
 876                _hide_tooltip $this
 877
 878                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 879                wm overrideredirect $tooltip_wm 1
 880                wm transient $tooltip_wm [winfo toplevel $cur_w]
 881                set tooltip_t $tooltip_wm.label
 882                text $tooltip_t \
 883                        -takefocus 0 \
 884                        -highlightthickness 0 \
 885                        -relief flat \
 886                        -borderwidth 0 \
 887                        -wrap none \
 888                        -background lightyellow \
 889                        -foreground black
 890                $tooltip_t tag conf section_header -font font_uibold
 891                pack $tooltip_t
 892        } else {
 893                $tooltip_t conf -state normal
 894                $tooltip_t delete 0.0 end
 895        }
 896
 897        set pos @[join [list \
 898                [expr {$pos_x - [winfo rootx $cur_w]}] \
 899                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 900        set lno [lindex [split [$cur_w index $pos] .] 0]
 901        if {$cur_w eq $w_amov} {
 902                set dat [lindex $amov_data $lno]
 903                set org {}
 904        } else {
 905                set dat [lindex $asim_data $lno]
 906                set org [lindex $amov_data $lno]
 907        }
 908
 909        set cmit [lindex $dat 0]
 910        set tooltip_commit [list $cmit]
 911
 912        set author_name {}
 913        set summary     {}
 914        set author_time {}
 915        catch {set author_name $header($cmit,author)}
 916        catch {set summary     $header($cmit,summary)}
 917        catch {set author_time [clock format \
 918                $header($cmit,author-time) \
 919                -format {%Y-%m-%d %H:%M:%S}
 920        ]}
 921
 922        $tooltip_t insert end "commit $cmit\n"
 923        $tooltip_t insert end "$author_name  $author_time\n"
 924        $tooltip_t insert end "$summary"
 925
 926        if {$org ne {} && [lindex $org 0] ne $cmit} {
 927                set save [$tooltip_t get 0.0 end]
 928                $tooltip_t delete 0.0 end
 929
 930                set cmit [lindex $org 0]
 931                set file [lindex $org 1]
 932                lappend tooltip_commit $cmit
 933
 934                set author_name {}
 935                set summary     {}
 936                set author_time {}
 937                catch {set author_name $header($cmit,author)}
 938                catch {set summary     $header($cmit,summary)}
 939                catch {set author_time [clock format \
 940                        $header($cmit,author-time) \
 941                        -format {%Y-%m-%d %H:%M:%S}
 942                ]}
 943
 944                $tooltip_t insert end "Originally By:\n" section_header
 945                $tooltip_t insert end "commit $cmit\n"
 946                $tooltip_t insert end "$author_name  $author_time\n"
 947                $tooltip_t insert end "$summary\n"
 948
 949                if {$file ne $path} {
 950                        $tooltip_t insert end "In File: " section_header
 951                        $tooltip_t insert end "$file\n"
 952                }
 953
 954                $tooltip_t insert end "\n"
 955                $tooltip_t insert end "Copied Or Moved Here By:\n" section_header
 956                $tooltip_t insert end $save
 957        }
 958
 959        $tooltip_t conf -state disabled
 960        _position_tooltip $this
 961}
 962
 963method _position_tooltip {} {
 964        set max_h [lindex [split [$tooltip_t index end] .] 0]
 965        set max_w 0
 966        for {set i 1} {$i <= $max_h} {incr i} {
 967                set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
 968                if {$c > $max_w} {set max_w $c}
 969        }
 970        $tooltip_t conf -width $max_w -height $max_h
 971
 972        set req_w [winfo reqwidth  $tooltip_t]
 973        set req_h [winfo reqheight $tooltip_t]
 974        set pos_x [expr {[winfo pointerx .] +  5}]
 975        set pos_y [expr {[winfo pointery .] + 10}]
 976
 977        set g "${req_w}x${req_h}"
 978        if {$pos_x >= 0} {append g +}
 979        append g $pos_x
 980        if {$pos_y >= 0} {append g +}
 981        append g $pos_y
 982
 983        wm geometry $tooltip_wm $g
 984        raise $tooltip_wm
 985}
 986
 987method _hide_tooltip {} {
 988        if {$tooltip_wm ne {}} {
 989                destroy $tooltip_wm
 990                set tooltip_wm {}
 991                set tooltip_commit {}
 992        }
 993        if {$tooltip_timer ne {}} {
 994                after cancel $tooltip_timer
 995                set tooltip_timer {}
 996        }
 997}
 998
 999method _resize {new_height} {
1000        set diff [expr {$new_height - $old_height}]
1001        if {$diff == 0} return
1002
1003        set my [expr {[winfo height $w.file_pane] - 25}]
1004        set o [$w.file_pane sash coord 0]
1005        set ox [lindex $o 0]
1006        set oy [expr {[lindex $o 1] + $diff}]
1007        if {$oy < 0}   {set oy 0}
1008        if {$oy > $my} {set oy $my}
1009        $w.file_pane sash place 0 $ox $oy
1010
1011        set old_height $new_height
1012}
1013
1014}