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

gitk: New infrastructure for working out branches & previous/next tags

Instead of working out descendent heads and descendent & ancestor branches in a two-pass algorithm, this reads and stores a simplified version of the graph topology, and works out descendent/ancestor tags and descendent heads on demand (with a bit of caching). The advantages of this are, first, that we now don't have to use --topo-order on the git rev-list process. Secondly, we don't have to re-read the whole graph when tags or heads change or even when the graph changes. Since we can cope with parents coming before children, we can update the graph by running a git rev-list with arguments that just give us the new commits, and merge the new commits into the simplified graph. The graph is simplified in the sense that commits with exactly one parent and one child (which is >90% of them in most cases) are grouped together into arcs joining nodes or 'branch/merge points', which are the commits that don't have exactly 1 parent and 1 child. This reduces the size of the graph substantially and decreases the time to traverse it correspondingly. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Jun 16, 2007 at 20:29 UTC e11f12331552427113bcfd3721008ffc7227aac0
1 file changed +890 -350
gitk
+890 -350
@@ -230,8 +230,9 @@ proc updatecommits {} {
230 catch {unset selectedline}
231 catch {unset thickerline}
232 catch {unset viewdata($n)}
233 - discardallcommits
233 readrefs
234 + changedrefs
235 + regetallcommits
236 showview $n
237 }
238
@@ -359,6 +360,30 @@ proc readrefs {} {
360 }
361 }
362
363 +# update things for a head moved to a child of its previous location
364 +proc movehead {id name} {
365 + global headids idheads
366 +
367 + removehead $headids($name) $name
368 + set headids($name) $id
369 + lappend idheads($id) $name
370 +}
371 +
372 +# update things when a head has been removed
373 +proc removehead {id name} {
374 + global headids idheads
375 +
376 + if {$idheads($id) eq $name} {
377 + unset idheads($id)
378 + } else {
379 + set i [lsearch -exact $idheads($id) $name]
380 + if {$i >= 0} {
381 + set idheads($id) [lreplace $idheads($id) $i $i]
382 + }
383 + }
384 + unset headids($name)
385 +}
386 +
387 proc show_error {w top msg} {
388 message $w.m -text $msg -justify center -aspect 400
389 pack $w.m -side top -fill x -padx 20 -pady 20
@@ -3805,22 +3830,31 @@ proc viewnextline {dir} {
3830
3831 # add a list of tag or branch names at position pos
3832 # returns the number of names inserted
3808 -proc appendrefs {pos tags var} {
3833 +proc appendrefs {pos ids var} {
3834 global ctext commitrow linknum curview $var
3835
3836 if {[catch {$ctext index $pos}]} {
3837 return 0
3838 }
3814 - set tags [lsort $tags]
3839 + $ctext conf -state normal
3840 + $ctext delete $pos "$pos lineend"
3841 + set tags {}
3842 + foreach id $ids {
3843 + foreach tag [set $var\($id\)] {
3844 + lappend tags [list $tag $id]
3845 + }
3846 + }
3847 + set tags [lsort -index 0 -decreasing $tags]
3848 set sep {}
3816 - foreach tag $tags {
3817 - set id [set $var\($tag\)]
3849 + foreach ti $tags {
3850 + set id [lindex $ti 1]
3851 set lk link$linknum
3852 incr linknum
3853 + $ctext tag delete $lk
3854 $ctext insert $pos $sep
3821 - $ctext insert $pos $tag $lk
3822 - $ctext tag conf $lk -foreground blue
3855 + $ctext insert $pos [lindex $ti 0] $lk
3856 if {[info exists commitrow($curview,$id)]} {
3857 + $ctext tag conf $lk -foreground blue
3858 $ctext tag bind $lk <1> \
3859 [list selectline $commitrow($curview,$id) 1]
3860 $ctext tag conf $lk -underline 1
@@ -3829,41 +3863,58 @@ proc appendrefs {pos tags var} {
3863 }
3864 set sep ", "
3865 }
3866 + $ctext conf -state disabled
3867 return [llength $tags]
3868 }
3869
3835 -proc taglist {ids} {
3836 - global idtags
3870 +# called when we have finished computing the nearby tags
3871 +proc dispneartags {delay} {
3872 + global selectedline currentid showneartags tagphase
3873
3838 - set tags {}
3839 - foreach id $ids {
3840 - foreach tag $idtags($id) {
3841 - lappend tags $tag
3842 - }
3874 + if {![info exists selectedline] || !$showneartags} return
3875 + after cancel dispnexttag
3876 + if {$delay} {
3877 + after 200 dispnexttag
3878 + set tagphase -1
3879 + } else {
3880 + after idle dispnexttag
3881 + set tagphase 0
3882 }
3844 - return $tags
3883 }
3884
3847 -# called when we have finished computing the nearby tags
3848 -proc dispneartags {} {
3849 - global selectedline currentid ctext anc_tags desc_tags showneartags
3850 - global desc_heads
3885 +proc dispnexttag {} {
3886 + global selectedline currentid showneartags tagphase ctext
3887
3888 if {![info exists selectedline] || !$showneartags} return
3853 - set id $currentid
3854 - $ctext conf -state normal
3855 - if {[info exists desc_heads($id)]} {
3856 - if {[appendrefs branch $desc_heads($id) headids] > 1} {
3857 - $ctext insert "branch -2c" "es"
3889 + switch -- $tagphase {
3890 + 0 {
3891 + set dtags [desctags $currentid]
3892 + if {$dtags ne {}} {
3893 + appendrefs precedes $dtags idtags
3894 + }
3895 + }
3896 + 1 {
3897 + set atags [anctags $currentid]
3898 + if {$atags ne {}} {
3899 + appendrefs follows $atags idtags
3900 + }
3901 + }
3902 + 2 {
3903 + set dheads [descheads $currentid]
3904 + if {$dheads ne {}} {
3905 + if {[appendrefs branch $dheads idheads] > 1
3906 + && [$ctext get "branch -3c"] eq "h"} {
3907 + # turn "Branch" into "Branches"
3908 + $ctext conf -state normal
3909 + $ctext insert "branch -2c" "es"
3910 + $ctext conf -state disabled
3911 + }
3912 + }
3913 }
3914 }
3860 - if {[info exists anc_tags($id)]} {
3861 - appendrefs follows [taglist $anc_tags($id)] tagids
3862 - }
3863 - if {[info exists desc_tags($id)]} {
3864 - appendrefs precedes [taglist $desc_tags($id)] tagids
3915 + if {[incr tagphase] <= 2} {
3916 + after idle dispnexttag
3917 }
3866 - $ctext conf -state disabled
3918 }
3919
3920 proc selectline {l isnew} {
@@ -3873,7 +3924,7 @@ proc selectline {l isnew} {
3924 global currentid sha1entry
3925 global commentend idtags linknum
3926 global mergemax numcommits pending_select
3876 - global cmitmode desc_tags anc_tags showneartags allcommits desc_heads
3927 + global cmitmode showneartags allcommits
3928
3929 catch {unset pending_select}
3930 $canv delete hover
@@ -3993,25 +4044,14 @@ proc selectline {l isnew} {
4044 $ctext insert end "Branch: "
4045 $ctext mark set branch "end -1c"
4046 $ctext mark gravity branch left
3996 - if {[info exists desc_heads($id)]} {
3997 - if {[appendrefs branch $desc_heads($id) headids] > 1} {
3998 - # turn "Branch" into "Branches"
3999 - $ctext insert "branch -2c" "es"
4000 - }
4001 - }
4047 $ctext insert end "\nFollows: "
4048 $ctext mark set follows "end -1c"
4049 $ctext mark gravity follows left
4005 - if {[info exists anc_tags($id)]} {
4006 - appendrefs follows [taglist $anc_tags($id)] tagids
4007 - }
4050 $ctext insert end "\nPrecedes: "
4051 $ctext mark set precedes "end -1c"
4052 $ctext mark gravity precedes left
4011 - if {[info exists desc_tags($id)]} {
4012 - appendrefs precedes [taglist $desc_tags($id)] tagids
4013 - }
4053 $ctext insert end "\n"
4054 + dispneartags 1
4055 }
4056 $ctext insert end "\n"
4057 appendwithlinks [lindex $info 5] {comment}
@@ -5297,26 +5337,28 @@ proc mkbrgo {top} {
5337 notbusy newbranch
5338 error_popup $err
5339 } else {
5340 + set headids($name) $id
5341 + lappend idheads($id) $name
5342 addedhead $id $name
5301 - # XXX should update list of heads displayed for selected commit
5343 notbusy newbranch
5344 redrawtags $id
5345 + dispneartags 0
5346 }
5347 }
5348
5349 proc cherrypick {} {
5350 global rowmenuid curview commitrow
5309 - global mainhead desc_heads anc_tags desc_tags allparents allchildren
5351 + global mainhead
5352
5311 - if {[info exists desc_heads($rowmenuid)]
5312 - && [lsearch -exact $desc_heads($rowmenuid) $mainhead] >= 0} {
5353 + set oldhead [exec git rev-parse HEAD]
5354 + set dheads [descheads $rowmenuid]
5355 + if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
5356 set ok [confirm_popup "Commit [string range $rowmenuid 0 7] is already\
5357 included in branch $mainhead -- really re-apply it?"]
5358 if {!$ok} return
5359 }
5360 nowbusy cherrypick
5361 update
5319 - set oldhead [exec git rev-parse HEAD]
5362 # Unfortunately git-cherry-pick writes stuff to stderr even when
5363 # no error occurs, and exec takes that as an indication of error...
5364 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
@@ -5330,16 +5372,11 @@ proc cherrypick {} {
5372 error_popup "No changes committed"
5373 return
5374 }
5333 - set allparents($newhead) $oldhead
5334 - lappend allchildren($oldhead) $newhead
5335 - set desc_heads($newhead) $mainhead
5336 - if {[info exists anc_tags($oldhead)]} {
5337 - set anc_tags($newhead) $anc_tags($oldhead)
5338 - }
5339 - set desc_tags($newhead) {}
5375 + addnewchild $newhead $oldhead
5376 if {[info exists commitrow($curview,$oldhead)]} {
5377 insertrow $commitrow($curview,$oldhead) $newhead
5378 if {$mainhead ne {}} {
5379 + movehead $newhead $mainhead
5380 movedhead $newhead $mainhead
5381 }
5382 redrawtags $oldhead
@@ -5380,7 +5417,7 @@ proc cobranch {} {
5417 }
5418
5419 proc rmbranch {} {
5383 - global desc_heads headmenuid headmenuhead mainhead
5420 + global headmenuid headmenuhead mainhead
5421 global headids idheads
5422
5423 set head $headmenuhead
@@ -5389,7 +5426,8 @@ proc rmbranch {} {
5426 error_popup "Cannot delete the currently checked-out branch"
5427 return
5428 }
5392 - if {$desc_heads($id) eq $head} {
5429 + set dheads [descheads $id]
5430 + if {$dheads eq $headids($head)} {
5431 # the stuff on this branch isn't on any other branch
5432 if {![confirm_popup "The commits on branch $head aren't on any other\
5433 branch.\nReally delete branch $head?"]} return
@@ -5401,385 +5439,887 @@ proc rmbranch {} {
5439 error_popup $err
5440 return
5441 }
5442 + removehead $id $head
5443 removedhead $id $head
5444 redrawtags $id
5445 notbusy rmbranch
5446 + dispneartags 0
5447 }
5448
5449 # Stuff for finding nearby tags
5450 proc getallcommits {} {
5411 - global allcstart allcommits allcfd allids
5451 + global allcommits allids nbmp nextarc seeds
5452
5453 set allids {}
5414 - set fd [open [concat | git rev-list --all --topo-order --parents] r]
5415 - set allcfd $fd
5416 - fconfigure $fd -blocking 0
5417 - set allcommits "reading"
5418 - nowbusy allcommits
5419 - restartgetall $fd
5454 + set nbmp 0
5455 + set nextarc 0
5456 + set allcommits 0
5457 + set seeds {}
5458 + regetallcommits
5459 }
5460
5422 -proc discardallcommits {} {
5423 - global allparents allchildren allcommits allcfd
5424 - global desc_tags anc_tags alldtags tagisdesc allids desc_heads
5461 +# Called when the graph might have changed
5462 +proc regetallcommits {} {
5463 + global allcommits seeds
5464
5426 - if {![info exists allcommits]} return
5427 - if {$allcommits eq "reading"} {
5428 - catch {close $allcfd}
5429 - }
5430 - foreach v {allcommits allchildren allparents allids desc_tags anc_tags
5431 - alldtags tagisdesc desc_heads} {
5432 - catch {unset $v}
5465 + set cmd [concat | git rev-list --all --parents]
5466 + foreach id $seeds {
5467 + lappend cmd "^$id"
5468 }
5469 + set fd [open $cmd r]
5470 + fconfigure $fd -blocking 0
5471 + incr allcommits
5472 + nowbusy allcommits
5473 + restartgetall $fd
5474 }
5475
5476 proc restartgetall {fd} {
5437 - global allcstart
5438 -
5477 fileevent $fd readable [list getallclines $fd]
5440 - set allcstart [clock clicks -milliseconds]
5441 -}
5442 -
5443 -proc combine_dtags {l1 l2} {
5444 - global tagisdesc notfirstd
5445 -
5446 - set res [lsort -unique [concat $l1 $l2]]
5447 - for {set i 0} {$i < [llength $res]} {incr i} {
5448 - set x [lindex $res $i]
5449 - for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
5450 - set y [lindex $res $j]
5451 - if {[info exists tagisdesc($x,$y)]} {
5452 - if {$tagisdesc($x,$y) > 0} {
5453 - # x is a descendent of y, exclude x
5454 - set res [lreplace $res $i $i]
5455 - incr i -1
5456 - break
5457 - } else {
5458 - # y is a descendent of x, exclude y
5459 - set res [lreplace $res $j $j]
5478 +}
5479 +
5480 +# Since most commits have 1 parent and 1 child, we group strings of
5481 +# such commits into "arcs" joining branch/merge points (BMPs), which
5482 +# are commits that either don't have 1 parent or don't have 1 child.
5483 +#
5484 +# arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
5485 +# arcout(id) - outgoing arcs for BMP
5486 +# arcids(a) - list of IDs on arc including end but not start
5487 +# arcstart(a) - BMP ID at start of arc
5488 +# arcend(a) - BMP ID at end of arc
5489 +# growing(a) - arc a is still growing
5490 +# arctags(a) - IDs out of arcids (excluding end) that have tags
5491 +# archeads(a) - IDs out of arcids (excluding end) that have heads
5492 +# The start of an arc is at the descendent end, so "incoming" means
5493 +# coming from descendents, and "outgoing" means going towards ancestors.
5494 +
5495 +proc getallclines {fd} {
5496 + global allids allparents allchildren idtags nextarc nbmp
5497 + global arcnos arcids arctags arcout arcend arcstart archeads growing
5498 + global seeds allcommits allcstart
5499 +
5500 + if {![info exists allcstart]} {
5501 + set allcstart [clock clicks -milliseconds]
5502 + }
5503 + set nid 0
5504 + while {[gets $fd line] >= 0} {
5505 + set id [lindex $line 0]
5506 + if {[info exists allparents($id)]} {
5507 + # seen it already
5508 + continue
5509 + }
5510 + lappend allids $id
5511 + set olds [lrange $line 1 end]
5512 + set allparents($id) $olds
5513 + if {![info exists allchildren($id)]} {
5514 + set allchildren($id) {}
5515 + set arcnos($id) {}
5516 + lappend seeds $id
5517 + } else {
5518 + set a $arcnos($id)
5519 + if {[llength $olds] == 1 && [llength $a] == 1} {
5520 + lappend arcids($a) $id
5521 + if {[info exists idtags($id)]} {
5522 + lappend arctags($a) $id
5523 }
5461 - } else {
5462 - # no relation, keep going
5463 - incr j
5524 + if {[info exists idheads($id)]} {
5525 + lappend archeads($a) $id
5526 + }
5527 + if {[info exists allparents($olds)]} {
5528 + # seen parent already
5529 + if {![info exists arcout($olds)]} {
5530 + splitarc $olds
5531 + }
5532 + lappend arcids($a) $olds
5533 + set arcend($a) $olds
5534 + unset growing($a)
5535 + }
5536 + lappend allchildren($olds) $id
5537 + lappend arcnos($olds) $a
5538 + continue
5539 + }
5540 + }
5541 + incr nbmp
5542 + foreach a $arcnos($id) {
5543 + lappend arcids($a) $id
5544 + set arcend($a) $id
5545 + unset growing($a)
5546 + }
5547 +
5548 + set ao {}
5549 + foreach p $olds {
5550 + lappend allchildren($p) $id
5551 + set a [incr nextarc]
5552 + set arcstart($a) $id
5553 + set archeads($a) {}
5554 + set arctags($a) {}
5555 + set archeads($a) {}
5556 + set arcids($a) {}
5557 + lappend ao $a
5558 + set growing($a) 1
5559 + if {[info exists allparents($p)]} {
5560 + # seen it already, may need to make a new branch
5561 + if {![info exists arcout($p)]} {
5562 + splitarc $p
5563 + }
5564 + lappend arcids($a) $p
5565 + set arcend($a) $p
5566 + unset growing($a)
5567 + }
5568 + lappend arcnos($p) $a
5569 + }
5570 + set arcout($id) $ao
5571 + if {[incr nid] >= 50} {
5572 + set nid 0
5573 + if {[clock clicks -milliseconds] - $allcstart >= 50} {
5574 + fileevent $fd readable {}
5575 + after idle restartgetall $fd
5576 + unset allcstart
5577 + return
5578 }
5579 }
5580 }
5467 - return $res
5581 + if {![eof $fd]} return
5582 + close $fd
5583 + if {[incr allcommits -1] == 0} {
5584 + notbusy allcommits
5585 + }
5586 + dispneartags 0
5587 }
5588
5470 -proc combine_atags {l1 l2} {
5471 - global tagisdesc
5589 +proc recalcarc {a} {
5590 + global arctags archeads arcids idtags idheads
5591
5473 - set res [lsort -unique [concat $l1 $l2]]
5474 - for {set i 0} {$i < [llength $res]} {incr i} {
5475 - set x [lindex $res $i]
5476 - for {set j [expr {$i+1}]} {$j < [llength $res]} {} {
5477 - set y [lindex $res $j]
5478 - if {[info exists tagisdesc($x,$y)]} {
5479 - if {$tagisdesc($x,$y) < 0} {
5480 - # x is an ancestor of y, exclude x
5481 - set res [lreplace $res $i $i]
5482 - incr i -1
5483 - break
5484 - } else {
5485 - # y is an ancestor of x, exclude y
5486 - set res [lreplace $res $j $j]
5487 - }
5488 - } else {
5489 - # no relation, keep going
5490 - incr j
5491 - }
5592 + set at {}
5593 + set ah {}
5594 + foreach id [lrange $arcids($a) 0 end-1] {
5595 + if {[info exists idtags($id)]} {
5596 + lappend at $id
5597 + }
5598 + if {[info exists idheads($id)]} {
5599 + lappend ah $id
5600 }
5601 }
5494 - return $res
5602 + set arctags($a) $at
5603 + set archeads($a) $ah
5604 }
5605
5497 -proc forward_pass {id children} {
5498 - global idtags desc_tags idheads desc_heads alldtags tagisdesc
5606 +proc splitarc {p} {
5607 + global arcnos arcids nextarc nbmp arctags archeads idtags idheads
5608 + global arcstart arcend arcout allparents growing
5609
5500 - set dtags {}
5501 - set dheads {}
5502 - foreach child $children {
5503 - if {[info exists idtags($child)]} {
5504 - set ctags [list $child]
5610 + set a $arcnos($p)
5611 + if {[llength $a] != 1} {
5612 + puts "oops splitarc called but [llength $a] arcs already"
5613 + return
5614 + }
5615 + set a [lindex $a 0]
5616 + set i [lsearch -exact $arcids($a) $p]
5617 + if {$i < 0} {
5618 + puts "oops splitarc $p not in arc $a"
5619 + return
5620 + }
5621 + set na [incr nextarc]
5622 + if {[info exists arcend($a)]} {
5623 + set arcend($na) $arcend($a)
5624 + } else {
5625 + set l [lindex $allparents([lindex $arcids($a) end]) 0]
5626 + set j [lsearch -exact $arcnos($l) $a]
5627 + set arcnos($l) [lreplace $arcnos($l) $j $j $na]
5628 + }
5629 + set tail [lrange $arcids($a) [expr {$i+1}] end]
5630 + set arcids($a) [lrange $arcids($a) 0 $i]
5631 + set arcend($a) $p
5632 + set arcstart($na) $p
5633 + set arcout($p) $na
5634 + set arcids($na) $tail
5635 + if {[info exists growing($a)]} {
5636 + set growing($na) 1
5637 + unset growing($a)
5638 + }
5639 + incr nbmp
5640 +
5641 + foreach id $tail {
5642 + if {[llength $arcnos($id)] == 1} {
5643 + set arcnos($id) $na
5644 } else {
5506 - set ctags $desc_tags($child)
5645 + set j [lsearch -exact $arcnos($id) $a]
5646 + set arcnos($id) [lreplace $arcnos($id) $j $j $na]
5647 }
5508 - if {$dtags eq {}} {
5509 - set dtags $ctags
5510 - } elseif {$ctags ne $dtags} {
5511 - set dtags [combine_dtags $dtags $ctags]
5648 + }
5649 +
5650 + # reconstruct tags and heads lists
5651 + if {$arctags($a) ne {} || $archeads($a) ne {}} {
5652 + recalcarc $a
5653 + recalcarc $na
5654 + } else {
5655 + set arctags($na) {}
5656 + set archeads($na) {}
5657 + }
5658 +}
5659 +
5660 +# Update things for a new commit added that is a child of one
5661 +# existing commit. Used when cherry-picking.
5662 +proc addnewchild {id p} {
5663 + global allids allparents allchildren idtags nextarc nbmp
5664 + global arcnos arcids arctags arcout arcend arcstart archeads growing
5665 + global seeds
5666 +
5667 + lappend allids $id
5668 + set allparents($id) [list $p]
5669 + set allchildren($id) {}
5670 + set arcnos($id) {}
5671 + lappend seeds $id
5672 + incr nbmp
5673 + lappend allchildren($p) $id
5674 + set a [incr nextarc]
5675 + set arcstart($a) $id
5676 + set archeads($a) {}
5677 + set arctags($a) {}
5678 + set arcids($a) [list $p]
5679 + set arcend($a) $p
5680 + if {![info exists arcout($p)]} {
5681 + splitarc $p
5682 + }
5683 + lappend arcnos($p) $a
5684 + set arcout($id) [list $a]
5685 +}
5686 +
5687 +# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
5688 +# or 0 if neither is true.
5689 +proc anc_or_desc {a b} {
5690 + global arcout arcstart arcend arcnos cached_isanc
5691 +
5692 + if {$arcnos($a) eq $arcnos($b)} {
5693 + # Both are on the same arc(s); either both are the same BMP,
5694 + # or if one is not a BMP, the other is also not a BMP or is
5695 + # the BMP at end of the arc (and it only has 1 incoming arc).
5696 + if {$a eq $b} {
5697 + return 0
5698 }
5513 - set cheads $desc_heads($child)
5514 - if {$dheads eq {}} {
5515 - set dheads $cheads
5516 - } elseif {$cheads ne $dheads} {
5517 - set dheads [lsort -unique [concat $dheads $cheads]]
5699 + # assert {[llength $arcnos($a)] == 1}
5700 + set arc [lindex $arcnos($a) 0]
5701 + set i [lsearch -exact $arcids($arc) $a]
5702 + set j [lsearch -exact $arcids($arc) $b]
5703 + if {$i < 0 || $i > $j} {
5704 + return 1
5705 + } else {
5706 + return -1
5707 }
5708 }
5520 - set desc_tags($id) $dtags
5521 - if {[info exists idtags($id)]} {
5522 - set adt $dtags
5523 - foreach tag $dtags {
5524 - set adt [concat $adt $alldtags($tag)]
5709 +
5710 + if {![info exists arcout($a)]} {
5711 + set arc [lindex $arcnos($a) 0]
5712 + if {[info exists arcend($arc)]} {
5713 + set aend $arcend($arc)
5714 + } else {
5715 + set aend {}
5716 }
5526 - set adt [lsort -unique $adt]
5527 - set alldtags($id) $adt
5528 - foreach tag $adt {
5529 - set tagisdesc($id,$tag) -1
5530 - set tagisdesc($tag,$id) 1
5717 + set a $arcstart($arc)
5718 + } else {
5719 + set aend $a
5720 + }
5721 + if {![info exists arcout($b)]} {
5722 + set arc [lindex $arcnos($b) 0]
5723 + if {[info exists arcend($arc)]} {
5724 + set bend $arcend($arc)
5725 + } else {
5726 + set bend {}
5727 }
5728 + set b $arcstart($arc)
5729 + } else {
5730 + set bend $b
5731 }
5533 - if {[info exists idheads($id)]} {
5534 - set dheads [concat $dheads $idheads($id)]
5732 + if {$a eq $bend} {
5733 + return 1
5734 + }
5735 + if {$b eq $aend} {
5736 + return -1
5737 + }
5738 + if {[info exists cached_isanc($a,$bend)]} {
5739 + if {$cached_isanc($a,$bend)} {
5740 + return 1
5741 + }
5742 + }
5743 + if {[info exists cached_isanc($b,$aend)]} {
5744 + if {$cached_isanc($b,$aend)} {
5745 + return -1
5746 + }
5747 + if {[info exists cached_isanc($a,$bend)]} {
5748 + return 0
5749 + }
5750 }
5536 - set desc_heads($id) $dheads
5537 -}
5751
5539 -proc getallclines {fd} {
5540 - global allparents allchildren allcommits allcstart
5541 - global desc_tags anc_tags idtags tagisdesc allids
5542 - global idheads travindex
5752 + set todo [list $a $b]
5753 + set anc($a) a
5754 + set anc($b) b
5755 + for {set i 0} {$i < [llength $todo]} {incr i} {
5756 + set x [lindex $todo $i]
5757 + if {$anc($x) eq {}} {
5758 + continue
5759 + }
5760 + foreach arc $arcnos($x) {
5761 + set xd $arcstart($arc)
5762 + if {$xd eq $bend} {
5763 + set cached_isanc($a,$bend) 1
5764 + set cached_isanc($b,$aend) 0
5765 + return 1
5766 + } elseif {$xd eq $aend} {
5767 + set cached_isanc($b,$aend) 1
5768 + set cached_isanc($a,$bend) 0
5769 + return -1
5770 + }
5771 + if {![info exists anc($xd)]} {
5772 + set anc($xd) $anc($x)
5773 + lappend todo $xd
5774 + } elseif {$anc($xd) ne $anc($x)} {
5775 + set anc($xd) {}
5776 + }
5777 + }
5778 + }
5779 + set cached_isanc($a,$bend) 0
5780 + set cached_isanc($b,$aend) 0
5781 + return 0
5782 +}
5783
5544 - while {[gets $fd line] >= 0} {
5545 - set id [lindex $line 0]
5546 - lappend allids $id
5547 - set olds [lrange $line 1 end]
5548 - set allparents($id) $olds
5549 - if {![info exists allchildren($id)]} {
5550 - set allchildren($id) {}
5784 +# This identifies whether $desc has an ancestor that is
5785 +# a growing tip of the graph and which is not an ancestor of $anc
5786 +# and returns 0 if so and 1 if not.
5787 +# If we subsequently discover a tag on such a growing tip, and that
5788 +# turns out to be a descendent of $anc (which it could, since we
5789 +# don't necessarily see children before parents), then $desc
5790 +# isn't a good choice to display as a descendent tag of
5791 +# $anc (since it is the descendent of another tag which is
5792 +# a descendent of $anc). Similarly, $anc isn't a good choice to
5793 +# display as a ancestor tag of $desc.
5794 +#
5795 +proc is_certain {desc anc} {
5796 + global arcnos arcout arcstart arcend growing problems
5797 +
5798 + set certain {}
5799 + if {[llength $arcnos($anc)] == 1} {
5800 + # tags on the same arc are certain
5801 + if {$arcnos($desc) eq $arcnos($anc)} {
5802 + return 1
5803 }
5552 - foreach p $olds {
5553 - lappend allchildren($p) $id
5804 + if {![info exists arcout($anc)]} {
5805 + # if $anc is partway along an arc, use the start of the arc instead
5806 + set a [lindex $arcnos($anc) 0]
5807 + set anc $arcstart($a)
5808 }
5555 - # compute nearest tagged descendents as we go
5556 - # also compute descendent heads
5557 - forward_pass $id $allchildren($id)
5558 - if {[clock clicks -milliseconds] - $allcstart >= 50} {
5559 - fileevent $fd readable {}
5560 - after idle restartgetall $fd
5561 - return
5809 + }
5810 + if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
5811 + set x $desc
5812 + } else {
5813 + set a [lindex $arcnos($desc) 0]
5814 + set x $arcend($a)
5815 + }
5816 + if {$x == $anc} {
5817 + return 1
5818 + }
5819 + set anclist [list $x]
5820 + set dl($x) 1
5821 + set nnh 1
5822 + set ngrowanc 0
5823 + for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
5824 + set x [lindex $anclist $i]
5825 + if {$dl($x)} {
5826 + incr nnh -1
5827 + }
5828 + set done($x) 1
5829 + foreach a $arcout($x) {
5830 + if {[info exists growing($a)]} {
5831 + if {![info exists growanc($x)] && $dl($x)} {
5832 + set growanc($x) 1
5833 + incr ngrowanc
5834 + }
5835 + } else {
5836 + set y $arcend($a)
5837 + if {[info exists dl($y)]} {
5838 + if {$dl($y)} {
5839 + if {!$dl($x)} {
5840 + set dl($y) 0
5841 + if {![info exists done($y)]} {
5842 + incr nnh -1
5843 + }
5844 + if {[info exists growanc($x)]} {
5845 + incr ngrowanc -1
5846 + }
5847 + set xl [list $y]
5848 + for {set k 0} {$k < [llength $xl]} {incr k} {
5849 + set z [lindex $xl $k]
5850 + foreach c $arcout($z) {
5851 + if {[info exists arcend($c)]} {
5852 + set v $arcend($c)
5853 + if {[info exists dl($v)] && $dl($v)} {
5854 + set dl($v) 0
5855 + if {![info exists done($v)]} {
5856 + incr nnh -1
5857 + }
5858 + if {[info exists growanc($v)]} {
5859 + incr ngrowanc -1
5860 + }
5861 + lappend xl $v
5862 + }
5863 + }
5864 + }
5865 + }
5866 + }
5867 + }
5868 + } elseif {$y eq $anc || !$dl($x)} {
5869 + set dl($y) 0
5870 + lappend anclist $y
5871 + } else {
5872 + set dl($y) 1
5873 + lappend anclist $y
5874 + incr nnh
5875 + }
5876 + }
5877 }
5878 }
5564 - if {[eof $fd]} {
5565 - set travindex [llength $allids]
5566 - set allcommits "traversing"
5567 - after idle restartatags
5568 - if {[catch {close $fd} err]} {
5569 - error_popup "Error reading full commit graph: $err.\n\
5570 - Results may be incomplete."
5879 + foreach x [array names growanc] {
5880 + if {$dl($x)} {
5881 + return 0
5882 }
5883 }
5884 + return 1
5885 }
5886
5575 -# walk backward through the tree and compute nearest tagged ancestors
5576 -proc restartatags {} {
5577 - global allids allparents idtags anc_tags travindex
5887 +proc validate_arctags {a} {
5888 + global arctags idtags
5889
5579 - set t0 [clock clicks -milliseconds]
5580 - set i $travindex
5581 - while {[incr i -1] >= 0} {
5582 - set id [lindex $allids $i]
5583 - set atags {}
5584 - foreach p $allparents($id) {
5585 - if {[info exists idtags($p)]} {
5586 - set ptags [list $p]
5587 - } else {
5588 - set ptags $anc_tags($p)
5890 + set i -1
5891 + set na $arctags($a)
5892 + foreach id $arctags($a) {
5893 + incr i
5894 + if {![info exists idtags($id)]} {
5895 + set na [lreplace $na $i $i]
5896 + incr i -1
5897 + }
5898 + }
5899 + set arctags($a) $na
5900 +}
5901 +
5902 +proc validate_archeads {a} {
5903 + global archeads idheads
5904 +
5905 + set i -1
5906 + set na $archeads($a)
5907 + foreach id $archeads($a) {
5908 + incr i
5909 + if {![info exists idheads($id)]} {
5910 + set na [lreplace $na $i $i]
5911 + incr i -1
5912 + }
5913 + }
5914 + set archeads($a) $na
5915 +}
5916 +
5917 +# Return the list of IDs that have tags that are descendents of id,
5918 +# ignoring IDs that are descendents of IDs already reported.
5919 +proc desctags {id} {
5920 + global arcnos arcstart arcids arctags idtags allparents
5921 + global growing cached_dtags
5922 +
5923 + if {![info exists allparents($id)]} {
5924 + return {}
5925 + }
5926 + set t1 [clock clicks -milliseconds]
5927 + set argid $id
5928 + if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
5929 + # part-way along an arc; check that arc first
5930 + set a [lindex $arcnos($id) 0]
5931 + if {$arctags($a) ne {}} {
5932 + validate_arctags $a
5933 + set i [lsearch -exact $arcids($a) $id]
5934 + set tid {}
5935 + foreach t $arctags($a) {
5936 + set j [lsearch -exact $arcids($a) $t]
5937 + if {$j >= $i} break
5938 + set tid $t
5939 }
5590 - if {$atags eq {}} {
5591 - set atags $ptags
5592 - } elseif {$ptags ne $atags} {
5593 - set atags [combine_atags $atags $ptags]
5940 + if {$tid ne {}} {
5941 + return $tid
5942 }
5943 }
5596 - set anc_tags($id) $atags
5597 - if {[clock clicks -milliseconds] - $t0 >= 50} {
5598 - set travindex $i
5599 - after idle restartatags
5600 - return
5944 + set id $arcstart($a)
5945 + if {[info exists idtags($id)]} {
5946 + return $id
5947 + }
5948 + }
5949 + if {[info exists cached_dtags($id)]} {
5950 + return $cached_dtags($id)
5951 + }
5952 +
5953 + set origid $id
5954 + set todo [list $id]
5955 + set queued($id) 1
5956 + set nc 1
5957 + for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
5958 + set id [lindex $todo $i]
5959 + set done($id) 1
5960 + set ta [info exists hastaggedancestor($id)]
5961 + if {!$ta} {
5962 + incr nc -1
5963 + }
5964 + # ignore tags on starting node
5965 + if {!$ta && $i > 0} {
5966 + if {[info exists idtags($id)]} {
5967 + set tagloc($id) $id
5968 + set ta 1
5969 + } elseif {[info exists cached_dtags($id)]} {
5970 + set tagloc($id) $cached_dtags($id)
5971 + set ta 1
5972 + }
5973 + }
5974 + foreach a $arcnos($id) {
5975 + set d $arcstart($a)
5976 + if {!$ta && $arctags($a) ne {}} {
5977 + validate_arctags $a
5978 + if {$arctags($a) ne {}} {
5979 + lappend tagloc($id) [lindex $arctags($a) end]
5980 + }
5981 + }
5982 + if {$ta || $arctags($a) ne {}} {
5983 + set tomark [list $d]
5984 + for {set j 0} {$j < [llength $tomark]} {incr j} {
5985 + set dd [lindex $tomark $j]
5986 + if {![info exists hastaggedancestor($dd)]} {
5987 + if {[info exists done($dd)]} {
5988 + foreach b $arcnos($dd) {
5989 + lappend tomark $arcstart($b)
5990 + }
5991 + if {[info exists tagloc($dd)]} {
5992 + unset tagloc($dd)
5993 + }
5994 + } elseif {[info exists queued($dd)]} {
5995 + incr nc -1
5996 + }
5997 + set hastaggedancestor($dd) 1
5998 + }
5999 + }
6000 + }
6001 + if {![info exists queued($d)]} {
6002 + lappend todo $d
6003 + set queued($d) 1
6004 + if {![info exists hastaggedancestor($d)]} {
6005 + incr nc
6006 + }
6007 + }
6008 }
6009 }
5603 - set allcommits "done"
5604 - set travindex 0
5605 - notbusy allcommits
5606 - dispneartags
5607 -}
6010 + set tags {}
6011 + foreach id [array names tagloc] {
6012 + if {![info exists hastaggedancestor($id)]} {
6013 + foreach t $tagloc($id) {
6014 + if {[lsearch -exact $tags $t] < 0} {
6015 + lappend tags $t
6016 + }
6017 + }
6018 + }
6019 + }
6020 + set t2 [clock clicks -milliseconds]
6021 + set loopix $i
6022
5609 -# update the desc_tags and anc_tags arrays for a new tag just added
5610 -proc addedtag {id} {
5611 - global desc_tags anc_tags allparents allchildren allcommits
5612 - global idtags tagisdesc alldtags
5613 -
5614 - if {![info exists desc_tags($id)]} return
5615 - set adt $desc_tags($id)
5616 - foreach t $desc_tags($id) {
5617 - set adt [concat $adt $alldtags($t)]
5618 - }
5619 - set adt [lsort -unique $adt]
5620 - set alldtags($id) $adt
5621 - foreach t $adt {
5622 - set tagisdesc($id,$t) -1
5623 - set tagisdesc($t,$id) 1
5624 - }
5625 - if {[info exists anc_tags($id)]} {
5626 - set todo $anc_tags($id)
5627 - while {$todo ne {}} {
5628 - set do [lindex $todo 0]
5629 - set todo [lrange $todo 1 end]
5630 - if {[info exists tagisdesc($id,$do)]} continue
5631 - set tagisdesc($do,$id) -1
5632 - set tagisdesc($id,$do) 1
5633 - if {[info exists anc_tags($do)]} {
5634 - set todo [concat $todo $anc_tags($do)]
6023 + # remove tags that are descendents of other tags
6024 + for {set i 0} {$i < [llength $tags]} {incr i} {
6025 + set a [lindex $tags $i]
6026 + for {set j 0} {$j < $i} {incr j} {
6027 + set b [lindex $tags $j]
6028 + set r [anc_or_desc $a $b]
6029 + if {$r == 1} {
6030 + set tags [lreplace $tags $j $j]
6031 + incr j -1
6032 + incr i -1
6033 + } elseif {$r == -1} {
6034 + set tags [lreplace $tags $i $i]
6035 + incr i -1
6036 + break
6037 }
6038 }
6039 }
6040
5639 - set lastold $desc_tags($id)
5640 - set lastnew [list $id]
5641 - set nup 0
5642 - set nch 0
5643 - set todo $allparents($id)
5644 - while {$todo ne {}} {
5645 - set do [lindex $todo 0]
5646 - set todo [lrange $todo 1 end]
5647 - if {![info exists desc_tags($do)]} continue
5648 - if {$desc_tags($do) ne $lastold} {
5649 - set lastold $desc_tags($do)
5650 - set lastnew [combine_dtags $lastold [list $id]]
5651 - incr nch
6041 + if {[array names growing] ne {}} {
6042 + # graph isn't finished, need to check if any tag could get
6043 + # eclipsed by another tag coming later. Simply ignore any
6044 + # tags that could later get eclipsed.
6045 + set ctags {}
6046 + foreach t $tags {
6047 + if {[is_certain $t $origid]} {
6048 + lappend ctags $t
6049 + }
6050 }
5653 - if {$lastold eq $lastnew} continue
5654 - set desc_tags($do) $lastnew
5655 - incr nup
5656 - if {![info exists idtags($do)]} {
5657 - set todo [concat $todo $allparents($do)]
6051 + if {$tags eq $ctags} {
6052 + set cached_dtags($origid) $tags
6053 + } else {
6054 + set tags $ctags
6055 }
6056 + } else {
6057 + set cached_dtags($origid) $tags
6058 }
6059 + set t3 [clock clicks -milliseconds]
6060 + if {0 && $t3 - $t1 >= 100} {
6061 + puts "iterating descendents ($loopix/[llength $todo] nodes) took\
6062 + [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
6063 + }
6064 + return $tags
6065 +}
6066
5661 - if {![info exists anc_tags($id)]} return
5662 - set lastold $anc_tags($id)
5663 - set lastnew [list $id]
5664 - set nup 0
5665 - set nch 0
5666 - set todo $allchildren($id)
5667 - while {$todo ne {}} {
5668 - set do [lindex $todo 0]
5669 - set todo [lrange $todo 1 end]
5670 - if {![info exists anc_tags($do)]} continue
5671 - if {$anc_tags($do) ne $lastold} {
5672 - set lastold $anc_tags($do)
5673 - set lastnew [combine_atags $lastold [list $id]]
5674 - incr nch
6067 +proc anctags {id} {
6068 + global arcnos arcids arcout arcend arctags idtags allparents
6069 + global growing cached_atags
6070 +
6071 + if {![info exists allparents($id)]} {
6072 + return {}
6073 + }
6074 + set t1 [clock clicks -milliseconds]
6075 + set argid $id
6076 + if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
6077 + # part-way along an arc; check that arc first
6078 + set a [lindex $arcnos($id) 0]
6079 + if {$arctags($a) ne {}} {
6080 + validate_arctags $a
6081 + set i [lsearch -exact $arcids($a) $id]
6082 + foreach t $arctags($a) {
6083 + set j [lsearch -exact $arcids($a) $t]
6084 + if {$j > $i} {
6085 + return $t
6086 + }
6087 + }
6088 + }
6089 + if {![info exists arcend($a)]} {
6090 + return {}
6091 + }
6092 + set id $arcend($a)
6093 + if {[info exists idtags($id)]} {
6094 + return $id
6095 + }
6096 + }
6097 + if {[info exists cached_atags($id)]} {
6098 + return $cached_atags($id)
6099 + }
6100 +
6101 + set origid $id
6102 + set todo [list $id]
6103 + set queued($id) 1
6104 + set taglist {}
6105 + set nc 1
6106 + for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
6107 + set id [lindex $todo $i]
6108 + set done($id) 1
6109 + set td [info exists hastaggeddescendent($id)]
6110 + if {!$td} {
6111 + incr nc -1
6112 + }
6113 + # ignore tags on starting node
6114 + if {!$td && $i > 0} {
6115 + if {[info exists idtags($id)]} {
6116 + set tagloc($id) $id
6117 + set td 1
6118 + } elseif {[info exists cached_atags($id)]} {
6119 + set tagloc($id) $cached_atags($id)
6120 + set td 1
6121 + }
6122 }
5676 - if {$lastold eq $lastnew} continue
5677 - set anc_tags($do) $lastnew
5678 - incr nup
5679 - if {![info exists idtags($do)]} {
5680 - set todo [concat $todo $allchildren($do)]
6123 + foreach a $arcout($id) {
6124 + if {!$td && $arctags($a) ne {}} {
6125 + validate_arctags $a
6126 + if {$arctags($a) ne {}} {
6127 + lappend tagloc($id) [lindex $arctags($a) 0]
6128 + }
6129 + }
6130 + if {![info exists arcend($a)]} continue
6131 + set d $arcend($a)
6132 + if {$td || $arctags($a) ne {}} {
6133 + set tomark [list $d]
6134 + for {set j 0} {$j < [llength $tomark]} {incr j} {
6135 + set dd [lindex $tomark $j]
6136 + if {![info exists hastaggeddescendent($dd)]} {
6137 + if {[info exists done($dd)]} {
6138 + foreach b $arcout($dd) {
6139 + if {[info exists arcend($b)]} {
6140 + lappend tomark $arcend($b)
6141 + }
6142 + }
6143 + if {[info exists tagloc($dd)]} {
6144 + unset tagloc($dd)
6145 + }
6146 + } elseif {[info exists queued($dd)]} {
6147 + incr nc -1
6148 + }
6149 + set hastaggeddescendent($dd) 1
6150 + }
6151 + }
6152 + }
6153 + if {![info exists queued($d)]} {
6154 + lappend todo $d
6155 + set queued($d) 1
6156 + if {![info exists hastaggeddescendent($d)]} {
6157 + incr nc
6158 + }
6159 + }
6160 + }
6161 + }
6162 + set t2 [clock clicks -milliseconds]
6163 + set loopix $i
6164 + set tags {}
6165 + foreach id [array names tagloc] {
6166 + if {![info exists hastaggeddescendent($id)]} {
6167 + foreach t $tagloc($id) {
6168 + if {[lsearch -exact $tags $t] < 0} {
6169 + lappend tags $t
6170 + }
6171 + }
6172 }
6173 }
5683 -}
6174
5685 -# update the desc_heads array for a new head just added
5686 -proc addedhead {hid head} {
5687 - global desc_heads allparents headids idheads
5688 -
5689 - set headids($head) $hid
5690 - lappend idheads($hid) $head
5691 -
5692 - set todo [list $hid]
5693 - while {$todo ne {}} {
5694 - set do [lindex $todo 0]
5695 - set todo [lrange $todo 1 end]
5696 - if {![info exists desc_heads($do)] ||
5697 - [lsearch -exact $desc_heads($do) $head] >= 0} continue
5698 - set oldheads $desc_heads($do)
5699 - lappend desc_heads($do) $head
5700 - set heads $desc_heads($do)
5701 - while {1} {
5702 - set p $allparents($do)
5703 - if {[llength $p] != 1 || ![info exists desc_heads($p)] ||
5704 - $desc_heads($p) ne $oldheads} break
5705 - set do $p
5706 - set desc_heads($do) $heads
6175 + # remove tags that are ancestors of other tags
6176 + for {set i 0} {$i < [llength $tags]} {incr i} {
6177 + set a [lindex $tags $i]
6178 + for {set j 0} {$j < $i} {incr j} {
6179 + set b [lindex $tags $j]
6180 + set r [anc_or_desc $a $b]
6181 + if {$r == -1} {
6182 + set tags [lreplace $tags $j $j]
6183 + incr j -1
6184 + incr i -1
6185 + } elseif {$r == 1} {
6186 + set tags [lreplace $tags $i $i]
6187 + incr i -1
6188 + break
6189 + }
6190 + }
6191 + }
6192 +
6193 + if {[array names growing] ne {}} {
6194 + # graph isn't finished, need to check if any tag could get
6195 + # eclipsed by another tag coming later. Simply ignore any
6196 + # tags that could later get eclipsed.
6197 + set ctags {}
6198 + foreach t $tags {
6199 + if {[is_certain $origid $t]} {
6200 + lappend ctags $t
6201 + }
6202 + }
6203 + if {$tags eq $ctags} {
6204 + set cached_atags($origid) $tags
6205 + } else {
6206 + set tags $ctags
6207 }
5708 - set todo [concat $todo $p]
6208 + } else {
6209 + set cached_atags($origid) $tags
6210 }
6211 + set t3 [clock clicks -milliseconds]
6212 + if {0 && $t3 - $t1 >= 100} {
6213 + puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
6214 + [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
6215 + }
6216 + return $tags
6217 }
6218
5712 -# update the desc_heads array for a head just removed
5713 -proc removedhead {hid head} {
5714 - global desc_heads allparents headids idheads
6219 +# Return the list of IDs that have heads that are descendents of id,
6220 +# including id itself if it has a head.
6221 +proc descheads {id} {
6222 + global arcnos arcstart arcids archeads idheads cached_dheads
6223 + global allparents
6224
5716 - unset headids($head)
5717 - if {$idheads($hid) eq $head} {
5718 - unset idheads($hid)
5719 - } else {
5720 - set i [lsearch -exact $idheads($hid) $head]
5721 - if {$i >= 0} {
5722 - set idheads($hid) [lreplace $idheads($hid) $i $i]
6225 + if {![info exists allparents($id)]} {
6226 + return {}
6227 + }
6228 + set ret {}
6229 + if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
6230 + # part-way along an arc; check it first
6231 + set a [lindex $arcnos($id) 0]
6232 + if {$archeads($a) ne {}} {
6233 + validate_archeads $a
6234 + set i [lsearch -exact $arcids($a) $id]
6235 + foreach t $archeads($a) {
6236 + set j [lsearch -exact $arcids($a) $t]
6237 + if {$j > $i} break
6238 + lappend $ret $t
6239 + }
6240 }
6241 + set id $arcstart($a)
6242 }
5725 -
5726 - set todo [list $hid]
5727 - while {$todo ne {}} {
5728 - set do [lindex $todo 0]
5729 - set todo [lrange $todo 1 end]
5730 - if {![info exists desc_heads($do)]} continue
5731 - set i [lsearch -exact $desc_heads($do) $head]
5732 - if {$i < 0} continue
5733 - set oldheads $desc_heads($do)
5734 - set heads [lreplace $desc_heads($do) $i $i]
5735 - while {1} {
5736 - set desc_heads($do) $heads
5737 - set p $allparents($do)
5738 - if {[llength $p] != 1 || ![info exists desc_heads($p)] ||
5739 - $desc_heads($p) ne $oldheads} break
5740 - set do $p
6243 + set origid $id
6244 + set todo [list $id]
6245 + set seen($id) 1
6246 + for {set i 0} {$i < [llength $todo]} {incr i} {
6247 + set id [lindex $todo $i]
6248 + if {[info exists cached_dheads($id)]} {
6249 + set ret [concat $ret $cached_dheads($id)]
6250 + } else {
6251 + if {[info exists idheads($id)]} {
6252 + lappend ret $id
6253 + }
6254 + foreach a $arcnos($id) {
6255 + if {$archeads($a) ne {}} {
6256 + set ret [concat $ret $archeads($a)]
6257 + }
6258 + set d $arcstart($a)
6259 + if {![info exists seen($d)]} {
6260 + lappend todo $d
6261 + set seen($d) 1
6262 + }
6263 + }
6264 }
5742 - set todo [concat $todo $p]
6265 }
6266 + set ret [lsort -unique $ret]
6267 + set cached_dheads($origid) $ret
6268 }
6269
5746 -# update things for a head moved to a child of its previous location
5747 -proc movedhead {id name} {
5748 - global headids idheads
6270 +proc addedtag {id} {
6271 + global arcnos arcout cached_dtags cached_atags
6272
5750 - set oldid $headids($name)
5751 - set headids($name) $id
5752 - if {$idheads($oldid) eq $name} {
5753 - unset idheads($oldid)
5754 - } else {
5755 - set i [lsearch -exact $idheads($oldid) $name]
5756 - if {$i >= 0} {
5757 - set idheads($oldid) [lreplace $idheads($oldid) $i $i]
5758 - }
6273 + if {![info exists arcnos($id)]} return
6274 + if {![info exists arcout($id)]} {
6275 + recalcarc [lindex $arcnos($id) 0]
6276 }
5760 - lappend idheads($id) $name
6277 + catch {unset cached_dtags}
6278 + catch {unset cached_atags}
6279 }
6280
5763 -proc changedrefs {} {
5764 - global desc_heads desc_tags anc_tags allcommits allids
5765 - global allchildren allparents idtags travindex
6281 +proc addedhead {hid head} {
6282 + global arcnos arcout cached_dheads
6283 +
6284 + if {![info exists arcnos($hid)]} return
6285 + if {![info exists arcout($hid)]} {
6286 + recalcarc [lindex $arcnos($hid) 0]
6287 + }
6288 + catch {unset cached_dheads}
6289 +}
6290 +
6291 +proc removedhead {hid head} {
6292 + global cached_dheads
6293 +
6294 + catch {unset cached_dheads}
6295 +}
6296 +
6297 +proc movedhead {hid head} {
6298 + global arcnos arcout cached_dheads
6299
5767 - if {![info exists allcommits]} return
5768 - catch {unset desc_heads}
5769 - catch {unset desc_tags}
5770 - catch {unset anc_tags}
5771 - catch {unset alldtags}
5772 - catch {unset tagisdesc}
5773 - foreach id $allids {
5774 - forward_pass $id $allchildren($id)
6300 + if {![info exists arcnos($hid)]} return
6301 + if {![info exists arcout($hid)]} {
6302 + recalcarc [lindex $arcnos($hid) 0]
6303 }
5776 - if {$allcommits ne "reading"} {
5777 - set travindex [llength $allids]
5778 - if {$allcommits ne "traversing"} {
5779 - set allcommits "traversing"
5780 - after idle restartatags
6304 + catch {unset cached_dheads}
6305 +}
6306 +
6307 +proc changedrefs {} {
6308 + global cached_dheads cached_dtags cached_atags
6309 + global arctags archeads arcnos arcout idheads idtags
6310 +
6311 + foreach id [concat [array names idheads] [array names idtags]] {
6312 + if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
6313 + set a [lindex $arcnos($id) 0]
6314 + if {![info exists donearc($a)]} {
6315 + recalcarc $a
6316 + set donearc($a) 1
6317 + }
6318 }
6319 }
6320 + catch {unset cached_dtags}
6321 + catch {unset cached_atags}
6322 + catch {unset cached_dheads}
6323 }
6324
6325 proc rereadrefs {} {