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