Rev 8: Handle newly added files. in http://bzr.arbash-meinel.com/plugins/update_copyright

John Arbash Meinel john at arbash-meinel.com
Fri Jan 8 21:36:51 GMT 2010


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

------------------------------------------------------------
revno: 8
revision-id: john at arbash-meinel.com-20100108213639-ucpnweq2au3xwiah
parent: john at arbash-meinel.com-20100108211907-surg3wfcko2f09a5
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: update_copyright
timestamp: Fri 2010-01-08 15:36:39 -0600
message:
  Handle newly added files.
  
  Also handle files whose copyright would be correct according to committed
  data, but now we have modified the file locally, and thus it needs
  this years datestamp. (only currently active for only_changed since
  otherwise we need to do a comparison check across every file in the tree.)
-------------- next part --------------
=== modified file 'test_update_copyright.py'
--- a/test_update_copyright.py	2010-01-08 21:19:07 +0000
+++ b/test_update_copyright.py	2010-01-08 21:36:39 +0000
@@ -170,6 +170,27 @@
         self.assertFileEqual('Copyright (c) 2008, 2009 Foo Bar\n'
                              'different content\n', 'file')
 
+    def test_update_copyright_correct_except_changed(self):
+        t, rev_ids = self.make_tree_with_dirs_modified()
+        # According to the committed data, this file is already correct.
+        # However it also needs to be marked for *this* year because it is
+        # modified versus the committed content
+        bt = t.basis_tree()
+        bt.lock_read()
+        self.addCleanup(bt.unlock)
+        self.build_tree_contents([
+            ('dir/file', 'Copyright 2008, 2009 Foo\r\nmodified content\r\n')])
+        res = update_copyright.update_copyright(t, bt, 'dir/file')
+        self.assertEqual('copyright-correct', res)
+        self.assertFileEqual('Copyright 2008, 2009 Foo\r\n'
+                             'modified content\r\n', 'dir/file')
+        res = update_copyright.update_copyright(t, bt, 'dir/file',
+                                                is_changed=True)
+        self.assertEqual('updated', res)
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright 2008, 2009, %d Foo\r\n'
+                             'modified content\r\n' % (year,), 'dir/file')
+
     def test_update_tree_one(self):
         t, rev_ids = self.make_old_modified_tree()
         res = update_copyright.update_tree_copyright(t, ['file'])
@@ -220,8 +241,32 @@
     def test_update_tree_only_changed(self):
         t, rev_ids = self.make_tree_with_dirs_modified()
         res = update_copyright.update_tree_copyright(t, None, only_changed=True)
+        self.check_tree_with_dirs_modified()
+        self.assertEqual({'count': 1, 'updated': 1, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
+
+    def test_update_tree_only_changed_new_file(self):
+        t, rev_ids = self.make_tree_with_dirs_modified()
+        self.build_tree_contents([('file2', 'Copyright 1700 Fooz\ncontent\n')])
+        t.add(['file2'])
+        res = update_copyright.update_tree_copyright(t, None, only_changed=True)
         year = datetime.datetime.now().year
         self.check_tree_with_dirs_modified()
+        self.assertEqual({'count': 2, 'updated': 2, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
+        self.assertFileEqual('Copyright %d Fooz\ncontent\n' % (year,), 'file2')
+
+    def test_update_tree_only_changed_correct_but_modified(self):
+        t, rev_ids = self.make_tree_with_dirs_modified()
+        # According to the committed data, this file is already correct.
+        # However it also needs to be marked for *this* year because it is
+        # modified versus the committed content
+        self.build_tree_contents([
+            ('dir/file', 'Copyright 2008, 2009 Foo\r\nmodified content\r\n')])
+        res = update_copyright.update_tree_copyright(t, None, only_changed=True)
+        self.check_tree_with_dirs_modified()
         self.assertEqual({'count': 1, 'updated': 1, 'no-copyright': 0,
                           'copyright-correct': 0, 'not-versioned': 0},
                          res)

=== modified file 'update_copyright.py'
--- a/update_copyright.py	2010-01-08 21:19:07 +0000
+++ b/update_copyright.py	2010-01-08 21:36:39 +0000
@@ -93,7 +93,10 @@
                     iterator = _iter_all_files(tree, relpaths, basis_tree)
                 for path in iterator:
                     result['count'] += 1
-                    action = update_copyright(tree, basis_tree, path)
+                    # If we are iterating changed files only, then we know this
+                    # file is changed
+                    action = update_copyright(tree, basis_tree, path,
+                                              is_changed=only_changed)
                     result[action] += 1
                     pb.update('%s %s' % (action, path), result[action],
                               result['count'])
@@ -106,8 +109,13 @@
     return result
 
 
-def update_copyright(tree, basis_tree, filename):
-    """Update the copyright line for a given file."""
+def update_copyright(tree, basis_tree, filename, is_changed=False):
+    """Update the copyright line for a given file.
+    
+    :param is_changed: If we know the file is changed, then we know it must
+        have this years date in the Copyright header. If it is unknown if the
+        file has changed, just pass False.
+    """
     file_id = tree.path2id(filename)
     if file_id is None:
         return 'not-versioned'
@@ -125,8 +133,16 @@
         #       as having a modification including today's year
         #       Also note that running this will modify the file, causing it to
         #       be marked as modified in the current year...
-        last_mod_revision = basis_tree.inventory[file_id].revision
-        years = get_years(tree.branch.repository, file_id, last_mod_revision)
+        if basis_tree.has_id(file_id):
+            last_mod_revision = basis_tree.inventory[file_id].revision
+            years = get_years(tree.branch.repository, file_id,
+                              last_mod_revision)
+            if is_changed:
+                years.add(this_year)
+        else:
+            # Regardless of the 'is_changed' flag, we know this file must be
+            # changed, because it didn't exist in the basis_tree
+            years = set([this_year])
         new_copyright_line = '%s %s %s\n' % (
             m.group('prefix').rstrip(), format_years(years), m.group('suffix'))
         if copyright_line == new_copyright_line:



More information about the bazaar-commits mailing list