/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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- 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>"


def linegraph(branch, start, maxnum):
    """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.
    """
    
    def getdirectparent(childrevid, 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:
                if parentrevid in revindex:
                    parentindex = revindex[parentrevid]
                    parentrevision = revisions[parentindex]
                    if childrevision.committer == parentrevision.committer:
                        # This may be a direct parent, but first check that
                        # for this parent, there are no other children, who are
                        # before us in the children list, for which this parent
                        # is also the direct parent.
                        # pc in all the below var name stands for parents child
                        first = True
                        for pcrevid in revisionchildren[parentindex]:
                            if pcrevid == childrevid:
                                break
                            pcindex = revindex[pcrevid]
                            pcdirectparent = getdirectparent(pcrevid,
                                                    pcindex,
                                                    revisionchildren[pcindex])
                            if pcdirectparent==parentrevid:
                                first = False
                                break
                        
                        if first:
                            directparent = parentrevid
                            break
            #no parents have the same commiter
            if directparent is None:
                directparent = ""
            directparentcache[childindex] = directparent
        return directparent
    
    def find_column_from_branchlineid(branchlineid):
        for index, column in enumerate(columns):
            if column is not None and column["branchlineid"] == branchlineid:
                return (index, column)
        return (None, None)
    
    def has_no_nodes_between (column, startindex, endindex):
        for nodeindex in column["nodeindexes"]:
            if nodeindex > startindex and nodeindex < endindex:
                return False
        return True
    
    def append_line (column , startindex, endindex):
        column["lines"].append((startindex,endindex))
        if endindex > column["maxindex"] :
            column["maxindex"] = endindex
    
    def append_column (column, minindex):
        columnindex = None
        for (i, c) in enumerate(columns):
            if c is None or (c["branchlineid"] == "finished" \
                             and c["maxindex"] <= minindex):
                columnindex = i
                columns[columnindex] = column
                break
        if columnindex is None:
            columnindex = len(columns)
            columns.append(column)
        return columnindex
    
    revids = []
    revindex = {}
    for (index, revid) in enumerate(reversed( \
            branch.repository.get_ancestry(start))):
        if revid is not None:
            revids.append(revid)
            revindex[revid] = index
        if maxnum is not None and index > maxnum:
            break
    
    mainline = branch.revision_history()
    revisions = branch.repository.get_revisions(revids)
    revisionparents = branch.repository.get_graph().get_parents(revids)    
    directparentcache = [None for revision in revisions]
    
    # This will hold what we plan to put in each column.
    # The position of the item in this list indicates the column, and it
    # it may change if we need to make space for other things.
    # Each typle in the list is in the form:
    # (branchlineid, nodeindexes, lines, maxindex)
    # A item may be None to indicate that there is nothing in the column
    columns = []
    
    linegraph = []
    
    lastbranchlineid = 0
    branchlineids = []
    revisionchildren = [[] for revision in revisions]
    notdrawnlines = []
    
    for (index, revision) in enumerate(revisions):
        parents = [parent for parent in revisionparents[index] \
                   if parent!="null:"]
        for parentrevid in parents:
            if parentrevid in revindex:
                revisionchildren[revindex[parentrevid]]\
                    .append(revision.revision_id)
        
        children = revisionchildren[index]
        
        #We use childrevid, childindex, childbranchlineid often, so cache it
        children_ext = []
        for childrevid in children:
            childindex = revindex[childrevid]
            children_ext.append((childrevid,
                                 childindex,
                                 branchlineids[childindex]))
        
        linegraph.append([revision, None,
                          [], parents, children])
        
        branchlineid = None
        
        if revision.revision_id in mainline:
            branchlineid = 0
        else:
            #Try and see if we are the same branchline as one of our children
            #If we are, use the same branchlineid
            for (childrevid, childindex, childbranchlineid) in children_ext: 
                childsparents = revisionparents[childindex]
                
                if len(children) == 1 and len(childsparents) == 1: 
                    # one-one relationship between parent and child
                    branchlineid = childbranchlineid
                    break
                
                #Is the current revision the direct parent of the child?
                if childbranchlineid != 0 and revision.revision_id == \
                        getdirectparent(childrevid, childindex, childsparents):
                    branchlineid = childbranchlineid
                    break
        
        if branchlineid is None:
            branchlineid = lastbranchlineid = lastbranchlineid + 1
        
        branchlineids.append(branchlineid)
        
        (columnindex, column) = find_column_from_branchlineid(branchlineid)
        
        if columnindex is None:
            for (childrevid, childindex, childbranchlineid) in children_ext:
                (i, c) = find_column_from_branchlineid(childbranchlineid)
                if c is not None and c["maxindex"] <= index:
                    (columnindex, column) = (i, c)
                    break
    

        
        if columnindex is None:
            minindex = index
            for childrevid in children:
                childindex = revindex[childrevid]
                if childindex<minindex:
                    minindex = childindex
            
            column = {"branchlineid": branchlineid,
                      "nodeindexes": [index],
                      "lines": [],
                      "maxindex": index}
            columnindex = append_column(column, minindex)
        else:
            column["branchlineid"] = branchlineid
            column["nodeindexes"].append(index)
        
        opentillparent = getdirectparent(revision.revision_id, index, parents)
        if opentillparent == "":
            if len(parents)>0:
                opentillparent = parents[0]
            else:
                opentillparent = None
        
        if opentillparent is not None and opentillparent in revindex:
            parentindex = revindex[opentillparent]
            if parentindex > column["maxindex"]:
                column["maxindex"] = parentindex
        
        for (childrevid, childindex, childbranchlineid) in children_ext: 
            (childcolumnindex, childcolumn) = \
                find_column_from_branchlineid(childbranchlineid)
            
            if index-childindex == 1 or childcolumnindex is None:
                append_line(column,childindex,index)
            elif childcolumnindex >= columnindex and \
                    has_no_nodes_between(childcolumn, childindex, index):
                append_line(childcolumn,childindex,index)
            elif childcolumnindex > columnindex and \
                    has_no_nodes_between(column, childindex, index):
                append_line(column,childindex,index)
            elif childcolumnindex < columnindex and \
                    has_no_nodes_between(column, childindex, index):
                append_line(column,childindex,index)
            elif childcolumnindex < columnindex and \
                    has_no_nodes_between(childcolumn, childindex, index):
                append_line(childcolumn,childindex,index)
            else:
                append_column({"branchlineid": "line",
                                "nodeindexes": [],
                                "lines": [(childindex,index)],
                                "maxindex": index}, childindex)
        
        for (columnindex, column) in enumerate(columns):
            if column is not None and column["maxindex"] <= index \
                    and column["branchlineid"] != "finished":
                for nodeindex in column["nodeindexes"]:
                    linegraph[nodeindex][1] = (columnindex,
                                               branchlineids[nodeindex])
            
                for (childindex, parentindex) in column["lines"]:
                    notdrawnlines.append((columnindex, childindex, parentindex))
                
                column["branchlineid"] = "finished"
                column["nodeindexes"] = []
                column["lines"] = []
        
        for (lineindex,(columnindex, childindex, parentindex))\
                in enumerate(notdrawnlines):
            
            childnode = linegraph[childindex][1]
            parentnode = linegraph[parentindex][1]
            if childnode is not None and parentnode is not None:
                notdrawnlines[lineindex] = None
                if parentindex>childindex+1:
                    #out from the child to line
                    linegraph[childindex][2].append(
                        (childnode[0],    #the column of the child
                         columnindex,     #the column of the line
                         parentnode[1]))
                    
                    #down the line
                    for linepartindex in range(childindex+1, parentindex-1):
                        linegraph[linepartindex][2].append(
                            (columnindex, #the column of the line
                             columnindex, #the column of the line
                             parentnode[1]))
                    
                    #in to the parent
                    linegraph[parentindex-1][2].append(
                        (columnindex,     #the column of the line
                         parentnode[0],   #the column of the parent
                         parentnode[1]))
                else:
                    #child to parent
                    linegraph[childindex][2].append(
                        (childnode[0],    #the column of the child
                         parentnode[0],   #the column of the parent
                         parentnode[1]))
        
        notdrawnlines = [line for line in notdrawnlines if line is not None]
        

    return (linegraph, revindex, revisions)


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