Rev 6: Add an --only-changed flag. in http://bzr.arbash-meinel.com/plugins/update_copyright

John Arbash Meinel john at arbash-meinel.com
Fri Jan 8 20:55:57 GMT 2010


At http://bzr.arbash-meinel.com/plugins/update_copyright

------------------------------------------------------------
revno: 6
revision-id: john at arbash-meinel.com-20100108205546-44sf89j4c0o4s25j
parent: john at arbash-meinel.com-20100107203038-yf4zpl6ikobnlznf
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: update_copyright
timestamp: Fri 2010-01-08 14:55:46 -0600
message:
  Add an --only-changed flag.
  
  This was a feature request that I ended up liking.
  Basically, it means that you can set the code up, such
  that you can auto-update files, but only the ones that
  you are actually working on. Rather than all files.
  
  The next thing will be a start_commit hook that runs
  this automatically.
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2010-01-07 20:30:38 +0000
+++ b/__init__.py	2010-01-08 20:55:46 +0000
@@ -34,9 +34,12 @@
     """Update the Copyright header for files using the bzr metadata."""
 
     takes_args = ['path*']
-    takes_options = []
+    takes_options = [option.Option('only-changed',
+                        help='Only update the copyright for files which are'
+                             ' marked as modified.'),
+                    ]
 
-    def run(self, path_list=None):
+    def run(self, path_list=None, only_changed=False):
         from bzrlib import builtins, osutils
         import update_copyright
 
@@ -46,7 +49,8 @@
             relpaths = None
         if relpaths is not None:
             relpaths = osutils.minimum_path_selection(relpaths)
-        result = update_copyright.update_tree_copyright(tree, relpaths)
+        result = update_copyright.update_tree_copyright(tree, relpaths,
+                only_changed=only_changed)
         self.outf.write('Checked %(count)d files\n'
                         'Updated %(updated)d\n'
                         'Already correct %(copyright-correct)d\n'

=== modified file 'test_update_copyright.py'
--- a/test_update_copyright.py	2010-01-07 20:08:50 +0000
+++ b/test_update_copyright.py	2010-01-08 20:55:46 +0000
@@ -197,6 +197,25 @@
                           'copyright-correct': 0, 'not-versioned': 0},
                          res)
 
+    def test_update_tree_only_changed(self):
+        t, rev_ids = self.make_tree_with_dirs()
+        # Set 'dir/file' as modified, but leave the other files alone.
+        # Passing only_changed should mean that the other files do not get
+        # modified
+        self.build_tree_contents([
+            ('dir/file', 'Copyright 2008 Foo\r\nmodified content\r\n')])
+        res = update_copyright.update_tree_copyright(t, None, only_changed=True)
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright (c) 2008 Foo Bar\n'
+                             'different content\n', 'file')
+        self.assertFileEqual('Copyright 2008, 2009, %d Foo\r\n'
+                             'modified content\r\n' % (year,), 'dir/file')
+        self.assertFileEqual('Copyright 2008 Bar\n'
+                             'content\n', 'dir/subdir/foo')
+        self.assertEqual({'count': 1, 'updated': 1, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
+
     def test_blackbox(self):
         t, rev_ids = self.make_old_modified_tree()
         # We need to unlock so that run_bzr can grab a lock
@@ -209,3 +228,24 @@
         year = datetime.datetime.now().year
         self.assertFileEqual('Copyright (c) 2008, 2009, %d Foo Bar\n'
                              'different content\n' % (year,), 'file')
+
+    def test_blackbox_only_changed(self):
+        t, rev_ids = self.make_tree_with_dirs()
+        # Set 'dir/file' as modified, but leave the other files alone.
+        # Passing only_changed should mean that the other files do not get
+        # modified
+        self.build_tree_contents([
+            ('dir/file', 'Copyright 2008 Foo\r\nmodified content\r\n')])
+        t.unlock()
+        try:
+            self.run_bzr('update-copyright --only-changed')
+        finally:
+            # leave the tree locked, because we have pending cleanup
+            t.lock_read()
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright (c) 2008 Foo Bar\n'
+                             'different content\n', 'file')
+        self.assertFileEqual('Copyright 2008, 2009, %d Foo\r\n'
+                             'modified content\r\n' % (year,), 'dir/file')
+        self.assertFileEqual('Copyright 2008 Bar\n'
+                             'content\n', 'dir/subdir/foo')

=== modified file 'update_copyright.py'
--- a/update_copyright.py	2010-01-07 20:30:38 +0000
+++ b/update_copyright.py	2010-01-08 20:55:46 +0000
@@ -54,7 +54,21 @@
     return ', '.join(map(str, sorted(years)))
 
 
-def update_tree_copyright(tree, relpaths):
+def _iter_all_files(tree, relpaths, basis_tree):
+    file_ids = tree.paths2ids(relpaths, trees=[basis_tree])
+    for path, ie in tree.iter_entries_by_dir(file_ids):
+        if ie.kind == 'file':
+            yield path
+
+
+def _iter_changed_files(tree, relpaths, basis_tree):
+    changes = tree.iter_changes(basis_tree, specific_files=relpaths)
+    for (file_id, path, content_change, versioned, _, _, kind, _) in changes:
+        if kind[1] == 'file' and versioned[1]:
+            yield path[1]
+
+
+def update_tree_copyright(tree, relpaths, only_changed=False):
     """Update all of the paths specified in the tree."""
     result = {'not-versioned': 0,
               'no-copyright': 0,
@@ -69,14 +83,16 @@
         try:
             pb = ui.ui_factory.nested_progress_bar()
             try:
-                file_ids = tree.paths2ids(relpaths, trees=[basis_tree])
-                for path, ie in tree.iter_entries_by_dir(file_ids):
-                    if ie.kind == 'file':
-                        result['count'] += 1
-                        action = update_copyright(tree, basis_tree, path)
-                        result[action] += 1
-                        pb.update('%s %s' % (action, path), result[action],
-                                  result['count'])
+                if only_changed:
+                    iterator = _iter_changed_files(tree, relpaths, basis_tree)
+                else:
+                    iterator = _iter_all_files(tree, relpaths, basis_tree)
+                for path in iterator:
+                    result['count'] += 1
+                    action = update_copyright(tree, basis_tree, path)
+                    result[action] += 1
+                    pb.update('%s %s' % (action, path), result[action],
+                              result['count'])
             finally:
                 pb.finished()
         finally:



More information about the bazaar-commits mailing list