/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2395.1.1 by Martin Pool
rename 'revision store' to 'repository' in bzr info
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1685.1.60 by Martin Pool
[broken] NotBranchError should unescape the url if possible
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
17
__all__ = ['show_bzrdir_info']
18
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
19
import os
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
1551.9.22 by Aaron Bentley
Use urlutils for info. Fixes bug #76229
23
from bzrlib import (
2363.5.5 by Aaron Bentley
add info.describe_format
24
    bzrdir,
1551.9.22 by Aaron Bentley
Use urlutils for info. Fixes bug #76229
25
    diff,
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
26
    errors,
1551.9.22 by Aaron Bentley
Use urlutils for info. Fixes bug #76229
27
    osutils,
28
    urlutils,
29
    )
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
30
from bzrlib.errors import (NoWorkingTree, NotBranchError,
31
                           NoRepositoryPresent, NotLocalUrl)
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
32
from bzrlib.missing import find_unmerged
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
33
from bzrlib.symbol_versioning import (deprecated_function,
2696.1.1 by Martin Pool
Remove things deprecated in 0.11 and earlier
34
        zero_eighteen)
77 by mbp at sourcefrog
- split info command out into separate file
35
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
36
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
37
def plural(n, base='', pl=None):
38
    if n == 1:
39
        return base
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
40
    elif pl is not None:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
41
        return pl
42
    else:
43
        return 's'
44
45
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
46
class LocationList(object):
47
48
    def __init__(self, base_path):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
49
        self.locs = []
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
50
        self.base_path = base_path
51
52
    def add_url(self, label, url):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
53
        """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
54
        if url is None:
55
            return
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
56
        try:
57
            path = urlutils.local_path_from_url(url)
58
        except errors.InvalidURL:
59
            self.locs.append((label, url))
60
        else:
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
61
            self.add_path(label, path)
2363.5.18 by Aaron Bentley
Get all tests passing
62
63
    def add_path(self, label, path):
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
64
        """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
65
        try:
66
            path = osutils.relpath(self.base_path, path)
67
        except errors.PathNotChild:
68
            pass
69
        else:
70
            if path == '':
71
                path = '.'
72
        if path != '/':
73
            path = path.rstrip('/')
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
74
        self.locs.append((label, path))
2363.5.18 by Aaron Bentley
Get all tests passing
75
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
76
    def get_lines(self):
77
        max_len = max(len(l) for l, u in self.locs)
78
        return ["  %*s: %s\n" % (max_len, l, u) for l, u in self.locs ]
2363.5.18 by Aaron Bentley
Get all tests passing
79
80
81
def gather_location_info(repository, branch=None, working=None):
82
    locs = {}
1694.2.6 by Martin Pool
[merge] bzr.dev
83
    repository_path = repository.bzrdir.root_transport.base
2363.5.18 by Aaron Bentley
Get all tests passing
84
    if branch is not None:
85
        branch_path = branch.bzrdir.root_transport.base
86
        master_path = branch.get_bound_location()
87
        if master_path is None:
88
            master_path = branch_path
89
    else:
90
        branch_path = None
91
        master_path = None
92
    if working:
1694.2.6 by Martin Pool
[merge] bzr.dev
93
        working_path = working.bzrdir.root_transport.base
94
        if working_path != branch_path:
2363.5.18 by Aaron Bentley
Get all tests passing
95
            locs['light checkout root'] = working_path
96
        if master_path != branch_path:
1694.2.6 by Martin Pool
[merge] bzr.dev
97
            if repository.is_shared():
2363.5.18 by Aaron Bentley
Get all tests passing
98
                locs['repository checkout root'] = branch_path
1694.2.6 by Martin Pool
[merge] bzr.dev
99
            else:
2363.5.18 by Aaron Bentley
Get all tests passing
100
                locs['checkout root'] = branch_path
101
        if working_path != master_path:
102
            locs['checkout of branch'] = master_path
1694.2.6 by Martin Pool
[merge] bzr.dev
103
        elif repository.is_shared():
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
104
            locs['repository branch'] = branch_path
2363.5.18 by Aaron Bentley
Get all tests passing
105
        elif branch_path is not None:
1694.2.6 by Martin Pool
[merge] bzr.dev
106
            # standalone
2363.5.18 by Aaron Bentley
Get all tests passing
107
            locs['branch root'] = branch_path
108
    else:
109
        working_path = None
1624.3.48 by Olaf Conradi
Add info on standalone branches without a working tree.
110
        if repository.is_shared():
2363.5.18 by Aaron Bentley
Get all tests passing
111
            # lightweight checkout of branch in shared repository
112
            if branch_path is not None:
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
113
                locs['repository branch'] = branch_path
2363.5.18 by Aaron Bentley
Get all tests passing
114
        elif branch_path is not None:
115
            # standalone
116
            locs['branch root'] = branch_path
2363.5.19 by Aaron Bentley
Add support for bound branches
117
            if master_path != branch_path:
118
                locs['bound to branch'] = master_path
1624.3.48 by Olaf Conradi
Add info on standalone branches without a working tree.
119
        else:
2363.5.18 by Aaron Bentley
Get all tests passing
120
            locs['repository'] = repository_path
121
    if repository.is_shared():
122
        # lightweight checkout of branch in shared repository
123
        locs['shared repository'] = repository_path
2363.5.23 by Aaron Bentley
Output 2-tuples from gather_locations
124
    order = ['light checkout root', 'repository checkout root',
125
             'checkout root', 'checkout of branch', 'shared repository',
126
             'repository', 'repository branch', 'branch root',
127
             'bound to branch']
128
    return [(n, locs[n]) for n in order if n in locs]
2363.5.18 by Aaron Bentley
Get all tests passing
129
130
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
131
def _show_location_info(locs, outfile):
2363.5.18 by Aaron Bentley
Get all tests passing
132
    """Show known locations for working, branch and repository."""
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
133
    outfile.write('Location:\n')
2804.4.3 by Alexander Belchenko
fix for test_info-tests: using osutils.getcwd instead of os.getcwd (sigh)
134
    path_list = LocationList(osutils.getcwd())
2363.5.23 by Aaron Bentley
Output 2-tuples from gather_locations
135
    for name, loc in locs:
136
        path_list.add_url(name, loc)
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
137
    outfile.writelines(path_list.get_lines())
138
1694.2.6 by Martin Pool
[merge] bzr.dev
139
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
140
def _gather_related_branches(branch):
2804.4.3 by Alexander Belchenko
fix for test_info-tests: using osutils.getcwd instead of os.getcwd (sigh)
141
    locs = LocationList(osutils.getcwd())
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
142
    locs.add_url('public branch', branch.get_public_branch())
143
    locs.add_url('push branch', branch.get_push_location())
144
    locs.add_url('parent branch', branch.get_parent())
145
    locs.add_url('submit branch', branch.get_submit_branch())
3221.11.21 by Robert Collins
Have info report on stacked branches.
146
    try:
147
        locs.add_url('stacked on', branch.get_stacked_on())
148
    except (errors.UnstackableBranchFormat, errors.UnstackableRepositoryFormat,
149
        errors.NotStacked):
150
        pass
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
151
    return locs
1694.2.6 by Martin Pool
[merge] bzr.dev
152
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
153
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
154
def _show_related_info(branch, outfile):
1694.2.6 by Martin Pool
[merge] bzr.dev
155
    """Show parent and push location of branch."""
1551.15.41 by Aaron Bentley
Make info provide more related brances, and format all branches nicely
156
    locs = _gather_related_branches(branch)
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
157
    if len(locs.locs) > 0:
2911.6.1 by Blake Winton
Change 'print >> f,'s to 'f.write('s.
158
        outfile.write('\n')
159
        outfile.write('Related branches:\n')
1551.15.43 by Aaron Bentley
Provide ways of getting at unicode-clean output
160
        outfile.writelines(locs.get_lines())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
161
162
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
163
def _show_format_info(control=None, repository=None, branch=None,
164
                      working=None, outfile=None):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
165
    """Show known formats for control, working, branch and repository."""
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
166
    outfile.write('\n')
167
    outfile.write('Format:\n')
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
168
    if control:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
169
        outfile.write('       control: %s\n' %
170
            control._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
171
    if working:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
172
        outfile.write('  working tree: %s\n' %
173
            working._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
174
    if branch:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
175
        outfile.write('        branch: %s\n' %
176
            branch._format.get_format_description())
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
177
    if repository:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
178
        outfile.write('    repository: %s\n' %
179
            repository._format.get_format_description())
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
180
181
182
def _show_locking_info(repository, branch=None, working=None, outfile=None):
1694.2.6 by Martin Pool
[merge] bzr.dev
183
    """Show locking status of working, branch and repository."""
184
    if (repository.get_physical_lock_status() or
185
        (branch and branch.get_physical_lock_status()) or
186
        (working and working.get_physical_lock_status())):
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
187
        outfile.write('\n')
188
        outfile.write('Lock status:\n')
1694.2.6 by Martin Pool
[merge] bzr.dev
189
        if working:
190
            if working.get_physical_lock_status():
191
                status = 'locked'
192
            else:
193
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
194
            outfile.write('  working tree: %s\n' % status)
1694.2.6 by Martin Pool
[merge] bzr.dev
195
        if branch:
196
            if branch.get_physical_lock_status():
197
                status = 'locked'
198
            else:
199
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
200
            outfile.write('        branch: %s\n' % status)
1694.2.6 by Martin Pool
[merge] bzr.dev
201
        if repository:
202
            if repository.get_physical_lock_status():
203
                status = 'locked'
204
            else:
205
                status = 'unlocked'
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
206
            outfile.write('    repository: %s\n' % status)
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
207
208
209
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
210
    """Show missing master revisions in branch."""
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
211
    # Try with inaccessible branch ?
1624.3.2 by Olaf Conradi
Implemented table of constructs from BzrInfo specification.
212
    master = branch.get_master_branch()
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
213
    if master:
1624.3.2 by Olaf Conradi
Implemented table of constructs from BzrInfo specification.
214
        local_extra, remote_extra = find_unmerged(branch, master)
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
215
        if remote_extra:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
216
            outfile.write('\n')
217
            outfile.write(('Branch is out of date: missing %d '
218
                'revision%s.\n') % (len(remote_extra),
219
                plural(len(remote_extra))))
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
220
221
222
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
223
    """Show missing revisions in working tree."""
224
    branch = working.branch
225
    basis = working.basis_tree()
226
    work_inv = working.inventory
2249.4.2 by Wouter van Heyst
Convert callers of Branch.revision_history() to Branch.last_revision_info() where sensible.
227
    branch_revno, branch_last_revision = branch.last_revision_info()
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
228
    try:
229
        tree_last_id = working.get_parent_ids()[0]
230
    except IndexError:
231
        tree_last_id = None
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
232
2249.4.2 by Wouter van Heyst
Convert callers of Branch.revision_history() to Branch.last_revision_info() where sensible.
233
    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
234
        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.
235
        missing_count = branch_revno - tree_last_revno
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
236
        outfile.write('\n')
237
        outfile.write(('Working tree is out of date: missing %d '
238
            'revision%s.\n') % (missing_count, plural(missing_count)))
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
239
240
241
def _show_working_stats(working, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
242
    """Show statistics about a working tree."""
243
    basis = working.basis_tree()
244
    work_inv = working.inventory
1852.10.3 by Robert Collins
Remove all uses of compare_trees and replace with Tree.changes_from throughout bzrlib.
245
    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
246
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
247
    outfile.write('\n')
248
    outfile.write('In the working tree:\n')
249
    outfile.write('  %8s unchanged\n' % len(delta.unchanged))
250
    outfile.write('  %8d modified\n' % len(delta.modified))
251
    outfile.write('  %8d added\n' % len(delta.added))
252
    outfile.write('  %8d removed\n' % len(delta.removed))
253
    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
254
255
    ignore_cnt = unknown_cnt = 0
256
    for path in working.extras():
257
        if working.is_ignored(path):
258
            ignore_cnt += 1
259
        else:
260
            unknown_cnt += 1
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
261
    outfile.write('  %8d unknown\n' % unknown_cnt)
262
    outfile.write('  %8d ignored\n' % ignore_cnt)
462 by Martin Pool
- New form 'file_id in tree' to check if the file is present
263
264
    dir_cnt = 0
1731.1.39 by Aaron Bentley
Reject removing is_root
265
    for file_id in work_inv:
266
        if (work_inv.get_file_kind(file_id) == 'directory' and 
267
            not work_inv.is_root(file_id)):
268
            dir_cnt += 1
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
269
    outfile.write('  %8d versioned %s\n' % (dir_cnt,
270
        plural(dir_cnt, 'subdirectory', 'subdirectories')))
77 by mbp at sourcefrog
- split info command out into separate file
271
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
272
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
273
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
274
    """Show statistics about a branch."""
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
275
    revno, head = branch.last_revision_info()
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
276
    outfile.write('\n')
277
    outfile.write('Branch history:\n')
278
    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).
279
    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
280
    if verbose:
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
281
        committers = stats['committers']
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
282
        outfile.write('  %8d committer%s\n' % (committers,
283
            plural(committers)))
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
284
    if revno:
285
        timestamp, timezone = stats['firstrev']
286
        age = int((time.time() - timestamp) / 3600 / 24)
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
287
        outfile.write('  %8d day%s old\n' % (age, plural(age)))
288
        outfile.write('   first revision: %s\n' %
289
            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).
290
        timestamp, timezone = stats['latestrev']
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
291
        outfile.write('  latest revision: %s\n' %
292
            osutils.format_date(timestamp, timezone))
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
293
    return stats
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
294
295
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
296
def _show_repository_info(repository, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
297
    """Show settings of a repository."""
298
    if repository.make_working_trees():
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
299
        outfile.write('\n')
300
        outfile.write('Create working tree for new branches inside '
301
            'the repository.\n')
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
302
303
304
def _show_repository_stats(stats, outfile):
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
305
    """Show statistics about a repository."""
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
306
    if 'revisions' in stats or 'size' in stats:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
307
        outfile.write('\n')
308
        outfile.write('Repository:\n')
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
309
    if 'revisions' in stats:
310
        revisions = stats['revisions']
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
311
        outfile.write('  %8d revision%s\n' % (revisions, plural(revisions)))
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
312
    if 'size' in stats:
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
313
        outfile.write('  %8d KiB\n' % (stats['size']/1024))
314
1624.3.21 by Olaf Conradi
Make bzr info command work on both local and remote locations. Support
315
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
316
def show_bzrdir_info(a_bzrdir, verbose=False, outfile=None):
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
317
    """Output to stdout the 'info' for a_bzrdir."""
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
318
    if outfile is None:
319
        outfile = sys.stdout
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
320
    try:
2363.5.9 by Aaron Bentley
Merge from bzr.dev
321
        tree = a_bzrdir.open_workingtree(
322
            recommend_upgrade=False)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
323
    except (NoWorkingTree, NotLocalUrl):
324
        tree = None
325
        try:
326
            branch = a_bzrdir.open_branch()
327
        except NotBranchError:
328
            branch = None
329
            try:
330
                repository = a_bzrdir.open_repository()
331
            except NoRepositoryPresent:
332
                # Return silently; cmd_info already returned NotBranchError
333
                # if no bzrdir could be opened.
334
                return
335
            else:
336
                lockable = repository
337
        else:
338
            repository = branch.repository
339
            lockable = branch
340
    else:
341
        branch = tree.branch
342
        repository = branch.repository
343
        lockable = tree
344
345
    lockable.lock_read()
346
    try:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
347
        show_component_info(a_bzrdir, repository, branch, tree, verbose,
348
                            outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
349
    finally:
350
        lockable.unlock()
351
352
353
def show_component_info(control, repository, branch=None, working=None,
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
354
    verbose=1, outfile=None):
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
355
    """Write info about all bzrdir components to stdout"""
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
356
    if outfile is None:
357
        outfile = sys.stdout
2363.5.7 by Aaron Bentley
Make verbose mean what I want
358
    if verbose is False:
359
        verbose = 1
360
    if verbose is True:
361
        verbose = 2
2363.5.6 by Aaron Bentley
Add short format description
362
    layout = describe_layout(repository, branch, working)
363
    format = describe_format(control, repository, branch, working)
2968.2.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``
364
    outfile.write("%s (format: %s)\n" % (layout, format))
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
365
    _show_location_info(gather_location_info(repository, branch, working),
366
                        outfile)
2584.2.1 by Adeodato Simó
Make `bzr info` show related branches in non-verbose mode.
367
    if branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
368
        _show_related_info(branch, outfile)
2363.5.7 by Aaron Bentley
Make verbose mean what I want
369
    if verbose == 0:
370
        return
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
371
    _show_format_info(control, repository, branch, working, outfile)
372
    _show_locking_info(repository, branch, working, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
373
    if branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
374
        _show_missing_revisions_branch(branch, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
375
    if working is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
376
        _show_missing_revisions_working(working, outfile)
377
        _show_working_stats(working, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
378
    elif branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
379
        _show_missing_revisions_branch(branch, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
380
    if branch is not None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
381
        stats = _show_branch_stats(branch, verbose==2, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
382
    else:
383
        stats = repository.gather_stats()
384
    if branch is None and working is None:
2904.3.1 by Lukáš Lalinský
Unicode-safe output from ``bzr info``.
385
        _show_repository_info(repository, outfile)
386
    _show_repository_stats(stats, outfile)
2363.5.1 by Aaron Bentley
Unify info display into show_component_info
387
388
2363.5.2 by Aaron Bentley
Implement layout description
389
def describe_layout(repository=None, branch=None, tree=None):
390
    """Convert a control directory layout into a user-understandable term
391
392
    Common outputs include "Standalone tree", "Repository branch" and
393
    "Checkout".  Uncommon outputs include "Unshared repository with trees"
394
    and "Empty control directory"
395
    """
396
    if repository is None:
397
        return 'Empty control directory'
398
    if branch is None and tree is None:
399
        if repository.is_shared():
400
            phrase = 'Shared repository'
401
        else:
402
            phrase = 'Unshared repository'
403
        if repository.make_working_trees():
404
            phrase += ' with trees'
405
        return phrase
406
    else:
407
        if repository.is_shared():
408
            independence = "Repository "
409
        else:
410
            independence = "Standalone "
411
        if tree is not None:
412
            phrase = "tree"
413
        else:
414
            phrase = "branch"
415
        if branch is None and tree is not None:
416
            phrase = "branchless tree"
417
        else:
418
            if (tree is not None and tree.bzrdir.root_transport.base !=
419
                branch.bzrdir.root_transport.base):
2363.5.4 by Aaron Bentley
Eliminate the concept of a 'repository lightweight checkout'
420
                independence = ''
2363.5.2 by Aaron Bentley
Implement layout description
421
                phrase = "Lightweight checkout"
422
            elif branch.get_bound_location() is not None:
423
                if independence == 'Standalone ':
424
                    independence = ''
425
                if tree is None:
426
                    phrase = "Bound branch"
427
                else:
428
                    phrase = "Checkout"
429
        if independence != "":
430
            phrase = phrase.lower()
431
        return "%s%s" % (independence, phrase)
432
433
2363.5.5 by Aaron Bentley
add info.describe_format
434
def describe_format(control, repository, branch, tree):
435
    """Determine the format of an existing control directory
436
437
    Several candidates may be found.  If so, the names are returned as a
2363.5.17 by Aaron Bentley
Change separator from '/' to 'or'
438
    single string, separated by ' or '.
2363.5.5 by Aaron Bentley
add info.describe_format
439
440
    If no matching candidate is found, "unnamed" is returned.
441
    """
442
    candidates  = []
2363.5.6 by Aaron Bentley
Add short format description
443
    if (branch is not None and tree is not None and
444
        branch.bzrdir.root_transport.base !=
445
        tree.bzrdir.root_transport.base):
446
        branch = None
447
        repository = None
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
448
    non_aliases = set(bzrdir.format_registry.keys())
449
    non_aliases.difference_update(bzrdir.format_registry.aliases())
450
    for key in non_aliases:
2363.5.5 by Aaron Bentley
add info.describe_format
451
        format = bzrdir.format_registry.make_bzrdir(key)
452
        if isinstance(format, bzrdir.BzrDirMetaFormat1):
453
            if (tree and format.workingtree_format !=
454
                tree._format):
455
                continue
456
            if (branch and format.get_branch_format() !=
457
                branch._format):
458
                continue
459
            if (repository and format.repository_format !=
460
                repository._format):
461
                continue
462
        if format.__class__ is not control._format.__class__:
463
            continue
464
        candidates.append(key)
465
    if len(candidates) == 0:
466
        return 'unnamed'
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
467
    candidates.sort()
2363.5.6 by Aaron Bentley
Add short format description
468
    new_candidates = [c for c in candidates if not
469
        bzrdir.format_registry.get_info(c).hidden]
470
    if len(new_candidates) > 0:
3152.2.2 by Robert Collins
The bzrdir format registry now accepts an ``alias`` keyword to
471
        # If there are any non-hidden formats that match, only return those to
472
        # avoid listing hidden formats except when only a hidden format will
473
        # do.
2363.5.6 by Aaron Bentley
Add short format description
474
        candidates = new_candidates
2363.5.17 by Aaron Bentley
Change separator from '/' to 'or'
475
    return ' or '.join(candidates)