@matheusbsbs / matheus-bagliano / commits / f8b28a4078
Fork 0

gitk: Add a tree-browsing mode

You can now select whether you want to see the patch for a commit or the whole tree. If you select the tree, gitk will now display the commit message plus the contents of one file in the bottom-left pane, when you click on the name of the file in the bottom-right pane. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed May 1, 2006 at 09:50 UTC f8b28a4078a29cbf93cac6f9edd8d5c203777313
1 file changed +358 -20
gitk
+358 -20
@@ -514,6 +514,13 @@ proc makewindow {} {
514 $ctext tag conf found -back yellow
515
516 frame .ctop.cdet.right
517 + frame .ctop.cdet.right.mode
518 + radiobutton .ctop.cdet.right.mode.patch -text "Patch" \
519 + -command reselectline -variable cmitmode -value "patch"
520 + radiobutton .ctop.cdet.right.mode.tree -text "Tree" \
521 + -command reselectline -variable cmitmode -value "tree"
522 + grid .ctop.cdet.right.mode.patch .ctop.cdet.right.mode.tree -sticky ew
523 + pack .ctop.cdet.right.mode -side top -fill x
524 set cflist .ctop.cdet.right.cfiles
525 set indent [font measure $mainfont "nn"]
526 text $cflist -width $geometry(cflistw) -background white -font $mainfont \
@@ -583,6 +590,7 @@ proc makewindow {} {
590 bind $sha1entry <<PasteSelection>> clearsha1
591 bind $cflist <1> {sel_flist %W %x %y; break}
592 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
593 + bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
594
595 set maincursor [. cget -cursor]
596 set textcursor [$ctext cget -cursor]
@@ -647,6 +655,7 @@ proc savestuff {w} {
655 global stuffsaved findmergefiles maxgraphpct
656 global maxwidth
657 global viewname viewfiles viewperm nextviewnum
658 + global cmitmode
659
660 if {$stuffsaved} return
661 if {![winfo viewable .]} return
@@ -658,6 +667,7 @@ proc savestuff {w} {
667 puts $f [list set findmergefiles $findmergefiles]
668 puts $f [list set maxgraphpct $maxgraphpct]
669 puts $f [list set maxwidth $maxwidth]
670 + puts $f [list set cmitmode $cmitmode]
671 puts $f "set geometry(width) [winfo width .ctop]"
672 puts $f "set geometry(height) [winfo height .ctop]"
673 puts $f "set geometry(canv1) [expr {[winfo width $canv]-2}]"
@@ -820,6 +830,236 @@ f Scroll diff view to next file
830
831 # Procedures for manipulating the file list window at the
832 # bottom right of the overall window.
833 +
834 +proc treeview {w l openlevs} {
835 + global treecontents treediropen treeheight treeparent treeindex
836 +
837 + set ix 0
838 + set treeindex() 0
839 + set lev 0
840 + set prefix {}
841 + set prefixend -1
842 + set prefendstack {}
843 + set htstack {}
844 + set ht 0
845 + set treecontents() {}
846 + $w conf -state normal
847 + foreach f $l {
848 + while {[string range $f 0 $prefixend] ne $prefix} {
849 + if {$lev <= $openlevs} {
850 + $w mark set e:$treeindex($prefix) "end -1c"
851 + $w mark gravity e:$treeindex($prefix) left
852 + }
853 + set treeheight($prefix) $ht
854 + incr ht [lindex $htstack end]
855 + set htstack [lreplace $htstack end end]
856 + set prefixend [lindex $prefendstack end]
857 + set prefendstack [lreplace $prefendstack end end]
858 + set prefix [string range $prefix 0 $prefixend]
859 + incr lev -1
860 + }
861 + set tail [string range $f [expr {$prefixend+1}] end]
862 + while {[set slash [string first "/" $tail]] >= 0} {
863 + lappend htstack $ht
864 + set ht 0
865 + lappend prefendstack $prefixend
866 + incr prefixend [expr {$slash + 1}]
867 + set d [string range $tail 0 $slash]
868 + lappend treecontents($prefix) $d
869 + set oldprefix $prefix
870 + append prefix $d
871 + set treecontents($prefix) {}
872 + set treeindex($prefix) [incr ix]
873 + set treeparent($prefix) $oldprefix
874 + set tail [string range $tail [expr {$slash+1}] end]
875 + if {$lev <= $openlevs} {
876 + set ht 1
877 + set treediropen($prefix) [expr {$lev < $openlevs}]
878 + set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
879 + $w mark set d:$ix "end -1c"
880 + $w mark gravity d:$ix left
881 + set str "\n"
882 + for {set i 0} {$i < $lev} {incr i} {append str "\t"}
883 + $w insert end $str
884 + $w image create end -align center -image $bm -padx 1 \
885 + -name a:$ix
886 + $w insert end $d
887 + $w mark set s:$ix "end -1c"
888 + $w mark gravity s:$ix left
889 + }
890 + incr lev
891 + }
892 + if {$tail ne {}} {
893 + if {$lev <= $openlevs} {
894 + incr ht
895 + set str "\n"
896 + for {set i 0} {$i < $lev} {incr i} {append str "\t"}
897 + $w insert end $str
898 + $w insert end $tail
899 + }
900 + lappend treecontents($prefix) $tail
901 + }
902 + }
903 + while {$htstack ne {}} {
904 + set treeheight($prefix) $ht
905 + incr ht [lindex $htstack end]
906 + set htstack [lreplace $htstack end end]
907 + }
908 + $w conf -state disabled
909 +}
910 +
911 +proc linetoelt {l} {
912 + global treeheight treecontents
913 +
914 + set y 2
915 + set prefix {}
916 + while {1} {
917 + foreach e $treecontents($prefix) {
918 + if {$y == $l} {
919 + return "$prefix$e"
920 + }
921 + set n 1
922 + if {[string index $e end] eq "/"} {
923 + set n $treeheight($prefix$e)
924 + if {$y + $n > $l} {
925 + append prefix $e
926 + incr y
927 + break
928 + }
929 + }
930 + incr y $n
931 + }
932 + }
933 +}
934 +
935 +proc treeclosedir {w dir} {
936 + global treediropen treeheight treeparent treeindex
937 +
938 + set ix $treeindex($dir)
939 + $w conf -state normal
940 + $w delete s:$ix e:$ix
941 + set treediropen($dir) 0
942 + $w image configure a:$ix -image tri-rt
943 + $w conf -state disabled
944 + set n [expr {1 - $treeheight($dir)}]
945 + while {$dir ne {}} {
946 + incr treeheight($dir) $n
947 + set dir $treeparent($dir)
948 + }
949 +}
950 +
951 +proc treeopendir {w dir} {
952 + global treediropen treeheight treeparent treecontents treeindex
953 +
954 + set ix $treeindex($dir)
955 + $w conf -state normal
956 + $w image configure a:$ix -image tri-dn
957 + $w mark set e:$ix s:$ix
958 + $w mark gravity e:$ix right
959 + set lev 0
960 + set str "\n"
961 + set n [llength $treecontents($dir)]
962 + for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
963 + incr lev
964 + append str "\t"
965 + incr treeheight($x) $n
966 + }
967 + foreach e $treecontents($dir) {
968 + if {[string index $e end] eq "/"} {
969 + set de $dir$e
970 + set iy $treeindex($de)
971 + $w mark set d:$iy e:$ix
972 + $w mark gravity d:$iy left
973 + $w insert e:$ix $str
974 + set treediropen($de) 0
975 + $w image create e:$ix -align center -image tri-rt -padx 1 \
976 + -name a:$iy
977 + $w insert e:$ix $e
978 + $w mark set s:$iy e:$ix
979 + $w mark gravity s:$iy left
980 + set treeheight($de) 1
981 + } else {
982 + $w insert e:$ix $str
983 + $w insert e:$ix $e
984 + }
985 + }
986 + $w mark gravity e:$ix left
987 + $w conf -state disabled
988 + set treediropen($dir) 1
989 + set top [lindex [split [$w index @0,0] .] 0]
990 + set ht [$w cget -height]
991 + set l [lindex [split [$w index s:$ix] .] 0]
992 + if {$l < $top} {
993 + $w yview $l.0
994 + } elseif {$l + $n + 1 > $top + $ht} {
995 + set top [expr {$l + $n + 2 - $ht}]
996 + if {$l < $top} {
997 + set top $l
998 + }
999 + $w yview $top.0
1000 + }
1001 +}
1002 +
1003 +proc treeclick {w x y} {
1004 + global treediropen cmitmode ctext cflist cflist_top
1005 +
1006 + if {$cmitmode ne "tree"} return
1007 + if {![info exists cflist_top]} return
1008 + set l [lindex [split [$w index "@$x,$y"] "."] 0]
1009 + $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
1010 + $cflist tag add highlight $l.0 "$l.0 lineend"
1011 + set cflist_top $l
1012 + if {$l == 1} {
1013 + $ctext yview 1.0
1014 + return
1015 + }
1016 + set e [linetoelt $l]
1017 + if {[string index $e end] ne "/"} {
1018 + showfile $e
1019 + } elseif {$treediropen($e)} {
1020 + treeclosedir $w $e
1021 + } else {
1022 + treeopendir $w $e
1023 + }
1024 +}
1025 +
1026 +proc setfilelist {id} {
1027 + global treefilelist cflist
1028 +
1029 + treeview $cflist $treefilelist($id) 0
1030 +}
1031 +
1032 +image create bitmap tri-rt -background black -foreground blue -data {
1033 + #define tri-rt_width 13
1034 + #define tri-rt_height 13
1035 + static unsigned char tri-rt_bits[] = {
1036 + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
1037 + 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
1038 + 0x00, 0x00};
1039 +} -maskdata {
1040 + #define tri-rt-mask_width 13
1041 + #define tri-rt-mask_height 13
1042 + static unsigned char tri-rt-mask_bits[] = {
1043 + 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
1044 + 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
1045 + 0x08, 0x00};
1046 +}
1047 +image create bitmap tri-dn -background black -foreground blue -data {
1048 + #define tri-dn_width 13
1049 + #define tri-dn_height 13
1050 + static unsigned char tri-dn_bits[] = {
1051 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
1052 + 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1053 + 0x00, 0x00};
1054 +} -maskdata {
1055 + #define tri-dn-mask_width 13
1056 + #define tri-dn-mask_height 13
1057 + static unsigned char tri-dn-mask_bits[] = {
1058 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
1059 + 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
1060 + 0x00, 0x00};
1061 +}
1062 +
1063 proc init_flist {first} {
1064 global cflist cflist_top cflist_bot selectedline difffilestart
1065
@@ -837,29 +1077,30 @@ proc init_flist {first} {
1077 set difffilestart {}
1078 }
1079
840 -proc add_flist {f} {
1080 +proc add_flist {fl} {
1081 global flistmode cflist
1082
1083 $cflist conf -state normal
1084 if {$flistmode eq "flat"} {
845 - $cflist insert end "\n$f"
1085 + foreach f $fl {
1086 + $cflist insert end "\n$f"
1087 + }
1088 }
1089 $cflist conf -state disabled
1090 }
1091
1092 proc sel_flist {w x y} {
851 - global flistmode ctext difffilestart cflist cflist_top
1093 + global flistmode ctext difffilestart cflist cflist_top cmitmode
1094
1095 + if {$cmitmode eq "tree"} return
1096 if {![info exists cflist_top]} return
1097 set l [lindex [split [$w index "@$x,$y"] "."] 0]
855 - if {$flistmode eq "flat"} {
856 - if {$l == 1} {
857 - $ctext yview 1.0
858 - } else {
859 - catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
860 - }
861 - highlight_flist $l
1098 + if {$l == 1} {
1099 + $ctext yview 1.0
1100 + } else {
1101 + catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
1102 }
1103 + highlight_flist $l
1104 }
1105
1106 proc scrolltext {f0 f1} {
@@ -2822,6 +3063,7 @@ proc selectline {l isnew} {
3063 global currentid sha1entry
3064 global commentend idtags linknum
3065 global mergemax numcommits pending_select
3066 + global cmitmode
3067
3068 catch {unset pending_select}
3069 $canv delete hover
@@ -2893,8 +3135,6 @@ proc selectline {l isnew} {
3135 $ctext conf -state normal
3136 $ctext delete 0.0 end
3137 set linknum 0
2896 - $ctext mark set fmark.0 0.0
2897 - $ctext mark gravity fmark.0 left
3138 set info $commitinfo($id)
3139 set date [formatdate [lindex $info 2]]
3140 $ctext insert end "Author: [lindex $info 1] $date\n"
@@ -2943,7 +3183,9 @@ proc selectline {l isnew} {
3183 set commentend [$ctext index "end - 1c"]
3184
3185 init_flist "Comments"
2946 - if {[llength $olds] <= 1} {
3186 + if {$cmitmode eq "tree"} {
3187 + gettree $id
3188 + } elseif {[llength $olds] <= 1} {
3189 startdiff $id
3190 } else {
3191 mergediff $id $l
@@ -2997,6 +3239,14 @@ proc unselectline {} {
3239 allcanvs delete secsel
3240 }
3241
3242 +proc reselectline {} {
3243 + global selectedline
3244 +
3245 + if {[info exists selectedline]} {
3246 + selectline $selectedline 0
3247 + }
3248 +}
3249 +
3250 proc addtohistory {cmd} {
3251 global history historyindex curview
3252
@@ -3058,6 +3308,94 @@ proc goforw {} {
3308 }
3309 }
3310
3311 +proc gettree {id} {
3312 + global treefilelist treeidlist diffids diffmergeid treepending
3313 +
3314 + set diffids $id
3315 + catch {unset diffmergeid}
3316 + if {![info exists treefilelist($id)]} {
3317 + if {![info exists treepending]} {
3318 + if {[catch {set gtf [open [concat | git-ls-tree -r $id] r]}]} {
3319 + return
3320 + }
3321 + set treepending $id
3322 + set treefilelist($id) {}
3323 + set treeidlist($id) {}
3324 + fconfigure $gtf -blocking 0
3325 + fileevent $gtf readable [list gettreeline $gtf $id]
3326 + }
3327 + } else {
3328 + setfilelist $id
3329 + }
3330 +}
3331 +
3332 +proc gettreeline {gtf id} {
3333 + global treefilelist treeidlist treepending cmitmode diffids
3334 +
3335 + while {[gets $gtf line] >= 0} {
3336 + if {[lindex $line 1] ne "blob"} continue
3337 + set sha1 [lindex $line 2]
3338 + set fname [lindex $line 3]
3339 + lappend treefilelist($id) $fname
3340 + lappend treeidlist($id) $sha1
3341 + }
3342 + if {![eof $gtf]} return
3343 + close $gtf
3344 + unset treepending
3345 + if {$cmitmode ne "tree"} {
3346 + if {![info exists diffmergeid]} {
3347 + gettreediffs $diffids
3348 + }
3349 + } elseif {$id ne $diffids} {
3350 + gettree $diffids
3351 + } else {
3352 + setfilelist $id
3353 + }
3354 +}
3355 +
3356 +proc showfile {f} {
3357 + global treefilelist treeidlist diffids
3358 + global ctext commentend
3359 +
3360 + set i [lsearch -exact $treefilelist($diffids) $f]
3361 + if {$i < 0} {
3362 + puts "oops, $f not in list for id $diffids"
3363 + return
3364 + }
3365 + set blob [lindex $treeidlist($diffids) $i]
3366 + if {[catch {set bf [open [concat | git-cat-file blob $blob] r]} err]} {
3367 + puts "oops, error reading blob $blob: $err"
3368 + return
3369 + }
3370 + fconfigure $bf -blocking 0
3371 + fileevent $bf readable [list getblobline $bf $diffids]
3372 + $ctext config -state normal
3373 + $ctext delete $commentend end
3374 + $ctext insert end "\n"
3375 + $ctext insert end "$f\n" filesep
3376 + $ctext config -state disabled
3377 + $ctext yview $commentend
3378 +}
3379 +
3380 +proc getblobline {bf id} {
3381 + global diffids cmitmode ctext
3382 +
3383 + if {$id ne $diffids || $cmitmode ne "tree"} {
3384 + catch {close $bf}
3385 + return
3386 + }
3387 + $ctext config -state normal
3388 + while {[gets $bf line] >= 0} {
3389 + $ctext insert end "$line\n"
3390 + }
3391 + if {[eof $bf]} {
3392 + # delete last newline
3393 + $ctext delete "end - 2c" "end - 1c"
3394 + close $bf
3395 + }
3396 + $ctext config -state disabled
3397 +}
3398 +
3399 proc mergediff {id l} {
3400 global diffmergeid diffopts mdifffd
3401 global diffids
@@ -3102,7 +3440,7 @@ proc getmergediffline {mdf id np} {
3440 $ctext mark set f:$fname $here
3441 $ctext mark gravity f:$fname left
3442 lappend difffilestart $here
3105 - add_flist $fname
3443 + add_flist [list $fname]
3444 set l [expr {(78 - [string length $fname]) / 2}]
3445 set pad [string range "----------------------------------------" 1 $l]
3446 $ctext insert end "$pad $fname $pad\n" filesep
@@ -3172,9 +3510,7 @@ proc startdiff {ids} {
3510
3511 proc addtocflist {ids} {
3512 global treediffs cflist
3175 - foreach f $treediffs($ids) {
3176 - add_flist $f
3177 - }
3513 + add_flist $treediffs($ids)
3514 getblobdiffs $ids
3515 }
3516
@@ -3191,6 +3527,7 @@ proc gettreediffs {ids} {
3527
3528 proc gettreediffline {gdtf ids} {
3529 global treediff treediffs treepending diffids diffmergeid
3530 + global cmitmode
3531
3532 set n [gets $gdtf line]
3533 if {$n < 0} {
@@ -3198,7 +3535,9 @@ proc gettreediffline {gdtf ids} {
3535 close $gdtf
3536 set treediffs($ids) $treediff
3537 unset treepending
3201 - if {$ids != $diffids} {
3538 + if {$cmitmode eq "tree"} {
3539 + gettree $diffids
3540 + } elseif {$ids != $diffids} {
3541 if {![info exists diffmergeid]} {
3542 gettreediffs $diffids
3543 }
@@ -3645,8 +3984,6 @@ proc doseldiff {oldid newid} {
3984
3985 $ctext conf -state normal
3986 $ctext delete 0.0 end
3648 - $ctext mark set fmark.0 0.0
3649 - $ctext mark gravity fmark.0 left
3987 init_flist "Top"
3988 $ctext insert end "From "
3989 $ctext tag conf link -foreground blue -underline 1
@@ -4331,6 +4668,7 @@ set uparrowlen 7
4668 set downarrowlen 7
4669 set mingaplen 30
4670 set flistmode "flat"
4671 +set cmitmode "patch"
4672
4673 set colors {green red blue magenta darkgrey brown orange}
4674