1
# Copyright (C) 2005-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
19
__all__ = ['show_bzrdir_info']
21
from io import StringIO
26
branch as _mod_branch,
36
from .errors import (NoWorkingTree, NotBranchError,
37
NoRepositoryPresent, NotLocalUrl)
38
from .missing import find_unmerged
41
def plural(n, base=u'', pl=None):
50
class LocationList(object):
52
def __init__(self, base_path):
54
self.base_path = base_path
56
def add_url(self, label, url):
57
"""Add a URL to the list, converting it to a path if possible"""
61
path = urlutils.local_path_from_url(url)
62
except urlutils.InvalidURL:
63
self.locs.append((label, url))
65
self.add_path(label, path)
67
def add_path(self, label, path):
68
"""Add a path, converting it to a relative path if possible"""
70
path = osutils.relpath(self.base_path, path)
71
except errors.PathNotChild:
77
path = path.rstrip('/')
78
self.locs.append((label, path))
81
max_len = max(len(l) for l, u in self.locs)
82
return [" %*s: %s\n" % (max_len, l, u) for l, u in self.locs]
85
def gather_location_info(repository=None, branch=None, working=None,
88
if branch is not None:
89
branch_path = branch.user_url
90
master_path = branch.get_bound_location()
91
if master_path is None:
92
master_path = branch_path
97
if control is not None and control.get_branch_reference():
98
locs['checkout of branch'] = control.get_branch_reference()
99
except NotBranchError:
102
working_path = working.user_url
103
if working_path != branch_path:
104
locs['light checkout root'] = working_path
105
if master_path != branch_path:
106
if repository.is_shared():
107
locs['repository checkout root'] = branch_path
109
locs['checkout root'] = branch_path
110
if working_path != master_path:
111
(master_path_base, params) = urlutils.split_segment_parameters(
113
if working_path == master_path_base:
114
locs['checkout of co-located branch'] = params['branch']
115
elif 'branch' in params:
116
locs['checkout of branch'] = "%s, branch %s" (
117
master_path_base, params['branch'])
119
locs['checkout of branch'] = master_path
120
elif repository.is_shared():
121
locs['repository branch'] = branch_path
122
elif branch_path is not None:
124
locs['branch root'] = branch_path
127
if repository is not None and repository.is_shared():
128
# lightweight checkout of branch in shared repository
129
if branch_path is not None:
130
locs['repository branch'] = branch_path
131
elif branch_path is not None:
133
locs['branch root'] = branch_path
134
elif repository is not None:
135
locs['repository'] = repository.user_url
136
elif control is not None:
137
locs['control directory'] = control.user_url
139
# Really, at least a control directory should be
140
# passed in for this method to be useful.
142
if master_path != branch_path:
143
locs['bound to branch'] = master_path
144
if repository is not None and repository.is_shared():
145
# lightweight checkout of branch in shared repository
146
locs['shared repository'] = repository.user_url
147
order = ['control directory', 'light checkout root',
148
'repository checkout root', 'checkout root',
149
'checkout of branch', 'checkout of co-located branch',
150
'shared repository', 'repository', 'repository branch',
151
'branch root', 'bound to branch']
152
return [(n, locs[n]) for n in order if n in locs]
155
def _show_location_info(locs, outfile):
156
"""Show known locations for working, branch and repository."""
157
outfile.write('Location:\n')
158
path_list = LocationList(osutils.getcwd())
159
for name, loc in locs:
160
path_list.add_url(name, loc)
161
outfile.writelines(path_list.get_lines())
164
def _gather_related_branches(branch):
165
locs = LocationList(osutils.getcwd())
166
locs.add_url('public branch', branch.get_public_branch())
167
locs.add_url('push branch', branch.get_push_location())
168
locs.add_url('parent branch', branch.get_parent())
169
locs.add_url('submit branch', branch.get_submit_branch())
171
locs.add_url('stacked on', branch.get_stacked_on_url())
172
except (_mod_branch.UnstackableBranchFormat, errors.UnstackableRepositoryFormat,
178
def _show_related_info(branch, outfile):
179
"""Show parent and push location of branch."""
180
locs = _gather_related_branches(branch)
181
if len(locs.locs) > 0:
183
outfile.write('Related branches:\n')
184
outfile.writelines(locs.get_lines())
187
def _show_control_dir_info(control, outfile):
188
"""Show control dir information."""
189
if control._format.colocated_branches:
191
outfile.write('Control directory:\n')
192
outfile.write(' %d branches\n' % len(control.list_branches()))
195
def _show_format_info(control=None, repository=None, branch=None,
196
working=None, outfile=None):
197
"""Show known formats for control, working, branch and repository."""
199
outfile.write('Format:\n')
201
outfile.write(' control: %s\n' %
202
control._format.get_format_description())
204
outfile.write(' working tree: %s\n' %
205
working._format.get_format_description())
207
outfile.write(' branch: %s\n' %
208
branch._format.get_format_description())
210
outfile.write(' repository: %s\n' %
211
repository._format.get_format_description())
214
def _show_locking_info(repository=None, branch=None, working=None,
216
"""Show locking status of working, branch and repository."""
217
if (repository and repository.get_physical_lock_status() or
218
(branch and branch.get_physical_lock_status()) or
219
(working and working.get_physical_lock_status())):
221
outfile.write('Lock status:\n')
223
if working.get_physical_lock_status():
227
outfile.write(' working tree: %s\n' % status)
229
if branch.get_physical_lock_status():
233
outfile.write(' branch: %s\n' % status)
235
if repository.get_physical_lock_status():
239
outfile.write(' repository: %s\n' % status)
242
def _show_missing_revisions_branch(branch, outfile):
243
"""Show missing master revisions in branch."""
244
# Try with inaccessible branch ?
245
master = branch.get_master_branch()
247
local_extra, remote_extra = find_unmerged(branch, master)
250
outfile.write(('Branch is out of date: missing %d '
251
'revision%s.\n') % (len(remote_extra),
252
plural(len(remote_extra))))
255
def _show_missing_revisions_working(working, outfile):
256
"""Show missing revisions in working tree."""
257
branch = working.branch
259
branch_revno, branch_last_revision = branch.last_revision_info()
260
except errors.UnsupportedOperation:
263
tree_last_id = working.get_parent_ids()[0]
267
if branch_revno and tree_last_id != branch_last_revision:
268
tree_last_revno = branch.revision_id_to_revno(tree_last_id)
269
missing_count = branch_revno - tree_last_revno
271
outfile.write(('Working tree is out of date: missing %d '
272
'revision%s.\n') % (missing_count, plural(missing_count)))
275
def _show_working_stats(working, outfile):
276
"""Show statistics about a working tree."""
277
basis = working.basis_tree()
278
delta = working.changes_from(basis, want_unchanged=True)
281
outfile.write('In the working tree:\n')
282
outfile.write(' %8s unchanged\n' % len(delta.unchanged))
283
outfile.write(' %8d modified\n' % len(delta.modified))
284
outfile.write(' %8d added\n' % len(delta.added))
285
outfile.write(' %8d removed\n' % len(delta.removed))
286
outfile.write(' %8d renamed\n' % len(delta.renamed))
288
ignore_cnt = unknown_cnt = 0
289
for path in working.extras():
290
if working.is_ignored(path):
294
outfile.write(' %8d unknown\n' % unknown_cnt)
295
outfile.write(' %8d ignored\n' % ignore_cnt)
298
for path, entry in working.iter_entries_by_dir():
299
if entry.kind == 'directory' and path != '':
301
outfile.write(' %8d versioned %s\n' % (dir_cnt,
302
plural(dir_cnt, 'subdirectory', 'subdirectories')))
305
def _show_branch_stats(branch, verbose, outfile):
306
"""Show statistics about a branch."""
308
revno, head = branch.last_revision_info()
309
except errors.UnsupportedOperation:
312
outfile.write('Branch history:\n')
313
outfile.write(' %8d revision%s\n' % (revno, plural(revno)))
314
stats = branch.repository.gather_stats(head, committers=verbose)
316
committers = stats['committers']
317
outfile.write(' %8d committer%s\n' % (committers,
320
timestamp, timezone = stats['firstrev']
321
age = int((time.time() - timestamp) / 3600 / 24)
322
outfile.write(' %8d day%s old\n' % (age, plural(age)))
323
outfile.write(' first revision: %s\n' %
324
osutils.format_date(timestamp, timezone))
325
timestamp, timezone = stats['latestrev']
326
outfile.write(' latest revision: %s\n' %
327
osutils.format_date(timestamp, timezone))
331
def _show_repository_info(repository, outfile):
332
"""Show settings of a repository."""
333
if repository.make_working_trees():
335
outfile.write('Create working tree for new branches inside '
339
def _show_repository_stats(repository, stats, outfile):
340
"""Show statistics about a repository."""
342
if 'revisions' in stats:
343
revisions = stats['revisions']
344
f.write(' %8d revision%s\n' % (revisions, plural(revisions)))
346
f.write(' %8d KiB\n' % (stats['size'] / 1024))
347
for hook in hooks['repository']:
348
hook(repository, stats, f)
349
if f.getvalue() != "":
351
outfile.write('Repository:\n')
352
outfile.write(f.getvalue())
355
def show_bzrdir_info(a_controldir, verbose=False, outfile=None):
356
"""Output to stdout the 'info' for a_controldir."""
360
tree = a_controldir.open_workingtree(
361
recommend_upgrade=False)
362
except (NoWorkingTree, NotLocalUrl, NotBranchError):
365
branch = a_controldir.open_branch(name="")
366
except NotBranchError:
369
repository = a_controldir.open_repository()
370
except NoRepositoryPresent:
374
lockable = repository
376
repository = branch.repository
380
repository = branch.repository
383
if lockable is not None:
386
show_component_info(a_controldir, repository, branch, tree, verbose,
389
if lockable is not None:
393
def show_component_info(control, repository, branch=None, working=None,
394
verbose=1, outfile=None):
395
"""Write info about all bzrdir components to stdout"""
402
layout = describe_layout(repository, branch, working, control)
403
format = describe_format(control, repository, branch, working)
404
outfile.write("%s (format: %s)\n" % (layout, format))
406
gather_location_info(control=control, repository=repository,
407
branch=branch, working=working),
409
if branch is not None:
410
_show_related_info(branch, outfile)
413
_show_format_info(control, repository, branch, working, outfile)
414
_show_locking_info(repository, branch, working, outfile)
415
_show_control_dir_info(control, outfile)
416
if branch is not None:
417
_show_missing_revisions_branch(branch, outfile)
418
if working is not None:
419
_show_missing_revisions_working(working, outfile)
420
_show_working_stats(working, outfile)
421
elif branch is not None:
422
_show_missing_revisions_branch(branch, outfile)
423
if branch is not None:
424
show_committers = verbose >= 2
425
stats = _show_branch_stats(branch, show_committers, outfile)
426
elif repository is not None:
427
stats = repository.gather_stats()
428
if branch is None and working is None and repository is not None:
429
_show_repository_info(repository, outfile)
430
if repository is not None:
431
_show_repository_stats(repository, stats, outfile)
434
def describe_layout(repository=None, branch=None, tree=None, control=None):
435
"""Convert a control directory layout into a user-understandable term
437
Common outputs include "Standalone tree", "Repository branch" and
438
"Checkout". Uncommon outputs include "Unshared repository with trees"
439
and "Empty control directory"
441
if branch is None and control is not None:
443
branch_reference = control.get_branch_reference()
444
except NotBranchError:
447
if branch_reference is not None:
448
return "Dangling branch reference"
449
if repository is None:
450
return 'Empty control directory'
451
if branch is None and tree is None:
452
if repository.is_shared():
453
phrase = 'Shared repository'
455
phrase = 'Unshared repository'
457
if repository.make_working_trees():
458
extra.append('trees')
459
if len(control.get_branches()) > 0:
460
extra.append('colocated branches')
462
phrase += ' with ' + " and ".join(extra)
465
if repository.is_shared():
466
independence = "Repository "
468
independence = "Standalone "
473
if branch is None and tree is not None:
474
phrase = "branchless tree"
476
if (tree is not None and tree.controldir.control_url !=
477
branch.controldir.control_url):
479
phrase = "Lightweight checkout"
480
elif branch.get_bound_location() is not None:
481
if independence == 'Standalone ':
484
phrase = "Bound branch"
487
if independence != "":
488
phrase = phrase.lower()
489
return "%s%s" % (independence, phrase)
492
def describe_format(control, repository, branch, tree):
493
"""Determine the format of an existing control directory
495
Several candidates may be found. If so, the names are returned as a
496
single string, separated by ' or '.
498
If no matching candidate is found, "unnamed" is returned.
501
if (branch is not None and tree is not None and
502
branch.user_url != tree.user_url):
505
non_aliases = set(controldir.format_registry.keys())
506
non_aliases.difference_update(controldir.format_registry.aliases())
507
for key in non_aliases:
508
format = controldir.format_registry.make_controldir(key)
509
if isinstance(format, bzrdir.BzrDirMetaFormat1):
510
if (tree and format.workingtree_format !=
513
if (branch and format.get_branch_format() !=
516
if (repository and format.repository_format !=
519
if format.__class__ is not control._format.__class__:
521
candidates.append(key)
522
if len(candidates) == 0:
525
new_candidates = [c for c in candidates if not
526
controldir.format_registry.get_info(c).hidden]
527
if len(new_candidates) > 0:
528
# If there are any non-hidden formats that match, only return those to
529
# avoid listing hidden formats except when only a hidden format will
531
candidates = new_candidates
532
return ' or '.join(candidates)
535
class InfoHooks(_mod_hooks.Hooks):
536
"""Hooks for the info command."""
539
super(InfoHooks, self).__init__("breezy.info", "hooks")
542
"Invoked when displaying the statistics for a repository. "
543
"repository is called with a statistics dictionary as returned "
544
"by the repository and a file-like object to write to.", (1, 15))