/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
# -*- 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.tsort import merge_sort

def linegraph(branch, start, maxnum):
    """Produce a directed graph of a bzr branch.

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

    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.
    """
       
    mainline = branch.revision_history()
    graph_parents = branch.repository.get_revision_graph(start)
    graph_children = {}
    for revid in graph_parents.keys():
        graph_children[revid] = []

    merge_sorted_revisions = merge_sort(
        graph_parents,
        start,
        mainline,
        generate_revno=True)
    
    revid_index = {}
    revno_index = {}
    branch_lines = {}
    linegraph = []    
    
    for (rev_index, (sequence_number,
                     revid,
                     merge_depth,
                     revno_sequence,
                     end_of_merge)) in enumerate(merge_sorted_revisions):
        
        revid_index[revid] = rev_index
        revno_index[revno_sequence] = rev_index
        
        branch_id = revno_sequence[0:-1]
        
        branch_line = None
        if branch_id not in branch_lines:
            branch_line = {"line_type": "branch_line",
                           "branch_id": branch_id,
                           "rev_indexes": [],
                           "min_index": rev_index,
                           "max_index": 0}
            branch_lines[branch_id] = branch_line
        else:
            branch_line = branch_lines[branch_id]
        branch_line["rev_indexes"].append(rev_index)
        if rev_index > branch_line["max_index"]:
            branch_line["max_index"] = rev_index
        
        parents = graph_parents[revid]
        for parent_revid in parents:
            graph_children[parent_revid].append(revid)
        
        linegraph.append([revid,
                          None,
                          [],
                          parents,
                          None,
                          revno_sequence])

    branch_ids = branch_lines.keys()
    def branch_id_cmp(x, y):
        len_x = len(x)
        len_y = len(y)
        if len_x == len_y:
            return -cmp(x, y)
        return cmp(len_x, len_y)
        
    branch_ids.sort(branch_id_cmp)
    lines = []
    columns = []
    
    for branch_id in branch_ids:
        branch_line = branch_lines[branch_id]
        
        append_line(columns, branch_line)
        branch_id = branch_line["branch_id"]
        color = reduce(lambda x, y: x+y, branch_id, 0)
        col_index = branch_line["col_index"]
        node = (col_index, color)        
        
        for rev_index in branch_line["rev_indexes"]:
            (sequence_number,
                 revid,
                 merge_depth,
                 revno_sequence,
                 end_of_merge) = merge_sorted_revisions[rev_index]
            
            linegraph[rev_index][1] = node
            linegraph[rev_index][4] = graph_children[revid]
            
            for parent_revid in graph_parents[revid]:
                if parent_revid in revid_index:
                    parent_index = revid_index[parent_revid]
                    parent_revno = merge_sorted_revisions[parent_index][3]
                    parent_branch_id = parent_revno[0:-1]
                    line = {"line_type": "inter_branch_line",
                            "min_index": rev_index,
                            "max_index": parent_index}
                    lines.append(line)
                    if branch_id != parent_branch_id and \
                                    parent_index - rev_index > 1:
                        append_line(columns, line)
    
    for line in lines:
        parent_index = line["max_index"]
        parent_node = linegraph[parent_index][1]
        parent_col_index = parent_node[0]
        color = parent_node[1]
        
        child_index = line["min_index"]
        child_col_index = linegraph[child_index][1][0]
        
        if "col_index" in line:
            line_col_index = line["col_index"]
            linegraph[child_index][2].append(
                (child_col_index,
                 line_col_index,
                 color))
            for line_part_index in range(child_index+1, parent_index-1):
                linegraph[line_part_index][2].append(
                    (line_col_index,   
                     line_col_index,
                     color))
            linegraph[parent_index-1][2].append(
                (line_col_index,
                 parent_col_index,
                 color))
        else:
            linegraph[child_index][2].append(
                (child_col_index,
                 parent_col_index,
                 color))
            for line_part_index in range(child_index+1, parent_index):
                linegraph[line_part_index][2].append(
                    (parent_col_index,   
                     parent_col_index,
                     color))
                    
    
    return (linegraph, revid_index)

def append_line(columns, line):
    for col_index, column in enumerate(columns):
        has_overlaping_line = False
        for col_line in column:
            if not (col_line["min_index"] >= line["max_index"] or \
                    col_line["max_index"] <=  line["min_index"]):
                has_overlaping_line = True
                break
        if not has_overlaping_line:
            break
    else:
        col_index = len(columns)
        columns.append([])
    line["col_index"] = col_index
    columns[col_index].append(line)
    


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