lib / blame.tclon commit git-gui: Automatically expand the line number column as needed (81fb7ef)
   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
   8field commit    ; # input commit to blame
   9field path      ; # input filename to view in $commit
  10field history {}; # viewer history: {commit path}
  11
  12field w          ; # top window in this viewer
  13field w_back     ; # our back button
  14field w_path     ; # label showing the current file path
  15field w_line     ; # text column: all line numbers
  16field w_cgrp     ; # text column: abbreviated commit SHA-1s
  17field w_file     ; # text column: actual file data
  18field w_cmit     ; # pane showing commit message
  19field status     ; # text variable bound to status bar
  20field old_height ; # last known height of $w.file_pane
  21
  22field current_fd       {} ; # background process running
  23field highlight_line   -1 ; # current line selected
  24field highlight_commit {} ; # sha1 of commit selected
  25
  26field total_lines       0  ; # total length of file
  27field blame_lines       0  ; # number of lines computed
  28field commit_count      0  ; # number of commits in $commit_list
  29field commit_list      {}  ; # list of commit sha1 in receipt order
  30field order                ; # array commit -> receipt order
  31field header               ; # array commit,key -> header field
  32field line_commit          ; # array line -> sha1 commit
  33field line_file            ; # array line -> file name
  34
  35field r_commit      ; # commit currently being parsed
  36field r_orig_line   ; # original line number
  37field r_final_line  ; # final line number
  38field r_line_count  ; # lines in this region
  39
  40field tooltip_wm     {} ; # Current tooltip toplevel, if open
  41field tooltip_timer  {} ; # Current timer event for our tooltip
  42field tooltip_commit {} ; # Commit in tooltip
  43field tooltip_text   {} ; # Text in current tooltip
  44
  45variable active_color #c0edc5
  46variable group_colors {
  47        #d6d6d6
  48        #e1e1e1
  49        #ececec
  50}
  51
  52constructor new {i_commit i_path} {
  53        variable active_color
  54        global cursor_ptr
  55
  56        set commit $i_commit
  57        set path   $i_path
  58
  59        make_toplevel top w
  60        wm title $top "[appname] ([reponame]): File Viewer"
  61
  62        frame $w.header -background orange
  63        label $w.header.commit_l \
  64                -text {Commit:} \
  65                -background orange \
  66                -anchor w \
  67                -justify left
  68        set w_back $w.header.commit_b
  69        label $w_back \
  70                -image ::blame::img_back_arrow \
  71                -borderwidth 0 \
  72                -relief flat \
  73                -state disabled \
  74                -background orange \
  75                -activebackground orange
  76        bind $w_back <Button-1> "
  77                if {\[$w_back cget -state\] eq {normal}} {
  78                        [cb _history_menu]
  79                }
  80                "
  81        label $w.header.commit \
  82                -textvariable @commit \
  83                -background orange \
  84                -anchor w \
  85                -justify left
  86        label $w.header.path_l \
  87                -text {File:} \
  88                -background orange \
  89                -anchor w \
  90                -justify left
  91        set w_path $w.header.path
  92        label $w_path \
  93                -background orange \
  94                -anchor w \
  95                -justify left
  96        pack $w.header.commit_l -side left
  97        pack $w_back -side left
  98        pack $w.header.commit -side left
  99        pack $w_path -fill x -side right
 100        pack $w.header.path_l -side right
 101
 102        panedwindow $w.file_pane -orient vertical
 103        frame $w.file_pane.out
 104        frame $w.file_pane.cm
 105        $w.file_pane add $w.file_pane.out \
 106                -sticky nsew \
 107                -minsize 100 \
 108                -height 100 \
 109                -width 100
 110        $w.file_pane add $w.file_pane.cm \
 111                -sticky nsew \
 112                -minsize 25 \
 113                -height 25 \
 114                -width 100
 115
 116        set w_line $w.file_pane.out.linenumber_t
 117        text $w_line \
 118                -takefocus 0 \
 119                -highlightthickness 0 \
 120                -padx 0 -pady 0 \
 121                -background white -borderwidth 0 \
 122                -state disabled \
 123                -wrap none \
 124                -height 40 \
 125                -width 6 \
 126                -font font_diff
 127        $w_line tag conf linenumber -justify right -rmargin 5
 128
 129        set w_cgrp $w.file_pane.out.commit_t
 130        text $w_cgrp \
 131                -takefocus 0 \
 132                -highlightthickness 0 \
 133                -padx 0 -pady 0 \
 134                -background white -borderwidth 0 \
 135                -state disabled \
 136                -wrap none \
 137                -height 40 \
 138                -width 4 \
 139                -font font_diff
 140        $w_cgrp tag conf curr_commit
 141        $w_cgrp tag conf prior_commit \
 142                -foreground blue \
 143                -underline 1
 144        $w_cgrp tag bind prior_commit \
 145                <Button-1> \
 146                "[cb _load_commit @%x,%y];break"
 147
 148        set w_file $w.file_pane.out.file_t
 149        text $w_file \
 150                -takefocus 0 \
 151                -highlightthickness 0 \
 152                -padx 0 -pady 0 \
 153                -background white -borderwidth 0 \
 154                -state disabled \
 155                -wrap none \
 156                -height 40 \
 157                -width 80 \
 158                -xscrollcommand [list $w.file_pane.out.sbx set] \
 159                -font font_diff
 160
 161        scrollbar $w.file_pane.out.sbx \
 162                -orient h \
 163                -command [list $w_file xview]
 164        scrollbar $w.file_pane.out.sby \
 165                -orient v \
 166                -command [list scrollbar2many [list \
 167                $w_line \
 168                $w_cgrp \
 169                $w_file \
 170                ] yview]
 171        grid \
 172                $w_cgrp \
 173                $w_line \
 174                $w_file \
 175                $w.file_pane.out.sby \
 176                -sticky nsew
 177        grid conf $w.file_pane.out.sbx -column 2 -sticky we
 178        grid columnconfigure $w.file_pane.out 2 -weight 1
 179        grid rowconfigure $w.file_pane.out 0 -weight 1
 180
 181        set w_cmit $w.file_pane.cm.t
 182        text $w_cmit \
 183                -background white -borderwidth 0 \
 184                -state disabled \
 185                -wrap none \
 186                -height 10 \
 187                -width 80 \
 188                -xscrollcommand [list $w.file_pane.cm.sbx set] \
 189                -yscrollcommand [list $w.file_pane.cm.sby set] \
 190                -font font_diff
 191        $w_cmit tag conf header_key \
 192                -tabs {3c} \
 193                -background $active_color \
 194                -font font_uibold
 195        $w_cmit tag conf header_val \
 196                -background $active_color \
 197                -font font_ui
 198        $w_cmit tag raise sel
 199        scrollbar $w.file_pane.cm.sbx \
 200                -orient h \
 201                -command [list $w_cmit xview]
 202        scrollbar $w.file_pane.cm.sby \
 203                -orient v \
 204                -command [list $w_cmit yview]
 205        pack $w.file_pane.cm.sby -side right -fill y
 206        pack $w.file_pane.cm.sbx -side bottom -fill x
 207        pack $w_cmit -expand 1 -fill both
 208
 209        frame $w.status \
 210                -borderwidth 1 \
 211                -relief sunken
 212        label $w.status.l \
 213                -textvariable @status \
 214                -anchor w \
 215                -justify left
 216        pack $w.status.l -side left
 217
 218        menu $w.ctxm -tearoff 0
 219        $w.ctxm add command \
 220                -label "Copy Commit" \
 221                -command [cb _copycommit]
 222
 223        foreach i [list \
 224                $w_cgrp \
 225                $w_line \
 226                $w_file] {
 227                $i conf -cursor $cursor_ptr
 228                $i conf -yscrollcommand \
 229                        [list many2scrollbar [list \
 230                        $w_cgrp \
 231                        $w_line \
 232                        $w_file \
 233                        ] yview $w.file_pane.out.sby]
 234                bind $i <Button-1> "
 235                        [cb _hide_tooltip]
 236                        [cb _click $i @%x,%y]
 237                        focus $i
 238                "
 239                bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
 240                bind $i <Any-Enter>   [cb _hide_tooltip]
 241                bind $i <Any-Leave>   [cb _hide_tooltip]
 242                bind_button3 $i "
 243                        [cb _hide_tooltip]
 244                        set cursorX %x
 245                        set cursorY %y
 246                        set cursorW %W
 247                        tk_popup $w.ctxm %X %Y
 248                "
 249        }
 250
 251        foreach i [list \
 252                $w_cgrp \
 253                $w_line \
 254                $w_file \
 255                $w_cmit] {
 256                bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
 257                bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
 258                bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
 259                bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
 260                bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
 261                bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
 262                bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
 263                bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
 264                bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
 265                bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
 266        }
 267
 268        bind $w_cmit <Button-1> [list focus $w_cmit]
 269        bind $top <Visibility> [list focus $top]
 270        bind $w_file <Destroy> [list delete_this $this]
 271
 272        grid configure $w.header -sticky ew
 273        grid configure $w.file_pane -sticky nsew
 274        grid configure $w.status -sticky ew
 275        grid columnconfigure $top 0 -weight 1
 276        grid rowconfigure $top 0 -weight 0
 277        grid rowconfigure $top 1 -weight 1
 278        grid rowconfigure $top 2 -weight 0
 279
 280        set req_w [winfo reqwidth  $top]
 281        set req_h [winfo reqheight $top]
 282        if {$req_w < 600} {set req_w 600}
 283        if {$req_h < 400} {set req_h 400}
 284        set g "${req_w}x${req_h}"
 285        wm geometry $top $g
 286        update
 287
 288        set old_height [winfo height $w.file_pane]
 289        $w.file_pane sash place 0 \
 290                [lindex [$w.file_pane sash coord 0] 0] \
 291                [expr {int($old_height * 0.70)}]
 292        bind $w.file_pane <Configure> \
 293        "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
 294
 295        _load $this
 296}
 297
 298method _load {} {
 299        _hide_tooltip $this
 300
 301        if {$total_lines != 0 || $current_fd ne {}} {
 302                if {$current_fd ne {}} {
 303                        catch {close $current_fd}
 304                        set current_fd {}
 305                }
 306
 307                set highlight_line -1
 308                set highlight_commit {}
 309                set total_lines 0
 310                set blame_lines 0
 311                set commit_count 0
 312                set commit_list {}
 313                array unset order
 314                array unset line_commit
 315                array unset line_file
 316
 317                $w_cgrp conf -state normal
 318                $w_line conf -state normal
 319                $w_file conf -state normal
 320
 321                $w_cgrp delete 0.0 end
 322                $w_line delete 0.0 end
 323                $w_file delete 0.0 end
 324
 325                $w_cgrp conf -state disabled
 326                $w_line conf -state disabled
 327                $w_file conf -state disabled
 328        }
 329
 330        if {[winfo exists $w.status.c]} {
 331                $w.status.c coords bar 0 0 0 20
 332        } else {
 333                canvas $w.status.c \
 334                        -width 100 \
 335                        -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
 336                        -borderwidth 1 \
 337                        -relief groove \
 338                        -highlightt 0
 339                $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
 340                pack $w.status.c -side right
 341        }
 342
 343        if {$history eq {}} {
 344                $w_back conf -state disabled
 345        } else {
 346                $w_back conf -state normal
 347        }
 348        lappend history [list $commit $path]
 349
 350        set status "Loading $commit:[escape_path $path]..."
 351        $w_path conf -text [escape_path $path]
 352        if {$commit eq {}} {
 353                set fd [open $path r]
 354        } else {
 355                set cmd [list git cat-file blob "$commit:$path"]
 356                set fd [open "| $cmd" r]
 357        }
 358        fconfigure $fd -blocking 0 -translation lf -encoding binary
 359        fileevent $fd readable [cb _read_file $fd]
 360        set current_fd $fd
 361}
 362
 363method _history_menu {} {
 364        set m $w.backmenu
 365        if {[winfo exists $m]} {
 366                $m delete 0 end
 367        } else {
 368                menu $m -tearoff 0
 369        }
 370
 371        for {set i [expr {[llength $history] - 2}]
 372                } {$i >= 0} {incr i -1} {
 373                set e [lindex $history $i]
 374                set c [lindex $e 0]
 375                set f [lindex $e 1]
 376
 377                if {[regexp {^[0-9a-f]{40}$} $c]} {
 378                        set t [string range $c 0 8]...
 379                } else {
 380                        set t $c
 381                }
 382                if {![catch {set summary $header($c,summary)}]} {
 383                        append t " $summary"
 384                        if {[string length $t] > 70} {
 385                                set t [string range $t 0 66]...
 386                        }
 387                }
 388
 389                $m add command -label $t -command [cb _goback $i $c $f]
 390        }
 391        set X [winfo rootx $w_back]
 392        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 393        tk_popup $m $X $Y
 394}
 395
 396method _goback {i c f} {
 397        set history [lrange $history 0 [expr {$i - 1}]]
 398        set commit $c
 399        set path $f
 400        _load $this
 401}
 402
 403method _read_file {fd} {
 404        if {$fd ne $current_fd} {
 405                catch {close $fd}
 406                return
 407        }
 408
 409        $w_cgrp conf -state normal
 410        $w_line conf -state normal
 411        $w_file conf -state normal
 412        while {[gets $fd line] >= 0} {
 413                regsub "\r\$" $line {} line
 414                incr total_lines
 415
 416                if {$total_lines > 1} {
 417                        $w_cgrp insert end "\n"
 418                        $w_line insert end "\n"
 419                        $w_file insert end "\n"
 420                }
 421
 422                $w_line insert end "$total_lines" linenumber
 423                $w_file insert end "$line"
 424        }
 425
 426        set ln_wc [expr {[string length $total_lines] + 2}]
 427        if {[$w_line cget -width] < $ln_wc} {
 428                $w_line conf -width $ln_wc
 429        }
 430
 431        $w_cgrp conf -state disabled
 432        $w_line conf -state disabled
 433        $w_file conf -state disabled
 434
 435        if {[eof $fd]} {
 436                close $fd
 437
 438                _status $this
 439                set cmd {nice git blame -M -C --incremental}
 440                if {$commit eq {}} {
 441                        lappend cmd --contents $path
 442                } else {
 443                        lappend cmd $commit
 444                }
 445                lappend cmd -- $path
 446                set fd [open "| $cmd" r]
 447                fconfigure $fd -blocking 0 -translation lf -encoding binary
 448                fileevent $fd readable [cb _read_blame $fd]
 449                set current_fd $fd
 450        }
 451} ifdeleted { catch {close $fd} }
 452
 453method _read_blame {fd} {
 454        variable group_colors
 455
 456        if {$fd ne $current_fd} {
 457                catch {close $fd}
 458                return
 459        }
 460
 461        $w_cgrp conf -state normal
 462        while {[gets $fd line] >= 0} {
 463                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 464                        cmit original_line final_line line_count]} {
 465                        set r_commit     $cmit
 466                        set r_orig_line  $original_line
 467                        set r_final_line $final_line
 468                        set r_line_count $line_count
 469
 470                        if {[catch {set g $order($cmit)}]} {
 471                                set bg [lindex $group_colors 0]
 472                                set group_colors [lrange $group_colors 1 end]
 473                                lappend group_colors $bg
 474
 475                                $w_cgrp tag conf g$cmit -background $bg
 476                                $w_line tag conf g$cmit -background $bg
 477                                $w_file tag conf g$cmit -background $bg
 478
 479                                set order($cmit) $commit_count
 480                                incr commit_count
 481                                lappend commit_list $cmit
 482                        }
 483                } elseif {[string match {filename *} $line]} {
 484                        set file [string range $line 9 end]
 485                        set n    $r_line_count
 486                        set lno  $r_final_line
 487                        set cmit $r_commit
 488
 489                        if {[regexp {^0{40}$} $cmit]} {
 490                                set commit_abbr work
 491                                set commit_type curr_commit
 492                        } elseif {$cmit eq $commit} {
 493                                set commit_abbr this
 494                                set commit_type curr_commit
 495                        } else {
 496                                set commit_type prior_commit
 497                                set commit_abbr [string range $cmit 0 4]
 498                        }
 499
 500                        set author_abbr {}
 501                        set a_name {}
 502                        catch {set a_name $header($cmit,author)}
 503                        while {$a_name ne {}} {
 504                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 505                                append author_abbr $_a
 506                                unset _a
 507                                if {![regsub \
 508                                        {^[[:upper:]][^\s]*\s+} \
 509                                        $a_name {} a_name ]} break
 510                        }
 511                        if {$author_abbr eq {}} {
 512                                set author_abbr { |}
 513                        } else {
 514                                set author_abbr [string range $author_abbr 0 3]
 515                                while {[string length $author_abbr] < 4} {
 516                                        set author_abbr " $author_abbr"
 517                                }
 518                        }
 519                        unset a_name
 520
 521                        set first_lno $lno
 522                        while {
 523                           ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
 524                        && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
 525                        && $ncmit eq $cmit
 526                        && $nfile eq $file
 527                        } {
 528                                incr first_lno -1
 529                        }
 530
 531                        while {$n > 0} {
 532                                set lno_e "$lno.0 lineend + 1c"
 533                                if {![catch {set g g$line_commit($lno)}]} {
 534                                        $w_cgrp tag remove g$g $lno.0 $lno_e
 535                                        $w_line tag remove g$g $lno.0 $lno_e
 536                                        $w_file tag remove g$g $lno.0 $lno_e
 537
 538                                        $w_cgrp tag remove a$g $lno.0 $lno_e
 539                                        $w_line tag remove a$g $lno.0 $lno_e
 540                                        $w_file tag remove a$g $lno.0 $lno_e
 541                                }
 542
 543                                set line_commit($lno) $cmit
 544                                set line_file($lno)   $file
 545
 546                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 547                                if {$lno == $first_lno} {
 548                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 549                                } elseif {$lno == [expr {$first_lno + 1}]} {
 550                                        $w_cgrp insert $lno.0 $author_abbr
 551                                } else {
 552                                        $w_cgrp insert $lno.0 { |}
 553                                }
 554
 555                                $w_cgrp tag add g$cmit $lno.0 $lno_e
 556                                $w_line tag add g$cmit $lno.0 $lno_e
 557                                $w_file tag add g$cmit $lno.0 $lno_e
 558
 559                                $w_cgrp tag add a$cmit $lno.0 $lno_e
 560                                $w_line tag add a$cmit $lno.0 $lno_e
 561                                $w_file tag add a$cmit $lno.0 $lno_e
 562
 563                                if {$highlight_line == -1} {
 564                                        if {[lindex [$w_file yview] 0] == 0} {
 565                                                $w_file see $lno.0
 566                                                _showcommit $this $lno
 567                                        }
 568                                } elseif {$highlight_line == $lno} {
 569                                        _showcommit $this $lno
 570                                }
 571
 572                                incr n -1
 573                                incr lno
 574                                incr blame_lines
 575                        }
 576
 577                        while {
 578                           ![catch {set ncmit $line_commit($lno)}]
 579                        && ![catch {set nfile $line_file($lno)}]
 580                        && $ncmit eq $cmit
 581                        && $nfile eq $file
 582                        } {
 583                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 584
 585                                if {$lno == $first_lno} {
 586                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 587                                } elseif {$lno == [expr {$first_lno + 1}]} {
 588                                        $w_cgrp insert $lno.0 $author_abbr
 589                                } else {
 590                                        $w_cgrp insert $lno.0 { |}
 591                                }
 592                                incr lno
 593                        }
 594
 595                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 596                        set header($r_commit,$key) $data
 597                }
 598        }
 599        $w_cgrp conf -state disabled
 600
 601        if {[eof $fd]} {
 602                close $fd
 603                set current_fd {}
 604                set status {Annotation complete.}
 605                destroy $w.status.c
 606        } else {
 607                _status $this
 608        }
 609} ifdeleted { catch {close $fd} }
 610
 611method _status {} {
 612        set have  $blame_lines
 613        set total $total_lines
 614        set pdone 0
 615        if {$total} {set pdone [expr {100 * $have / $total}]}
 616
 617        set status [format \
 618                "Loading annotations... %i of %i lines annotated (%2i%%)" \
 619                $have $total $pdone]
 620        $w.status.c coords bar 0 0 $pdone 20
 621}
 622
 623method _click {cur_w pos} {
 624        set lno [lindex [split [$cur_w index $pos] .] 0]
 625        if {$lno eq {}} return
 626        _showcommit $this $lno
 627}
 628
 629method _load_commit {pos} {
 630        set lno [lindex [split [$w_cgrp index $pos] .] 0]
 631        if {[catch {set cmit $line_commit($lno)}]} return
 632        if {[catch {set file $line_file($lno)  }]} return
 633
 634        set commit $cmit
 635        set path $file
 636        _load $this
 637}
 638
 639method _showcommit {lno} {
 640        global repo_config
 641        variable active_color
 642
 643        if {$highlight_commit ne {}} {
 644                set cmit $highlight_commit
 645                $w_cgrp tag conf a$cmit -background {}
 646                $w_line tag conf a$cmit -background {}
 647                $w_file tag conf a$cmit -background {}
 648        }
 649
 650        $w_cmit conf -state normal
 651        $w_cmit delete 0.0 end
 652        if {[catch {set cmit $line_commit($lno)}]} {
 653                set cmit {}
 654                $w_cmit insert end "Loading annotation..."
 655        } else {
 656                $w_cgrp tag conf a$cmit -background $active_color
 657                $w_line tag conf a$cmit -background $active_color
 658                $w_file tag conf a$cmit -background $active_color
 659
 660                set author_name {}
 661                set author_email {}
 662                set author_time {}
 663                catch {set author_name $header($cmit,author)}
 664                catch {set author_email $header($cmit,author-mail)}
 665                catch {set author_time [clock format \
 666                        $header($cmit,author-time) \
 667                        -format {%Y-%m-%d %H:%M:%S}
 668                ]}
 669
 670                set committer_name {}
 671                set committer_email {}
 672                set committer_time {}
 673                catch {set committer_name $header($cmit,committer)}
 674                catch {set committer_email $header($cmit,committer-mail)}
 675                catch {set committer_time [clock format \
 676                        $header($cmit,committer-time) \
 677                        -format {%Y-%m-%d %H:%M:%S}
 678                ]}
 679
 680                if {[catch {set msg $header($cmit,message)}]} {
 681                        set msg {}
 682                        catch {
 683                                set fd [open "| git cat-file commit $cmit" r]
 684                                fconfigure $fd -encoding binary -translation lf
 685                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 686                                        set enc utf-8
 687                                }
 688                                while {[gets $fd line] > 0} {
 689                                        if {[string match {encoding *} $line]} {
 690                                                set enc [string tolower [string range $line 9 end]]
 691                                        }
 692                                }
 693                                set msg [encoding convertfrom $enc [read $fd]]
 694                                set msg [string trim $msg]
 695                                close $fd
 696
 697                                set author_name [encoding convertfrom $enc $author_name]
 698                                set committer_name [encoding convertfrom $enc $committer_name]
 699
 700                                set header($cmit,author) $author_name
 701                                set header($cmit,committer) $committer_name
 702                        }
 703                        set header($cmit,message) $msg
 704                }
 705
 706                $w_cmit insert end "commit $cmit\n" header_key
 707                $w_cmit insert end "Author:\t" header_key
 708                $w_cmit insert end "$author_name $author_email" header_val
 709                $w_cmit insert end "$author_time\n" header_val
 710
 711                $w_cmit insert end "Committer:\t" header_key
 712                $w_cmit insert end "$committer_name $committer_email" header_val
 713                $w_cmit insert end "$committer_time\n" header_val
 714
 715                if {$line_file($lno) ne $path} {
 716                        $w_cmit insert end "Original File:\t" header_key
 717                        $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
 718                }
 719
 720                $w_cmit insert end "\n$msg"
 721        }
 722        $w_cmit conf -state disabled
 723
 724        set highlight_line $lno
 725        set highlight_commit $cmit
 726
 727        if {$highlight_commit eq $tooltip_commit} {
 728                _hide_tooltip $this
 729        }
 730}
 731
 732method _copycommit {} {
 733        set pos @$::cursorX,$::cursorY
 734        set lno [lindex [split [$::cursorW index $pos] .] 0]
 735        if {![catch {set commit $line_commit($lno)}]} {
 736                clipboard clear
 737                clipboard append \
 738                        -format STRING \
 739                        -type STRING \
 740                        -- $commit
 741        }
 742}
 743
 744method _show_tooltip {cur_w pos} {
 745        set lno [lindex [split [$cur_w index $pos] .] 0]
 746        if {[catch {set cmit $line_commit($lno)}]} {
 747                _hide_tooltip $this
 748                return
 749        }
 750
 751        if {$cmit eq $highlight_commit} {
 752                _hide_tooltip $this
 753                return
 754        }
 755
 756        if {$cmit eq $tooltip_commit} {
 757                _position_tooltip $this
 758        } elseif {$tooltip_wm ne {}} {
 759                _open_tooltip $this $cur_w
 760        } elseif {$tooltip_timer eq {}} {
 761                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 762        }
 763}
 764
 765method _open_tooltip {cur_w} {
 766        set tooltip_timer {}
 767        set pos_x [winfo pointerx $cur_w]
 768        set pos_y [winfo pointery $cur_w]
 769        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 770                _hide_tooltip $this
 771                return
 772        }
 773
 774        set pos @[join [list \
 775                [expr {$pos_x - [winfo rootx $cur_w]}] \
 776                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 777        set lno [lindex [split [$cur_w index $pos] .] 0]
 778        set cmit $line_commit($lno)
 779
 780        set author_name {}
 781        set author_email {}
 782        set author_time {}
 783        catch {set author_name $header($cmit,author)}
 784        catch {set author_email $header($cmit,author-mail)}
 785        catch {set author_time [clock format \
 786                $header($cmit,author-time) \
 787                -format {%Y-%m-%d %H:%M:%S}
 788        ]}
 789
 790        set committer_name {}
 791        set committer_email {}
 792        set committer_time {}
 793        catch {set committer_name $header($cmit,committer)}
 794        catch {set committer_email $header($cmit,committer-mail)}
 795        catch {set committer_time [clock format \
 796                $header($cmit,committer-time) \
 797                -format {%Y-%m-%d %H:%M:%S}
 798        ]}
 799
 800        set summary {}
 801        catch {set summary $header($cmit,summary)}
 802
 803        set tooltip_commit $cmit
 804        set tooltip_text "commit $cmit
 805$author_name $author_email  $author_time
 806$summary"
 807
 808        set file $line_file($lno)
 809        if {$file ne $path} {
 810                append tooltip_text "
 811
 812Original File: $file"
 813        }
 814
 815        if {$tooltip_wm ne "$cur_w.tooltip"} {
 816                _hide_tooltip $this
 817
 818                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 819                wm overrideredirect $tooltip_wm 1
 820                wm transient $tooltip_wm [winfo toplevel $cur_w]
 821                pack [label $tooltip_wm.label \
 822                        -background lightyellow \
 823                        -foreground black \
 824                        -textvariable @tooltip_text \
 825                        -justify left]
 826        }
 827        _position_tooltip $this
 828}
 829
 830method _position_tooltip {} {
 831        set req_w [winfo reqwidth  $tooltip_wm.label]
 832        set req_h [winfo reqheight $tooltip_wm.label]
 833        set pos_x [expr {[winfo pointerx .] +  5}]
 834        set pos_y [expr {[winfo pointery .] + 10}]
 835
 836        set g "${req_w}x${req_h}"
 837        if {$pos_x >= 0} {append g +}
 838        append g $pos_x
 839        if {$pos_y >= 0} {append g +}
 840        append g $pos_y
 841
 842        wm geometry $tooltip_wm $g
 843        raise $tooltip_wm
 844}
 845
 846method _hide_tooltip {} {
 847        if {$tooltip_wm ne {}} {
 848                destroy $tooltip_wm
 849                set tooltip_wm {}
 850                set tooltip_commit {}
 851        }
 852        if {$tooltip_timer ne {}} {
 853                after cancel $tooltip_timer
 854                set tooltip_timer {}
 855        }
 856}
 857
 858method _resize {new_height} {
 859        set diff [expr {$new_height - $old_height}]
 860        if {$diff == 0} return
 861
 862        set my [expr {[winfo height $w.file_pane] - 25}]
 863        set o [$w.file_pane sash coord 0]
 864        set ox [lindex $o 0]
 865        set oy [expr {[lindex $o 1] + $diff}]
 866        if {$oy < 0}   {set oy 0}
 867        if {$oy > $my} {set oy $my}
 868        $w.file_pane sash place 0 $ox $oy
 869
 870        set old_height $new_height
 871}
 872
 873}