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