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
|
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
# Some parts of the code are:
# Copyright (C) 2005, 2006 by Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import time
import bzrlib
import bzrlib.osutils as osutils
from bzrlib.info import _repo_rel_url
from bzrlib.missing import find_unmerged
def get_location_info(repository, branch=None, working=None):
""" Get known locations for working, branch and repository.
:return: a dictionary containing the needed infos
"""
ret = {}
repository_path = repository.bzrdir.root_transport.base
if working and branch:
working_path = working.bzrdir.root_transport.base
branch_path = branch.bzrdir.root_transport.base
if working_path != branch_path:
# lightweight checkout
ret['lightcoroot'] = working_path
if repository.is_shared():
# lightweight checkout of branch in shared repository
ret['sharedrepo'] = repository_path
ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
else:
# lightweight checkout of standalone branch
ret['cobranch'] = branch_path
elif repository.is_shared():
# branch with tree inside shared repository
ret['sharedrepo'] = repository_path
ret['repoco'] = _repo_rel_url(repository_path, branch_path)
elif branch.get_bound_location():
# normal checkout
ret['coroot'] = working_path
ret['cobranch'] = branch.get_bound_location()
else:
# standalone
ret['branchroot'] = working_path
elif branch:
branch_path = branch.bzrdir.root_transport.base
if repository.is_shared():
# branch is part of shared repository
ret['sharedrepo'] = repository_path
ret['repobranch'] = _repo_rel_url(repository_path, branch_path)
else:
# standalone branch
ret['branchroot'] = branch_path
else:
# shared repository
assert repository.is_shared()
ret['sharedrepo'] = repository_path
return ret
def get_related_info(branch):
""" Get parent and push location of branch.
:return: a dictionary containing the needed infos
"""
ret = {}
if branch.get_parent() or branch.get_push_location():
if branch.get_parent():
ret['parentbranch'] = branch.get_parent()
if branch.get_push_location():
ret['publishbranch'] = branch.get_push_location()
return ret
def get_format_info(control=None, repository=None, branch=None, working=None):
""" Get known formats for control, working, branch and repository.
:return: a dictionary containing the needed infos
"""
ret = {}
if control:
ret['control'] = control._format.get_format_description()
if working:
ret['workingtree'] = working._format.get_format_description()
if branch:
ret['branch'] = branch._format.get_format_description()
if repository:
ret['repository'] = repository._format.get_format_description()
return ret
def get_locking_info(repository, branch=None, working=None):
""" Get locking status of working, branch and repository.
:return: a dictionary containing the needed infos
"""
ret = {}
if (repository.get_physical_lock_status() or
(branch and branch.get_physical_lock_status()) or
(working and working.get_physical_lock_status())):
if working:
if working.get_physical_lock_status():
status = 'locked'
else:
status = 'unlocked'
ret['workingtree'] = status
if branch:
if branch.get_physical_lock_status():
status = 'locked'
else:
status = 'unlocked'
ret['branch'] = status
if repository:
if repository.get_physical_lock_status():
status = 'locked'
else:
status = 'unlocked'
ret['repository'] = status
return ret
def get_missing_revisions_branch(branch):
""" Get missing master revisions in branch.
:return: a dictionary containing the needed infos
"""
ret = {}
# Try with inaccessible branch ?
master = branch.get_master_branch()
if master:
local_extra, remote_extra = find_unmerged(branch, master)
if remote_extra:
ret = len(remote_extra)
return ret
def get_missing_revisions_working(working):
""" Get missing revisions in working tree.
:return: a dictionary containing the needed infos
"""
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
# function deprecated after 0.9
from bzrlib.delta import compare_trees
ret = {}
branch = working.branch
basis = working.basis_tree()
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
delta = compare_trees(basis, working, want_unchanged=True)
else:
delta = working.changes_from(basis, want_unchanged=True)
history = branch.revision_history()
tree_last_id = working.last_revision()
if len(history) and tree_last_id != history[-1]:
tree_last_revno = branch.revision_id_to_revno(tree_last_id)
missing_count = len(history) - tree_last_revno
ret = missing_count
return ret
def get_working_stats(working):
""" Get statistics about a working tree.
:return: a dictionary containing the needed infos
"""
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
# function deprecated after 0.9
from bzrlib.delta import compare_trees
ret = {}
basis = working.basis_tree()
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
delta = compare_trees(basis, working, want_unchanged=True)
else:
delta = working.changes_from(basis, want_unchanged=True)
ret['unchanged'] = len(delta.unchanged)
ret['modified'] = len(delta.modified)
ret['added'] = len(delta.added)
ret['removed'] = len(delta.removed)
ret['renamed'] = len(delta.renamed)
ignore_cnt = unknown_cnt = 0
for path in working.extras():
if working.is_ignored(path):
ignore_cnt += 1
else:
unknown_cnt += 1
ret['unknown'] = unknown_cnt
ret['ignored'] = ignore_cnt
dir_cnt = 0
for path, ie in working.iter_entries_by_dir():
if ie.kind == 'directory':
dir_cnt += 1
ret['subdirs'] = dir_cnt
return ret
def get_branch_stats(branch):
""" Get statistics about a branch.
:return: a dictionary containing the needed infos
"""
ret = {}
repository = branch.repository
history = branch.revision_history()
revno = len(history)
ret['revno'] = revno
committers = {}
for rev in history:
committers[repository.get_revision(rev).committer] = True
ret['commiters'] = len(committers)
if revno > 0:
firstrev = repository.get_revision(history[0])
age = int((time.time() - firstrev.timestamp) / 3600 / 24)
ret['age'] = age
ret['firstrev'] = osutils.format_date(firstrev.timestamp,
firstrev.timezone)
lastrev = repository.get_revision(history[-1])
ret['lastrev'] = osutils.format_date(lastrev.timestamp,
lastrev.timezone)
return ret
def get_repository_stats(repository):
""" Get statistics about a repository.
:return: a dictionary containing the needed infos
"""
ret = {}
if repository.bzrdir.root_transport.listable():
c, t = repository._revision_store.total_size(repository.get_transaction())
ret['revisions'] = c
ret['size'] = t
return ret
def diff_helper(tree, specific_files, external_diff_options,
old_revision_spec=None, new_revision_spec=None,
old_label='a/', new_label='b/', output=None):
""" Helper for diff.
:param tree: a WorkingTree
:param specific_files: the specific files to compare, or None
:param external_diff_options: if non-None, run an external diff, and pass it these options
:param old_revision_spec: if None, use basis tree as old revision, otherwise use the tree for the specified revision.
:param new_revision_spec: if None, use working tree as new revision, otherwise use the tree for the specified revision.
"""
import sys
from bzrlib.diff import show_diff_trees
if output == None:
output = sys.stdout
def spec_tree(spec):
revision_id = spec.in_store(tree.branch).rev_id
return tree.branch.repository.revision_tree(revision_id)
if old_revision_spec is None:
old_tree = tree.basis_tree()
else:
old_tree = spec_tree(old_revision_spec)
if new_revision_spec is None:
new_tree = tree
else:
new_tree = spec_tree(new_revision_spec)
return show_diff_trees(old_tree, new_tree, output, specific_files,
external_diff_options,
old_label=old_label, new_label=new_label)
|