~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to graph.py

  • Committer: Aaron Bentley
  • Date: 2005-08-30 16:33:39 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050830163339-0ced18607bbaf642
Got initial graphing functionality working

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from dotgraph import Node
 
2
from bzrlib.branch import Branch
 
3
import bzrlib.errors
 
4
 
 
5
def add_relations(rev_id):
 
6
    if rev_id in ancestors:
 
7
        return
 
8
    print rev_id
 
9
    if rev_id not in nodes:
 
10
        nodes[rev_id] = Node("n%d" % counter, label = rev_id)
 
11
        counter += 1
 
12
    revision = branch.get_revision(rev_id)
 
13
    ancestors [rev_id] = []
 
14
    for p in (p.revision_id for p in revision.parents):
 
15
        add_relations(p)
 
16
        if p not in descendants:
 
17
            descendants[p] = []
 
18
        descendants[p].append(rev_id)
 
19
        ancestors [rev_id].append(rev_id)
 
20
 
 
21
def graph_ancestry(branch):
 
22
    nodes = {}
 
23
    q = ((i+1, n) for (i, n) in enumerate(branch.revision_history()))
 
24
    r = 1
 
25
    for (revno, rev_id) in q:
 
26
        nodes[rev_id] = Node("R%d" % revno, color="#ffff00")
 
27
 
 
28
    ancestors = {} 
 
29
    descendants = {}
 
30
    counter = 0
 
31
    lines = [branch.last_patch()]
 
32
    while len(lines) > 0:
 
33
        new_lines = set()
 
34
        for rev_id in lines:
 
35
            if rev_id not in nodes:
 
36
                nodes[rev_id] = Node("n%d" % counter, label=rev_id)
 
37
                counter+=1
 
38
                
 
39
            try:
 
40
                revision = branch.get_revision(rev_id)
 
41
            except bzrlib.errors.NoSuchRevision:
 
42
                continue
 
43
            parent_ids = [r.revision_id for r in revision.parents]
 
44
            ancestors [rev_id] = parent_ids
 
45
            for parent in parent_ids:
 
46
                if parent not in ancestors:
 
47
                    new_lines.add(parent)
 
48
                    descendants[parent] = []
 
49
                descendants[parent].append(rev_id)
 
50
        lines = new_lines
 
51
    node_relations = []
 
52
    for key, values in descendants.iteritems():
 
53
        for value in values:
 
54
            node_relations.append((nodes[key], nodes[value]))
 
55
    return node_relations