Rev 5126: (mbp, for ajeans) refactor status code in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed Mar 31 01:42:29 BST 2010


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5126 [merge]
revision-id: pqm at pqm.ubuntu.com-20100331004226-11lgm5jnty8y82et
parent: pqm at pqm.ubuntu.com-20100330205139-0yvgqwbswhokqq7o
parent: arnaud.jeansen at gmail.com-20100330174031-vhh00cflhkez0rix
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2010-03-31 01:42:26 +0100
message:
  (mbp, for ajeans) refactor status code
modified:
  bzrlib/delta.py                delta.py-20050729221636-54cf14ef94783d0a
  bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
  bzrlib/status.py               status.py-20050505062338-431bfa63ec9b19e6
  bzrlib/tests/test_delta.py     test_delta.py-20070110134455-sqpd1y7mbjndelxf-1
=== modified file 'bzrlib/delta.py'
--- a/bzrlib/delta.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/delta.py	2010-03-19 23:58:06 +0000
@@ -107,112 +107,11 @@
                 return True
         return False
 
-
-    def show(self, to_file, show_ids=False, show_unchanged=False,
-             short_status=False, indent='',
-             filter=None):
-        """Output this delta in status-like form to to_file.
-
-        :param to_file: A file-like object where the output is displayed.
-
-        :param show_ids: Output the file ids if True.
-
-        :param show_unchanged: Output the unchanged files if True.
-
-        :param short_status: Single-line status if True.
-
-        :param indent: Added at the beginning of all output lines (for merged
-            revisions).
-
-        :param filter: A callable receiving a path and a file id and
-            returning True if the path should be displayed.
-        """
-
-        def decorate_path(path, kind, meta_modified=None):
-            if kind == 'directory':
-                path += '/'
-            elif kind == 'symlink':
-                path += '@'
-            if meta_modified:
-                path += '*'
-            return path
-
-        def show_more_renamed(item):
-            (oldpath, file_id, kind,
-             text_modified, meta_modified, newpath) = item
-            dec_new_path = decorate_path(newpath, kind, meta_modified)
-            to_file.write(' => %s' % dec_new_path)
-            if text_modified or meta_modified:
-                extra_modified.append((newpath, file_id, kind,
-                                       text_modified, meta_modified))
-
-        def show_more_kind_changed(item):
-            (path, file_id, old_kind, new_kind) = item
-            to_file.write(' (%s => %s)' % (old_kind, new_kind))
-
-        def show_path(path, file_id, kind, meta_modified,
-                      default_format, with_file_id_format):
-            dec_path = decorate_path(path, kind, meta_modified)
-            if show_ids:
-                to_file.write(with_file_id_format % dec_path)
-            else:
-                to_file.write(default_format % dec_path)
-
-        def show_list(files, long_status_name, short_status_letter,
-                      default_format='%s', with_file_id_format='%-30s',
-                      show_more=None):
-            if files:
-                header_shown = False
-                if short_status:
-                    prefix = short_status_letter
-                else:
-                    prefix = ''
-                prefix = indent + prefix + '  '
-
-                for item in files:
-                    path, file_id, kind = item[:3]
-                    if (filter is not None and not filter(path, file_id)):
-                        continue
-                    if not header_shown and not short_status:
-                        to_file.write(indent + long_status_name + ':\n')
-                        header_shown = True
-                    meta_modified = None
-                    if len(item) == 5:
-                        meta_modified = item[4]
-
-                    to_file.write(prefix)
-                    show_path(path, file_id, kind, meta_modified,
-                              default_format, with_file_id_format)
-                    if show_more is not None:
-                        show_more(item)
-                    if show_ids:
-                        to_file.write(' %s' % file_id)
-                    to_file.write('\n')
-
-        show_list(self.removed, 'removed', 'D')#
-        show_list(self.added, 'added', 'A')
-        extra_modified = []
-        # Reorder self.renamed tuples so that all lists share the same
-        # order for their 3 first fields and that they also begin like
-        # the self.modified tuples
-        renamed = [(p, i, k, tm, mm, np)
-                   for  p, np, i, k, tm, mm  in self.renamed]
-        show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
-                  show_more=show_more_renamed)
-        show_list(self.kind_changed, 'kind changed', 'K',
-                  with_file_id_format='%s',
-                  show_more=show_more_kind_changed)
-        show_list(self.modified + extra_modified, 'modified', 'M')
-        if show_unchanged:
-            show_list(self.unchanged, 'unchanged', 'S')
-
-        show_list(self.unversioned, 'unknown', ' ')
-
     def get_changes_as_text(self, show_ids=False, show_unchanged=False,
-             short_status=False):
+                            short_status=False):
         import StringIO
         output = StringIO.StringIO()
-        self.show(output, show_ids, show_unchanged, short_status)
+        report_delta(output, self, short_status, show_ids, show_unchanged)
         return output.getvalue()
 
 
@@ -393,7 +292,6 @@
         self.output("%s%s%s %s%s", rename, self.modified_map[modified], exe,
                     old_path, path)
 
-
 def report_changes(change_iterator, reporter):
     """Report the changes from a change iterator.
 
@@ -437,3 +335,105 @@
         versioned_change = versioned_change_map[versioned]
         reporter.report(file_id, path, versioned_change, renamed, modified,
                         exe_change, kind)
+
+def report_delta(to_file, delta, short_status=False, show_ids=False, 
+         show_unchanged=False, indent='', filter=None):
+    """Output this delta in status-like form to to_file.
+
+    :param to_file: A file-like object where the output is displayed.
+
+    :param delta: A TreeDelta containing the changes to be displayed
+
+    :param short_status: Single-line status if True.
+
+    :param show_ids: Output the file ids if True.
+
+    :param show_unchanged: Output the unchanged files if True.
+
+    :param indent: Added at the beginning of all output lines (for merged
+        revisions).
+
+    :param filter: A callable receiving a path and a file id and
+        returning True if the path should be displayed.
+    """
+
+    def decorate_path(path, kind, meta_modified=None):
+        if kind == 'directory':
+            path += '/'
+        elif kind == 'symlink':
+            path += '@'
+        if meta_modified:
+            path += '*'
+        return path
+
+    def show_more_renamed(item):
+        (oldpath, file_id, kind,
+         text_modified, meta_modified, newpath) = item
+        dec_new_path = decorate_path(newpath, kind, meta_modified)
+        to_file.write(' => %s' % dec_new_path)
+        if text_modified or meta_modified:
+            extra_modified.append((newpath, file_id, kind,
+                                   text_modified, meta_modified))
+
+    def show_more_kind_changed(item):
+        (path, file_id, old_kind, new_kind) = item
+        to_file.write(' (%s => %s)' % (old_kind, new_kind))
+
+    def show_path(path, file_id, kind, meta_modified,
+                  default_format, with_file_id_format):
+        dec_path = decorate_path(path, kind, meta_modified)
+        if show_ids:
+            to_file.write(with_file_id_format % dec_path)
+        else:
+            to_file.write(default_format % dec_path)
+
+    def show_list(files, long_status_name, short_status_letter,
+                  default_format='%s', with_file_id_format='%-30s',
+                  show_more=None):
+        if files:
+            header_shown = False
+            if short_status:
+                prefix = short_status_letter
+            else:
+                prefix = ''
+            prefix = indent + prefix + '  '
+
+            for item in files:
+                path, file_id, kind = item[:3]
+                if (filter is not None and not filter(path, file_id)):
+                    continue
+                if not header_shown and not short_status:
+                    to_file.write(indent + long_status_name + ':\n')
+                    header_shown = True
+                meta_modified = None
+                if len(item) == 5:
+                    meta_modified = item[4]
+
+                to_file.write(prefix)
+                show_path(path, file_id, kind, meta_modified,
+                          default_format, with_file_id_format)
+                if show_more is not None:
+                    show_more(item)
+                if show_ids:
+                    to_file.write(' %s' % file_id)
+                to_file.write('\n')
+
+    show_list(delta.removed, 'removed', 'D')
+    show_list(delta.added, 'added', 'A')
+    extra_modified = []
+    # Reorder delta.renamed tuples so that all lists share the same
+    # order for their 3 first fields and that they also begin like
+    # the delta.modified tuples
+    renamed = [(p, i, k, tm, mm, np)
+               for  p, np, i, k, tm, mm  in delta.renamed]
+    show_list(renamed, 'renamed', 'R', with_file_id_format='%s',
+              show_more=show_more_renamed)
+    show_list(delta.kind_changed, 'kind changed', 'K',
+              with_file_id_format='%s',
+              show_more=show_more_kind_changed)
+    show_list(delta.modified + extra_modified, 'modified', 'M')
+    if show_unchanged:
+        show_list(delta.unchanged, 'unchanged', 'S')
+
+    show_list(delta.unversioned, 'unknown', ' ')
+

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2010-03-19 10:16:06 +0000
+++ b/bzrlib/log.py	2010-03-31 00:42:26 +0000
@@ -1526,9 +1526,10 @@
         to_file = self.to_file
         to_file.write("%s%s\n" % (indent, ('\n' + indent).join(lines)))
         if revision.delta is not None:
-            # We don't respect delta_format for compatibility
-            revision.delta.show(to_file, self.show_ids, indent=indent,
-                                short_status=False)
+            # Use the standard status output to display changes
+            from bzrlib.delta import report_delta
+            report_delta(to_file, revision.delta, short_status=False, 
+                         show_ids=self.show_ids, indent=indent)
         if revision.diff is not None:
             to_file.write(indent + 'diff:\n')
             to_file.flush()
@@ -1597,8 +1598,11 @@
                 to_file.write(indent + offset + '%s\n' % (l,))
 
         if revision.delta is not None:
-            revision.delta.show(to_file, self.show_ids, indent=indent + offset,
-                                short_status=self.delta_format==1)
+            # Use the standard status output to display changes
+            from bzrlib.delta import report_delta
+            report_delta(to_file, revision.delta, 
+                         short_status=self.delta_format==1, 
+                         show_ids=self.show_ids, indent=indent + offset)
         if revision.diff is not None:
             self.show_diff(self.to_exact_file, revision.diff, '      ')
         to_file.write('\n')

=== modified file 'bzrlib/status.py'
--- a/bzrlib/status.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/status.py	2010-03-20 00:07:45 +0000
@@ -34,6 +34,54 @@
 # if known, but only if really going to the terminal (not into a file)
 
 
+def report_changes(to_file, old, new, specific_files, 
+                   show_short_reporter, show_long_callback, 
+                   short=False, want_unchanged=False, 
+                   want_unversioned=False, show_ids=False):
+    """Display summary of changes.
+
+    This compares two trees with regards to a list of files, and delegates 
+    the display to underlying elements.
+
+    For short output, it creates an iterator on all changes, and lets a given
+    reporter display these changes.
+
+    For stantard output, it creates a delta of the changes, and forwards it
+    to a callback
+
+    :param to_file: If set, write to this file (default stdout.)
+    :param old: Start tree for the comparison
+    :param end: End tree for the comparison
+    :param specific_files: If set, a list of filenames whose status should be
+        shown.  It is an error to give a filename that is not in the working
+        tree, or in the working inventory or in the basis inventory.
+    :param show_short_reporter: Reporter in charge of display for short output
+    :param show_long_callback: Callback in charge of display for normal output
+    :param short: If True, gives short SVN-style status lines.
+    :param want_unchanged: Deprecated parameter. If set, includes unchanged
+        files.
+    :param show_ids: If set, includes each file's id.
+    :param want_unversioned: If False, only shows versioned files.
+    """
+
+    if short:
+        changes = new.iter_changes(old, want_unchanged, specific_files,
+            require_versioned=False, want_unversioned=want_unversioned)
+        _mod_delta.report_changes(changes, show_short_reporter)
+        
+    else:
+        delta = new.changes_from(old, want_unchanged=want_unchanged,
+                              specific_files=specific_files,
+                              want_unversioned=want_unversioned)
+        # filter out unknown files. We may want a tree method for
+        # this
+        delta.unversioned = [unversioned for unversioned in
+            delta.unversioned if not new.is_ignored(unversioned[0])]
+        show_long_callback(to_file, delta, 
+                           show_ids=show_ids,
+                           show_unchanged=want_unchanged)
+
+
 def show_tree_status(wt, show_unchanged=None,
                      specific_files=None,
                      show_ids=False,
@@ -42,7 +90,8 @@
                      revision=None,
                      short=False,
                      verbose=False,
-                     versioned=False):
+                     versioned=False,
+                     show_long_callback=_mod_delta.report_delta):
     """Display summary of changes.
 
     By default this compares the working tree to a previous revision.
@@ -71,6 +120,8 @@
     :param verbose: If True, show all merged revisions, not just
         the merge tips
     :param versioned: If True, only shows versioned files.
+    :param show_long_callback: A callback: message = show_long_callback(to_file, delta, 
+        show_ids, show_unchanged, indent, filter), only used with the long output
     """
     if show_unchanged is not None:
         warn("show_tree_status with show_unchanged has been deprecated "
@@ -106,24 +157,15 @@
             specific_files, nonexistents \
                 = _filter_nonexistent(specific_files, old, new)
             want_unversioned = not versioned
-            if short:
-                changes = new.iter_changes(old, show_unchanged, specific_files,
-                    require_versioned=False, want_unversioned=want_unversioned)
-                reporter = _mod_delta._ChangeReporter(output_file=to_file,
-                    unversioned_filter=new.is_ignored)
-                _mod_delta.report_changes(changes, reporter)
-            else:
-                delta = new.changes_from(old, want_unchanged=show_unchanged,
-                                      specific_files=specific_files,
-                                      want_unversioned=want_unversioned)
-                # filter out unknown files. We may want a tree method for
-                # this
-                delta.unversioned = [unversioned for unversioned in
-                    delta.unversioned if not new.is_ignored(unversioned[0])]
-                delta.show(to_file,
-                           show_ids=show_ids,
-                           show_unchanged=show_unchanged,
-                           short_status=False)
+
+            # Reporter used for short outputs
+            reporter = _mod_delta._ChangeReporter(output_file=to_file,
+                unversioned_filter=new.is_ignored)
+            report_changes(to_file, old, new, specific_files, 
+                           reporter, show_long_callback, 
+                           short=short, want_unchanged=show_unchanged, 
+                           want_unversioned=want_unversioned, show_ids=show_ids)
+
             # show the new conflicts only for now. XXX: get them from the
             # delta.
             conflicts = new.conflicts()

=== modified file 'bzrlib/tests/test_delta.py'
--- a/bzrlib/tests/test_delta.py	2009-03-23 14:59:43 +0000
+++ b/bzrlib/tests/test_delta.py	2010-03-30 17:40:31 +0000
@@ -221,7 +221,7 @@
 
     def show_string(self, delta, *args,  **kwargs):
         to_file = StringIO()
-        delta.show(to_file, *args, **kwargs)
+        _mod_delta.report_delta(to_file, delta, *args, **kwargs)
         return to_file.getvalue()
 
     def test_kind_change(self):
@@ -307,13 +307,13 @@
     def test_delta_show_short_status_no_filter(self):
         d, long_status, short_status = self._get_delta()
         out = StringIO()
-        d.show(out, short_status=True)
+        _mod_delta.report_delta(out, d, short_status=True)
         self.assertEquals(short_status, out.getvalue())
 
     def test_delta_show_long_status_no_filter(self):
         d, long_status, short_status = self._get_delta()
         out = StringIO()
-        d.show(out, short_status=False)
+        _mod_delta.report_delta(out, d, short_status=False)
         self.assertEquals(long_status, out.getvalue())
 
     def test_delta_show_no_filter(self):
@@ -321,7 +321,7 @@
         out = StringIO()
         def not_a_filter(path, file_id):
             return True
-        d.show(out, short_status=True, filter=not_a_filter)
+        _mod_delta.report_delta(out, d, short_status=True, filter=not_a_filter)
         self.assertEquals(short_status, out.getvalue())
 
     def test_delta_show_short_status_single_file_filter(self):
@@ -329,7 +329,7 @@
         out = StringIO()
         def only_f2(path, file_id):
             return path == 'f2'
-        d.show(out, short_status=True, filter=only_f2)
+        _mod_delta.report_delta(out, d, short_status=True, filter=only_f2)
         self.assertEquals("A  f2\n", out.getvalue())
 
     def test_delta_show_long_status_single_file_filter(self):
@@ -337,7 +337,7 @@
         out = StringIO()
         def only_f2(path, file_id):
             return path == 'f2'
-        d.show(out, short_status=False, filter=only_f2)
+        _mod_delta.report_delta(out, d, short_status=False, filter=only_f2)
         self.assertEquals("added:\n  f2\n", out.getvalue())
 
     def test_delta_show_short_status_single_file_id_filter(self):
@@ -345,6 +345,6 @@
         out = StringIO()
         def only_f2_id(path, file_id):
             return file_id == 'f2-id'
-        d.show(out, short_status=True, filter=only_f2_id)
+        _mod_delta.report_delta(out, d, short_status=True, filter=only_f2_id)
         self.assertEquals("A  f2\n", out.getvalue())
 




More information about the bazaar-commits mailing list