~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/status.py

Initial stab at repository format support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
import sys
18
 
 
19
 
from bzrlib import (
20
 
    delta as _mod_delta,
21
 
    hooks as _mod_hooks,
22
 
    log,
23
 
    osutils,
24
 
    tsort,
25
 
    revision as _mod_revision,
26
 
    )
27
 
import bzrlib.errors as errors
28
 
from bzrlib.trace import mutter, warning
29
 
 
30
 
# TODO: when showing single-line logs, truncate to the width of the terminal
31
 
# if known, but only if really going to the terminal (not into a file)
32
 
 
33
 
 
34
 
def report_changes(to_file, old, new, specific_files, 
35
 
                   show_short_reporter, show_long_callback, 
36
 
                   short=False, want_unchanged=False, 
37
 
                   want_unversioned=False, show_ids=False, classify=True):
38
 
    """Display summary of changes.
39
 
 
40
 
    This compares two trees with regards to a list of files, and delegates 
41
 
    the display to underlying elements.
42
 
 
43
 
    For short output, it creates an iterator on all changes, and lets a given
44
 
    reporter display these changes.
45
 
 
46
 
    For stantard output, it creates a delta of the changes, and forwards it
47
 
    to a callback
48
 
 
49
 
    :param to_file: If set, write to this file (default stdout.)
50
 
    :param old: Start tree for the comparison
51
 
    :param end: End tree for the comparison
52
 
    :param specific_files: If set, a list of filenames whose status should be
53
 
        shown.  It is an error to give a filename that is not in the working
54
 
        tree, or in the working inventory or in the basis inventory.
55
 
    :param show_short_reporter: Reporter in charge of display for short output
56
 
    :param show_long_callback: Callback in charge of display for normal output
57
 
    :param short: If True, gives short SVN-style status lines.
58
 
    :param want_unchanged: Deprecated parameter. If set, includes unchanged
59
 
        files.
60
 
    :param show_ids: If set, includes each file's id.
61
 
    :param want_unversioned: If False, only shows versioned files.
62
 
    :param classify: Add special symbols to indicate file kind.
63
 
    """
64
 
 
65
 
    if short:
66
 
        changes = new.iter_changes(old, want_unchanged, specific_files,
67
 
            require_versioned=False, want_unversioned=want_unversioned)
68
 
        _mod_delta.report_changes(changes, show_short_reporter)
69
 
        
70
 
    else:
71
 
        delta = new.changes_from(old, want_unchanged=want_unchanged,
72
 
                              specific_files=specific_files,
73
 
                              want_unversioned=want_unversioned)
74
 
        # filter out unknown files. We may want a tree method for
75
 
        # this
76
 
        delta.unversioned = [unversioned for unversioned in
77
 
            delta.unversioned if not new.is_ignored(unversioned[0])]
78
 
        show_long_callback(to_file, delta, 
79
 
                           show_ids=show_ids,
80
 
                           show_unchanged=want_unchanged,
81
 
                           classify=classify)
82
 
 
83
 
 
84
 
def show_tree_status(wt, show_unchanged=None,
85
 
                     specific_files=None,
86
 
                     show_ids=False,
87
 
                     to_file=None,
88
 
                     show_pending=True,
89
 
                     revision=None,
90
 
                     short=False,
91
 
                     verbose=False,
92
 
                     versioned=False,
93
 
                     classify=True,
94
 
                     show_long_callback=_mod_delta.report_delta):
95
 
    """Display summary of changes.
96
 
 
97
 
    By default this compares the working tree to a previous revision.
98
 
    If the revision argument is given, summarizes changes between the
99
 
    working tree and another, or between two revisions.
100
 
 
101
 
    The result is written out as Unicode and to_file should be able
102
 
    to encode that.
103
 
 
104
 
    If showing the status of a working tree, extra information is included
105
 
    about unknown files, conflicts, and pending merges.
106
 
 
107
 
    :param show_unchanged: Deprecated parameter. If set, includes unchanged
108
 
        files.
109
 
    :param specific_files: If set, a list of filenames whose status should be
110
 
        shown.  It is an error to give a filename that is not in the working
111
 
        tree, or in the working inventory or in the basis inventory.
112
 
    :param show_ids: If set, includes each file's id.
113
 
    :param to_file: If set, write to this file (default stdout.)
114
 
    :param show_pending: If set, write pending merges.
115
 
    :param revision: If None, compare latest revision with working tree
116
 
        If not None, it must be a RevisionSpec list.
117
 
        If one revision, compare with working tree.
118
 
        If two revisions, show status between first and second.
119
 
    :param short: If True, gives short SVN-style status lines.
120
 
    :param verbose: If True, show all merged revisions, not just
121
 
        the merge tips
122
 
    :param versioned: If True, only shows versioned files.
123
 
    :param classify: Add special symbols to indicate file kind.
124
 
    :param show_long_callback: A callback: message = show_long_callback(to_file, delta, 
125
 
        show_ids, show_unchanged, indent, filter), only used with the long output
126
 
    """
127
 
    if show_unchanged is not None:
128
 
        warn("show_tree_status with show_unchanged has been deprecated "
129
 
             "since bzrlib 0.9", DeprecationWarning, stacklevel=2)
130
 
 
131
 
    if to_file is None:
132
 
        to_file = sys.stdout
133
 
 
134
 
    wt.lock_read()
135
 
    try:
136
 
        new_is_working_tree = True
137
 
        if revision is None:
138
 
            if wt.last_revision() != wt.branch.last_revision():
139
 
                warning("working tree is out of date, run 'bzr update'")
140
 
            new = wt
141
 
            old = new.basis_tree()
142
 
        elif len(revision) > 0:
143
 
            try:
144
 
                old = revision[0].as_tree(wt.branch)
145
 
            except errors.NoSuchRevision, e:
146
 
                raise errors.BzrCommandError(str(e))
147
 
            if (len(revision) > 1) and (revision[1].spec is not None):
148
 
                try:
149
 
                    new = revision[1].as_tree(wt.branch)
150
 
                    new_is_working_tree = False
151
 
                except errors.NoSuchRevision, e:
152
 
                    raise errors.BzrCommandError(str(e))
153
 
            else:
154
 
                new = wt
155
 
        old.lock_read()
156
 
        new.lock_read()
157
 
        try:
158
 
            for hook in hooks['pre_status']:
159
 
                hook(StatusHookParams(old, new, to_file, versioned,
160
 
                    show_ids, short, verbose))
161
 
 
162
 
            specific_files, nonexistents \
163
 
                = _filter_nonexistent(specific_files, old, new)
164
 
            want_unversioned = not versioned
165
 
 
166
 
            # Reporter used for short outputs
167
 
            reporter = _mod_delta._ChangeReporter(output_file=to_file,
168
 
                unversioned_filter=new.is_ignored, classify=classify)
169
 
            report_changes(to_file, old, new, specific_files, 
170
 
                           reporter, show_long_callback, 
171
 
                           short=short, want_unchanged=show_unchanged, 
172
 
                           want_unversioned=want_unversioned, show_ids=show_ids,
173
 
                           classify=classify)
174
 
 
175
 
            # show the ignored files among specific files (i.e. show the files
176
 
            # identified from input that we choose to ignore). 
177
 
            if specific_files is not None:
178
 
                # Ignored files is sorted because specific_files is already sorted
179
 
                ignored_files = [specific for specific in
180
 
                    specific_files if new.is_ignored(specific)]
181
 
                if len(ignored_files) > 0 and not short:
182
 
                    to_file.write("ignored:\n")
183
 
                    prefix = ' '
184
 
                else:
185
 
                    prefix = 'I  '
186
 
                for ignored_file in ignored_files:
187
 
                    to_file.write("%s %s\n" % (prefix, ignored_file))
188
 
 
189
 
            # show the new conflicts only for now. XXX: get them from the
190
 
            # delta.
191
 
            conflicts = new.conflicts()
192
 
            if specific_files is not None:
193
 
                conflicts = conflicts.select_conflicts(new, specific_files,
194
 
                    ignore_misses=True, recurse=True)[1]
195
 
            if len(conflicts) > 0 and not short:
196
 
                to_file.write("conflicts:\n")
197
 
            for conflict in conflicts:
198
 
                if short:
199
 
                    prefix = 'C  '
200
 
                else:
201
 
                    prefix = ' '
202
 
                to_file.write("%s %s\n" % (prefix, unicode(conflict)))
203
 
            # Show files that were requested but don't exist (and are
204
 
            # not versioned).  We don't involve delta in this; these
205
 
            # paths are really the province of just the status
206
 
            # command, since they have more to do with how it was
207
 
            # invoked than with the tree it's operating on.
208
 
            if nonexistents and not short:
209
 
                to_file.write("nonexistent:\n")
210
 
            for nonexistent in nonexistents:
211
 
                # We could calculate prefix outside the loop but, given
212
 
                # how rarely this ought to happen, it's OK and arguably
213
 
                # slightly faster to do it here (ala conflicts above)
214
 
                if short:
215
 
                    prefix = 'X  '
216
 
                else:
217
 
                    prefix = ' '
218
 
                to_file.write("%s %s\n" % (prefix, nonexistent))
219
 
            if (new_is_working_tree and show_pending):
220
 
                show_pending_merges(new, to_file, short, verbose=verbose)
221
 
            if nonexistents:
222
 
                raise errors.PathsDoNotExist(nonexistents)
223
 
            for hook in hooks['post_status']:
224
 
                hook(StatusHookParams(old, new, to_file, versioned,
225
 
                    show_ids, short, verbose))
226
 
        finally:
227
 
            old.unlock()
228
 
            new.unlock()
229
 
    finally:
230
 
        wt.unlock()
231
 
 
232
 
 
233
 
def _get_sorted_revisions(tip_revision, revision_ids, parent_map):
234
 
    """Get an iterator which will return the revisions in merge sorted order.
235
 
 
236
 
    This will build up a list of all nodes, such that only nodes in the list
237
 
    are referenced. It then uses MergeSorter to return them in 'merge-sorted'
238
 
    order.
239
 
 
240
 
    :param revision_ids: A set of revision_ids
241
 
    :param parent_map: The parent information for each node. Revisions which
242
 
        are considered ghosts should not be present in the map.
243
 
    :return: iterator from MergeSorter.iter_topo_order()
244
 
    """
245
 
    # MergeSorter requires that all nodes be present in the graph, so get rid
246
 
    # of any references pointing outside of this graph.
247
 
    parent_graph = {}
248
 
    for revision_id in revision_ids:
249
 
        if revision_id not in parent_map: # ghost
250
 
            parent_graph[revision_id] = []
251
 
        else:
252
 
            # Only include parents which are in this sub-graph
253
 
            parent_graph[revision_id] = [p for p in parent_map[revision_id]
254
 
                                            if p in revision_ids]
255
 
    sorter = tsort.MergeSorter(parent_graph, tip_revision)
256
 
    return sorter.iter_topo_order()
257
 
 
258
 
 
259
 
def show_pending_merges(new, to_file, short=False, verbose=False):
260
 
    """Write out a display of pending merges in a working tree."""
261
 
    parents = new.get_parent_ids()
262
 
    if len(parents) < 2:
263
 
        return
264
 
 
265
 
    term_width = osutils.terminal_width()
266
 
    if term_width is not None:
267
 
        # we need one extra space for terminals that wrap on last char
268
 
        term_width = term_width - 1
269
 
    if short:
270
 
        first_prefix = 'P   '
271
 
        sub_prefix = 'P.   '
272
 
    else:
273
 
        first_prefix = '  '
274
 
        sub_prefix = '    '
275
 
 
276
 
    def show_log_message(rev, prefix):
277
 
        if term_width is None:
278
 
            width = term_width
279
 
        else:
280
 
            width = term_width - len(prefix)
281
 
        log_message = log_formatter.log_string(None, rev, width, prefix=prefix)
282
 
        to_file.write(log_message + '\n')
283
 
 
284
 
    pending = parents[1:]
285
 
    branch = new.branch
286
 
    last_revision = parents[0]
287
 
    if not short:
288
 
        if verbose:
289
 
            to_file.write('pending merges:\n')
290
 
        else:
291
 
            to_file.write('pending merge tips:'
292
 
                          ' (use -v to see all merge revisions)\n')
293
 
    graph = branch.repository.get_graph()
294
 
    other_revisions = [last_revision]
295
 
    log_formatter = log.LineLogFormatter(to_file)
296
 
    for merge in pending:
297
 
        try:
298
 
            rev = branch.repository.get_revisions([merge])[0]
299
 
        except errors.NoSuchRevision:
300
 
            # If we are missing a revision, just print out the revision id
301
 
            to_file.write(first_prefix + '(ghost) ' + merge + '\n')
302
 
            other_revisions.append(merge)
303
 
            continue
304
 
 
305
 
        # Log the merge, as it gets a slightly different formatting
306
 
        show_log_message(rev, first_prefix)
307
 
        if not verbose:
308
 
            continue
309
 
 
310
 
        # Find all of the revisions in the merge source, which are not in the
311
 
        # last committed revision.
312
 
        merge_extra = graph.find_unique_ancestors(merge, other_revisions)
313
 
        other_revisions.append(merge)
314
 
        merge_extra.discard(_mod_revision.NULL_REVISION)
315
 
 
316
 
        # Get a handle to all of the revisions we will need
317
 
        try:
318
 
            revisions = dict((rev.revision_id, rev) for rev in
319
 
                             branch.repository.get_revisions(merge_extra))
320
 
        except errors.NoSuchRevision:
321
 
            # One of the sub nodes is a ghost, check each one
322
 
            revisions = {}
323
 
            for revision_id in merge_extra:
324
 
                try:
325
 
                    rev = branch.repository.get_revisions([revision_id])[0]
326
 
                except errors.NoSuchRevision:
327
 
                    revisions[revision_id] = None
328
 
                else:
329
 
                    revisions[revision_id] = rev
330
 
 
331
 
        # Display the revisions brought in by this merge.
332
 
        rev_id_iterator = _get_sorted_revisions(merge, merge_extra,
333
 
                            branch.repository.get_parent_map(merge_extra))
334
 
        # Skip the first node
335
 
        num, first, depth, eom = rev_id_iterator.next()
336
 
        if first != merge:
337
 
            raise AssertionError('Somehow we misunderstood how'
338
 
                ' iter_topo_order works %s != %s' % (first, merge))
339
 
        for num, sub_merge, depth, eom in rev_id_iterator:
340
 
            rev = revisions[sub_merge]
341
 
            if rev is None:
342
 
                to_file.write(sub_prefix + '(ghost) ' + sub_merge + '\n')
343
 
                continue
344
 
            show_log_message(revisions[sub_merge], sub_prefix)
345
 
 
346
 
 
347
 
def _filter_nonexistent(orig_paths, old_tree, new_tree):
348
 
    """Convert orig_paths to two sorted lists and return them.
349
 
 
350
 
    The first is orig_paths paths minus the items in the second list,
351
 
    and the second list is paths that are not in either inventory or
352
 
    tree (they don't qualify if they exist in the tree's inventory, or
353
 
    if they exist in the tree but are not versioned.)
354
 
 
355
 
    If either of the two lists is empty, return it as an empty list.
356
 
 
357
 
    This can be used by operations such as bzr status that can accept
358
 
    unknown or ignored files.
359
 
    """
360
 
    mutter("check paths: %r", orig_paths)
361
 
    if not orig_paths:
362
 
        return orig_paths, []
363
 
    s = old_tree.filter_unversioned_files(orig_paths)
364
 
    s = new_tree.filter_unversioned_files(s)
365
 
    nonexistent = [path for path in s if not new_tree.has_filename(path)]
366
 
    remaining   = [path for path in orig_paths if not path in nonexistent]
367
 
    # Sorting the 'remaining' list doesn't have much effect in
368
 
    # practice, since the various status output sections will sort
369
 
    # their groups individually.  But for consistency of this
370
 
    # function's API, it's better to sort both than just 'nonexistent'.
371
 
    return sorted(remaining), sorted(nonexistent)
372
 
 
373
 
 
374
 
class StatusHooks(_mod_hooks.Hooks):
375
 
    """A dictionary mapping hook name to a list of callables for status hooks.
376
 
 
377
 
    e.g. ['post_status'] Is the list of items to be called when the
378
 
    status command has finished printing the status.
379
 
    """
380
 
 
381
 
    def __init__(self):
382
 
        """Create the default hooks.
383
 
 
384
 
        These are all empty initially, because by default nothing should get
385
 
        notified.
386
 
        """
387
 
        _mod_hooks.Hooks.__init__(self, "bzrlib.status", "hooks")
388
 
        self.add_hook('post_status',
389
 
            "Called with argument StatusHookParams after Bazaar has "
390
 
            "displayed the status. StatusHookParams has the attributes "
391
 
            "(old_tree, new_tree, to_file, versioned, show_ids, short, "
392
 
            "verbose). The last four arguments correspond to the command "
393
 
            "line options specified by the user for the status command. "
394
 
            "to_file is the output stream for writing.",
395
 
            (2, 3))
396
 
        self.add_hook('pre_status',
397
 
            "Called with argument StatusHookParams before Bazaar "
398
 
            "displays the status. StatusHookParams has the attributes "
399
 
            "(old_tree, new_tree, to_file, versioned, show_ids, short, "
400
 
            "verbose). The last four arguments correspond to the command "
401
 
            "line options specified by the user for the status command. "
402
 
            "to_file is the output stream for writing.",
403
 
            (2, 3))
404
 
 
405
 
 
406
 
class StatusHookParams(object):
407
 
    """Object holding parameters passed to post_status hooks.
408
 
 
409
 
    :ivar old_tree: Start tree (basis tree) for comparison.
410
 
    :ivar new_tree: Working tree.
411
 
    :ivar to_file: If set, write to this file.
412
 
    :ivar versioned: Show only versioned files.
413
 
    :ivar show_ids: Show internal object ids.
414
 
    :ivar short: Use short status indicators.
415
 
    :ivar verbose: Verbose flag.
416
 
    """
417
 
 
418
 
    def __init__(self, old_tree, new_tree, to_file, versioned, show_ids,
419
 
            short, verbose):
420
 
        """Create a group of post_status hook parameters.
421
 
 
422
 
        :param old_tree: Start tree (basis tree) for comparison.
423
 
        :param new_tree: Working tree.
424
 
        :param to_file: If set, write to this file.
425
 
        :param versioned: Show only versioned files.
426
 
        :param show_ids: Show internal object ids.
427
 
        :param short: Use short status indicators.
428
 
        :param verbose: Verbose flag.
429
 
        """
430
 
        self.old_tree = old_tree
431
 
        self.new_tree = new_tree
432
 
        self.to_file = to_file
433
 
        self.versioned = versioned
434
 
        self.show_ids = show_ids
435
 
        self.short = short
436
 
        self.verbose = verbose
437
 
 
438
 
    def __eq__(self, other):
439
 
        return self.__dict__ == other.__dict__
440
 
 
441
 
    def __repr__(self):
442
 
        return "<%s(%s, %s, %s, %s, %s, %s, %s)>" % (self.__class__.__name__,
443
 
            self.old_tree, self.new_tree, self.to_file, self.versioned,
444
 
            self.show_ids, self.short, self.verbose)
445
 
 
446
 
 
447
 
def _show_shelve_summary(params):
448
 
    """post_status hook to display a summary of shelves.
449
 
 
450
 
    :param params: StatusHookParams.
451
 
    """
452
 
    get_shelf_manager = getattr(params.new_tree, 'get_shelf_manager', None)
453
 
    if get_shelf_manager is None:
454
 
        return
455
 
    manager = get_shelf_manager()
456
 
    shelves = manager.active_shelves()
457
 
    if shelves:
458
 
        singular = '%d shelf exists. '
459
 
        plural = '%d shelves exist. '
460
 
        if len(shelves) == 1:
461
 
            fmt = singular
462
 
        else:
463
 
            fmt = plural
464
 
        params.to_file.write(fmt % len(shelves))
465
 
        params.to_file.write('See "bzr shelve --list" for details.\n')
466
 
 
467
 
 
468
 
hooks = StatusHooks()
469
 
 
470
 
 
471
 
hooks.install_named_hook('post_status', _show_shelve_summary,
472
 
    'bzr status')
473