@matheusbsbs / matheus-bagliano / commits / 35874c163e
Fork 0

git-gui: Implemented file browser and incremental blame.

This rather huge change provides a browser for the current branch. The browser simply shows the contents of tree HEAD, and lets the user drill down through the tree. The icons used really stink, as I just copied in icon which we already had. I really need to replace the file_dir and file_uplevel icons with something more useful. If the user double clicks on a file within the browser we open it in a blame viewer. This makes use of the new incremental blame feature that Linus just added yesterday to core Git. Fortunately the feature will be in 1.5.0 final so we can rely on having it available here. Since the blame engine is incremental the user will get blame data for groups which can be determined early. Git will slowly fill in the remaining lines as it goes. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jan 29, 2007 at 00:50 UTC 35874c163e2f5c1def3cc5c3465beceea0355b8f
1 file changed +459
git-gui.sh
+459
@@ -2945,6 +2945,431 @@ proc reset_hard_wait {fd} {
2945 }
2946 }
2947
2948 +######################################################################
2949 +##
2950 +## browser
2951 +
2952 +set next_browser_id 0
2953 +
2954 +proc new_browser {} {
2955 + global next_browser_id cursor_ptr
2956 + global browser_commit browser_status browser_stack browser_path browser_busy
2957 +
2958 + set w .browser[incr next_browser_id]
2959 + set w_list $w.list.l
2960 + set browser_commit($w_list) HEAD
2961 + set browser_status($w_list) {Starting...}
2962 + set browser_stack($w_list) {}
2963 + set browser_path($w_list) $browser_commit($w_list):
2964 + set browser_busy($w_list) 1
2965 +
2966 + toplevel $w
2967 + label $w.path -textvariable browser_path($w_list) \
2968 + -anchor w \
2969 + -justify left \
2970 + -borderwidth 1 \
2971 + -relief sunken \
2972 + -font font_uibold
2973 + pack $w.path -anchor w -side top -fill x
2974 +
2975 + frame $w.list
2976 + text $w_list -background white -borderwidth 0 \
2977 + -cursor $cursor_ptr \
2978 + -state disabled \
2979 + -wrap none \
2980 + -height 20 \
2981 + -width 70 \
2982 + -xscrollcommand [list $w.list.sbx set] \
2983 + -yscrollcommand [list $w.list.sby set] \
2984 + -font font_ui
2985 + $w_list tag conf in_sel \
2986 + -background [$w_list cget -foreground] \
2987 + -foreground [$w_list cget -background]
2988 + scrollbar $w.list.sbx -orient h -command [list $w_list xview]
2989 + scrollbar $w.list.sby -orient v -command [list $w_list yview]
2990 + pack $w.list.sbx -side bottom -fill x
2991 + pack $w.list.sby -side right -fill y
2992 + pack $w_list -side left -fill both -expand 1
2993 + pack $w.list -side top -fill both -expand 1
2994 +
2995 + label $w.status -textvariable browser_status($w_list) \
2996 + -anchor w \
2997 + -justify left \
2998 + -borderwidth 1 \
2999 + -relief sunken \
3000 + -font font_ui
3001 + pack $w.status -anchor w -side bottom -fill x
3002 +
3003 + bind $w_list <Button-1> "browser_click 0 $w_list @%x,%y;break"
3004 + bind $w_list <Double-Button-1> "browser_click 1 $w_list @%x,%y;break"
3005 +
3006 + bind $w <Visibility> "focus $w"
3007 + bind $w <Destroy> "
3008 + array unset browser_buffer $w_list
3009 + array unset browser_files $w_list
3010 + array unset browser_status $w_list
3011 + array unset browser_stack $w_list
3012 + array unset browser_path $w_list
3013 + array unset browser_commit $w_list
3014 + array unset browser_busy $w_list
3015 + "
3016 + wm title $w "[appname] ([reponame]): File Browser"
3017 + ls_tree $w_list $browser_commit($w_list) {}
3018 +}
3019 +
3020 +proc browser_click {was_double_click w pos} {
3021 + global browser_files browser_status browser_path
3022 + global browser_commit browser_stack browser_busy
3023 +
3024 + if {$browser_busy($w)} return
3025 + set lno [lindex [split [$w index $pos] .] 0]
3026 + set info [lindex $browser_files($w) [expr {$lno - 1}]]
3027 +
3028 + $w conf -state normal
3029 + $w tag remove sel 0.0 end
3030 + $w tag remove in_sel 0.0 end
3031 + if {$info ne {}} {
3032 + $w tag add in_sel $lno.0 [expr {$lno + 1}].0
3033 + if {$was_double_click} {
3034 + switch -- [lindex $info 0] {
3035 + parent {
3036 + set parent [lindex $browser_stack($w) end-1]
3037 + set browser_stack($w) [lrange $browser_stack($w) 0 end-2]
3038 + if {$browser_stack($w) eq {}} {
3039 + regsub {:.*$} $browser_path($w) {:} browser_path($w)
3040 + } else {
3041 + regsub {/[^/]+$} $browser_path($w) {} browser_path($w)
3042 + }
3043 + set browser_status($w) "Loading $browser_path($w)..."
3044 + ls_tree $w [lindex $parent 0] [lindex $parent 1]
3045 + }
3046 + tree {
3047 + set name [lindex $info 2]
3048 + set escn [escape_path $name]
3049 + set browser_status($w) "Loading $escn..."
3050 + append browser_path($w) $escn
3051 + ls_tree $w [lindex $info 1] $name
3052 + }
3053 + blob {
3054 + set name [lindex $info 2]
3055 + set p {}
3056 + foreach n $browser_stack($w) {
3057 + append p [lindex $n 1]
3058 + }
3059 + append p $name
3060 + show_blame $browser_commit($w) $p
3061 + }
3062 + }
3063 + }
3064 + }
3065 + $w conf -state disabled
3066 +}
3067 +
3068 +proc ls_tree {w tree_id name} {
3069 + global browser_buffer browser_files browser_stack browser_busy
3070 +
3071 + set browser_buffer($w) {}
3072 + set browser_files($w) {}
3073 + set browser_busy($w) 1
3074 +
3075 + $w conf -state normal
3076 + $w tag remove in_sel 0.0 end
3077 + $w tag remove sel 0.0 end
3078 + $w delete 0.0 end
3079 + if {$browser_stack($w) ne {}} {
3080 + $w image create end \
3081 + -align center -padx 5 -pady 1 \
3082 + -name icon0 \
3083 + -image file_uplevel
3084 + $w insert end {[Up To Parent]}
3085 + lappend browser_files($w) parent
3086 + }
3087 + lappend browser_stack($w) [list $tree_id $name]
3088 + $w conf -state disabled
3089 +
3090 + set fd [open "| git ls-tree -z $tree_id" r]
3091 + fconfigure $fd -blocking 0 -translation binary -encoding binary
3092 + fileevent $fd readable [list read_ls_tree $fd $w]
3093 +}
3094 +
3095 +proc read_ls_tree {fd w} {
3096 + global browser_buffer browser_files browser_status browser_busy
3097 +
3098 + if {![winfo exists $w]} {
3099 + catch {close $fd}
3100 + return
3101 + }
3102 +
3103 + append browser_buffer($w) [read $fd]
3104 + set pck [split $browser_buffer($w) "\0"]
3105 + set browser_buffer($w) [lindex $pck end]
3106 +
3107 + set n [llength $browser_files($w)]
3108 + $w conf -state normal
3109 + foreach p [lrange $pck 0 end-1] {
3110 + set info [split $p "\t"]
3111 + set path [lindex $info 1]
3112 + set info [split [lindex $info 0] { }]
3113 + set type [lindex $info 1]
3114 + set object [lindex $info 2]
3115 +
3116 + switch -- $type {
3117 + blob {
3118 + set image file_plain
3119 + }
3120 + tree {
3121 + set image file_dir
3122 + append path /
3123 + }
3124 + default {
3125 + set image file_question
3126 + }
3127 + }
3128 +
3129 + if {$n > 0} {$w insert end "\n"}
3130 + $w image create end \
3131 + -align center -padx 5 -pady 1 \
3132 + -name icon[incr n] \
3133 + -image $image
3134 + $w insert end [escape_path $path]
3135 + lappend browser_files($w) [list $type $object $path]
3136 + }
3137 + $w conf -state disabled
3138 +
3139 + if {[eof $fd]} {
3140 + close $fd
3141 + set browser_status($w) Ready.
3142 + set browser_busy($w) 0
3143 + array unset browser_buffer $w
3144 + }
3145 +}
3146 +
3147 +proc show_blame {commit path} {
3148 + global next_browser_id blame_status blame_data
3149 +
3150 + set w .browser[incr next_browser_id]
3151 + set blame_status($w) {Loading current file content...}
3152 + set texts [list]
3153 +
3154 + toplevel $w
3155 + panedwindow $w.out -orient horizontal
3156 +
3157 + label $w.path -text "$commit:$path" \
3158 + -anchor w \
3159 + -justify left \
3160 + -borderwidth 1 \
3161 + -relief sunken \
3162 + -font font_uibold
3163 + pack $w.path -anchor w -side top -fill x
3164 +
3165 + text $w.out.commit -background white -borderwidth 0 \
3166 + -state disabled \
3167 + -wrap none \
3168 + -height 40 \
3169 + -width 8 \
3170 + -font font_diff
3171 + $w.out add $w.out.commit
3172 + lappend texts $w.out.commit
3173 +
3174 + text $w.out.author -background white -borderwidth 0 \
3175 + -state disabled \
3176 + -wrap none \
3177 + -height 40 \
3178 + -width 20 \
3179 + -font font_diff
3180 + $w.out add $w.out.author
3181 + lappend texts $w.out.author
3182 +
3183 + text $w.out.date -background white -borderwidth 0 \
3184 + -state disabled \
3185 + -wrap none \
3186 + -height 40 \
3187 + -width [string length "yyyy-mm-dd hh:mm:ss"] \
3188 + -font font_diff
3189 + $w.out add $w.out.date
3190 + lappend texts $w.out.date
3191 +
3192 + text $w.out.linenumber -background white -borderwidth 0 \
3193 + -state disabled \
3194 + -wrap none \
3195 + -height 40 \
3196 + -width 5 \
3197 + -font font_diff
3198 + $w.out.linenumber tag conf linenumber -justify right
3199 + $w.out add $w.out.linenumber
3200 + lappend texts $w.out.linenumber
3201 +
3202 + text $w.out.file -background white -borderwidth 0 \
3203 + -state disabled \
3204 + -wrap none \
3205 + -height 40 \
3206 + -width 80 \
3207 + -font font_diff
3208 + $w.out add $w.out.file
3209 + lappend texts $w.out.file
3210 +
3211 + label $w.status -textvariable blame_status($w) \
3212 + -anchor w \
3213 + -justify left \
3214 + -borderwidth 1 \
3215 + -relief sunken \
3216 + -font font_ui
3217 + pack $w.status -anchor w -side bottom -fill x
3218 +
3219 + scrollbar $w.sby -orient v -command [list scrollbar2many $texts yview]
3220 + pack $w.sby -side right -fill y
3221 + pack $w.out -side left -fill both -expand 1
3222 +
3223 + menu $w.ctxm -tearoff 0
3224 + $w.ctxm add command -label "Copy Commit" \
3225 + -font font_ui \
3226 + -command "blame_copycommit $w \$cursorW @\$cursorX,\$cursorY"
3227 +
3228 + foreach i $texts {
3229 + $i tag conf in_sel \
3230 + -background [$i cget -foreground] \
3231 + -foreground [$i cget -background]
3232 + $i conf -yscrollcommand [list many2scrollbar $texts yview $w.sby]
3233 + bind $i <Button-1> "blame_highlight $i @%x,%y $texts;break"
3234 + bind_button3 $i "
3235 + set cursorX %x
3236 + set cursorY %y
3237 + set cursorW %W
3238 + tk_popup $w.ctxm %X %Y
3239 + "
3240 + }
3241 +
3242 + bind $w <Visibility> "focus $w"
3243 + bind $w <Destroy> "
3244 + array unset blame_status $w
3245 + array unset blame_data $w,*
3246 + "
3247 + wm title $w "[appname] ([reponame]): File Viewer"
3248 +
3249 + set blame_data($w,total_lines) 0
3250 + set fd [open "| git cat-file blob $commit:$path" r]
3251 + fconfigure $fd -blocking 0 -translation lf -encoding binary
3252 + fileevent $fd readable [list read_blame_catfile $fd $w $commit $path \
3253 + $texts $w.out.linenumber $w.out.file]
3254 +}
3255 +
3256 +proc read_blame_catfile {fd w commit path texts w_lno w_file} {
3257 + global blame_status blame_data
3258 +
3259 + if {![winfo exists $w_file]} {
3260 + catch {close $fd}
3261 + return
3262 + }
3263 +
3264 + set n $blame_data($w,total_lines)
3265 + foreach i $texts {$i conf -state normal}
3266 + while {[gets $fd line] >= 0} {
3267 + regsub "\r\$" $line {} line
3268 + incr n
3269 + $w_lno insert end $n linenumber
3270 + $w_file insert end $line
3271 + foreach i $texts {$i insert end "\n"}
3272 + }
3273 + foreach i $texts {$i conf -state disabled}
3274 + set blame_data($w,total_lines) $n
3275 +
3276 + if {[eof $fd]} {
3277 + close $fd
3278 + set blame_status($w) {Loading annotations...}
3279 + set fd [open "| git blame --incremental $commit -- $path" r]
3280 + fconfigure $fd -blocking 0 -translation lf -encoding binary
3281 + fileevent $fd readable "read_blame_incremental $fd $w $texts"
3282 + }
3283 +}
3284 +
3285 +proc read_blame_incremental {fd w w_commit w_author w_date w_lno w_file} {
3286 + global blame_status blame_data
3287 +
3288 + if {![winfo exists $w_commit]} {
3289 + catch {close $fd}
3290 + return
3291 + }
3292 +
3293 + $w_commit conf -state normal
3294 + $w_author conf -state normal
3295 + $w_date conf -state normal
3296 +
3297 + while {[gets $fd line] >= 0} {
3298 + if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
3299 + commit original_line final_line line_count]} {
3300 + set blame_data($w,commit) $commit
3301 + set blame_data($w,original_line) $original_line
3302 + set blame_data($w,final_line) $final_line
3303 + set blame_data($w,line_count) $line_count
3304 + } elseif {[string match {filename *} $line]} {
3305 + set n $blame_data($w,line_count)
3306 + set lno $blame_data($w,final_line)
3307 + set file [string range $line 9 end]
3308 + set commit $blame_data($w,commit)
3309 + set abbrev [string range $commit 0 8]
3310 +
3311 + if {[catch {set author $blame_data($w,$commit,author)} err]} {
3312 + puts $err
3313 + set author {}
3314 + }
3315 +
3316 + if {[catch {set atime $blame_data($w,$commit,author-time)}]} {
3317 + set atime {}
3318 + } else {
3319 + set atime [clock format $atime -format {%Y-%m-%d %T}]
3320 + }
3321 +
3322 + while {$n > 0} {
3323 + $w_commit delete $lno.0 "$lno.0 lineend"
3324 + $w_author delete $lno.0 "$lno.0 lineend"
3325 + $w_date delete $lno.0 "$lno.0 lineend"
3326 +
3327 + $w_commit insert $lno.0 $abbrev
3328 + $w_author insert $lno.0 $author
3329 + $w_date insert $lno.0 $atime
3330 + set blame_data($w,line$lno,commit) $commit
3331 +
3332 + incr n -1
3333 + incr lno
3334 + }
3335 + } elseif {[regexp {^([a-z-]+) (.*)$} $line line header data]} {
3336 + set blame_data($w,$blame_data($w,commit),$header) $data
3337 + }
3338 + }
3339 +
3340 + $w_commit conf -state disabled
3341 + $w_author conf -state disabled
3342 + $w_date conf -state disabled
3343 +
3344 + if {[eof $fd]} {
3345 + close $fd
3346 + set blame_status($w) {Annotation complete.}
3347 + }
3348 +}
3349 +
3350 +proc blame_highlight {w pos args} {
3351 + set lno [lindex [split [$w index $pos] .] 0]
3352 + foreach i $args {
3353 + $i tag remove in_sel 0.0 end
3354 + }
3355 + if {$lno eq {}} return
3356 + foreach i $args {
3357 + $i tag add in_sel $lno.0 "$lno.0 + 1 line"
3358 + }
3359 +}
3360 +
3361 +proc blame_copycommit {w i pos} {
3362 + global blame_data
3363 + set lno [lindex [split [$i index $pos] .] 0]
3364 + if {![catch {set commit $blame_data($w,line$lno,commit)}]} {
3365 + clipboard clear
3366 + clipboard append \
3367 + -format STRING \
3368 + -type STRING \
3369 + -- $commit
3370 + }
3371 +}
3372 +
3373 ######################################################################
3374 ##
3375 ## icons
@@ -3021,6 +3446,24 @@ static unsigned char file_merge_bits[] = {
3446 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
3447 } -maskdata $filemask
3448
3449 +image create bitmap file_dir -background white -foreground blue -data {
3450 +#define mod_width 14
3451 +#define mod_height 15
3452 +static unsigned char mod_bits[] = {
3453 + 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
3454 + 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
3455 + 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
3456 +} -maskdata $filemask
3457 +
3458 +image create bitmap file_uplevel -background white -foreground blue -data {
3459 +#define mod_width 14
3460 +#define mod_height 15
3461 +static unsigned char mod_bits[] = {
3462 + 0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
3463 + 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10, 0xfa, 0x17, 0x02, 0x10,
3464 + 0xfa, 0x17, 0x02, 0x10, 0xfe, 0x1f};
3465 +} -maskdata $filemask
3466 +
3467 set ui_index .vpane.files.index.list
3468 set ui_workdir .vpane.files.workdir.list
3469
@@ -3077,6 +3520,15 @@ proc bind_button3 {w cmd} {
3520 }
3521 }
3522
3523 +proc scrollbar2many {list mode args} {
3524 + foreach w $list {eval $w $mode $args}
3525 +}
3526 +
3527 +proc many2scrollbar {list mode sb top bottom} {
3528 + $sb set $top $bottom
3529 + foreach w $list {$w $mode moveto $top}
3530 +}
3531 +
3532 proc incr_font_size {font {amt 1}} {
3533 set sz [font configure $font -size]
3534 incr sz $amt
@@ -4251,6 +4703,13 @@ if {!$single_commit} {
4703 # -- Repository Menu
4704 #
4705 menu .mbar.repository
4706 +
4707 +.mbar.repository add command \
4708 + -label {Browse Current Branch} \
4709 + -command new_browser \
4710 + -font font_ui
4711 +.mbar.repository add separator
4712 +
4713 .mbar.repository add command \
4714 -label {Visualize Current Branch} \
4715 -command {do_gitk {}} \