/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 Canonical Ltd
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2
#
77 by mbp at sourcefrog
- split info command out into separate file
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.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
7
#
77 by mbp at sourcefrog
- split info command out into separate file
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.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
12
#
77 by mbp at sourcefrog
- split info command out into separate file
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
77 by mbp at sourcefrog
- split info command out into separate file
16
6379.6.3 by Jelmer Vernooij
Use absolute_import.
17
from __future__ import absolute_import
18
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
19
__all__ = ['show_bzrdir_info']
20
77 by mbp at sourcefrog
- split info command out into separate file
21
import time
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
22
import sys
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from . import (
6734.1.11 by Jelmer Vernooij
Move UnstackableBranchFormat.
25
    branch as _mod_branch,
6207.3.3 by jelmer at samba
Fix tests and the like.
26
    controldir,
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
27
    errors,
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
28
    hooks as _mod_hooks,
1551.9.22 by Aaron Bentley
Use urlutils for info. Fixes bug #76229
29
    osutils,
30
    urlutils,
31
    )
6670.4.1 by Jelmer Vernooij
Update imports.
32
from .bzr import (
33
    bzrdir,
34
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
35
from .errors import (NoWorkingTree, NotBranchError,
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
36
                           NoRepositoryPresent, NotLocalUrl)
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
37
from .missing import find_unmerged
38
from .sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
39
    BytesIO,
40
    )
77 by mbp at sourcefrog
- split info command out into separate file
41
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
42
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
43
def plural(n, base='', pl=None):
44
    if n == 1:
45
        return base
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
46
    elif pl is not None:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
47
        return pl
48
    else:
49
        return 's'
50
51
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
52
class LocationList(object):
53
54
    def __init__(self, base_path):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
55
        self.locs = []
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
56
        self.base_path = base_path
57
58
    def add_url(self, label, url):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
59
        """Add a URL to the list, converting it to a path if possible"""
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
60
        if url is None:
61
            return
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
62
        try:
63
            path = urlutils.local_path_from_url(url)
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
64
        except urlutils.InvalidURL:
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
65
            self.locs.append((label, url))
66
        else:
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
67
            self.add_path(label, path)
2363.5.18 by Aaron Bentley
Get all tests passing
68
69
    def add_path(self, label, path):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
70
        """Add a path, converting it to a relative path if possible"""
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
71
        try:
72
            path = osutils.relpath(self.base_path, path)
73
        except errors.PathNotChild:
74
            pass
75
        else:
76
            if path == '':
77
                path = '.'
78
        if path != '/':
79
            path = path.rstrip('/')
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
80
        self.locs.append((label, path))
2363.5.18 by Aaron Bentley
Get all tests passing
81
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
82
    def get_lines(self):
83
        max_len = max(len(l) for l, u in self.locs)
84
        return ["  %*s: %s\n" % (max_len, l, u) for l, u in self.locs ]
2363.5.18 by Aaron Bentley
Get all tests passing
85
86
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
87
def gather_location_info(repository=None, branch=None, working=None,
88
        control=None):
2363.5.18 by Aaron Bentley
Get all tests passing
89
    locs = {}
90
    if branch is not None:
5158.6.6 by Martin Pool
Change info code to use user_url etc
91
        branch_path = branch.user_url
2363.5.18 by Aaron Bentley
Get all tests passing
92
        master_path = branch.get_bound_location()
93
        if master_path is None:
94
            master_path = branch_path
95
    else:
96
        branch_path = None
97
        master_path = None
6241.4.3 by Jelmer Vernooij
Add test for dangling tree references.
98
        try:
99
            if control is not None and control.get_branch_reference():
100
                locs['checkout of branch'] = control.get_branch_reference()
101
        except NotBranchError:
102
            pass
2363.5.18 by Aaron Bentley
Get all tests passing
103
    if working:
5158.6.6 by Martin Pool
Change info code to use user_url etc
104
        working_path = working.user_url
1694.2.6 by Martin Pool
[merge] bzr.dev
105
        if working_path != branch_path:
2363.5.18 by Aaron Bentley
Get all tests passing
106
            locs['light checkout root'] = working_path
107
        if master_path != branch_path:
1694.2.6 by Martin Pool
[merge] bzr.dev
108
            if repository.is_shared():
2363.5.18 by Aaron Bentley
Get all tests passing
109
                locs['repository checkout root'] = branch_path
1694.2.6 by Martin Pool
[merge] bzr.dev
110
            else:
2363.5.18 by Aaron Bentley
Get all tests passing
111
                locs['checkout root'] = branch_path
112
        if working_path != master_path:
113
            locs['checkout of branch'] = master_path
1694.2.6 by Martin Pool
[merge] bzr.dev
114
        elif repository.is_shared():
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
115
            locs['repository branch'] = branch_path
2363.5.18 by Aaron Bentley
Get all tests passing
116
        elif branch_path is not None:
1694.2.6 by Martin Pool
[merge] bzr.dev
117
            # standalone
2363.5.18 by Aaron Bentley
Get all tests passing
118
            locs['branch root'] = branch_path
119
    else:
120
        working_path = None
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
121
        if repository is not None and repository.is_shared():
2363.5.18 by Aaron Bentley
Get all tests passing
122
            # lightweight checkout of branch in shared repository
123
            if branch_path is not None:
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
124
                locs['repository branch'] = branch_path
2363.5.18 by Aaron Bentley
Get all tests passing
125
        elif branch_path is not None:
126
            # standalone
127
            locs['branch root'] = branch_path
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
128
        elif repository is not None:
129
            locs['repository'] = repository.user_url
130
        elif control is not None:
131
            locs['control directory'] = control.user_url
1624.3.48 by Olaf Conradi
Add info on standalone branches without a working tree.
132
        else:
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
133
            # Really, at least a control directory should be
134
            # passed in for this method to be useful.
135
            pass
6241.2.1 by Jelmer Vernooij
bzr info now shows the bound location too for local branches without tree.
136
        if master_path != branch_path:
137
            locs['bound to branch'] = master_path
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
138
    if repository is not None and repository.is_shared():
2363.5.18 by Aaron Bentley
Get all tests passing
139
        # lightweight checkout of branch in shared repository
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
140
        locs['shared repository'] = repository.user_url
141
    order = ['control directory', 'light checkout root',
142
             'repository checkout root', 'checkout root',
143
             'checkout of branch', 'shared repository',
2363.5.23 by Aaron Bentley
Output 2-tuples from gather_locations
144
             'repository', 'repository branch', 'branch root',
145
             'bound to branch']
146
    return [(n, locs[n]) for n in order if n in locs]
2363.5.18 by Aaron Bentley
Get all tests passing
147
148
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
149
def _show_location_info(locs, outfile):
2363.5.18 by Aaron Bentley
Get all tests passing
150
    """Show known locations for working, branch and repository."""
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
151
    outfile.write('Location:\n')
2804.4.3 by Alexander Belchenko
fix for test_info-tests: using osutils.getcwd instead of os.getcwd (sigh)
152
    path_list = LocationList(osutils.getcwd())
2363.5.23 by Aaron Bentley
Output 2-tuples from gather_locations
153
    for name, loc in locs:
154
        path_list.add_url(name, loc)
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
155
    outfile.writelines(path_list.get_lines())
156
1694.2.6 by Martin Pool
[merge] bzr.dev
157
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
158
def _gather_related_branches(branch):
2804.4.3 by Alexander Belchenko
fix for test_info-tests: using osutils.getcwd instead of os.getcwd (sigh)
159
    locs = LocationList(osutils.getcwd())
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
160
    locs.add_url('public branch', branch.get_public_branch())
161
    locs.add_url('push branch', branch.get_push_location())
162
    locs.add_url('parent branch', branch.get_parent())
163
    locs.add_url('submit branch', branch.get_submit_branch())
3221.11.21 by Robert Collins
Have info report on stacked branches.
164
    try:
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
165
        locs.add_url('stacked on', branch.get_stacked_on_url())
6734.1.11 by Jelmer Vernooij
Move UnstackableBranchFormat.
166
    except (_mod_branch.UnstackableBranchFormat, errors.UnstackableRepositoryFormat,
3221.11.21 by Robert Collins
Have info report on stacked branches.
167
        errors.NotStacked):
168
        pass
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
169
    return locs
1694.2.6 by Martin Pool
[merge] bzr.dev
170
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
171
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
172
def _show_related_info(branch, outfile):
1694.2.6 by Martin Pool
[merge] bzr.dev
173
    """Show parent and push location of branch."""
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
174
    locs = _gather_related_branches(branch)
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
175
    if len(locs.locs) > 0:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
176
        outfile.write('\n')
177
        outfile.write('Related branches:\n')
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
178
        outfile.writelines(locs.get_lines())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
179
180
6240.1.1 by Jelmer Vernooij
Show the number of colocated branches in 'bzr info -v'.
181
def _show_control_dir_info(control, outfile):
182
    """Show control dir information."""
183
    if control._format.colocated_branches:
184
        outfile.write('\n')
185
        outfile.write('Control directory:\n')
186
        outfile.write('         %d branches\n' % len(control.list_branches()))
187
188
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
189
def _show_format_info(control=None, repository=None, branch=None,
190
                      working=None, outfile=None):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
191
    """Show known formats for control, working, branch and repository."""
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
192
    outfile.write('\n')
193
    outfile.write('Format:\n')
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
194
    if control:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
195
        outfile.write('       control: %s\n' %
196
            control._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
197
    if working:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
198
        outfile.write('  working tree: %s\n' %
199
            working._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
200
    if branch:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
201
        outfile.write('        branch: %s\n' %
202
            branch._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
203
    if repository:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
204
        outfile.write('    repository: %s\n' %
205
            repository._format.get_format_description())
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
206
207
6437.33.2 by Jelmer Vernooij
Cope with repository being missing in 'bzr info'.
208
def _show_locking_info(repository=None, branch=None, working=None,
209
        outfile=None):
1694.2.6 by Martin Pool
[merge] bzr.dev
210
    """Show locking status of working, branch and repository."""
6437.33.2 by Jelmer Vernooij
Cope with repository being missing in 'bzr info'.
211
    if (repository and repository.get_physical_lock_status() or
1694.2.6 by Martin Pool
[merge] bzr.dev
212
        (branch and branch.get_physical_lock_status()) or
213
        (working and working.get_physical_lock_status())):
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
214
        outfile.write('\n')
215
        outfile.write('Lock status:\n')
1694.2.6 by Martin Pool
[merge] bzr.dev
216
        if working:
217
            if working.get_physical_lock_status():
218
                status = 'locked'
219
            else:
220
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
221
            outfile.write('  working tree: %s\n' % status)
1694.2.6 by Martin Pool
[merge] bzr.dev
222
        if branch:
223
            if branch.get_physical_lock_status():
224
                status = 'locked'
225
            else:
226
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
227
            outfile.write('        branch: %s\n' % status)
1694.2.6 by Martin Pool
[merge] bzr.dev
228
        if repository:
229
            if repository.get_physical_lock_status():
230
                status = 'locked'
231
            else:
232
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
233
            outfile.write('    repository: %s\n' % status)
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
234
235
236
def _show_missing_revisions_branch(branch, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
237
    """Show missing master revisions in branch."""
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
238
    # Try with inaccessible branch ?
1624.3.2 by Olaf Conradi
Implemented table of constructs from BzrInfo specification.
239
    master = branch.get_master_branch()
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
240
    if master:
1624.3.2 by Olaf Conradi
Implemented table of constructs from BzrInfo specification.
241
        local_extra, remote_extra = find_unmerged(branch, master)
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
242
        if remote_extra:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
243
            outfile.write('\n')
244
            outfile.write(('Branch is out of date: missing %d '
245
                'revision%s.\n') % (len(remote_extra),
246
                plural(len(remote_extra))))
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
247
248
249
def _show_missing_revisions_working(working, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
250
    """Show missing revisions in working tree."""
251
    branch = working.branch
252
    basis = working.basis_tree()
6181.1.1 by Jelmer Vernooij
If the branch doesn't support last_revision_info, don't display
253
    try:
254
        branch_revno, branch_last_revision = branch.last_revision_info()
255
    except errors.UnsupportedOperation:
256
        return
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
257
    try:
258
        tree_last_id = working.get_parent_ids()[0]
259
    except IndexError:
260
        tree_last_id = None
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
261
2249.4.2 by Wouter van Heyst
Convert callers of Branch.revision_history() to Branch.last_revision_info() where sensible.
262
    if branch_revno and tree_last_id != branch_last_revision:
1624.3.11 by Olaf Conradi
Test cases exposed a bug in missing revisions count of working tree. It
263
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
2249.4.2 by Wouter van Heyst
Convert callers of Branch.revision_history() to Branch.last_revision_info() where sensible.
264
        missing_count = branch_revno - tree_last_revno
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
265
        outfile.write('\n')
266
        outfile.write(('Working tree is out of date: missing %d '
267
            'revision%s.\n') % (missing_count, plural(missing_count)))
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
268
269
270
def _show_working_stats(working, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
271
    """Show statistics about a working tree."""
272
    basis = working.basis_tree()
1852.10.3 by Robert Collins
Remove all uses of compare_trees and replace with Tree.changes_from throughout bzrlib.
273
    delta = working.changes_from(basis, want_unchanged=True)
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
274
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
275
    outfile.write('\n')
276
    outfile.write('In the working tree:\n')
277
    outfile.write('  %8s unchanged\n' % len(delta.unchanged))
278
    outfile.write('  %8d modified\n' % len(delta.modified))
279
    outfile.write('  %8d added\n' % len(delta.added))
280
    outfile.write('  %8d removed\n' % len(delta.removed))
281
    outfile.write('  %8d renamed\n' % len(delta.renamed))
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
282
283
    ignore_cnt = unknown_cnt = 0
284
    for path in working.extras():
285
        if working.is_ignored(path):
286
            ignore_cnt += 1
287
        else:
288
            unknown_cnt += 1
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
289
    outfile.write('  %8d unknown\n' % unknown_cnt)
290
    outfile.write('  %8d ignored\n' % ignore_cnt)
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
291
292
    dir_cnt = 0
5777.5.4 by Jelmer Vernooij
Avoid the use of inventory in 'bzr info'.
293
    root_id = working.get_root_id()
5777.5.5 by Jelmer Vernooij
Use working.iter_entries_by_dir.
294
    for path, entry in working.iter_entries_by_dir():
295
        if entry.kind == 'directory' and entry.file_id != root_id:
1731.1.39 by Aaron Bentley
Reject removing is_root
296
            dir_cnt += 1
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
297
    outfile.write('  %8d versioned %s\n' % (dir_cnt,
298
        plural(dir_cnt, 'subdirectory', 'subdirectories')))
77 by mbp at sourcefrog
- split info command out into separate file
299
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
300
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
301
def _show_branch_stats(branch, verbose, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
302
    """Show statistics about a branch."""
6181.1.1 by Jelmer Vernooij
If the branch doesn't support last_revision_info, don't display
303
    try:
304
        revno, head = branch.last_revision_info()
305
    except errors.UnsupportedOperation:
306
        return {}
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
307
    outfile.write('\n')
308
    outfile.write('Branch history:\n')
309
    outfile.write('  %8d revision%s\n' % (revno, plural(revno)))
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
310
    stats = branch.repository.gather_stats(head, committers=verbose)
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
311
    if verbose:
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
312
        committers = stats['committers']
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
313
        outfile.write('  %8d committer%s\n' % (committers,
314
            plural(committers)))
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
315
    if revno:
316
        timestamp, timezone = stats['firstrev']
317
        age = int((time.time() - timestamp) / 3600 / 24)
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
318
        outfile.write('  %8d day%s old\n' % (age, plural(age)))
319
        outfile.write('   first revision: %s\n' %
320
            osutils.format_date(timestamp, timezone))
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
321
        timestamp, timezone = stats['latestrev']
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
322
        outfile.write('  latest revision: %s\n' %
323
            osutils.format_date(timestamp, timezone))
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
324
    return stats
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
325
326
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
327
def _show_repository_info(repository, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
328
    """Show settings of a repository."""
329
    if repository.make_working_trees():
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
330
        outfile.write('\n')
331
        outfile.write('Create working tree for new branches inside '
332
            'the repository.\n')
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
333
334
4307.3.3 by Jelmer Vernooij
Add repository argument to 'repository' info hook, per Roberts review.
335
def _show_repository_stats(repository, stats, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
336
    """Show statistics about a repository."""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
337
    f = BytesIO()
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
338
    if 'revisions' in stats:
339
        revisions = stats['revisions']
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
340
        f.write('  %8d revision%s\n' % (revisions, plural(revisions)))
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
341
    if 'size' in stats:
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
342
        f.write('  %8d KiB\n' % (stats['size']/1024))
343
    for hook in hooks['repository']:
4307.3.3 by Jelmer Vernooij
Add repository argument to 'repository' info hook, per Roberts review.
344
        hook(repository, stats, f)
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
345
    if f.getvalue() != "":
346
        outfile.write('\n')
347
        outfile.write('Repository:\n')
348
        outfile.write(f.getvalue())
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
349
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
350
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
351
def show_bzrdir_info(a_controldir, verbose=False, outfile=None):
352
    """Output to stdout the 'info' for a_controldir."""
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
353
    if outfile is None:
354
        outfile = sys.stdout
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
355
    try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
356
        tree = a_controldir.open_workingtree(
2363.5.9 by Aaron Bentley
Merge from bzr.dev
357
            recommend_upgrade=False)
6241.4.3 by Jelmer Vernooij
Add test for dangling tree references.
358
    except (NoWorkingTree, NotLocalUrl, NotBranchError):
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
359
        tree = None
360
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
361
            branch = a_controldir.open_branch(name="")
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
362
        except NotBranchError:
363
            branch = None
364
            try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
365
                repository = a_controldir.open_repository()
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
366
            except NoRepositoryPresent:
6241.4.2 by Jelmer Vernooij
No longer show empty output when only control directory is present.
367
                lockable = None
368
                repository = None
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
369
            else:
370
                lockable = repository
371
        else:
372
            repository = branch.repository
373
            lockable = branch
374
    else:
375
        branch = tree.branch
376
        repository = branch.repository
377
        lockable = tree
378
6241.4.2 by Jelmer Vernooij
No longer show empty output when only control directory is present.
379
    if lockable is not None:
380
        lockable.lock_read()
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
381
    try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
382
        show_component_info(a_controldir, repository, branch, tree, verbose,
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
383
                            outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
384
    finally:
6241.4.2 by Jelmer Vernooij
No longer show empty output when only control directory is present.
385
        if lockable is not None:
386
            lockable.unlock()
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
387
388
389
def show_component_info(control, repository, branch=None, working=None,
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
390
    verbose=1, outfile=None):
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
391
    """Write info about all bzrdir components to stdout"""
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
392
    if outfile is None:
393
        outfile = sys.stdout
2363.5.7 by Aaron Bentley
Make verbose mean what I want
394
    if verbose is False:
395
        verbose = 1
396
    if verbose is True:
397
        verbose = 2
6241.4.3 by Jelmer Vernooij
Add test for dangling tree references.
398
    layout = describe_layout(repository, branch, working, control)
2363.5.6 by Aaron Bentley
Add short format description
399
    format = describe_format(control, repository, branch, working)
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
400
    outfile.write("%s (format: %s)\n" % (layout, format))
6241.4.1 by Jelmer Vernooij
Make gather_location_info understand control directories.
401
    _show_location_info(
402
        gather_location_info(control=control, repository=repository,
403
            branch=branch, working=working),
404
        outfile)
2584.2.1 by Adeodato Simó
Make `bzr info` show related branches in non-verbose mode.
405
    if branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
406
        _show_related_info(branch, outfile)
2363.5.7 by Aaron Bentley
Make verbose mean what I want
407
    if verbose == 0:
408
        return
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
409
    _show_format_info(control, repository, branch, working, outfile)
410
    _show_locking_info(repository, branch, working, outfile)
6240.1.1 by Jelmer Vernooij
Show the number of colocated branches in 'bzr info -v'.
411
    _show_control_dir_info(control, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
412
    if branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
413
        _show_missing_revisions_branch(branch, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
414
    if working is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
415
        _show_missing_revisions_working(working, outfile)
416
        _show_working_stats(working, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
417
    elif branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
418
        _show_missing_revisions_branch(branch, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
419
    if branch is not None:
4032.2.1 by Ian Clatworthy
omit branch committers from info -v (now requires -vv)
420
        show_committers = verbose >= 2
421
        stats = _show_branch_stats(branch, show_committers, outfile)
6437.33.3 by Jelmer Vernooij
Cope with repository being missing in more cases.
422
    elif repository is not None:
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
423
        stats = repository.gather_stats()
6437.33.3 by Jelmer Vernooij
Cope with repository being missing in more cases.
424
    if branch is None and working is None and repository is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
425
        _show_repository_info(repository, outfile)
6437.33.3 by Jelmer Vernooij
Cope with repository being missing in more cases.
426
    if repository is not None:
427
        _show_repository_stats(repository, stats, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
428
429
6241.4.3 by Jelmer Vernooij
Add test for dangling tree references.
430
def describe_layout(repository=None, branch=None, tree=None, control=None):
2363.5.2 by Aaron Bentley
Implement layout description
431
    """Convert a control directory layout into a user-understandable term
432
433
    Common outputs include "Standalone tree", "Repository branch" and
434
    "Checkout".  Uncommon outputs include "Unshared repository with trees"
435
    and "Empty control directory"
436
    """
6241.4.3 by Jelmer Vernooij
Add test for dangling tree references.
437
    if branch is None and control is not None:
438
        try:
439
            branch_reference = control.get_branch_reference()
440
        except NotBranchError:
441
            pass
442
        else:
443
            if branch_reference is not None:
444
                return "Dangling branch reference"
2363.5.2 by Aaron Bentley
Implement layout description
445
    if repository is None:
446
        return 'Empty control directory'
447
    if branch is None and tree is None:
448
        if repository.is_shared():
449
            phrase = 'Shared repository'
450
        else:
451
            phrase = 'Unshared repository'
6437.9.1 by Jelmer Vernooij
Report present but unused colocated branches in `bzr info`.
452
        extra = []
2363.5.2 by Aaron Bentley
Implement layout description
453
        if repository.make_working_trees():
6437.9.1 by Jelmer Vernooij
Report present but unused colocated branches in `bzr info`.
454
            extra.append('trees')
455
        if len(control.get_branches()) > 0:
456
            extra.append('colocated branches')
457
        if extra:
458
            phrase += ' with ' + " and ".join(extra)
2363.5.2 by Aaron Bentley
Implement layout description
459
        return phrase
460
    else:
461
        if repository.is_shared():
462
            independence = "Repository "
463
        else:
464
            independence = "Standalone "
465
        if tree is not None:
466
            phrase = "tree"
467
        else:
468
            phrase = "branch"
469
        if branch is None and tree is not None:
470
            phrase = "branchless tree"
471
        else:
5158.6.6 by Martin Pool
Change info code to use user_url etc
472
            if (tree is not None and tree.user_url !=
473
                branch.user_url):
2363.5.4 by Aaron Bentley
Eliminate the concept of a 'repository lightweight checkout'
474
                independence = ''
2363.5.2 by Aaron Bentley
Implement layout description
475
                phrase = "Lightweight checkout"
476
            elif branch.get_bound_location() is not None:
477
                if independence == 'Standalone ':
478
                    independence = ''
479
                if tree is None:
480
                    phrase = "Bound branch"
481
                else:
482
                    phrase = "Checkout"
483
        if independence != "":
484
            phrase = phrase.lower()
485
        return "%s%s" % (independence, phrase)
486
487
2363.5.5 by Aaron Bentley
add info.describe_format
488
def describe_format(control, repository, branch, tree):
489
    """Determine the format of an existing control directory
490
491
    Several candidates may be found.  If so, the names are returned as a
2363.5.17 by Aaron Bentley
Change separator from '/' to 'or'
492
    single string, separated by ' or '.
2363.5.5 by Aaron Bentley
add info.describe_format
493
494
    If no matching candidate is found, "unnamed" is returned.
495
    """
496
    candidates  = []
2363.5.6 by Aaron Bentley
Add short format description
497
    if (branch is not None and tree is not None and
5158.6.6 by Martin Pool
Change info code to use user_url etc
498
        branch.user_url != tree.user_url):
2363.5.6 by Aaron Bentley
Add short format description
499
        branch = None
500
        repository = None
6207.3.3 by jelmer at samba
Fix tests and the like.
501
    non_aliases = set(controldir.format_registry.keys())
502
    non_aliases.difference_update(controldir.format_registry.aliases())
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
503
    for key in non_aliases:
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
504
        format = controldir.format_registry.make_controldir(key)
2363.5.5 by Aaron Bentley
add info.describe_format
505
        if isinstance(format, bzrdir.BzrDirMetaFormat1):
506
            if (tree and format.workingtree_format !=
507
                tree._format):
508
                continue
509
            if (branch and format.get_branch_format() !=
510
                branch._format):
511
                continue
512
            if (repository and format.repository_format !=
513
                repository._format):
514
                continue
515
        if format.__class__ is not control._format.__class__:
516
            continue
517
        candidates.append(key)
518
    if len(candidates) == 0:
519
        return 'unnamed'
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
520
    candidates.sort()
2363.5.6 by Aaron Bentley
Add short format description
521
    new_candidates = [c for c in candidates if not
6207.3.3 by jelmer at samba
Fix tests and the like.
522
        controldir.format_registry.get_info(c).hidden]
2363.5.6 by Aaron Bentley
Add short format description
523
    if len(new_candidates) > 0:
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
524
        # If there are any non-hidden formats that match, only return those to
525
        # avoid listing hidden formats except when only a hidden format will
526
        # do.
2363.5.6 by Aaron Bentley
Add short format description
527
        candidates = new_candidates
2363.5.17 by Aaron Bentley
Change separator from '/' to 'or'
528
    return ' or '.join(candidates)
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
529
530
531
class InfoHooks(_mod_hooks.Hooks):
532
    """Hooks for the info command."""
533
5622.3.10 by Jelmer Vernooij
Don't require arguments to hooks.
534
    def __init__(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
535
        super(InfoHooks, self).__init__("breezy.info", "hooks")
5622.3.2 by Jelmer Vernooij
Add more lazily usable hook points.
536
        self.add_hook('repository',
4307.3.1 by Jelmer Vernooij
Allow registering hooks that extend the Repository section in 'bzr info -v'.
537
            "Invoked when displaying the statistics for a repository. "
538
            "repository is called with a statistics dictionary as returned "
5622.3.2 by Jelmer Vernooij
Add more lazily usable hook points.
539
            "by the repository and a file-like object to write to.", (1, 15))
540
541
5622.3.10 by Jelmer Vernooij
Don't require arguments to hooks.
542
hooks = InfoHooks()