/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: UTF-8 -*-
"""Directed graph production.

This module contains the code to produce an ordered directed graph of a
bzr branch, such as we display in the tree view at the top of the bzrk
window.
"""

__copyright__ = "Copyright © 2005 Canonical Ltd."
__author__    = "Scott James Remnant <scott@ubuntu.com>"


from bzrlib.revision import Revision
from bzrlib.tsort import merge_sort

def linegraph(revisions, revisionparents, revindex):
    """Produce a directed graph of a bzr branch.

    Returns a list of tuples of (revision, node, lines, parents, children).

    Node is a tuple of (column, colour) with column being a zero-indexed
    column number of the graph that this revision represents and colour
    being a zero-indexed colour (which doesn't specify any actual colour
    in particular) to draw the node in.

    Lines is a list of tuples which represent lines you should draw away
    from the revision, if you also need to draw lines into the revision
    you should use the lines list from the previous iteration.  Each
    typle in the list is in the form (start, end, colour) with start and
    end being zero-indexed column numbers and colour as in node.

    It's up to you how to actually draw the nodes and lines (straight,
    curved, kinked, etc.) and to pick the actual colours for each index.
    """
    
    directparentcache = [None for revision in revisions]
    def getdirectparent(childindex, childsparents):
        """Return the revision id of the direct parent
        
        The direct parent is the first parent with the same committer"""
        childrevision = revisions[childindex]
        directparent = directparentcache[childindex]
        if directparent is None:
            for parentrevid in childsparents:
                parentrevision = revisions[revindex[parentrevid]]
                if childrevision.committer == parentrevision.committer:
                    directparent = parentrevid
                    break
            #no parents have the same commiter
            if directparent is None:
                directparent = ""
            directparentcache[childindex] = directparent
        return directparent
        

    
    #This will hold the lines we have not yet added to lines
    #The position of the item in this list indicates the column, and it
    #it may change if we need to make space for other branches.
    #Each typle in the list is in the form (child index, parent revision)
    #A item may be None to indicate that there is no line for a column
    activelines = []
    
    linegraph = []
    
    lastcolor = 0
    
    for (index, revision) in enumerate(revisions):
        parents = [parent for parent in revisionparents[index]\
                   if parent!="null:"]
        
        revnodecolumn = None
        
        #This will hold a list of lines whose parent is this rev
        linesforcurrentrev = []
        
        children = []
        
        #We should maybe should pop None's at the end of activelines.
        #I'm not sure what will cost more: resizing the list, or
        #have this loop ittrate more.
        
        #Find lines that end at this rev
        for (column, activeline) in enumerate(activelines):
            if (activeline is not None):
                (childindex, parentrevid) = activeline
                if parentrevid == revision.revision_id:
                    linesforcurrentrev.append((childindex, parentrevid, column))
                    activelines[column] = None
                    children.append(linegraph[childindex][0].revision_id)
                    
                    #The node column for this rev will be the smallest
                    #column for the lines that end at this rev
                    #The smallest column is the first one we get to.
                    if revnodecolumn is None:
                        revnodecolumn = column
        
        #This will happen for the latest revision
        if revnodecolumn is None:
            revnodecolumn = 0
        
        color = None
        
        #Try and see if we are the same "branch" as one of our children
        #If we are, use the childs color
        for childrevid in children:
            childindex = revindex[childrevid]
            childsparents = revisionparents[childindex]
            if len(children) == 1 and len(childsparents) == 1: 
                # one-one relationship between parent and child, same colour
                #1st [1] selects the node
                #2nd [1] selects the color
                color = linegraph[childindex][1][1]
                break
            
            #Is the current revision the direct parent of the child?
            if revision.revision_id == getdirectparent(childindex, childsparents):
                color = linegraph[childindex][1][1]
                break
        
        if color is None:
            color = lastcolor = lastcolor + 1
        
        #We now have every thing (except for the lines) so we can add
        #our tuple to our list.
        linegraph.append((revision, (revnodecolumn, color),
                          [], parents, children))
        
        #add all the line bits to the rev that the line passes
        for (childindex, parentrevid, column) in linesforcurrentrev:
            if index>childindex+1:
                #out from the child to line
                linegraph[childindex][2].append(
                    (linegraph[childindex][1][0], #the column of the child
                     column,                      #the column of the line
                     color))
                
                #down the line
                for linepartindex in range(childindex+1, index-1):
                    linegraph[linepartindex][2].append(
                        (column,                  #the column of the line
                         column,                  #the column of the line
                         color))
                
                #in to the parent
                linegraph[index-1][2].append(
                    (column,                      #the column of the line
                     revnodecolumn,               #the column of the parent
                     color))
            else:
                #child to parent
                linegraph[childindex][2].append(
                    (linegraph[childindex][1][0], #the column of the child
                     revnodecolumn,               #the column of the parent
                     color))
                
        for parentrevid in parents:
            column = revnodecolumn
            line = (index,parentrevid)
            while True:
                if column<len(activelines):
                    if activelines[column] is None:
                        #An empty column. Put line here
                        activelines[column] = line
                        break
                    else:
                        if activelines[column][0] == index:
                            #This column is allready used for a line for
                            #this rev, Move along.
                            column += 1
                        else:
                            #This column is allready used for a line for
                            #another rev. Insert this line at this column,
                            #and in the process, move all the other lines out.
                            activelines.insert(column, line)
                            break
                else:
                    #no more columns, so add one to the end
                    activelines.append(line)
                    break

    return linegraph


def same_branch(a, b):
    """Return whether we think revisions a and b are on the same branch."""
    if len(a.parent_ids) == 1:
        # Defacto same branch if only parent
        return True
    elif a.committer == b.committer:
        # Same committer so may as well be
        return True
    else:
        return False