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