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

gitk: Show nearby tags

This adds a feature to the diff display window where it will show the tags that this commit follows (is a descendent of) and precedes (is an ancestor of). Specifically, it will show the tags for all tagged descendents that are not a descendent of another tagged descendent of this commit, and the tags for all tagged ancestors that are not ancestors of another tagged ancestor of this commit. To do this, gitk reads the complete commit graph using git rev-list and performs a couple of traversals of the tree. This is done in the background, but since it can be time-consuming, there is an option to turn it off in the `edit preferences' window. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Jun 3, 2006 at 19:11 UTC b8ab2e177a7dacc37af933ec382ef9ea929127ee
1 file changed +247 -23
gitk
+247 -23
@@ -743,7 +743,7 @@ proc click {w} {
743 proc savestuff {w} {
744 global canv canv2 canv3 ctext cflist mainfont textfont uifont
745 global stuffsaved findmergefiles maxgraphpct
746 - global maxwidth
746 + global maxwidth showneartags
747 global viewname viewfiles viewargs viewperm nextviewnum
748 global cmitmode wrapcomment
749
@@ -759,6 +759,7 @@ proc savestuff {w} {
759 puts $f [list set maxwidth $maxwidth]
760 puts $f [list set cmitmode $cmitmode]
761 puts $f [list set wrapcomment $wrapcomment]
762 + puts $f [list set showneartags $showneartags]
763 puts $f "set geometry(width) [winfo width .ctop]"
764 puts $f "set geometry(height) [winfo height .ctop]"
765 puts $f "set geometry(canv1) [expr {[winfo width $canv]-2}]"
@@ -3523,7 +3524,7 @@ proc commit_descriptor {p} {
3524 if {[llength $commitinfo($p)] > 1} {
3525 set l [lindex $commitinfo($p) 0]
3526 }
3526 - return "$p ($l)"
3527 + return "$p ($l)\n"
3528 }
3529
3530 # append some text to the ctext widget, and make any SHA1 ID
@@ -3533,7 +3534,6 @@ proc appendwithlinks {text tags} {
3534
3535 set start [$ctext index "end - 1c"]
3536 $ctext insert end $text $tags
3536 - $ctext insert end "\n"
3537 set links [regexp -indices -all -inline {[0-9a-f]{40}} $text]
3538 foreach l $links {
3539 set s [lindex $l 0]
@@ -3568,6 +3568,54 @@ proc viewnextline {dir} {
3568 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
3569 }
3570
3571 +# add a list of tag names at position pos
3572 +proc appendrefs {pos l} {
3573 + global ctext commitrow linknum curview idtags
3574 +
3575 + if {[catch {$ctext index $pos}]} return
3576 + set tags {}
3577 + foreach id $l {
3578 + foreach tag $idtags($id) {
3579 + lappend tags [concat $tag $id]
3580 + }
3581 + }
3582 + set tags [lsort -index 1 $tags]
3583 + set sep {}
3584 + foreach tag $tags {
3585 + set name [lindex $tag 0]
3586 + set id [lindex $tag 1]
3587 + set lk link$linknum
3588 + incr linknum
3589 + $ctext insert $pos $sep
3590 + $ctext insert $pos $name $lk
3591 + $ctext tag conf $lk -foreground blue
3592 + if {[info exists commitrow($curview,$id)]} {
3593 + $ctext tag bind $lk <1> \
3594 + [list selectline $commitrow($curview,$id) 1]
3595 + $ctext tag conf $lk -underline 1
3596 + $ctext tag bind $lk <Enter> { %W configure -cursor hand2 }
3597 + $ctext tag bind $lk <Leave> { %W configure -cursor $curtextcursor }
3598 + }
3599 + set sep ", "
3600 + }
3601 +}
3602 +
3603 +# called when we have finished computing the nearby tags
3604 +proc dispneartags {} {
3605 + global selectedline currentid ctext anc_tags desc_tags showneartags
3606 +
3607 + if {![info exists selectedline] || !$showneartags} return
3608 + set id $currentid
3609 + $ctext conf -state normal
3610 + if {[info exists anc_tags($id)]} {
3611 + appendrefs follows $anc_tags($id)
3612 + }
3613 + if {[info exists desc_tags($id)]} {
3614 + appendrefs precedes $desc_tags($id)
3615 + }
3616 + $ctext conf -state disabled
3617 +}
3618 +
3619 proc selectline {l isnew} {
3620 global canv canv2 canv3 ctext commitinfo selectedline
3621 global displayorder linehtag linentag linedtag
@@ -3575,7 +3623,7 @@ proc selectline {l isnew} {
3623 global currentid sha1entry
3624 global commentend idtags linknum
3625 global mergemax numcommits pending_select
3578 - global cmitmode
3626 + global cmitmode desc_tags anc_tags showneartags allcommits
3627
3628 catch {unset pending_select}
3629 $canv delete hover
@@ -3678,16 +3726,35 @@ proc selectline {l isnew} {
3726 }
3727 } else {
3728 foreach p $olds {
3681 - append headers "Parent: [commit_descriptor $p]\n"
3729 + append headers "Parent: [commit_descriptor $p]"
3730 }
3731 }
3732
3733 foreach c [lindex $childlist $l] {
3686 - append headers "Child: [commit_descriptor $c]\n"
3734 + append headers "Child: [commit_descriptor $c]"
3735 }
3736
3737 # make anything that looks like a SHA1 ID be a clickable link
3738 appendwithlinks $headers {}
3739 + if {$showneartags} {
3740 + if {![info exists allcommits]} {
3741 + getallcommits
3742 + }
3743 + $ctext insert end "Follows: "
3744 + $ctext mark set follows "end -1c"
3745 + $ctext mark gravity follows left
3746 + if {[info exists anc_tags($id)]} {
3747 + appendrefs follows $anc_tags($id)
3748 + }
3749 + $ctext insert end "\nPrecedes: "
3750 + $ctext mark set precedes "end -1c"
3751 + $ctext mark gravity precedes left
3752 + if {[info exists desc_tags($id)]} {
3753 + appendrefs precedes $desc_tags($id)
3754 + }
3755 + $ctext insert end "\n"
3756 + }
3757 + $ctext insert end "\n"
3758 appendwithlinks [lindex $info 5] {comment}
3759
3760 $ctext tag delete Comments
@@ -4814,12 +4881,19 @@ proc domktag {} {
4881
4882 proc redrawtags {id} {
4883 global canv linehtag commitrow idpos selectedline curview
4884 + global mainfont
4885
4886 if {![info exists commitrow($curview,$id)]} return
4887 drawcmitrow $commitrow($curview,$id)
4888 $canv delete tag.$id
4889 set xt [eval drawtags $id $idpos($id)]
4890 $canv coords $linehtag($commitrow($curview,$id)) $xt [lindex $idpos($id) 2]
4891 + set text [$canv itemcget $linehtag($commitrow($curview,$id)) -text]
4892 + set xr [expr {$xt + [font measure $mainfont $text]}]
4893 + if {$xr > $canvxmax} {
4894 + set canvxmax $xr
4895 + setcanvscroll
4896 + }
4897 if {[info exists selectedline]
4898 && $selectedline == $commitrow($curview,$id)} {
4899 selectline $selectedline 0
@@ -4893,22 +4967,164 @@ proc wrcomcan {} {
4967 unset wrcomtop
4968 }
4969
4896 -proc listrefs {id} {
4897 - global idtags idheads idotherrefs
4970 +# Stuff for finding nearby tags
4971 +proc getallcommits {} {
4972 + global allcstart allcommits
4973
4899 - set x {}
4900 - if {[info exists idtags($id)]} {
4901 - set x $idtags($id)
4974 + set fd [open [concat | git rev-list --all --topo-order --parents] r]
4975 + fconfigure $fd -blocking 0
4976 + set allcommits "reading"
4977 + nowbusy allcommits
4978 + restartgetall $fd
4979 +}
4980 +
4981 +proc restartgetall {fd} {
4982 + global allcstart
4983 +
4984 + fileevent $fd readable [list getallclines $fd]
4985 + set allcstart [clock clicks -milliseconds]
4986 +}
4987 +
4988 +proc combine_dtags {l1 l2} {
4989 + global tagisdesc notfirstd
4990 +
4991 + set res [lsort -unique [concat $l1 $l2]]
4992 + for {set i 0} {$i < [llength $res]} {incr i} {
4993 + set x [lindex $res $i]
4994 + for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
4995 + set y [lindex $res $j]
4996 + if {[info exists tagisdesc($x,$y)]} {
4997 + if {$tagisdesc($x,$y) > 0} {
4998 + # x is a descendent of y, exclude x
4999 + set res [lreplace $res $i $i]
5000 + incr i -1
5001 + break
5002 + } else {
5003 + # y is a descendent of x, exclude y
5004 + set res [lreplace $res $j $j]
5005 + }
5006 + } else {
5007 + # no relation, keep going
5008 + incr j
5009 + }
5010 + }
5011 }
4903 - set y {}
4904 - if {[info exists idheads($id)]} {
4905 - set y $idheads($id)
5012 + return $res
5013 +}
5014 +
5015 +proc combine_atags {l1 l2} {
5016 + global tagisdesc
5017 +
5018 + set res [lsort -unique [concat $l1 $l2]]
5019 + for {set i 0} {$i < [llength $res]} {incr i} {
5020 + set x [lindex $res $i]
5021 + for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
5022 + set y [lindex $res $j]
5023 + if {[info exists tagisdesc($x,$y)]} {
5024 + if {$tagisdesc($x,$y) < 0} {
5025 + # x is an ancestor of y, exclude x
5026 + set res [lreplace $res $i $i]
5027 + incr i -1
5028 + break
5029 + } else {
5030 + # y is an ancestor of x, exclude y
5031 + set res [lreplace $res $j $j]
5032 + }
5033 + } else {
5034 + # no relation, keep going
5035 + incr j
5036 + }
5037 + }
5038 }
4907 - set z {}
4908 - if {[info exists idotherrefs($id)]} {
4909 - set z $idotherrefs($id)
5039 + return $res
5040 +}
5041 +
5042 +proc getallclines {fd} {
5043 + global allparents allchildren allcommits allcstart
5044 + global desc_tags anc_tags idtags alldtags tagisdesc allids
5045 +
5046 + while {[gets $fd line] >= 0} {
5047 + set id [lindex $line 0]
5048 + lappend allids $id
5049 + set olds [lrange $line 1 end]
5050 + set allparents($id) $olds
5051 + if {![info exists allchildren($id)]} {
5052 + set allchildren($id) {}
5053 + }
5054 + foreach p $olds {
5055 + lappend allchildren($p) $id
5056 + }
5057 + # compute nearest tagged descendents as we go
5058 + set dtags {}
5059 + foreach child $allchildren($id) {
5060 + if {[info exists idtags($child)]} {
5061 + set ctags [list $child]
5062 + } else {
5063 + set ctags $desc_tags($child)
5064 + }
5065 + if {$dtags eq {}} {
5066 + set dtags $ctags
5067 + } elseif {$ctags ne $dtags} {
5068 + set dtags [combine_dtags $dtags $ctags]
5069 + }
5070 + }
5071 + set desc_tags($id) $dtags
5072 + if {[info exists idtags($id)]} {
5073 + set adt $dtags
5074 + foreach tag $dtags {
5075 + set adt [concat $adt $alldtags($tag)]
5076 + }
5077 + set adt [lsort -unique $adt]
5078 + set alldtags($id) $adt
5079 + foreach tag $adt {
5080 + set tagisdesc($id,$tag) -1
5081 + set tagisdesc($tag,$id) 1
5082 + }
5083 + }
5084 + if {[clock clicks -milliseconds] - $allcstart >= 50} {
5085 + fileevent $fd readable {}
5086 + after idle restartgetall $fd
5087 + return
5088 + }
5089 + }
5090 + if {[eof $fd]} {
5091 + after idle restartatags [llength $allids]
5092 + if {[catch {close $fd} err]} {
5093 + error_popup "Error reading full commit graph: $err.\n\
5094 + Results may be incomplete."
5095 + }
5096 + }
5097 +}
5098 +
5099 +# walk backward through the tree and compute nearest tagged ancestors
5100 +proc restartatags {i} {
5101 + global allids allparents idtags anc_tags t0
5102 +
5103 + set t0 [clock clicks -milliseconds]
5104 + while {[incr i -1] >= 0} {
5105 + set id [lindex $allids $i]
5106 + set atags {}
5107 + foreach p $allparents($id) {
5108 + if {[info exists idtags($p)]} {
5109 + set ptags [list $p]
5110 + } else {
5111 + set ptags $anc_tags($p)
5112 + }
5113 + if {$atags eq {}} {
5114 + set atags $ptags
5115 + } elseif {$ptags ne $atags} {
5116 + set atags [combine_atags $atags $ptags]
5117 + }
5118 + }
5119 + set anc_tags($id) $atags
5120 + if {[clock clicks -milliseconds] - $t0 >= 50} {
5121 + after idle restartatags $i
5122 + return
5123 + }
5124 }
4911 - return [list $x $y $z]
5125 + set allcommits "done"
5126 + notbusy allcommits
5127 + dispneartags
5128 }
5129
5130 proc rereadrefs {} {
@@ -4959,7 +5175,7 @@ proc doquit {} {
5175
5176 proc doprefs {} {
5177 global maxwidth maxgraphpct diffopts
4962 - global oldprefs prefstop
5178 + global oldprefs prefstop showneartags
5179
5180 set top .gitkprefs
5181 set prefstop $top
@@ -4967,7 +5183,7 @@ proc doprefs {} {
5183 raise $top
5184 return
5185 }
4970 - foreach v {maxwidth maxgraphpct diffopts} {
5186 + foreach v {maxwidth maxgraphpct diffopts showneartags} {
5187 set oldprefs($v) [set $v]
5188 }
5189 toplevel $top
@@ -4989,6 +5205,11 @@ proc doprefs {} {
5205 -font optionfont
5206 entry $top.diffopt -width 20 -textvariable diffopts
5207 grid x $top.diffoptl $top.diffopt -sticky w
5208 + frame $top.ntag
5209 + label $top.ntag.l -text "Display nearby tags" -font optionfont
5210 + checkbutton $top.ntag.b -variable showneartags
5211 + pack $top.ntag.b $top.ntag.l -side left
5212 + grid x $top.ntag -sticky w
5213 frame $top.buts
5214 button $top.buts.ok -text "OK" -command prefsok
5215 button $top.buts.can -text "Cancel" -command prefscan
@@ -5000,9 +5221,9 @@ proc doprefs {} {
5221
5222 proc prefscan {} {
5223 global maxwidth maxgraphpct diffopts
5003 - global oldprefs prefstop
5224 + global oldprefs prefstop showneartags
5225
5005 - foreach v {maxwidth maxgraphpct diffopts} {
5226 + foreach v {maxwidth maxgraphpct diffopts showneartags} {
5227 set $v $oldprefs($v)
5228 }
5229 catch {destroy $prefstop}
@@ -5011,13 +5232,15 @@ proc prefscan {} {
5232
5233 proc prefsok {} {
5234 global maxwidth maxgraphpct
5014 - global oldprefs prefstop
5235 + global oldprefs prefstop showneartags
5236
5237 catch {destroy $prefstop}
5238 unset prefstop
5239 if {$maxwidth != $oldprefs(maxwidth)
5240 || $maxgraphpct != $oldprefs(maxgraphpct)} {
5241 redisplay
5242 + } elseif {$showneartags != $oldprefs(showneartags)} {
5243 + reselectline
5244 }
5245 }
5246
@@ -5328,6 +5551,7 @@ set downarrowlen 7
5551 set mingaplen 30
5552 set cmitmode "patch"
5553 set wrapcomment "none"
5554 +set showneartags 1
5555
5556 set colors {green red blue magenta darkgrey brown orange}
5557