/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to backend/info_helper.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-08 17:36:59 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060708173659-8ab442681dadd8ea
2006-07-08  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * backend/errors.py: added some exceptions related to diff() and log()
    * backend/info.py: implemented log()
    * backend/info.py: diff() works well with revnos
    * added e-mail address to copyright header

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.diff as diff
 
22
import bzrlib.osutils as osutils
 
23
 
 
24
from bzrlib.info import _repo_relpath
 
25
from bzrlib.missing import find_unmerged
 
26
 
 
27
def get_location_info(repository, branch=None, working=None):
 
28
    """ Get known locations for working, branch and repository.
 
29
    
 
30
    :return: a dictionary containing the needed infos
 
31
    """
 
32
    ret = {}
 
33
    repository_path = repository.bzrdir.root_transport.base
 
34
    if working and branch:
 
35
        working_path = working.bzrdir.root_transport.base
 
36
        branch_path = branch.bzrdir.root_transport.base
 
37
        if working_path != branch_path:
 
38
            # lightweight checkout
 
39
            ret['lightcoroot'] = working_path
 
40
            if repository.is_shared():
 
41
                # lightweight checkout of branch in shared repository
 
42
                ret['sharedrepo'] = repository_path
 
43
                ret['repobranch'] = _repo_relpath(repository_path, branch_path)
 
44
            else:
 
45
                # lightweight checkout of standalone branch
 
46
                ret['cobranch'] = branch_path
 
47
        elif repository.is_shared():
 
48
            # branch with tree inside shared repository
 
49
            ret['sharedrepo'] = repository_path
 
50
            ret['repoco'] = _repo_relpath(repository_path, branch_path)
 
51
        elif branch.get_bound_location():
 
52
            # normal checkout
 
53
            ret['coroot'] = working_path
 
54
            ret['cobranch'] = branch.get_bound_location()
 
55
        else:
 
56
            # standalone
 
57
            ret['branchroot'] = working_path
 
58
    elif branch:
 
59
        branch_path = branch.bzrdir.root_transport.base
 
60
        if repository.is_shared():
 
61
            # branch is part of shared repository
 
62
            ret['sharedrepo'] = repository_path
 
63
            ret['repobranch'] = _repo_relpath(repository_path, branch_path)
 
64
        else:
 
65
            # standalone branch
 
66
            ret['branchroot'] = branch_path
 
67
    else:
 
68
        # shared repository
 
69
        assert repository.is_shared()
 
70
        ret['sharedrepo'] = repository_path
 
71
    
 
72
    return ret
 
73
 
 
74
def get_related_info(branch):
 
75
    """ Get parent and push location of branch.
 
76
    
 
77
    :return: a dictionary containing the needed infos
 
78
    """
 
79
    ret = {}
 
80
    if branch.get_parent() or branch.get_push_location():
 
81
        if branch.get_parent():
 
82
            ret['parentbranch'] = branch.get_parent()
 
83
        if branch.get_push_location():
 
84
            ret['publishbranch'] = branch.get_push_location()
 
85
    
 
86
    return ret
 
87
 
 
88
 
 
89
def get_format_info(control=None, repository=None, branch=None, working=None):
 
90
    """ Get known formats for control, working, branch and repository.
 
91
    
 
92
    :return: a dictionary containing the needed infos
 
93
    """
 
94
    ret = {}
 
95
    if control:
 
96
        ret['control'] = control._format.get_format_description()
 
97
    if working:
 
98
        ret['workingtree'] = working._format.get_format_description()
 
99
    if branch:
 
100
        ret['branch'] = branch._format.get_format_description()
 
101
    if repository:
 
102
        ret['repository'] = repository._format.get_format_description()
 
103
    
 
104
    return ret
 
105
 
 
106
 
 
107
def get_locking_info(repository, branch=None, working=None):
 
108
    """ Get locking status of working, branch and repository.
 
109
    
 
110
    :return: a dictionary containing the needed infos
 
111
    """
 
112
    ret = {}
 
113
    if (repository.get_physical_lock_status() or
 
114
        (branch and branch.get_physical_lock_status()) or
 
115
        (working and working.get_physical_lock_status())):
 
116
        if working:
 
117
            if working.get_physical_lock_status():
 
118
                status = 'locked'
 
119
            else:
 
120
                status = 'unlocked'
 
121
            ret['workingtree'] = status
 
122
        if branch:
 
123
            if branch.get_physical_lock_status():
 
124
                status = 'locked'
 
125
            else:
 
126
                status = 'unlocked'
 
127
            ret['branch'] = status
 
128
        if repository:
 
129
            if repository.get_physical_lock_status():
 
130
                status = 'locked'
 
131
            else:
 
132
                status = 'unlocked'
 
133
            ret['repository'] = status
 
134
    
 
135
    return ret
 
136
 
 
137
def get_missing_revisions_branch(branch):
 
138
    """ Get missing master revisions in branch.
 
139
    
 
140
    :return: a dictionary containing the needed infos
 
141
    """
 
142
    ret = {}
 
143
    # Try with inaccessible branch ?
 
144
    master = branch.get_master_branch()
 
145
    if master:
 
146
        local_extra, remote_extra = find_unmerged(branch, master)
 
147
        if remote_extra:
 
148
            ret['missing'] = len(remote_extra)
 
149
    
 
150
    return ret
 
151
 
 
152
 
 
153
def get_missing_revisions_working(working):
 
154
    """ Get missing revisions in working tree.
 
155
    
 
156
    :return: a dictionary containing the needed infos
 
157
    """
 
158
    ret = {}
 
159
    branch = working.branch
 
160
    basis = working.basis_tree()
 
161
    work_inv = working.inventory
 
162
    delta = diff.compare_trees(basis, working, want_unchanged=True)
 
163
    history = branch.revision_history()
 
164
    tree_last_id = working.last_revision()
 
165
 
 
166
    if len(history) and tree_last_id != history[-1]:
 
167
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
 
168
        missing_count = len(history) - tree_last_revno
 
169
        ret['missing'] = missing_count
 
170
    
 
171
    return ret
 
172
 
 
173
 
 
174
def get_working_stats(working):
 
175
    """ Get statistics about a working tree.
 
176
    
 
177
    :return: a dictionary containing the needed infos
 
178
    """
 
179
    ret = {}
 
180
    basis = working.basis_tree()
 
181
    work_inv = working.inventory
 
182
    delta = diff.compare_trees(basis, working, want_unchanged=True)
 
183
 
 
184
    ret['unchanged'] = len(delta.unchanged)
 
185
    ret['modified'] = len(delta.modified)
 
186
    ret['added'] = len(delta.added)
 
187
    ret['removed'] = len(delta.removed)
 
188
    ret['renamed'] = len(delta.renamed)
 
189
 
 
190
    ignore_cnt = unknown_cnt = 0
 
191
    for path in working.extras():
 
192
        if working.is_ignored(path):
 
193
            ignore_cnt += 1
 
194
        else:
 
195
            unknown_cnt += 1
 
196
    ret['unknown'] = unknown_cnt
 
197
    ret['ignored'] = ignore_cnt
 
198
 
 
199
    dir_cnt = 0
 
200
    for file_id in work_inv:
 
201
        if work_inv.get_file_kind(file_id) == 'directory':
 
202
            dir_cnt += 1
 
203
    ret['subdirs'] = dir_cnt
 
204
 
 
205
    return ret
 
206
 
 
207
def get_branch_stats(branch):
 
208
    """ Get statistics about a branch.
 
209
    
 
210
    :return: a dictionary containing the needed infos
 
211
    """
 
212
    ret = {}
 
213
    repository = branch.repository
 
214
    history = branch.revision_history()
 
215
 
 
216
    revno = len(history)
 
217
    ret['revno'] = revno 
 
218
    committers = {}
 
219
    for rev in history:
 
220
        committers[repository.get_revision(rev).committer] = True
 
221
    ret['commiters'] = len(committers)
 
222
    if revno > 0:
 
223
        firstrev = repository.get_revision(history[0])
 
224
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
 
225
        ret['age'] = age
 
226
        ret['firstrev'] = osutils.format_date(firstrev.timestamp,
 
227
                                              firstrev.timezone)
 
228
 
 
229
        lastrev = repository.get_revision(history[-1])
 
230
        ret['lastrev'] = osutils.format_date(lastrev.timestamp,
 
231
                                             lastrev.timezone)
 
232
 
 
233
    return ret
 
234
 
 
235
def get_repository_stats(repository):
 
236
    """ Get statistics about a repository.
 
237
    
 
238
    :return: a dictionary containing the needed infos
 
239
    """
 
240
    ret = {}
 
241
    if repository.bzrdir.root_transport.listable():
 
242
        c, t = repository._revision_store.total_size(repository.get_transaction())
 
243
        ret['revisions'] = c
 
244
        ret['size'] = t
 
245
 
 
246
    return ret
 
247
 
 
248
def diff_helper(tree, specific_files, external_diff_options, 
 
249
                    old_revision_spec=None, new_revision_spec=None,
 
250
                    old_label='a/', new_label='b/', output=None):
 
251
    """ Helper for diff.
 
252
 
 
253
    :param tree: a WorkingTree
 
254
 
 
255
    :param specific_files: the specific files to compare, or None
 
256
 
 
257
    :param external_diff_options: if non-None, run an external diff, and pass it these options
 
258
 
 
259
    :param old_revision_spec: if None, use basis tree as old revision, otherwise use the tree for the specified revision. 
 
260
 
 
261
    :param new_revision_spec:  if None, use working tree as new revision, otherwise use the tree for the specified revision.
 
262
    """
 
263
    import sys
 
264
    
 
265
    from bzrlib.diff import show_diff_trees
 
266
    
 
267
    if output == None:
 
268
        output = sys.stdout
 
269
    
 
270
    def spec_tree(spec):
 
271
        revision_id = spec.in_store(tree.branch).rev_id
 
272
        return tree.branch.repository.revision_tree(revision_id)
 
273
    
 
274
    if old_revision_spec is None:
 
275
        old_tree = tree.basis_tree()
 
276
    else:
 
277
        old_tree = spec_tree(old_revision_spec)
 
278
 
 
279
    if new_revision_spec is None:
 
280
        new_tree = tree
 
281
    else:
 
282
        new_tree = spec_tree(new_revision_spec)
 
283
 
 
284
    return show_diff_trees(old_tree, new_tree, output, specific_files,
 
285
                           external_diff_options,
 
286
                           old_label=old_label, new_label=new_label)