git-gui / lib / blame.tclon commit Merge branch 'maint' of git://repo.or.cz/git-gui into maint (1b2782a)
   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        if {$req_w < 600} {set req_w 600}
 308        if {$req_h < 400} {set req_h 400}
 309        set g "${req_w}x${req_h}"
 310        wm geometry $top $g
 311        update
 312
 313        set old_height [winfo height $w.file_pane]
 314        $w.file_pane sash place 0 \
 315                [lindex [$w.file_pane sash coord 0] 0] \
 316                [expr {int($old_height * 0.70)}]
 317        bind $w.file_pane <Configure> \
 318        "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
 319
 320        _load $this {}
 321}
 322
 323method _load {jump} {
 324        variable group_colors
 325
 326        _hide_tooltip $this
 327
 328        if {$total_lines != 0 || $current_fd ne {}} {
 329                if {$current_fd ne {}} {
 330                        catch {close $current_fd}
 331                        set current_fd {}
 332                }
 333
 334                foreach i $w_columns {
 335                        $i conf -state normal
 336                        $i delete 0.0 end
 337                        foreach g [$i tag names] {
 338                                if {[regexp {^g[0-9a-f]{40}$} $g]} {
 339                                        $i tag delete $g
 340                                }
 341                        }
 342                        $i conf -state disabled
 343                }
 344
 345                $w_cviewer conf -state normal
 346                $w_cviewer delete 0.0 end
 347                $w_cviewer conf -state disabled
 348
 349                set highlight_line -1
 350                set highlight_column {}
 351                set highlight_commit {}
 352                set total_lines 0
 353        }
 354
 355        if {[winfo exists $w.status.c]} {
 356                $w.status.c coords bar 0 0 0 20
 357        } else {
 358                canvas $w.status.c \
 359                        -width 100 \
 360                        -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
 361                        -borderwidth 1 \
 362                        -relief groove \
 363                        -highlightt 0
 364                $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
 365                pack $w.status.c -side right
 366        }
 367
 368        if {$history eq {}} {
 369                $w_back conf -state disabled
 370        } else {
 371                $w_back conf -state normal
 372        }
 373
 374        # Index 0 is always empty.  There is never line 0 as
 375        # we use only 1 based lines, as that matches both with
 376        # git-blame output and with Tk's text widget.
 377        #
 378        set amov_data [list [list]]
 379        set asim_data [list [list]]
 380
 381        set status "Loading $commit:[escape_path $path]..."
 382        $w_path conf -text [escape_path $path]
 383        if {$commit eq {}} {
 384                set fd [open $path r]
 385        } else {
 386                set cmd [list git cat-file blob "$commit:$path"]
 387                set fd [open "| $cmd" r]
 388        }
 389        fconfigure $fd -blocking 0 -translation lf -encoding binary
 390        fileevent $fd readable [cb _read_file $fd $jump]
 391        set current_fd $fd
 392}
 393
 394method _history_menu {} {
 395        set m $w.backmenu
 396        if {[winfo exists $m]} {
 397                $m delete 0 end
 398        } else {
 399                menu $m -tearoff 0
 400        }
 401
 402        for {set i [expr {[llength $history] - 1}]
 403                } {$i >= 0} {incr i -1} {
 404                set e [lindex $history $i]
 405                set c [lindex $e 0]
 406                set f [lindex $e 1]
 407
 408                if {[regexp {^[0-9a-f]{40}$} $c]} {
 409                        set t [string range $c 0 8]...
 410                } elseif {$c eq {}} {
 411                        set t {Working Directory}
 412                } else {
 413                        set t $c
 414                }
 415                if {![catch {set summary $header($c,summary)}]} {
 416                        append t " $summary"
 417                        if {[string length $t] > 70} {
 418                                set t [string range $t 0 66]...
 419                        }
 420                }
 421
 422                $m add command -label $t -command [cb _goback $i]
 423        }
 424        set X [winfo rootx $w_back]
 425        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 426        tk_popup $m $X $Y
 427}
 428
 429method _goback {i} {
 430        set dat [lindex $history $i]
 431        set history [lrange $history 0 [expr {$i - 1}]]
 432        set commit [lindex $dat 0]
 433        set path [lindex $dat 1]
 434        _load $this [lrange $dat 2 5]
 435}
 436
 437method _read_file {fd jump} {
 438        if {$fd ne $current_fd} {
 439                catch {close $fd}
 440                return
 441        }
 442
 443        foreach i $w_columns {$i conf -state normal}
 444        while {[gets $fd line] >= 0} {
 445                regsub "\r\$" $line {} line
 446                incr total_lines
 447                lappend amov_data {}
 448                lappend asim_data {}
 449
 450                if {$total_lines > 1} {
 451                        foreach i $w_columns {$i insert end "\n"}
 452                }
 453
 454                $w_line insert end "$total_lines" linenumber
 455                $w_file insert end "$line"
 456        }
 457
 458        set ln_wc [expr {[string length $total_lines] + 2}]
 459        if {[$w_line cget -width] < $ln_wc} {
 460                $w_line conf -width $ln_wc
 461        }
 462
 463        foreach i $w_columns {$i conf -state disabled}
 464
 465        if {[eof $fd]} {
 466                close $fd
 467
 468                # If we don't force Tk to update the widgets *right now*
 469                # none of our jump commands will cause a change in the UI.
 470                #
 471                update
 472
 473                if {[llength $jump] == 1} {
 474                        set highlight_line [lindex $jump 0]
 475                        $w_file see "$highlight_line.0"
 476                } elseif {[llength $jump] == 4} {
 477                        set highlight_column [lindex $jump 0]
 478                        set highlight_line [lindex $jump 1]
 479                        $w_file xview moveto [lindex $jump 2]
 480                        $w_file yview moveto [lindex $jump 3]
 481                }
 482
 483                _exec_blame $this $w_asim @asim_data \
 484                        [list] \
 485                        { copy/move tracking}
 486        }
 487} ifdeleted { catch {close $fd} }
 488
 489method _exec_blame {cur_w cur_d options cur_s} {
 490        set cmd [list]
 491        if {![is_Windows] || [is_Cygwin]} {
 492                lappend cmd nice
 493        }
 494        lappend cmd git blame
 495        set cmd [concat $cmd $options]
 496        lappend cmd --incremental
 497        if {$commit eq {}} {
 498                lappend cmd --contents $path
 499        } else {
 500                lappend cmd $commit
 501        }
 502        lappend cmd -- $path
 503        set fd [open "| $cmd" r]
 504        fconfigure $fd -blocking 0 -translation lf -encoding binary
 505        fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d $cur_s]
 506        set current_fd $fd
 507        set blame_lines 0
 508        _status $this $cur_s
 509}
 510
 511method _read_blame {fd cur_w cur_d cur_s} {
 512        upvar #0 $cur_d line_data
 513        variable group_colors
 514
 515        if {$fd ne $current_fd} {
 516                catch {close $fd}
 517                return
 518        }
 519
 520        $cur_w conf -state normal
 521        while {[gets $fd line] >= 0} {
 522                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 523                        cmit original_line final_line line_count]} {
 524                        set r_commit     $cmit
 525                        set r_orig_line  $original_line
 526                        set r_final_line $final_line
 527                        set r_line_count $line_count
 528                } elseif {[string match {filename *} $line]} {
 529                        set file [string range $line 9 end]
 530                        set n    $r_line_count
 531                        set lno  $r_final_line
 532                        set oln  $r_orig_line
 533                        set cmit $r_commit
 534
 535                        if {[regexp {^0{40}$} $cmit]} {
 536                                set commit_abbr work
 537                                set commit_type curr_commit
 538                        } elseif {$cmit eq $commit} {
 539                                set commit_abbr this
 540                                set commit_type curr_commit
 541                        } else {
 542                                set commit_type prior_commit
 543                                set commit_abbr [string range $cmit 0 3]
 544                        }
 545
 546                        set author_abbr {}
 547                        set a_name {}
 548                        catch {set a_name $header($cmit,author)}
 549                        while {$a_name ne {}} {
 550                                if {$author_abbr ne {}
 551                                        && [string index $a_name 0] eq {'}} {
 552                                        regsub {^'[^']+'\s+} $a_name {} a_name
 553                                }
 554                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 555                                append author_abbr $_a
 556                                unset _a
 557                                if {![regsub \
 558                                        {^[[:upper:]][^\s]*\s+} \
 559                                        $a_name {} a_name ]} break
 560                        }
 561                        if {$author_abbr eq {}} {
 562                                set author_abbr { |}
 563                        } else {
 564                                set author_abbr [string range $author_abbr 0 3]
 565                        }
 566                        unset a_name
 567
 568                        set first_lno $lno
 569                        while {
 570                           $first_lno > 1
 571                        && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
 572                        && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
 573                        } {
 574                                incr first_lno -1
 575                        }
 576
 577                        set color {}
 578                        if {$first_lno < $lno} {
 579                                foreach g [$w_file tag names $first_lno.0] {
 580                                        if {[regexp {^color[0-9]+$} $g]} {
 581                                                set color $g
 582                                                break
 583                                        }
 584                                }
 585                        } else {
 586                                set i [lsort [concat \
 587                                        [$w_file tag names "[expr {$first_lno - 1}].0"] \
 588                                        [$w_file tag names "[expr {$lno + $n}].0"] \
 589                                        ]]
 590                                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 591                                        if {[lsearch -sorted -exact $i color$g] == -1} {
 592                                                set color color$g
 593                                                break
 594                                        }
 595                                }
 596                        }
 597                        if {$color eq {}} {
 598                                set color color0
 599                        }
 600
 601                        while {$n > 0} {
 602                                set lno_e "$lno.0 lineend + 1c"
 603                                if {[lindex $line_data $lno] ne {}} {
 604                                        set g [lindex $line_data $lno 0]
 605                                        foreach i $w_columns {
 606                                                $i tag remove g$g $lno.0 $lno_e
 607                                        }
 608                                }
 609                                lset line_data $lno [list $cmit $file $oln]
 610
 611                                $cur_w delete $lno.0 "$lno.0 lineend"
 612                                if {$lno == $first_lno} {
 613                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 614                                } elseif {$lno == [expr {$first_lno + 1}]} {
 615                                        $cur_w insert $lno.0 $author_abbr author_abbr
 616                                } else {
 617                                        $cur_w insert $lno.0 { |}
 618                                }
 619
 620                                foreach i $w_columns {
 621                                        if {$cur_w eq $w_amov} {
 622                                                for {set g 0} \
 623                                                        {$g < [llength $group_colors]} \
 624                                                        {incr g} {
 625                                                        $i tag remove color$g $lno.0 $lno_e
 626                                                }
 627                                                $i tag add $color $lno.0 $lno_e
 628                                        }
 629                                        $i tag add g$cmit $lno.0 $lno_e
 630                                }
 631
 632                                if {$highlight_column eq $cur_w} {
 633                                        if {$highlight_line == -1
 634                                         && [lindex [$w_file yview] 0] == 0} {
 635                                                $w_file see $lno.0
 636                                                set highlight_line $lno
 637                                        }
 638                                        if {$highlight_line == $lno} {
 639                                                _showcommit $this $cur_w $lno
 640                                        }
 641                                }
 642
 643                                incr n -1
 644                                incr lno
 645                                incr oln
 646                                incr blame_lines
 647                        }
 648
 649                        while {
 650                           $cmit eq [lindex $line_data $lno 0]
 651                        && $file eq [lindex $line_data $lno 1]
 652                        } {
 653                                $cur_w delete $lno.0 "$lno.0 lineend"
 654
 655                                if {$lno == $first_lno} {
 656                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 657                                } elseif {$lno == [expr {$first_lno + 1}]} {
 658                                        $cur_w insert $lno.0 $author_abbr author_abbr
 659                                } else {
 660                                        $cur_w insert $lno.0 { |}
 661                                }
 662
 663                                if {$cur_w eq $w_amov} {
 664                                        foreach i $w_columns {
 665                                                for {set g 0} \
 666                                                        {$g < [llength $group_colors]} \
 667                                                        {incr g} {
 668                                                        $i tag remove color$g $lno.0 $lno_e
 669                                                }
 670                                                $i tag add $color $lno.0 $lno_e
 671                                        }
 672                                }
 673
 674                                incr lno
 675                        }
 676
 677                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 678                        set header($r_commit,$key) $data
 679                }
 680        }
 681        $cur_w conf -state disabled
 682
 683        if {[eof $fd]} {
 684                close $fd
 685                if {$cur_w eq $w_asim} {
 686                        _exec_blame $this $w_amov @amov_data \
 687                                [list -M -C -C] \
 688                                { original location}
 689                } else {
 690                        set current_fd {}
 691                        set status {Annotation complete.}
 692                        destroy $w.status.c
 693                }
 694        } else {
 695                _status $this $cur_s
 696        }
 697} ifdeleted { catch {close $fd} }
 698
 699method _status {cur_s} {
 700        set have  $blame_lines
 701        set total $total_lines
 702        set pdone 0
 703        if {$total} {set pdone [expr {100 * $have / $total}]}
 704
 705        set status [format \
 706                "Loading%s annotations... %i of %i lines annotated (%2i%%)" \
 707                $cur_s $have $total $pdone]
 708        $w.status.c coords bar 0 0 $pdone 20
 709}
 710
 711method _click {cur_w pos} {
 712        set lno [lindex [split [$cur_w index $pos] .] 0]
 713        _showcommit $this $cur_w $lno
 714}
 715
 716method _load_commit {cur_w cur_d pos} {
 717        upvar #0 $cur_d line_data
 718        set lno [lindex [split [$cur_w index $pos] .] 0]
 719        set dat [lindex $line_data $lno]
 720        if {$dat ne {}} {
 721                lappend history [list \
 722                        $commit $path \
 723                        $highlight_column \
 724                        $highlight_line \
 725                        [lindex [$w_file xview] 0] \
 726                        [lindex [$w_file yview] 0] \
 727                        ]
 728                set commit [lindex $dat 0]
 729                set path   [lindex $dat 1]
 730                _load $this [list [lindex $dat 2]]
 731        }
 732}
 733
 734method _showcommit {cur_w lno} {
 735        global repo_config
 736        variable active_color
 737
 738        if {$highlight_commit ne {}} {
 739                foreach i $w_columns {
 740                        $i tag conf g$highlight_commit -background {}
 741                        $i tag lower g$highlight_commit
 742                }
 743        }
 744
 745        if {$cur_w eq $w_asim} {
 746                set dat [lindex $asim_data $lno]
 747                set highlight_column $w_asim
 748        } else {
 749                set dat [lindex $amov_data $lno]
 750                set highlight_column $w_amov
 751        }
 752
 753        $w_cviewer conf -state normal
 754        $w_cviewer delete 0.0 end
 755
 756        if {$dat eq {}} {
 757                set cmit {}
 758                $w_cviewer insert end "Loading annotation..." still_loading
 759        } else {
 760                set cmit [lindex $dat 0]
 761                set file [lindex $dat 1]
 762
 763                foreach i $w_columns {
 764                        $i tag conf g$cmit -background $active_color
 765                        $i tag raise g$cmit
 766                }
 767
 768                set author_name {}
 769                set author_email {}
 770                set author_time {}
 771                catch {set author_name $header($cmit,author)}
 772                catch {set author_email $header($cmit,author-mail)}
 773                catch {set author_time [clock format \
 774                        $header($cmit,author-time) \
 775                        -format {%Y-%m-%d %H:%M:%S}
 776                ]}
 777
 778                set committer_name {}
 779                set committer_email {}
 780                set committer_time {}
 781                catch {set committer_name $header($cmit,committer)}
 782                catch {set committer_email $header($cmit,committer-mail)}
 783                catch {set committer_time [clock format \
 784                        $header($cmit,committer-time) \
 785                        -format {%Y-%m-%d %H:%M:%S}
 786                ]}
 787
 788                if {[catch {set msg $header($cmit,message)}]} {
 789                        set msg {}
 790                        catch {
 791                                set fd [open "| git cat-file commit $cmit" r]
 792                                fconfigure $fd -encoding binary -translation lf
 793                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 794                                        set enc utf-8
 795                                }
 796                                while {[gets $fd line] > 0} {
 797                                        if {[string match {encoding *} $line]} {
 798                                                set enc [string tolower [string range $line 9 end]]
 799                                        }
 800                                }
 801                                set msg [encoding convertfrom $enc [read $fd]]
 802                                set msg [string trim $msg]
 803                                close $fd
 804
 805                                set author_name [encoding convertfrom $enc $author_name]
 806                                set committer_name [encoding convertfrom $enc $committer_name]
 807
 808                                set header($cmit,author) $author_name
 809                                set header($cmit,committer) $committer_name
 810                        }
 811                        set header($cmit,message) $msg
 812                }
 813
 814                $w_cviewer insert end "commit $cmit\n" header_key
 815                $w_cviewer insert end "Author:\t" header_key
 816                $w_cviewer insert end "$author_name $author_email" header_val
 817                $w_cviewer insert end "  $author_time\n" header_val
 818
 819                $w_cviewer insert end "Committer:\t" header_key
 820                $w_cviewer insert end "$committer_name $committer_email" header_val
 821                $w_cviewer insert end "  $committer_time\n" header_val
 822
 823                if {$file ne $path} {
 824                        $w_cviewer insert end "Original File:\t" header_key
 825                        $w_cviewer insert end "[escape_path $file]\n" header_val
 826                }
 827
 828                $w_cviewer insert end "\n$msg"
 829        }
 830        $w_cviewer conf -state disabled
 831
 832        set highlight_line $lno
 833        set highlight_commit $cmit
 834
 835        if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
 836                _hide_tooltip $this
 837        }
 838}
 839
 840method _copycommit {} {
 841        set pos @$::cursorX,$::cursorY
 842        set lno [lindex [split [$::cursorW index $pos] .] 0]
 843        set dat [lindex $amov_data $lno]
 844        if {$dat ne {}} {
 845                clipboard clear
 846                clipboard append \
 847                        -format STRING \
 848                        -type STRING \
 849                        -- [lindex $dat 0]
 850        }
 851}
 852
 853method _show_tooltip {cur_w pos} {
 854        if {$tooltip_wm ne {}} {
 855                _open_tooltip $this $cur_w
 856        } elseif {$tooltip_timer eq {}} {
 857                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 858        }
 859}
 860
 861method _open_tooltip {cur_w} {
 862        set tooltip_timer {}
 863        set pos_x [winfo pointerx $cur_w]
 864        set pos_y [winfo pointery $cur_w]
 865        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 866                _hide_tooltip $this
 867                return
 868        }
 869
 870        if {$tooltip_wm ne "$cur_w.tooltip"} {
 871                _hide_tooltip $this
 872
 873                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 874                wm overrideredirect $tooltip_wm 1
 875                wm transient $tooltip_wm [winfo toplevel $cur_w]
 876                set tooltip_t $tooltip_wm.label
 877                text $tooltip_t \
 878                        -takefocus 0 \
 879                        -highlightthickness 0 \
 880                        -relief flat \
 881                        -borderwidth 0 \
 882                        -wrap none \
 883                        -background lightyellow \
 884                        -foreground black
 885                $tooltip_t tag conf section_header -font font_uibold
 886                pack $tooltip_t
 887        } else {
 888                $tooltip_t conf -state normal
 889                $tooltip_t delete 0.0 end
 890        }
 891
 892        set pos @[join [list \
 893                [expr {$pos_x - [winfo rootx $cur_w]}] \
 894                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 895        set lno [lindex [split [$cur_w index $pos] .] 0]
 896        if {$cur_w eq $w_amov} {
 897                set dat [lindex $amov_data $lno]
 898                set org {}
 899        } else {
 900                set dat [lindex $asim_data $lno]
 901                set org [lindex $amov_data $lno]
 902        }
 903
 904        set cmit [lindex $dat 0]
 905        set tooltip_commit [list $cmit]
 906
 907        set author_name {}
 908        set summary     {}
 909        set author_time {}
 910        catch {set author_name $header($cmit,author)}
 911        catch {set summary     $header($cmit,summary)}
 912        catch {set author_time [clock format \
 913                $header($cmit,author-time) \
 914                -format {%Y-%m-%d %H:%M:%S}
 915        ]}
 916
 917        $tooltip_t insert end "commit $cmit\n"
 918        $tooltip_t insert end "$author_name  $author_time\n"
 919        $tooltip_t insert end "$summary"
 920
 921        if {$org ne {} && [lindex $org 0] ne $cmit} {
 922                set save [$tooltip_t get 0.0 end]
 923                $tooltip_t delete 0.0 end
 924
 925                set cmit [lindex $org 0]
 926                set file [lindex $org 1]
 927                lappend tooltip_commit $cmit
 928
 929                set author_name {}
 930                set summary     {}
 931                set author_time {}
 932                catch {set author_name $header($cmit,author)}
 933                catch {set summary     $header($cmit,summary)}
 934                catch {set author_time [clock format \
 935                        $header($cmit,author-time) \
 936                        -format {%Y-%m-%d %H:%M:%S}
 937                ]}
 938
 939                $tooltip_t insert end "Originally By:\n" section_header
 940                $tooltip_t insert end "commit $cmit\n"
 941                $tooltip_t insert end "$author_name  $author_time\n"
 942                $tooltip_t insert end "$summary\n"
 943
 944                if {$file ne $path} {
 945                        $tooltip_t insert end "In File: " section_header
 946                        $tooltip_t insert end "$file\n"
 947                }
 948
 949                $tooltip_t insert end "\n"
 950                $tooltip_t insert end "Copied Or Moved Here By:\n" section_header
 951                $tooltip_t insert end $save
 952        }
 953
 954        $tooltip_t conf -state disabled
 955        _position_tooltip $this
 956}
 957
 958method _position_tooltip {} {
 959        set max_h [lindex [split [$tooltip_t index end] .] 0]
 960        set max_w 0
 961        for {set i 1} {$i <= $max_h} {incr i} {
 962                set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
 963                if {$c > $max_w} {set max_w $c}
 964        }
 965        $tooltip_t conf -width $max_w -height $max_h
 966
 967        set req_w [winfo reqwidth  $tooltip_t]
 968        set req_h [winfo reqheight $tooltip_t]
 969        set pos_x [expr {[winfo pointerx .] +  5}]
 970        set pos_y [expr {[winfo pointery .] + 10}]
 971
 972        set g "${req_w}x${req_h}"
 973        if {$pos_x >= 0} {append g +}
 974        append g $pos_x
 975        if {$pos_y >= 0} {append g +}
 976        append g $pos_y
 977
 978        wm geometry $tooltip_wm $g
 979        raise $tooltip_wm
 980}
 981
 982method _hide_tooltip {} {
 983        if {$tooltip_wm ne {}} {
 984                destroy $tooltip_wm
 985                set tooltip_wm {}
 986                set tooltip_commit {}
 987        }
 988        if {$tooltip_timer ne {}} {
 989                after cancel $tooltip_timer
 990                set tooltip_timer {}
 991        }
 992}
 993
 994method _resize {new_height} {
 995        set diff [expr {$new_height - $old_height}]
 996        if {$diff == 0} return
 997
 998        set my [expr {[winfo height $w.file_pane] - 25}]
 999        set o [$w.file_pane sash coord 0]
1000        set ox [lindex $o 0]
1001        set oy [expr {[lindex $o 1] + $diff}]
1002        if {$oy < 0}   {set oy 0}
1003        if {$oy > $my} {set oy $my}
1004        $w.file_pane sash place 0 $ox $oy
1005
1006        set old_height $new_height
1007}
1008
1009}