/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
2
# Some parts of the code are:
3
# Copyright (C) 2005, 2006 by Canonical Ltd
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
import time
20
21
import bzrlib
22
23
import bzrlib.osutils as osutils
24
66.2.17 by Aaron Bentley
Update olive to use current bzr.dev API. Used _repo_relpath. Bad phanatic!
25
from bzrlib.info import _repo_rel_url
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
26
from bzrlib.missing import find_unmerged
27
28
def get_location_info(repository, branch=None, working=None):
29
    """ Get known locations for working, branch and repository.
30
    
31
    :return: a dictionary containing the needed infos
32
    """
33
    ret = {}
34
    repository_path = repository.bzrdir.root_transport.base
35
    if working and branch:
36
        working_path = working.bzrdir.root_transport.base
37
        branch_path = branch.bzrdir.root_transport.base
38
        if working_path != branch_path:
39
            # lightweight checkout
40
            ret['lightcoroot'] = working_path
41
            if repository.is_shared():
42
                # lightweight checkout of branch in shared repository
43
                ret['sharedrepo'] = repository_path
66.2.17 by Aaron Bentley
Update olive to use current bzr.dev API. Used _repo_relpath. Bad phanatic!
44
                ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
45
            else:
46
                # lightweight checkout of standalone branch
47
                ret['cobranch'] = branch_path
48
        elif repository.is_shared():
49
            # branch with tree inside shared repository
50
            ret['sharedrepo'] = repository_path
66.2.17 by Aaron Bentley
Update olive to use current bzr.dev API. Used _repo_relpath. Bad phanatic!
51
            ret['repoco'] = _repo_rel_url(repository_path, branch_path)
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
52
        elif branch.get_bound_location():
53
            # normal checkout
54
            ret['coroot'] = working_path
55
            ret['cobranch'] = branch.get_bound_location()
56
        else:
57
            # standalone
58
            ret['branchroot'] = working_path
59
    elif branch:
60
        branch_path = branch.bzrdir.root_transport.base
61
        if repository.is_shared():
62
            # branch is part of shared repository
63
            ret['sharedrepo'] = repository_path
66.2.17 by Aaron Bentley
Update olive to use current bzr.dev API. Used _repo_relpath. Bad phanatic!
64
            ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
65
        else:
66
            # standalone branch
67
            ret['branchroot'] = branch_path
68
    else:
69
        # shared repository
70
        assert repository.is_shared()
71
        ret['sharedrepo'] = repository_path
72
    
73
    return ret
74
75
def get_related_info(branch):
76
    """ Get parent and push location of branch.
77
    
78
    :return: a dictionary containing the needed infos
79
    """
80
    ret = {}
81
    if branch.get_parent() or branch.get_push_location():
82
        if branch.get_parent():
83
            ret['parentbranch'] = branch.get_parent()
84
        if branch.get_push_location():
85
            ret['publishbranch'] = branch.get_push_location()
86
    
87
    return ret
88
89
90
def get_format_info(control=None, repository=None, branch=None, working=None):
91
    """ Get known formats for control, working, branch and repository.
92
    
93
    :return: a dictionary containing the needed infos
94
    """
95
    ret = {}
96
    if control:
97
        ret['control'] = control._format.get_format_description()
98
    if working:
99
        ret['workingtree'] = working._format.get_format_description()
100
    if branch:
101
        ret['branch'] = branch._format.get_format_description()
102
    if repository:
103
        ret['repository'] = repository._format.get_format_description()
104
    
105
    return ret
106
107
108
def get_locking_info(repository, branch=None, working=None):
109
    """ Get locking status of working, branch and repository.
110
    
111
    :return: a dictionary containing the needed infos
112
    """
113
    ret = {}
114
    if (repository.get_physical_lock_status() or
115
        (branch and branch.get_physical_lock_status()) or
116
        (working and working.get_physical_lock_status())):
117
        if working:
118
            if working.get_physical_lock_status():
119
                status = 'locked'
120
            else:
121
                status = 'unlocked'
122
            ret['workingtree'] = status
123
        if branch:
124
            if branch.get_physical_lock_status():
125
                status = 'locked'
126
            else:
127
                status = 'unlocked'
128
            ret['branch'] = status
129
        if repository:
130
            if repository.get_physical_lock_status():
131
                status = 'locked'
132
            else:
133
                status = 'unlocked'
134
            ret['repository'] = status
135
    
136
    return ret
137
138
def get_missing_revisions_branch(branch):
139
    """ Get missing master revisions in branch.
140
    
141
    :return: a dictionary containing the needed infos
142
    """
143
    ret = {}
144
    # Try with inaccessible branch ?
145
    master = branch.get_master_branch()
146
    if master:
147
        local_extra, remote_extra = find_unmerged(branch, master)
148
        if remote_extra:
149
            ret = len(remote_extra)
150
    
151
    return ret
152
153
154
def get_missing_revisions_working(working):
155
    """ Get missing revisions in working tree.
156
    
157
    :return: a dictionary containing the needed infos
158
    """
159
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
160
        # function deprecated after 0.9
161
        from bzrlib.delta import compare_trees
162
163
    ret = {}
164
    branch = working.branch
165
    basis = working.basis_tree()
188.3.3 by John Arbash Meinel
Avoid using working.inventory, instead use working.iter_entries_by_dir()
166
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
167
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
168
        delta = compare_trees(basis, working, want_unchanged=True)
169
    else:
170
        delta = working.changes_from(basis, want_unchanged=True)
171
172
    history = branch.revision_history()
173
    tree_last_id = working.last_revision()
174
175
    if len(history) and tree_last_id != history[-1]:
176
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
177
        missing_count = len(history) - tree_last_revno
178
        ret = missing_count
179
    
180
    return ret
181
182
183
def get_working_stats(working):
184
    """ Get statistics about a working tree.
185
    
186
    :return: a dictionary containing the needed infos
187
    """
188
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
189
        # function deprecated after 0.9
190
        from bzrlib.delta import compare_trees
191
    
192
    ret = {}
193
    basis = working.basis_tree()
188.3.3 by John Arbash Meinel
Avoid using working.inventory, instead use working.iter_entries_by_dir()
194
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
195
    if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
196
        delta = compare_trees(basis, working, want_unchanged=True)
197
    else:
198
        delta = working.changes_from(basis, want_unchanged=True)
199
200
    ret['unchanged'] = len(delta.unchanged)
201
    ret['modified'] = len(delta.modified)
202
    ret['added'] = len(delta.added)
203
    ret['removed'] = len(delta.removed)
204
    ret['renamed'] = len(delta.renamed)
205
206
    ignore_cnt = unknown_cnt = 0
207
    for path in working.extras():
208
        if working.is_ignored(path):
209
            ignore_cnt += 1
210
        else:
211
            unknown_cnt += 1
212
    ret['unknown'] = unknown_cnt
213
    ret['ignored'] = ignore_cnt
214
215
    dir_cnt = 0
188.3.3 by John Arbash Meinel
Avoid using working.inventory, instead use working.iter_entries_by_dir()
216
    for path, ie in working.iter_entries_by_dir():
217
        if ie.kind == 'directory':
0.8.84 by Szilveszter Farkas (Phanatic)
Huge cleanup after removing backend codebase.
218
            dir_cnt += 1
219
    ret['subdirs'] = dir_cnt
220
221
    return ret
222
223
def get_branch_stats(branch):
224
    """ Get statistics about a branch.
225
    
226
    :return: a dictionary containing the needed infos
227
    """
228
    ret = {}
229
    repository = branch.repository
230
    history = branch.revision_history()
231
232
    revno = len(history)
233
    ret['revno'] = revno 
234
    committers = {}
235
    for rev in history:
236
        committers[repository.get_revision(rev).committer] = True
237
    ret['commiters'] = len(committers)
238
    if revno > 0:
239
        firstrev = repository.get_revision(history[0])
240
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
241
        ret['age'] = age
242
        ret['firstrev'] = osutils.format_date(firstrev.timestamp,
243
                                              firstrev.timezone)
244
245
        lastrev = repository.get_revision(history[-1])
246
        ret['lastrev'] = osutils.format_date(lastrev.timestamp,
247
                                             lastrev.timezone)
248
249
    return ret
250
251
def get_repository_stats(repository):
252
    """ Get statistics about a repository.
253
    
254
    :return: a dictionary containing the needed infos
255
    """
256
    ret = {}
257
    if repository.bzrdir.root_transport.listable():
258
        c, t = repository._revision_store.total_size(repository.get_transaction())
259
        ret['revisions'] = c
260
        ret['size'] = t
261
262
    return ret
263
264
def diff_helper(tree, specific_files, external_diff_options, 
265
                    old_revision_spec=None, new_revision_spec=None,
266
                    old_label='a/', new_label='b/', output=None):
267
    """ Helper for diff.
268
269
    :param tree: a WorkingTree
270
271
    :param specific_files: the specific files to compare, or None
272
273
    :param external_diff_options: if non-None, run an external diff, and pass it these options
274
275
    :param old_revision_spec: if None, use basis tree as old revision, otherwise use the tree for the specified revision. 
276
277
    :param new_revision_spec:  if None, use working tree as new revision, otherwise use the tree for the specified revision.
278
    """
279
    import sys
280
    
281
    from bzrlib.diff import show_diff_trees
282
    
283
    if output == None:
284
        output = sys.stdout
285
    
286
    def spec_tree(spec):
287
        revision_id = spec.in_store(tree.branch).rev_id
288
        return tree.branch.repository.revision_tree(revision_id)
289
    
290
    if old_revision_spec is None:
291
        old_tree = tree.basis_tree()
292
    else:
293
        old_tree = spec_tree(old_revision_spec)
294
295
    if new_revision_spec is None:
296
        new_tree = tree
297
    else:
298
        new_tree = spec_tree(new_revision_spec)
299
300
    return show_diff_trees(old_tree, new_tree, output, specific_files,
301
                           external_diff_options,
302
                           old_label=old_label, new_label=new_label)