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