git-gui / lib / blame.tclon commit gitweb: Handle invalid regexp in regexp search (36612e4)
   1# git-gui blame viewer
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4class blame {
   5
   6image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
   7
   8# Persistant data (survives loads)
   9#
  10field history {}; # viewer history: {commit path}
  11field header    ; # array commit,key -> header field
  12
  13# Tk UI control paths
  14#
  15field w          ; # top window in this viewer
  16field w_back     ; # our back button
  17field w_path     ; # label showing the current file path
  18field w_columns  ; # list of all column widgets in the viewer
  19field w_line     ; # text column: all line numbers
  20field w_amov     ; # text column: annotations + move tracking
  21field w_asim     ; # text column: annotations (simple computation)
  22field w_file     ; # text column: actual file data
  23field w_cviewer  ; # pane showing commit message
  24field finder     ; # find mini-dialog frame
  25field status     ; # status mega-widget instance
  26field old_height ; # last known height of $w.file_pane
  27
  28
  29# Tk UI colors
  30#
  31variable active_color #c0edc5
  32variable group_colors {
  33        #d6d6d6
  34        #e1e1e1
  35        #ececec
  36}
  37
  38# Current blame data; cleared/reset on each load
  39#
  40field commit               ; # input commit to blame
  41field path                 ; # input filename to view in $commit
  42
  43field current_fd        {} ; # background process running
  44field highlight_line    -1 ; # current line selected
  45field highlight_column  {} ; # current commit column selected
  46field highlight_commit  {} ; # sha1 of commit selected
  47
  48field total_lines       0  ; # total length of file
  49field blame_lines       0  ; # number of lines computed
  50field amov_data            ; # list of {commit origfile origline}
  51field asim_data            ; # list of {commit origfile origline}
  52
  53field r_commit             ; # commit currently being parsed
  54field r_orig_line          ; # original line number
  55field r_final_line         ; # final line number
  56field r_line_count         ; # lines in this region
  57
  58field tooltip_wm        {} ; # Current tooltip toplevel, if open
  59field tooltip_t         {} ; # Text widget in $tooltip_wm
  60field tooltip_timer     {} ; # Current timer event for our tooltip
  61field tooltip_commit    {} ; # Commit(s) in tooltip
  62
  63constructor new {i_commit i_path i_jump} {
  64        global cursor_ptr M1B M1T have_tk85 use_ttk NS
  65        variable active_color
  66        variable group_colors
  67
  68        set commit $i_commit
  69        set path   $i_path
  70
  71        make_toplevel top w
  72        wm title $top [append "[appname] ([reponame]): " [mc "File Viewer"]]
  73
  74        set font_w [font measure font_diff "0"]
  75
  76        gold_frame $w.header
  77        tlabel $w.header.commit_l \
  78                -text [mc "Commit:"] \
  79                -background gold \
  80                -foreground black \
  81                -anchor w \
  82                -justify left
  83        set w_back $w.header.commit_b
  84        tlabel $w_back \
  85                -image ::blame::img_back_arrow \
  86                -borderwidth 0 \
  87                -relief flat \
  88                -state disabled \
  89                -background gold \
  90                -foreground black \
  91                -activebackground gold
  92        bind $w_back <Button-1> "
  93                if {\[$w_back cget -state\] eq {normal}} {
  94                        [cb _history_menu]
  95                }
  96                "
  97        tlabel $w.header.commit \
  98                -textvariable @commit \
  99                -background gold \
 100                -foreground black \
 101                -anchor w \
 102                -justify left
 103        tlabel $w.header.path_l \
 104                -text [mc "File:"] \
 105                -background gold \
 106                -foreground black \
 107                -anchor w \
 108                -justify left
 109        set w_path $w.header.path
 110        tlabel $w_path \
 111                -background gold \
 112                -foreground black \
 113                -anchor w \
 114                -justify left
 115        pack $w.header.commit_l -side left
 116        pack $w_back -side left
 117        pack $w.header.commit -side left
 118        pack $w_path -fill x -side right
 119        pack $w.header.path_l -side right
 120
 121        panedwindow $w.file_pane -orient vertical -borderwidth 0 -sashwidth 3
 122        frame $w.file_pane.out -relief flat -borderwidth 1
 123        frame $w.file_pane.cm -relief sunken -borderwidth 1
 124        $w.file_pane add $w.file_pane.out \
 125                -sticky nsew \
 126                -minsize 100 \
 127                -height 100 \
 128                -width 100
 129        $w.file_pane add $w.file_pane.cm \
 130                -sticky nsew \
 131                -minsize 25 \
 132                -height 25 \
 133                -width 100
 134
 135        set w_line $w.file_pane.out.linenumber_t
 136        text $w_line \
 137                -takefocus 0 \
 138                -highlightthickness 0 \
 139                -padx 0 -pady 0 \
 140                -background white \
 141                -foreground black \
 142                -borderwidth 0 \
 143                -state disabled \
 144                -wrap none \
 145                -height 40 \
 146                -width 6 \
 147                -font font_diff
 148        $w_line tag conf linenumber -justify right -rmargin 5
 149
 150        set w_amov $w.file_pane.out.amove_t
 151        text $w_amov \
 152                -takefocus 0 \
 153                -highlightthickness 0 \
 154                -padx 0 -pady 0 \
 155                -background white \
 156                -foreground black \
 157                -borderwidth 0 \
 158                -state disabled \
 159                -wrap none \
 160                -height 40 \
 161                -width 5 \
 162                -font font_diff
 163        $w_amov tag conf author_abbr -justify right -rmargin 5
 164        $w_amov tag conf curr_commit
 165        $w_amov tag conf prior_commit -foreground blue -underline 1
 166        $w_amov tag bind prior_commit \
 167                <Button-1> \
 168                "[cb _load_commit $w_amov @amov_data @%x,%y];break"
 169
 170        set w_asim $w.file_pane.out.asimple_t
 171        text $w_asim \
 172                -takefocus 0 \
 173                -highlightthickness 0 \
 174                -padx 0 -pady 0 \
 175                -background white \
 176                -foreground black \
 177                -borderwidth 0 \
 178                -state disabled \
 179                -wrap none \
 180                -height 40 \
 181                -width 4 \
 182                -font font_diff
 183        $w_asim tag conf author_abbr -justify right
 184        $w_asim tag conf curr_commit
 185        $w_asim tag conf prior_commit -foreground blue -underline 1
 186        $w_asim tag bind prior_commit \
 187                <Button-1> \
 188                "[cb _load_commit $w_asim @asim_data @%x,%y];break"
 189
 190        set w_file $w.file_pane.out.file_t
 191        text $w_file \
 192                -takefocus 0 \
 193                -highlightthickness 0 \
 194                -padx 0 -pady 0 \
 195                -background white \
 196                -foreground black \
 197                -borderwidth 0 \
 198                -state disabled \
 199                -wrap none \
 200                -height 40 \
 201                -width 80 \
 202                -xscrollcommand [list $w.file_pane.out.sbx set] \
 203                -font font_diff
 204        if {$have_tk85} {
 205                $w_file configure -inactiveselectbackground darkblue
 206        }
 207        $w_file tag conf found \
 208                -background yellow
 209
 210        set w_columns [list $w_amov $w_asim $w_line $w_file]
 211
 212        ${NS}::scrollbar $w.file_pane.out.sbx \
 213                -orient h \
 214                -command [list $w_file xview]
 215        ${NS}::scrollbar $w.file_pane.out.sby \
 216                -orient v \
 217                -command [list scrollbar2many $w_columns yview]
 218        eval grid $w_columns $w.file_pane.out.sby -sticky nsew
 219        grid conf \
 220                $w.file_pane.out.sbx \
 221                -column [expr {[llength $w_columns] - 1}] \
 222                -sticky we
 223        grid columnconfigure \
 224                $w.file_pane.out \
 225                [expr {[llength $w_columns] - 1}] \
 226                -weight 1
 227        grid rowconfigure $w.file_pane.out 0 -weight 1
 228
 229        set finder [::searchbar::new \
 230                $w.file_pane.out.ff $w_file \
 231                -column [expr {[llength $w_columns] - 1}] \
 232                ]
 233
 234        set w_cviewer $w.file_pane.cm.t
 235        text $w_cviewer \
 236                -background white \
 237                -foreground black \
 238                -borderwidth 0 \
 239                -state disabled \
 240                -wrap none \
 241                -height 10 \
 242                -width 80 \
 243                -xscrollcommand [list $w.file_pane.cm.sbx set] \
 244                -yscrollcommand [list $w.file_pane.cm.sby set] \
 245                -font font_diff
 246        $w_cviewer tag conf still_loading \
 247                -font font_uiitalic \
 248                -justify center
 249        $w_cviewer tag conf header_key \
 250                -tabs {3c} \
 251                -background $active_color \
 252                -font font_uibold
 253        $w_cviewer tag conf header_val \
 254                -background $active_color \
 255                -font font_ui
 256        $w_cviewer tag raise sel
 257        ${NS}::scrollbar $w.file_pane.cm.sbx \
 258                -orient h \
 259                -command [list $w_cviewer xview]
 260        ${NS}::scrollbar $w.file_pane.cm.sby \
 261                -orient v \
 262                -command [list $w_cviewer yview]
 263        pack $w.file_pane.cm.sby -side right -fill y
 264        pack $w.file_pane.cm.sbx -side bottom -fill x
 265        pack $w_cviewer -expand 1 -fill both
 266
 267        set status [::status_bar::new $w.status]
 268
 269        menu $w.ctxm -tearoff 0
 270        $w.ctxm add command \
 271                -label [mc "Copy Commit"] \
 272                -command [cb _copycommit]
 273        $w.ctxm add separator
 274        $w.ctxm add command \
 275                -label [mc "Find Text..."] \
 276                -accelerator F7 \
 277                -command [list searchbar::show $finder]
 278        menu $w.ctxm.enc
 279        build_encoding_menu $w.ctxm.enc [cb _setencoding]
 280        $w.ctxm add cascade \
 281                -label [mc "Encoding"] \
 282                -menu $w.ctxm.enc
 283        $w.ctxm add command \
 284                -label [mc "Do Full Copy Detection"] \
 285                -command [cb _fullcopyblame]
 286        $w.ctxm add separator
 287        $w.ctxm add command \
 288                -label [mc "Show History Context"] \
 289                -command [cb _gitkcommit]
 290        $w.ctxm add command \
 291                -label [mc "Blame Parent Commit"] \
 292                -command [cb _blameparent]
 293
 294        foreach i $w_columns {
 295                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 296                        $i tag conf color$g -background [lindex $group_colors $g]
 297                }
 298
 299                if {$i eq $w_file} {
 300                        $w_file tag raise found
 301                }
 302                $i tag raise sel
 303
 304                $i conf -cursor $cursor_ptr
 305                $i conf -yscrollcommand \
 306                        "[list ::searchbar::scrolled $finder]
 307                         [list many2scrollbar $w_columns yview $w.file_pane.out.sby]"
 308                bind $i <Button-1> "
 309                        [cb _hide_tooltip]
 310                        [cb _click $i @%x,%y]
 311                        focus $i
 312                "
 313                bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
 314                bind $i <Any-Enter>   [cb _hide_tooltip]
 315                bind $i <Any-Leave>   [cb _hide_tooltip]
 316                bind_button3 $i "
 317                        [cb _hide_tooltip]
 318                        set cursorX %x
 319                        set cursorY %y
 320                        set cursorW %W
 321                        tk_popup $w.ctxm %X %Y
 322                "
 323                bind $i <Shift-Tab> "[list focus $w_cviewer];break"
 324                bind $i <Tab>       "[cb _focus_search $w_cviewer];break"
 325        }
 326
 327        foreach i [concat $w_columns $w_cviewer] {
 328                bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
 329                bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
 330                bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
 331                bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
 332                bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
 333                bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
 334                bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
 335                bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
 336                bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
 337                bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
 338        }
 339
 340        bind $w_cviewer <Shift-Tab> "[cb _focus_search $w_file];break"
 341        bind $w_cviewer <Tab>       "[list focus $w_file];break"
 342        bind $w_cviewer <Button-1>   [list focus $w_cviewer]
 343        bind $w_file    <Visibility> [cb _focus_search $w_file]
 344        bind $top       <F7>         [list searchbar::show $finder]
 345        bind $top       <Escape>     [list searchbar::hide $finder]
 346        bind $top       <F3>         [list searchbar::find_next $finder]
 347        bind $top       <Shift-F3>   [list searchbar::find_prev $finder]
 348        catch { bind $top <Shift-Key-XF86_Switch_VT_3> [list searchbar::find_prev $finder] }
 349
 350        grid configure $w.header -sticky ew
 351        grid configure $w.file_pane -sticky nsew
 352        grid configure $w.status -sticky ew
 353        grid columnconfigure $top 0 -weight 1
 354        grid rowconfigure $top 0 -weight 0
 355        grid rowconfigure $top 1 -weight 1
 356        grid rowconfigure $top 2 -weight 0
 357
 358        set req_w [winfo reqwidth  $top]
 359        set req_h [winfo reqheight $top]
 360        set scr_w [expr {[winfo screenwidth $top] - 40}]
 361        set scr_h [expr {[winfo screenheight $top] - 120}]
 362        set opt_w [expr {$font_w * (80 + 5*3 + 3)}]
 363        if {$req_w < $opt_w} {set req_w $opt_w}
 364        if {$req_w > $scr_w} {set req_w $scr_w}
 365        set opt_h [expr {$req_w*4/3}]
 366        if {$req_h < $scr_h} {set req_h $scr_h}
 367        if {$req_h > $opt_h} {set req_h $opt_h}
 368        set g "${req_w}x${req_h}"
 369        wm geometry $top $g
 370        update
 371
 372        set old_height [winfo height $w.file_pane]
 373        $w.file_pane sash place 0 \
 374                [lindex [$w.file_pane sash coord 0] 0] \
 375                [expr {int($old_height * 0.80)}]
 376        bind $w.file_pane <Configure> \
 377        "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
 378
 379        wm protocol $top WM_DELETE_WINDOW "destroy $top"
 380        bind $top <Destroy> [cb _handle_destroy %W]
 381
 382        _load $this $i_jump
 383}
 384
 385method _focus_search {win} {
 386        if {[searchbar::visible $finder]} {
 387                focus [searchbar::editor $finder]
 388        } else {
 389                focus $win
 390        }
 391}
 392
 393method _handle_destroy {win} {
 394        if {$win eq $w} {
 395                _kill $this
 396                delete_this
 397        }
 398}
 399
 400method _kill {} {
 401        if {$current_fd ne {}} {
 402                kill_file_process $current_fd
 403                catch {close $current_fd}
 404                set current_fd {}
 405        }
 406}
 407
 408method _load {jump} {
 409        variable group_colors
 410
 411        _hide_tooltip $this
 412
 413        if {$total_lines != 0 || $current_fd ne {}} {
 414                _kill $this
 415
 416                foreach i $w_columns {
 417                        $i conf -state normal
 418                        $i delete 0.0 end
 419                        foreach g [$i tag names] {
 420                                if {[regexp {^g[0-9a-f]{40}$} $g]} {
 421                                        $i tag delete $g
 422                                }
 423                        }
 424                        $i conf -state disabled
 425                }
 426
 427                $w_cviewer conf -state normal
 428                $w_cviewer delete 0.0 end
 429                $w_cviewer conf -state disabled
 430
 431                set highlight_line -1
 432                set highlight_column {}
 433                set highlight_commit {}
 434                set total_lines 0
 435        }
 436
 437        if {$history eq {}} {
 438                $w_back conf -state disabled
 439        } else {
 440                $w_back conf -state normal
 441        }
 442
 443        # Index 0 is always empty.  There is never line 0 as
 444        # we use only 1 based lines, as that matches both with
 445        # git-blame output and with Tk's text widget.
 446        #
 447        set amov_data [list [list]]
 448        set asim_data [list [list]]
 449
 450        $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
 451        $w_path conf -text [escape_path $path]
 452
 453        set do_textconv 0
 454        if {![is_config_false gui.textconv] && [git-version >= 1.7.2]} {
 455                set filter [gitattr $path diff set]
 456                set textconv [get_config [join [list diff $filter textconv] .]]
 457                if {$filter ne {set} && $textconv ne {}} {
 458                        set do_textconv 1
 459                }
 460        }
 461        if {$commit eq {}} {
 462                if {$do_textconv ne 0} {
 463                        # Run textconv with sh -c "..." to allow it to
 464                        # contain command + arguments. On windows, just
 465                        # call the filter command.
 466                        if {![file executable [shellpath]]} {
 467                                set fd [open |[linsert $textconv end $path] r]
 468                        } else {
 469                                set fd [open |[list [shellpath] -c "$textconv \"\$0\"" $path] r]
 470                        }
 471                } else {
 472                        set fd [open $path r]
 473                }
 474                fconfigure $fd -eofchar {}
 475        } else {
 476                if {$do_textconv ne 0} {
 477                        set fd [git_read cat-file --textconv "$commit:$path"]
 478                } else {
 479                        set fd [git_read cat-file blob "$commit:$path"]
 480                }
 481        }
 482        fconfigure $fd \
 483                -blocking 0 \
 484                -translation lf \
 485                -encoding [get_path_encoding $path]
 486        fileevent $fd readable [cb _read_file $fd $jump]
 487        set current_fd $fd
 488}
 489
 490method _history_menu {} {
 491        set m $w.backmenu
 492        if {[winfo exists $m]} {
 493                $m delete 0 end
 494        } else {
 495                menu $m -tearoff 0
 496        }
 497
 498        for {set i [expr {[llength $history] - 1}]
 499                } {$i >= 0} {incr i -1} {
 500                set e [lindex $history $i]
 501                set c [lindex $e 0]
 502                set f [lindex $e 1]
 503
 504                if {[regexp {^[0-9a-f]{40}$} $c]} {
 505                        set t [string range $c 0 8]...
 506                } elseif {$c eq {}} {
 507                        set t {Working Directory}
 508                } else {
 509                        set t $c
 510                }
 511                if {![catch {set summary $header($c,summary)}]} {
 512                        append t " $summary"
 513                        if {[string length $t] > 70} {
 514                                set t [string range $t 0 66]...
 515                        }
 516                }
 517
 518                $m add command -label $t -command [cb _goback $i]
 519        }
 520        set X [winfo rootx $w_back]
 521        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 522        tk_popup $m $X $Y
 523}
 524
 525method _goback {i} {
 526        set dat [lindex $history $i]
 527        set history [lrange $history 0 [expr {$i - 1}]]
 528        set commit [lindex $dat 0]
 529        set path [lindex $dat 1]
 530        _load $this [lrange $dat 2 5]
 531}
 532
 533method _read_file {fd jump} {
 534        if {$fd ne $current_fd} {
 535                catch {close $fd}
 536                return
 537        }
 538
 539        foreach i $w_columns {$i conf -state normal}
 540        while {[gets $fd line] >= 0} {
 541                regsub "\r\$" $line {} line
 542                incr total_lines
 543                lappend amov_data {}
 544                lappend asim_data {}
 545
 546                if {$total_lines > 1} {
 547                        foreach i $w_columns {$i insert end "\n"}
 548                }
 549
 550                $w_line insert end "$total_lines" linenumber
 551                $w_file insert end "$line"
 552        }
 553
 554        set ln_wc [expr {[string length $total_lines] + 2}]
 555        if {[$w_line cget -width] < $ln_wc} {
 556                $w_line conf -width $ln_wc
 557        }
 558
 559        foreach i $w_columns {$i conf -state disabled}
 560
 561        if {[eof $fd]} {
 562                close $fd
 563
 564                # If we don't force Tk to update the widgets *right now*
 565                # none of our jump commands will cause a change in the UI.
 566                #
 567                update
 568
 569                if {[llength $jump] == 1} {
 570                        set highlight_line [lindex $jump 0]
 571                        $w_file see "$highlight_line.0"
 572                } elseif {[llength $jump] == 4} {
 573                        set highlight_column [lindex $jump 0]
 574                        set highlight_line [lindex $jump 1]
 575                        $w_file xview moveto [lindex $jump 2]
 576                        $w_file yview moveto [lindex $jump 3]
 577                }
 578
 579                _exec_blame $this $w_asim @asim_data \
 580                        [list] \
 581                        [mc "Loading copy/move tracking annotations..."]
 582        }
 583} ifdeleted { catch {close $fd} }
 584
 585method _exec_blame {cur_w cur_d options cur_s} {
 586        lappend options --incremental --encoding=utf-8
 587        if {$commit eq {}} {
 588                lappend options --contents $path
 589        } else {
 590                lappend options $commit
 591        }
 592        lappend options -- $path
 593        set fd [eval git_read --nice blame $options]
 594        fconfigure $fd -blocking 0 -translation lf -encoding utf-8
 595        fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
 596        set current_fd $fd
 597        set blame_lines 0
 598
 599        $status start \
 600                $cur_s \
 601                [mc "lines annotated"]
 602}
 603
 604method _read_blame {fd cur_w cur_d} {
 605        upvar #0 $cur_d line_data
 606        variable group_colors
 607
 608        if {$fd ne $current_fd} {
 609                catch {close $fd}
 610                return
 611        }
 612
 613        $cur_w conf -state normal
 614        while {[gets $fd line] >= 0} {
 615                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 616                        cmit original_line final_line line_count]} {
 617                        set r_commit     $cmit
 618                        set r_orig_line  $original_line
 619                        set r_final_line $final_line
 620                        set r_line_count $line_count
 621                } elseif {[string match {filename *} $line]} {
 622                        set file [string range $line 9 end]
 623                        set n    $r_line_count
 624                        set lno  $r_final_line
 625                        set oln  $r_orig_line
 626                        set cmit $r_commit
 627
 628                        if {[regexp {^0{40}$} $cmit]} {
 629                                set commit_abbr work
 630                                set commit_type curr_commit
 631                        } elseif {$cmit eq $commit} {
 632                                set commit_abbr this
 633                                set commit_type curr_commit
 634                        } else {
 635                                set commit_type prior_commit
 636                                set commit_abbr [string range $cmit 0 3]
 637                        }
 638
 639                        set author_abbr {}
 640                        set a_name {}
 641                        catch {set a_name $header($cmit,author)}
 642                        while {$a_name ne {}} {
 643                                if {$author_abbr ne {}
 644                                        && [string index $a_name 0] eq {'}} {
 645                                        regsub {^'[^']+'\s+} $a_name {} a_name
 646                                }
 647                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 648                                append author_abbr $_a
 649                                unset _a
 650                                if {![regsub \
 651                                        {^[[:upper:]][^\s]*\s+} \
 652                                        $a_name {} a_name ]} break
 653                        }
 654                        if {$author_abbr eq {}} {
 655                                set author_abbr { |}
 656                        } else {
 657                                set author_abbr [string range $author_abbr 0 3]
 658                        }
 659                        unset a_name
 660
 661                        set first_lno $lno
 662                        while {
 663                           $first_lno > 1
 664                        && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
 665                        && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
 666                        } {
 667                                incr first_lno -1
 668                        }
 669
 670                        set color {}
 671                        if {$first_lno < $lno} {
 672                                foreach g [$w_file tag names $first_lno.0] {
 673                                        if {[regexp {^color[0-9]+$} $g]} {
 674                                                set color $g
 675                                                break
 676                                        }
 677                                }
 678                        } else {
 679                                set i [lsort [concat \
 680                                        [$w_file tag names "[expr {$first_lno - 1}].0"] \
 681                                        [$w_file tag names "[expr {$lno + $n}].0"] \
 682                                        ]]
 683                                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 684                                        if {[lsearch -sorted -exact $i color$g] == -1} {
 685                                                set color color$g
 686                                                break
 687                                        }
 688                                }
 689                        }
 690                        if {$color eq {}} {
 691                                set color color0
 692                        }
 693
 694                        while {$n > 0} {
 695                                set lno_e "$lno.0 lineend + 1c"
 696                                if {[lindex $line_data $lno] ne {}} {
 697                                        set g [lindex $line_data $lno 0]
 698                                        foreach i $w_columns {
 699                                                $i tag remove g$g $lno.0 $lno_e
 700                                        }
 701                                }
 702                                lset line_data $lno [list $cmit $file $oln]
 703
 704                                $cur_w delete $lno.0 "$lno.0 lineend"
 705                                if {$lno == $first_lno} {
 706                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 707                                } elseif {$lno == [expr {$first_lno + 1}]} {
 708                                        $cur_w insert $lno.0 $author_abbr author_abbr
 709                                } else {
 710                                        $cur_w insert $lno.0 { |}
 711                                }
 712
 713                                foreach i $w_columns {
 714                                        if {$cur_w eq $w_amov} {
 715                                                for {set g 0} \
 716                                                        {$g < [llength $group_colors]} \
 717                                                        {incr g} {
 718                                                        $i tag remove color$g $lno.0 $lno_e
 719                                                }
 720                                                $i tag add $color $lno.0 $lno_e
 721                                        }
 722                                        $i tag add g$cmit $lno.0 $lno_e
 723                                }
 724
 725                                if {$highlight_column eq $cur_w} {
 726                                        if {$highlight_line == -1
 727                                         && [lindex [$w_file yview] 0] == 0} {
 728                                                $w_file see $lno.0
 729                                                set highlight_line $lno
 730                                        }
 731                                        if {$highlight_line == $lno} {
 732                                                _showcommit $this $cur_w $lno
 733                                        }
 734                                }
 735
 736                                incr n -1
 737                                incr lno
 738                                incr oln
 739                                incr blame_lines
 740                        }
 741
 742                        while {
 743                           $cmit eq [lindex $line_data $lno 0]
 744                        && $file eq [lindex $line_data $lno 1]
 745                        } {
 746                                $cur_w delete $lno.0 "$lno.0 lineend"
 747
 748                                if {$lno == $first_lno} {
 749                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 750                                } elseif {$lno == [expr {$first_lno + 1}]} {
 751                                        $cur_w insert $lno.0 $author_abbr author_abbr
 752                                } else {
 753                                        $cur_w insert $lno.0 { |}
 754                                }
 755
 756                                if {$cur_w eq $w_amov} {
 757                                        foreach i $w_columns {
 758                                                for {set g 0} \
 759                                                        {$g < [llength $group_colors]} \
 760                                                        {incr g} {
 761                                                        $i tag remove color$g $lno.0 $lno_e
 762                                                }
 763                                                $i tag add $color $lno.0 $lno_e
 764                                        }
 765                                }
 766
 767                                incr lno
 768                        }
 769
 770                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 771                        set header($r_commit,$key) $data
 772                }
 773        }
 774        $cur_w conf -state disabled
 775
 776        if {[eof $fd]} {
 777                close $fd
 778                if {$cur_w eq $w_asim} {
 779                        # Switches for original location detection
 780                        set threshold [get_config gui.copyblamethreshold]
 781                        set original_options [list "-C$threshold"]
 782
 783                        if {![is_config_true gui.fastcopyblame]} {
 784                                # thorough copy search; insert before the threshold
 785                                set original_options [linsert $original_options 0 -C]
 786                        }
 787                        if {[git-version >= 1.5.3]} {
 788                                lappend original_options -w ; # ignore indentation changes
 789                        }
 790
 791                        _exec_blame $this $w_amov @amov_data \
 792                                $original_options \
 793                                [mc "Loading original location annotations..."]
 794                } else {
 795                        set current_fd {}
 796                        $status stop [mc "Annotation complete."]
 797                }
 798        } else {
 799                $status update $blame_lines $total_lines
 800        }
 801} ifdeleted { catch {close $fd} }
 802
 803method _find_commit_bound {data_list start_idx delta} {
 804        upvar #0 $data_list line_data
 805        set pos $start_idx
 806        set limit       [expr {[llength $line_data] - 1}]
 807        set base_commit [lindex $line_data $pos 0]
 808
 809        while {$pos > 0 && $pos < $limit} {
 810                set new_pos [expr {$pos + $delta}]
 811                if {[lindex $line_data $new_pos 0] ne $base_commit} {
 812                        return $pos
 813                }
 814
 815                set pos $new_pos
 816        }
 817
 818        return $pos
 819}
 820
 821method _fullcopyblame {} {
 822        if {$current_fd ne {}} {
 823                tk_messageBox \
 824                        -icon error \
 825                        -type ok \
 826                        -title [mc "Busy"] \
 827                        -message [mc "Annotation process is already running."]
 828
 829                return
 830        }
 831
 832        # Switches for original location detection
 833        set threshold [get_config gui.copyblamethreshold]
 834        set original_options [list -C -C "-C$threshold"]
 835
 836        if {[git-version >= 1.5.3]} {
 837                lappend original_options -w ; # ignore indentation changes
 838        }
 839
 840        # Find the line range
 841        set pos @$::cursorX,$::cursorY
 842        set lno [lindex [split [$::cursorW index $pos] .] 0]
 843        set min_amov_lno [_find_commit_bound $this @amov_data $lno -1]
 844        set max_amov_lno [_find_commit_bound $this @amov_data $lno 1]
 845        set min_asim_lno [_find_commit_bound $this @asim_data $lno -1]
 846        set max_asim_lno [_find_commit_bound $this @asim_data $lno 1]
 847
 848        if {$min_asim_lno < $min_amov_lno} {
 849                set min_amov_lno $min_asim_lno
 850        }
 851
 852        if {$max_asim_lno > $max_amov_lno} {
 853                set max_amov_lno $max_asim_lno
 854        }
 855
 856        lappend original_options -L "$min_amov_lno,$max_amov_lno"
 857
 858        # Clear lines
 859        for {set i $min_amov_lno} {$i <= $max_amov_lno} {incr i} {
 860                lset amov_data $i [list ]
 861        }
 862
 863        # Start the back-end process
 864        _exec_blame $this $w_amov @amov_data \
 865                $original_options \
 866                [mc "Running thorough copy detection..."]
 867}
 868
 869method _click {cur_w pos} {
 870        set lno [lindex [split [$cur_w index $pos] .] 0]
 871        _showcommit $this $cur_w $lno
 872}
 873
 874method _setencoding {enc} {
 875        force_path_encoding $path $enc
 876        _load $this [list \
 877                $highlight_column \
 878                $highlight_line \
 879                [lindex [$w_file xview] 0] \
 880                [lindex [$w_file yview] 0] \
 881                ]
 882}
 883
 884method _load_commit {cur_w cur_d pos} {
 885        upvar #0 $cur_d line_data
 886        set lno [lindex [split [$cur_w index $pos] .] 0]
 887        set dat [lindex $line_data $lno]
 888        if {$dat ne {}} {
 889                _load_new_commit $this  \
 890                        [lindex $dat 0] \
 891                        [lindex $dat 1] \
 892                        [list [lindex $dat 2]]
 893        }
 894}
 895
 896method _load_new_commit {new_commit new_path jump} {
 897        lappend history [list \
 898                $commit $path \
 899                $highlight_column \
 900                $highlight_line \
 901                [lindex [$w_file xview] 0] \
 902                [lindex [$w_file yview] 0] \
 903                ]
 904
 905        set commit $new_commit
 906        set path   $new_path
 907        _load $this $jump
 908}
 909
 910method _showcommit {cur_w lno} {
 911        global repo_config
 912        variable active_color
 913
 914        if {$highlight_commit ne {}} {
 915                foreach i $w_columns {
 916                        $i tag conf g$highlight_commit -background {}
 917                        $i tag lower g$highlight_commit
 918                }
 919        }
 920
 921        if {$cur_w eq $w_asim} {
 922                set dat [lindex $asim_data $lno]
 923                set highlight_column $w_asim
 924        } else {
 925                set dat [lindex $amov_data $lno]
 926                set highlight_column $w_amov
 927        }
 928
 929        $w_cviewer conf -state normal
 930        $w_cviewer delete 0.0 end
 931
 932        if {$dat eq {}} {
 933                set cmit {}
 934                $w_cviewer insert end [mc "Loading annotation..."] still_loading
 935        } else {
 936                set cmit [lindex $dat 0]
 937                set file [lindex $dat 1]
 938
 939                foreach i $w_columns {
 940                        $i tag conf g$cmit -background $active_color
 941                        $i tag raise g$cmit
 942                        if {$i eq $w_file} {
 943                                $w_file tag raise found
 944                        }
 945                        $i tag raise sel
 946                }
 947
 948                set author_name {}
 949                set author_email {}
 950                set author_time {}
 951                catch {set author_name $header($cmit,author)}
 952                catch {set author_email $header($cmit,author-mail)}
 953                catch {set author_time [format_date $header($cmit,author-time)]}
 954
 955                set committer_name {}
 956                set committer_email {}
 957                set committer_time {}
 958                catch {set committer_name $header($cmit,committer)}
 959                catch {set committer_email $header($cmit,committer-mail)}
 960                catch {set committer_time [format_date $header($cmit,committer-time)]}
 961
 962                if {[catch {set msg $header($cmit,message)}]} {
 963                        set msg {}
 964                        catch {
 965                                set fd [git_read cat-file commit $cmit]
 966                                fconfigure $fd -encoding binary -translation lf
 967                                # By default commits are assumed to be in utf-8
 968                                set enc utf-8
 969                                while {[gets $fd line] > 0} {
 970                                        if {[string match {encoding *} $line]} {
 971                                                set enc [string tolower [string range $line 9 end]]
 972                                        }
 973                                }
 974                                set msg [read $fd]
 975                                close $fd
 976
 977                                set enc [tcl_encoding $enc]
 978                                if {$enc ne {}} {
 979                                        set msg [encoding convertfrom $enc $msg]
 980                                }
 981                                set msg [string trim $msg]
 982                        }
 983                        set header($cmit,message) $msg
 984                }
 985
 986                $w_cviewer insert end "commit $cmit\n" header_key
 987                $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
 988                $w_cviewer insert end "$author_name $author_email" header_val
 989                $w_cviewer insert end "  $author_time\n" header_val
 990
 991                $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
 992                $w_cviewer insert end "$committer_name $committer_email" header_val
 993                $w_cviewer insert end "  $committer_time\n" header_val
 994
 995                if {$file ne $path} {
 996                        $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
 997                        $w_cviewer insert end "[escape_path $file]\n" header_val
 998                }
 999
1000                $w_cviewer insert end "\n$msg"
1001        }
1002        $w_cviewer conf -state disabled
1003
1004        set highlight_line $lno
1005        set highlight_commit $cmit
1006
1007        if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
1008                _hide_tooltip $this
1009        }
1010}
1011
1012method _get_click_amov_info {} {
1013        set pos @$::cursorX,$::cursorY
1014        set lno [lindex [split [$::cursorW index $pos] .] 0]
1015        return [lindex $amov_data $lno]
1016}
1017
1018method _copycommit {} {
1019        set dat [_get_click_amov_info $this]
1020        if {$dat ne {}} {
1021                clipboard clear
1022                clipboard append \
1023                        -format STRING \
1024                        -type STRING \
1025                        -- [lindex $dat 0]
1026        }
1027}
1028
1029method _format_offset_date {base offset} {
1030        set exval [expr {$base + $offset*24*60*60}]
1031        return [clock format $exval -format {%Y-%m-%d}]
1032}
1033
1034method _gitkcommit {} {
1035        global nullid
1036
1037        set dat [_get_click_amov_info $this]
1038        if {$dat ne {}} {
1039                set cmit [lindex $dat 0]
1040
1041                # If the line belongs to the working copy, use HEAD instead
1042                if {$cmit eq $nullid} {
1043                        if {[catch {set cmit [git rev-parse --verify HEAD]} err]} {
1044                                error_popup [strcat [mc "Cannot find HEAD commit:"] "\n\n$err"]
1045                                return;
1046                        }
1047                }
1048
1049                set radius [get_config gui.blamehistoryctx]
1050                set cmdline [list --select-commit=$cmit]
1051
1052                if {$radius > 0} {
1053                        set author_time {}
1054                        set committer_time {}
1055
1056                        catch {set author_time $header($cmit,author-time)}
1057                        catch {set committer_time $header($cmit,committer-time)}
1058
1059                        if {$committer_time eq {}} {
1060                                set committer_time $author_time
1061                        }
1062
1063                        set after_time [_format_offset_date $this $committer_time [expr {-$radius}]]
1064                        set before_time [_format_offset_date $this $committer_time $radius]
1065
1066                        lappend cmdline --after=$after_time --before=$before_time
1067                }
1068
1069                lappend cmdline $cmit
1070
1071                set base_rev "HEAD"
1072                if {$commit ne {}} {
1073                        set base_rev $commit
1074                }
1075
1076                if {$base_rev ne $cmit} {
1077                        lappend cmdline $base_rev
1078                }
1079
1080                do_gitk $cmdline
1081        }
1082}
1083
1084method _blameparent {} {
1085        global nullid
1086
1087        set dat [_get_click_amov_info $this]
1088        if {$dat ne {}} {
1089                set cmit [lindex $dat 0]
1090                set new_path [lindex $dat 1]
1091
1092                # Allow using Blame Parent on lines modified in the working copy
1093                if {$cmit eq $nullid} {
1094                        set parent_ref "HEAD"
1095                } else {
1096                        set parent_ref "$cmit^"
1097                }
1098                if {[catch {set cparent [git rev-parse --verify $parent_ref]} err]} {
1099                        error_popup [strcat [mc "Cannot find parent commit:"] "\n\n$err"]
1100                        return;
1101                }
1102
1103                _kill $this
1104
1105                # Generate a diff between the commit and its parent,
1106                # and use the hunks to update the line number.
1107                # Request zero context to simplify calculations.
1108                if {$cmit eq $nullid} {
1109                        set diffcmd [list diff-index --unified=0 $cparent -- $new_path]
1110                } else {
1111                        set diffcmd [list diff-tree --unified=0 $cparent $cmit -- $new_path]
1112                }
1113                if {[catch {set fd [eval git_read $diffcmd]} err]} {
1114                        $status stop [mc "Unable to display parent"]
1115                        error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
1116                        return
1117                }
1118
1119                set r_orig_line [lindex $dat 2]
1120
1121                fconfigure $fd \
1122                        -blocking 0 \
1123                        -encoding binary \
1124                        -translation binary
1125                fileevent $fd readable [cb _read_diff_load_commit \
1126                        $fd $cparent $new_path $r_orig_line]
1127                set current_fd $fd
1128        }
1129}
1130
1131method _read_diff_load_commit {fd cparent new_path tline} {
1132        if {$fd ne $current_fd} {
1133                catch {close $fd}
1134                return
1135        }
1136
1137        while {[gets $fd line] >= 0} {
1138                if {[regexp {^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@} $line line \
1139                        old_line osz old_size new_line nsz new_size]} {
1140
1141                        if {$osz eq {}} { set old_size 1 }
1142                        if {$nsz eq {}} { set new_size 1 }
1143
1144                        if {$new_line <= $tline} {
1145                                if {[expr {$new_line + $new_size}] > $tline} {
1146                                        # Target line within the hunk
1147                                        set line_shift [expr {
1148                                                ($new_size-$old_size)*($tline-$new_line)/$new_size
1149                                                }]
1150                                } else {
1151                                        set line_shift [expr {$new_size-$old_size}]
1152                                }
1153
1154                                set r_orig_line [expr {$r_orig_line - $line_shift}]
1155                        }
1156                }
1157        }
1158
1159        if {[eof $fd]} {
1160                close $fd;
1161                set current_fd {}
1162
1163                _load_new_commit $this  \
1164                        $cparent        \
1165                        $new_path       \
1166                        [list $r_orig_line]
1167        }
1168} ifdeleted { catch {close $fd} }
1169
1170method _show_tooltip {cur_w pos} {
1171        if {$tooltip_wm ne {}} {
1172                _open_tooltip $this $cur_w
1173        } elseif {$tooltip_timer eq {}} {
1174                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
1175        }
1176}
1177
1178method _open_tooltip {cur_w} {
1179        set tooltip_timer {}
1180        set pos_x [winfo pointerx $cur_w]
1181        set pos_y [winfo pointery $cur_w]
1182        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
1183                _hide_tooltip $this
1184                return
1185        }
1186
1187        if {$tooltip_wm ne "$cur_w.tooltip"} {
1188                _hide_tooltip $this
1189
1190                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
1191                wm overrideredirect $tooltip_wm 1
1192                wm transient $tooltip_wm [winfo toplevel $cur_w]
1193                set tooltip_t $tooltip_wm.label
1194                text $tooltip_t \
1195                        -takefocus 0 \
1196                        -highlightthickness 0 \
1197                        -relief flat \
1198                        -borderwidth 0 \
1199                        -wrap none \
1200                        -background lightyellow \
1201                        -foreground black
1202                $tooltip_t tag conf section_header -font font_uibold
1203                pack $tooltip_t
1204        } else {
1205                $tooltip_t conf -state normal
1206                $tooltip_t delete 0.0 end
1207        }
1208
1209        set pos @[join [list \
1210                [expr {$pos_x - [winfo rootx $cur_w]}] \
1211                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
1212        set lno [lindex [split [$cur_w index $pos] .] 0]
1213        if {$cur_w eq $w_amov} {
1214                set dat [lindex $amov_data $lno]
1215                set org {}
1216        } else {
1217                set dat [lindex $asim_data $lno]
1218                set org [lindex $amov_data $lno]
1219        }
1220
1221        if {$dat eq {}} {
1222                _hide_tooltip $this
1223                return
1224        }
1225
1226        set cmit [lindex $dat 0]
1227        set tooltip_commit [list $cmit]
1228
1229        set author_name {}
1230        set summary     {}
1231        set author_time {}
1232        catch {set author_name $header($cmit,author)}
1233        catch {set summary     $header($cmit,summary)}
1234        catch {set author_time [format_date $header($cmit,author-time)]}
1235
1236        $tooltip_t insert end "commit $cmit\n"
1237        $tooltip_t insert end "$author_name  $author_time\n"
1238        $tooltip_t insert end "$summary"
1239
1240        if {$org ne {} && [lindex $org 0] ne $cmit} {
1241                set save [$tooltip_t get 0.0 end]
1242                $tooltip_t delete 0.0 end
1243
1244                set cmit [lindex $org 0]
1245                set file [lindex $org 1]
1246                lappend tooltip_commit $cmit
1247
1248                set author_name {}
1249                set summary     {}
1250                set author_time {}
1251                catch {set author_name $header($cmit,author)}
1252                catch {set summary     $header($cmit,summary)}
1253                catch {set author_time [format_date $header($cmit,author-time)]}
1254
1255                $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
1256                $tooltip_t insert end "commit $cmit\n"
1257                $tooltip_t insert end "$author_name  $author_time\n"
1258                $tooltip_t insert end "$summary\n"
1259
1260                if {$file ne $path} {
1261                        $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
1262                        $tooltip_t insert end "$file\n"
1263                }
1264
1265                $tooltip_t insert end "\n"
1266                $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
1267                $tooltip_t insert end $save
1268        }
1269
1270        $tooltip_t conf -state disabled
1271        _position_tooltip $this
1272
1273        # On MacOS raising a window causes it to acquire focus.
1274        # Tk 8.5 on MacOS seems to properly support wm transient,
1275        # so we can safely counter the effect there.
1276        if {$::have_tk85 && [is_MacOSX]} {
1277                update
1278                if {$w eq {}} {
1279                        raise .
1280                } else {
1281                        raise $w
1282                }
1283        }
1284}
1285
1286method _position_tooltip {} {
1287        set max_h [lindex [split [$tooltip_t index end] .] 0]
1288        set max_w 0
1289        for {set i 1} {$i <= $max_h} {incr i} {
1290                set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
1291                if {$c > $max_w} {set max_w $c}
1292        }
1293        $tooltip_t conf -width $max_w -height $max_h
1294
1295        set req_w [winfo reqwidth  $tooltip_t]
1296        set req_h [winfo reqheight $tooltip_t]
1297        set pos_x [expr {[winfo pointerx .] +  5}]
1298        set pos_y [expr {[winfo pointery .] + 10}]
1299
1300        set g "${req_w}x${req_h}"
1301        if {$pos_x >= 0} {append g +}
1302        append g $pos_x
1303        if {$pos_y >= 0} {append g +}
1304        append g $pos_y
1305
1306        wm geometry $tooltip_wm $g
1307        if {![is_MacOSX]} {
1308                raise $tooltip_wm
1309        }
1310}
1311
1312method _hide_tooltip {} {
1313        if {$tooltip_wm ne {}} {
1314                destroy $tooltip_wm
1315                set tooltip_wm {}
1316                set tooltip_commit {}
1317        }
1318        if {$tooltip_timer ne {}} {
1319                after cancel $tooltip_timer
1320                set tooltip_timer {}
1321        }
1322}
1323
1324method _resize {new_height} {
1325        set diff [expr {$new_height - $old_height}]
1326        if {$diff == 0} return
1327
1328        set my [expr {[winfo height $w.file_pane] - 25}]
1329        set o [$w.file_pane sash coord 0]
1330        set ox [lindex $o 0]
1331        set oy [expr {[lindex $o 1] + $diff}]
1332        if {$oy < 0}   {set oy 0}
1333        if {$oy > $my} {set oy $my}
1334        $w.file_pane sash place 0 $ox $oy
1335
1336        set old_height $new_height
1337}
1338
1339}