Quellcode durchsuchen

Fix order of parameters to Tree.add.

Jelmer Vernooij vor 14 Jahren
Ursprung
Commit
ea4e3748af

+ 6 - 0
NEWS

@@ -16,6 +16,12 @@
 
   * Sphinxified documentation. (Lukasz Balcerzak)
 
+ API CHANGES
+
+  * The order of the parameters to Tree.add(name, mode, sha) has changed, and
+    is now consistent with the rest of Dulwich. Existing code will still
+    work but print a DeprecationWarning. (Jelmer Vernooij, #663550)
+
 0.7.0	2011-01-21
 
  FEATURES

+ 1 - 1
docs/tutorial/object-store.txt

@@ -27,7 +27,7 @@ give this content a name::
 
   >>> from dulwich.objects import Tree
   >>> tree = Tree()
-  >>> tree.add(0100644, "spam", blob.id)
+  >>> tree.add("spam", 0100644, blob.id)
 
 Note that "0100644" is the octal form for a regular file with common
 permissions. You can hardcode them or you can use the ``stat`` module.

+ 1 - 1
dulwich/index.py

@@ -330,7 +330,7 @@ def commit_tree(object_store, blobs):
                 sha = build_tree(pathjoin(path, basename))
             else:
                 (mode, sha) = entry
-            tree.add(mode, basename, sha)
+            tree.add(basename, mode, sha)
         object_store.add_object(tree)
         return tree.id
     return build_tree("")

+ 8 - 3
dulwich/objects.py

@@ -27,6 +27,7 @@ from cStringIO import (
 import os
 import posixpath
 import stat
+import warnings
 import zlib
 
 from dulwich.errors import (
@@ -819,14 +820,18 @@ class Tree(ShaFile):
         self._ensure_parsed()
         return iter(self._entries)
 
-    def add(self, mode, name, hexsha):
+    def add(self, name, mode, hexsha):
         """Add an entry to the tree.
 
-        :param mode: The mode of the entry as an integral type. Not all possible
-            modes are supported by git; see check() for details.
+        :param mode: The mode of the entry as an integral type. Not all 
+            possible modes are supported by git; see check() for details.
         :param name: The name of the entry, as a string.
         :param hexsha: The hex SHA of the entry as a string.
         """
+        if type(name) is int and type(mode) is str:
+            (name, mode) = (mode, name)
+            warnings.warn("Please use Tree.add(name, mode, hexsha)",
+                category=DeprecationWarning, stacklevel=2)
         self._ensure_parsed()
         self._entries[name] = mode, hexsha
         self._needs_serialization = True

+ 1 - 1
dulwich/tests/test_fastexport.py

@@ -61,7 +61,7 @@ class GitFastExporterTests(TestCase):
         b = Blob()
         b.data = "FOO"
         t = Tree()
-        t.add(stat.S_IFREG | 0644, "foo", b.id)
+        t.add("foo", stat.S_IFREG | 0644, b.id)
         c = Commit()
         c.committer = c.author = "Jelmer <jelmer@host>"
         c.author_time = c.commit_time = 1271345553

+ 21 - 0
dulwich/tests/test_objects.py

@@ -26,6 +26,7 @@ from cStringIO import StringIO
 import datetime
 import os
 import stat
+import warnings
 
 from dulwich.errors import (
     ObjectFormatException,
@@ -404,6 +405,26 @@ _SORTED_TREE_ITEMS = [
 
 class TreeTests(ShaFileCheckTests):
 
+    def test_add(self):
+        myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
+        x = Tree()
+        x.add("myname", 0100755, myhexsha)
+        self.assertEquals(x["myname"], (0100755, myhexsha))
+        self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
+                x.as_raw_string())
+
+    def test_add_old_order(self):
+        myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
+        x = Tree()
+        warnings.simplefilter("ignore", DeprecationWarning)
+        try:
+            x.add(0100755, "myname", myhexsha)
+        finally:
+            warnings.resetwarnings()
+        self.assertEquals(x["myname"], (0100755, myhexsha))
+        self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
+                x.as_raw_string())
+
     def test_simple(self):
         myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
         x = Tree()

+ 6 - 6
dulwich/tests/test_patch.py

@@ -256,13 +256,13 @@ class DiffTests(TestCase):
         changed2 = Blob.from_string("unchanged\nadded\n")
         unchanged = Blob.from_string("unchanged\n")
         tree1 = Tree()
-        tree1.add(0644, "removed.txt", removed.id)
-        tree1.add(0644, "changed.txt", changed1.id)
-        tree1.add(0644, "unchanged.txt", changed1.id)
+        tree1.add("removed.txt", 0644, removed.id)
+        tree1.add("changed.txt", 0644, changed1.id)
+        tree1.add("unchanged.txt", 0644, changed1.id)
         tree2 = Tree()
-        tree2.add(0644, "added.txt", added.id)
-        tree2.add(0644, "changed.txt", changed2.id)
-        tree2.add(0644, "unchanged.txt", changed1.id)
+        tree2.add("added.txt", 0644, added.id)
+        tree2.add("changed.txt", 0644, changed2.id)
+        tree2.add("unchanged.txt", 0644, changed1.id)
         store.add_objects([(o, None) for o in [
             tree1, tree2, added, removed, changed1, changed2, unchanged]])
         write_tree_diff(f, store, tree1.id, tree2.id)