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