lib / blame.tclon commit git-gui: Convert blame to the "class" way of doing things (685caf9)
   1# git-gui blame viewer
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4class blame {
   5
   6field commit  ; # input commit to blame
   7field path    ; # input filename to view in $commit
   8
   9field w
  10field w_line
  11field w_load
  12field w_file
  13field w_cmit
  14field status
  15
  16field highlight_line   -1 ; # current line selected
  17field highlight_commit {} ; # sha1 of commit selected
  18
  19field total_lines       0  ; # total length of file
  20field blame_lines       0  ; # number of lines computed
  21field commit_count      0  ; # number of commits in $commit_list
  22field commit_list      {}  ; # list of commit sha1 in receipt order
  23field order                ; # array commit -> receipt order
  24field header               ; # array commit,key -> header field
  25field line_commit          ; # array line -> sha1 commit
  26field line_file            ; # array line -> file name
  27
  28field r_commit      ; # commit currently being parsed
  29field r_orig_line   ; # original line number
  30field r_final_line  ; # final line number
  31field r_line_count  ; # lines in this region
  32
  33constructor new {i_commit i_path} {
  34        set commit $i_commit
  35        set path   $i_path
  36
  37        make_toplevel top w
  38        wm title $top "[appname] ([reponame]): File Viewer"
  39        set status "Loading $commit:$path..."
  40
  41        label $w.path -text "$commit:$path" \
  42                -anchor w \
  43                -justify left \
  44                -borderwidth 1 \
  45                -relief sunken \
  46                -font font_uibold
  47        pack $w.path -side top -fill x
  48
  49        frame $w.out
  50        text $w.out.loaded_t \
  51                -background white -borderwidth 0 \
  52                -state disabled \
  53                -wrap none \
  54                -height 40 \
  55                -width 1 \
  56                -font font_diff
  57        $w.out.loaded_t tag conf annotated -background grey
  58
  59        text $w.out.linenumber_t \
  60                -background white -borderwidth 0 \
  61                -state disabled \
  62                -wrap none \
  63                -height 40 \
  64                -width 5 \
  65                -font font_diff
  66        $w.out.linenumber_t tag conf linenumber -justify right
  67
  68        text $w.out.file_t \
  69                -background white -borderwidth 0 \
  70                -state disabled \
  71                -wrap none \
  72                -height 40 \
  73                -width 80 \
  74                -xscrollcommand [list $w.out.sbx set] \
  75                -font font_diff
  76
  77        scrollbar $w.out.sbx -orient h -command [list $w.out.file_t xview]
  78        scrollbar $w.out.sby -orient v \
  79                -command [list scrollbar2many [list \
  80                $w.out.loaded_t \
  81                $w.out.linenumber_t \
  82                $w.out.file_t \
  83                ] yview]
  84        grid \
  85                $w.out.linenumber_t \
  86                $w.out.loaded_t \
  87                $w.out.file_t \
  88                $w.out.sby \
  89                -sticky nsew
  90        grid conf $w.out.sbx -column 2 -sticky we
  91        grid columnconfigure $w.out 2 -weight 1
  92        grid rowconfigure $w.out 0 -weight 1
  93        pack $w.out -fill both -expand 1
  94
  95        label $w.status \
  96                -textvariable @status \
  97                -anchor w \
  98                -justify left \
  99                -borderwidth 1 \
 100                -relief sunken
 101        pack $w.status -side bottom -fill x
 102
 103        frame $w.cm
 104        text $w.cm.t \
 105                -background white -borderwidth 0 \
 106                -state disabled \
 107                -wrap none \
 108                -height 10 \
 109                -width 80 \
 110                -xscrollcommand [list $w.cm.sbx set] \
 111                -yscrollcommand [list $w.cm.sby set] \
 112                -font font_diff
 113        scrollbar $w.cm.sbx -orient h -command [list $w.cm.t xview]
 114        scrollbar $w.cm.sby -orient v -command [list $w.cm.t yview]
 115        pack $w.cm.sby -side right -fill y
 116        pack $w.cm.sbx -side bottom -fill x
 117        pack $w.cm.t -expand 1 -fill both
 118        pack $w.cm -side bottom -fill x
 119
 120        menu $w.ctxm -tearoff 0
 121        $w.ctxm add command \
 122                -label "Copy Commit" \
 123                -command [cb _copycommit]
 124
 125        set w_line $w.out.linenumber_t
 126        set w_load $w.out.loaded_t
 127        set w_file $w.out.file_t
 128        set w_cmit $w.cm.t
 129
 130        foreach i [list \
 131                $w.out.loaded_t \
 132                $w.out.linenumber_t \
 133                $w.out.file_t] {
 134                $i tag conf in_sel \
 135                        -background [$i cget -foreground] \
 136                        -foreground [$i cget -background]
 137                $i conf -yscrollcommand \
 138                        [list many2scrollbar [list \
 139                        $w.out.loaded_t \
 140                        $w.out.linenumber_t \
 141                        $w.out.file_t \
 142                        ] yview $w.out.sby]
 143                bind $i <Button-1> "[cb _click $i @%x,%y]; focus $i"
 144                bind_button3 $i "
 145                        set cursorX %x
 146                        set cursorY %y
 147                        set cursorW %W
 148                        tk_popup $w.ctxm %X %Y
 149                "
 150        }
 151
 152        foreach i [list \
 153                $w.out.loaded_t \
 154                $w.out.linenumber_t \
 155                $w.out.file_t \
 156                $w.cm.t] {
 157                bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
 158                bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
 159                bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
 160                bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
 161                bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
 162                bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
 163                bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
 164                bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
 165                bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
 166                bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
 167        }
 168
 169        bind $w.cm.t <Button-1> "focus $w.cm.t"
 170        bind $top <Visibility> "focus $top"
 171        bind $top <Destroy> [list delete_this $this]
 172
 173        set cmd [list git cat-file blob "$commit:$path"]
 174        set fd [open "| $cmd" r]
 175        fconfigure $fd -blocking 0 -translation lf -encoding binary
 176        fileevent $fd readable [cb _read_file $fd]
 177}
 178
 179method _read_file {fd} {
 180        $w_load conf -state normal
 181        $w_line conf -state normal
 182        $w_file conf -state normal
 183        while {[gets $fd line] >= 0} {
 184                regsub "\r\$" $line {} line
 185                incr total_lines
 186                $w_load insert end "\n"
 187                $w_line insert end "$total_lines\n" linenumber
 188                $w_file insert end "$line\n"
 189        }
 190        $w_load conf -state disabled
 191        $w_line conf -state disabled
 192        $w_file conf -state disabled
 193
 194        if {[eof $fd]} {
 195                close $fd
 196                _status $this
 197                set cmd [list git blame -M -C --incremental $commit -- $path]
 198                set fd [open "| $cmd" r]
 199                fconfigure $fd -blocking 0 -translation lf -encoding binary
 200                fileevent $fd readable [cb _read_blame $fd]
 201        }
 202} ifdeleted { catch {close $fd} }
 203
 204method _read_blame {fd} {
 205        while {[gets $fd line] >= 0} {
 206                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 207                        cmit original_line final_line line_count]} {
 208                        set r_commit     $cmit
 209                        set r_orig_line  $original_line
 210                        set r_final_line $final_line
 211                        set r_line_count $line_count
 212
 213                        if {[catch {set g $order($cmit)}]} {
 214                                $w_line tag conf g$cmit
 215                                $w_file tag conf g$cmit
 216                                $w_line tag raise in_sel
 217                                $w_file tag raise in_sel
 218                                $w_file tag raise sel
 219                                set order($cmit) $commit_count
 220                                incr commit_count
 221                                lappend commit_list $cmit
 222                        }
 223                } elseif {[string match {filename *} $line]} {
 224                        set file [string range $line 9 end]
 225                        set n    $r_line_count
 226                        set lno  $r_final_line
 227                        set cmit $r_commit
 228
 229                        while {$n > 0} {
 230                                if {[catch {set g g$line_commit($lno)}]} {
 231                                        $w_load tag add annotated $lno.0 "$lno.0 lineend + 1c"
 232                                } else {
 233                                        $w_line tag remove g$g $lno.0 "$lno.0 lineend + 1c"
 234                                        $w_file tag remove g$g $lno.0 "$lno.0 lineend + 1c"
 235                                }
 236
 237                                set line_commit($lno) $cmit
 238                                set line_file($lno)   $file
 239                                $w_line tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
 240                                $w_file tag add g$cmit $lno.0 "$lno.0 lineend + 1c"
 241
 242                                if {$highlight_line == -1} {
 243                                        if {[lindex [$w_file yview] 0] == 0} {
 244                                                $w_file see $lno.0
 245                                                _showcommit $this $lno
 246                                        }
 247                                } elseif {$highlight_line == $lno} {
 248                                        _showcommit $this $lno
 249                                }
 250
 251                                incr n -1
 252                                incr lno
 253                                incr blame_lines
 254                        }
 255
 256                        set hc $highlight_commit
 257                        if {$hc ne {}
 258                                && [expr {$order($hc) + 1}] == $order($cmit)} {
 259                                _showcommit $this $highlight_line
 260                        }
 261                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 262                        set header($r_commit,$key) $data
 263                }
 264        }
 265
 266        if {[eof $fd]} {
 267                close $fd
 268                set status {Annotation complete.}
 269        } else {
 270                _status $this
 271        }
 272} ifdeleted { catch {close $fd} }
 273
 274method _status {} {
 275        set have  $blame_lines
 276        set total $total_lines
 277        set pdone 0
 278        if {$total} {set pdone [expr {100 * $have / $total}]}
 279
 280        set status [format \
 281                "Loading annotations... %i of %i lines annotated (%2i%%)" \
 282                $have $total $pdone]
 283}
 284
 285method _click {cur_w pos} {
 286        set lno [lindex [split [$cur_w index $pos] .] 0]
 287        if {$lno eq {}} return
 288
 289        $w_line tag remove in_sel 0.0 end
 290        $w_file tag remove in_sel 0.0 end
 291        $w_line tag add in_sel $lno.0 "$lno.0 + 1 line"
 292        $w_file tag add in_sel $lno.0 "$lno.0 + 1 line"
 293
 294        _showcommit $this $lno
 295}
 296
 297variable blame_colors {
 298        #ff4040
 299        #ff40ff
 300        #4040ff
 301}
 302
 303method _showcommit {lno} {
 304        global repo_config
 305        variable blame_colors
 306
 307        if {$highlight_commit ne {}} {
 308                set idx $order($highlight_commit)
 309                set i 0
 310                foreach c $blame_colors {
 311                        set h [lindex $commit_list [expr {$idx - 1 + $i}]]
 312                        $w_line tag conf g$h -background white
 313                        $w_file tag conf g$h -background white
 314                        incr i
 315                }
 316        }
 317
 318        $w_cmit conf -state normal
 319        $w_cmit delete 0.0 end
 320        if {[catch {set cmit $line_commit($lno)} myerr]} {
 321                puts "myerr = $myerr"
 322                set cmit {}
 323                $w_cmit insert end "Loading annotation..."
 324        } else {
 325                set idx $order($cmit)
 326                set i 0
 327                foreach c $blame_colors {
 328                        set h [lindex $commit_list [expr {$idx - 1 + $i}]]
 329                        $w_line tag conf g$h -background $c
 330                        $w_file tag conf g$h -background $c
 331                        incr i
 332                }
 333
 334                set author_name {}
 335                set author_email {}
 336                set author_time {}
 337                catch {set author_name $header($cmit,author)}
 338                catch {set author_email $header($cmit,author-mail)}
 339                catch {set author_time [clock format $header($cmit,author-time)]}
 340
 341                set committer_name {}
 342                set committer_email {}
 343                set committer_time {}
 344                catch {set committer_name $header($cmit,committer)}
 345                catch {set committer_email $header($cmit,committer-mail)}
 346                catch {set committer_time [clock format $header($cmit,committer-time)]}
 347
 348                if {[catch {set msg $header($cmit,message)}]} {
 349                        set msg {}
 350                        catch {
 351                                set fd [open "| git cat-file commit $cmit" r]
 352                                fconfigure $fd -encoding binary -translation lf
 353                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 354                                        set enc utf-8
 355                                }
 356                                while {[gets $fd line] > 0} {
 357                                        if {[string match {encoding *} $line]} {
 358                                                set enc [string tolower [string range $line 9 end]]
 359                                        }
 360                                }
 361                                set msg [encoding convertfrom $enc [read $fd]]
 362                                set msg [string trim $msg]
 363                                close $fd
 364
 365                                set author_name [encoding convertfrom $enc $author_name]
 366                                set committer_name [encoding convertfrom $enc $committer_name]
 367
 368                                set header($cmit,author) $author_name
 369                                set header($cmit,committer) $committer_name
 370                        }
 371                        set header($cmit,message) $msg
 372                }
 373
 374                $w_cmit insert end "commit $cmit\n"
 375                $w_cmit insert end "Author: $author_name $author_email $author_time\n"
 376                $w_cmit insert end "Committer: $committer_name $committer_email $committer_time\n"
 377                $w_cmit insert end "Original File: [escape_path $line_file($lno)]\n"
 378                $w_cmit insert end "\n"
 379                $w_cmit insert end $msg
 380        }
 381        $w_cmit conf -state disabled
 382
 383        set highlight_line $lno
 384        set highlight_commit $cmit
 385}
 386
 387method _copycommit {} {
 388        set pos @$::cursorX,$::cursorY
 389        set lno [lindex [split [$::cursorW index $pos] .] 0]
 390        if {![catch {set commit $line_commit($lno)}]} {
 391                clipboard clear
 392                clipboard append \
 393                        -format STRING \
 394                        -type STRING \
 395                        -- $commit
 396        }
 397}
 398
 399}