~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright (C) 2005, 2006 by Canonical Ltd
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

__all__ = ['show_bzrdir_info']

import time


import bzrlib.diff as diff
from bzrlib.errors import (NoWorkingTree, NotBranchError,
                           NoRepositoryPresent, NotLocalUrl)
from bzrlib.missing import find_unmerged
import bzrlib.osutils as osutils
from bzrlib.symbol_versioning import (deprecated_function, 
        zero_eight)


def plural(n, base='', pl=None):
    if n == 1:
        return base
    elif pl != None:
        return pl
    else:
        return 's'


def _repo_relpath(repo_path, path):
    """Return path with common prefix of repository path removed.

    If path is not part of the repository, the original path is returned.
    If path is equal to the repository, the current directory marker '.' is
    returned.
    """
    path = osutils.normalizepath(path)
    repo_path = osutils.normalizepath(repo_path)
    if path == repo_path:
        return '.'
    if osutils.is_inside(repo_path, path):
        return osutils.relpath(repo_path, path)
    return path


def _show_location_info(repository, branch=None, working=None):
    """Show known locations for working, branch and repository."""
    repository_path = repository.bzrdir.root_transport.base
    print 'Location:'
    if working and branch:
        working_path = working.bzrdir.root_transport.base
        branch_path = branch.bzrdir.root_transport.base
        if working_path != branch_path:
            # lightweight checkout
            print ' light checkout root: %s' % working_path
            if repository.is_shared():
                # lightweight checkout of branch in shared repository
                print '   shared repository: %s' % repository_path
                print '   repository branch: %s' % (
                    _repo_relpath(repository_path, branch_path))
            else:
                # lightweight checkout of standalone branch
                print '  checkout of branch: %s' % branch_path
        elif repository.is_shared():
            # branch with tree inside shared repository
            print '    shared repository: %s' % repository_path
            print '  repository checkout: %s' % (
                _repo_relpath(repository_path, branch_path))
        elif branch.get_bound_location():
            # normal checkout
            print '       checkout root: %s' % working_path
            print '  checkout of branch: %s' % branch.get_bound_location()
        else:
            # standalone
            print '  branch root: %s' % working_path
    elif branch:
        branch_path = branch.bzrdir.root_transport.base
        if repository.is_shared():
            # branch is part of shared repository
            print '  shared repository: %s' % repository_path
            print '  repository branch: %s' % (
                _repo_relpath(repository_path, branch_path))
        else:
            # standalone branch
            print '  branch root: %s' % branch_path
    else:
        # shared repository
        assert repository.is_shared()
        print '  shared repository: %s' % repository_path


def _show_related_info(branch):
    """Show parent and push location of branch."""
    if branch.get_parent() or branch.get_push_location():
        print
        print 'Related branches:'
        if branch.get_parent():
            if branch.get_push_location():
                print '      parent branch: %s' % branch.get_parent()
            else:
                print '  parent branch: %s' % branch.get_parent()
        if branch.get_push_location():
            print '  publish to branch: %s' % branch.get_push_location()


def _show_format_info(control=None, repository=None, branch=None, working=None):
    """Show known formats for control, working, branch and repository."""
    print
    print 'Format:'
    if control:
        print '       control: %s' % control._format.get_format_description()
    if working:
        print '  working tree: %s' % working._format.get_format_description()
    if branch:
        print '        branch: %s' % branch._format.get_format_description()
    if repository:
        print '    repository: %s' % repository._format.get_format_description()


def _show_locking_info(repository, branch=None, working=None):
    """Show locking status of working, branch and repository."""
    if (repository.get_physical_lock_status() or
        (branch and branch.get_physical_lock_status()) or
        (working and working.get_physical_lock_status())):
        print
        print 'Lock status:'
        if working:
            if working.get_physical_lock_status():
                status = 'locked'
            else:
                status = 'unlocked'
            print '  working tree: %s' % status
        if branch:
            if branch.get_physical_lock_status():
                status = 'locked'
            else:
                status = 'unlocked'
            print '        branch: %s' % status
        if repository:
            if repository.get_physical_lock_status():
                status = 'locked'
            else:
                status = 'unlocked'
            print '    repository: %s' % status


def _show_missing_revisions_branch(branch):
    """Show missing master revisions in branch."""
    # Try with inaccessible branch ?
    master = branch.get_master_branch()
    if master:
        local_extra, remote_extra = find_unmerged(branch, master)
        if remote_extra:
            print
            print 'Branch is out of date: missing %d revision%s.' % (
                len(remote_extra), plural(len(remote_extra)))


def _show_missing_revisions_working(working):
    """Show missing revisions in working tree."""
    branch = working.branch
    basis = working.basis_tree()
    work_inv = working.inventory
    delta = working.changes_from(basis, want_unchanged=True)
    history = branch.revision_history()
    tree_last_id = working.last_revision()

    if len(history) and tree_last_id != history[-1]:
        tree_last_revno = branch.revision_id_to_revno(tree_last_id)
        missing_count = len(history) - tree_last_revno
        print
        print 'Working tree is out of date: missing %d revision%s.' % (
            missing_count, plural(missing_count))


def _show_working_stats(working):
    """Show statistics about a working tree."""
    basis = working.basis_tree()
    work_inv = working.inventory
    delta = working.changes_from(basis, want_unchanged=True)

    print
    print 'In the working tree:'
    print '  %8s unchanged' % len(delta.unchanged)
    print '  %8d modified' % len(delta.modified)
    print '  %8d added' % len(delta.added)
    print '  %8d removed' % len(delta.removed)
    print '  %8d renamed' % len(delta.renamed)

    ignore_cnt = unknown_cnt = 0
    for path in working.extras():
        if working.is_ignored(path):
            ignore_cnt += 1
        else:
            unknown_cnt += 1
    print '  %8d unknown' % unknown_cnt
    print '  %8d ignored' % ignore_cnt

    dir_cnt = 0
    for file_id in work_inv:
        if work_inv.get_file_kind(file_id) == 'directory':
            dir_cnt += 1
    print '  %8d versioned %s' \
          % (dir_cnt,
             plural(dir_cnt, 'subdirectory', 'subdirectories'))


def _show_branch_stats(branch, verbose):
    """Show statistics about a branch."""
    repository = branch.repository
    history = branch.revision_history()

    print
    print 'Branch history:'
    revno = len(history)
    print '  %8d revision%s' % (revno, plural(revno))
    if verbose:
        committers = {}
        for rev in history:
            committers[repository.get_revision(rev).committer] = True
        print '  %8d committer%s' % (len(committers), plural(len(committers)))
    if revno > 0:
        firstrev = repository.get_revision(history[0])
        age = int((time.time() - firstrev.timestamp) / 3600 / 24)
        print '  %8d day%s old' % (age, plural(age))
        print '   first revision: %s' % osutils.format_date(firstrev.timestamp,
                                                            firstrev.timezone)

        lastrev = repository.get_revision(history[-1])
        print '  latest revision: %s' % osutils.format_date(lastrev.timestamp,
                                                            lastrev.timezone)

#     print
#     print 'Text store:'
#     c, t = branch.text_store.total_size()
#     print '  %8d file texts' % c
#     print '  %8d KiB' % (t/1024)

#     print
#     print 'Inventory store:'
#     c, t = branch.inventory_store.total_size()
#     print '  %8d inventories' % c
#     print '  %8d KiB' % (t/1024)


def _show_repository_info(repository):
    """Show settings of a repository."""
    if repository.make_working_trees():
        print
        print 'Create working tree for new branches inside the repository.'


def _show_repository_stats(repository):
    """Show statistics about a repository."""
    if repository.bzrdir.root_transport.listable():
        print
        print 'Revision store:'
        c, t = repository._revision_store.total_size(repository.get_transaction())
        print '  %8d revision%s' % (c, plural(c))
        print '  %8d KiB' % (t/1024)


@deprecated_function(zero_eight)
def show_info(b):
    """Please see show_bzrdir_info."""
    return show_bzrdir_info(b.bzrdir)


def show_bzrdir_info(a_bzrdir, verbose=False):
    """Output to stdout the 'info' for a_bzrdir."""
    try:
        working = a_bzrdir.open_workingtree()
        working.lock_read()
        try:
            show_tree_info(working, verbose)
        finally:
            working.unlock()
        return
    except (NoWorkingTree, NotLocalUrl):
        pass

    try:
        branch = a_bzrdir.open_branch()
        branch.lock_read()
        try:
            show_branch_info(branch, verbose)
        finally:
            branch.unlock()
        return
    except NotBranchError:
        pass

    try:
        repository = a_bzrdir.open_repository()
        repository.lock_read()
        try:
            show_repository_info(repository, verbose)
        finally:
            repository.unlock()
        return
    except NoRepositoryPresent:
        pass

    # Return silently, cmd_info already returned NotBranchError if no bzrdir
    # could be opened.


def show_tree_info(working, verbose):
    """Output to stdout the 'info' for working."""
    branch = working.branch
    repository = branch.repository
    control = working.bzrdir

    _show_location_info(repository, branch, working)
    _show_related_info(branch)
    _show_format_info(control, repository, branch, working)
    _show_locking_info(repository, branch, working)
    _show_missing_revisions_branch(branch)
    _show_missing_revisions_working(working)
    _show_working_stats(working)
    _show_branch_stats(branch, verbose)
    _show_repository_stats(repository)


def show_branch_info(branch, verbose):
    """Output to stdout the 'info' for branch."""
    repository = branch.repository
    control = branch.bzrdir

    _show_location_info(repository, branch)
    _show_related_info(branch)
    _show_format_info(control, repository, branch)
    _show_locking_info(repository, branch)
    _show_missing_revisions_branch(branch)
    _show_branch_stats(branch, verbose)
    _show_repository_stats(repository)


def show_repository_info(repository, verbose):
    """Output to stdout the 'info' for repository."""
    control = repository.bzrdir

    _show_location_info(repository)
    _show_format_info(control, repository)
    _show_locking_info(repository)
    _show_repository_info(repository)
    _show_repository_stats(repository)