Browse Source

Add `dulwich.objectspec`.

Jelmer Vernooij 11 years ago
parent
commit
08640c1f73
4 changed files with 116 additions and 0 deletions
  1. 3 0
      NEWS
  2. 42 0
      dulwich/objectspec.py
  3. 1 0
      dulwich/tests/__init__.py
  4. 70 0
      dulwich/tests/test_objectspec.py

+ 3 - 0
NEWS

@@ -4,6 +4,9 @@
 
  * Add porcelain 'tag'. (Ryan Faulkner)
 
+ * New module `dulwich.objectspec` for parsing strings referencing
+   objects and commit ranges. (Jelmer Vernooij)
+
  CHANGES
 
  * Drop support for Python 2.4 and 2.5. (Jelmer Vernooij)

+ 42 - 0
dulwich/objectspec.py

@@ -0,0 +1,42 @@
+# objectspec.py -- Object specification
+# Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License or (at your option) a later version of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.
+
+"""Object specification."""
+
+
+def parse_object(repo, objectish):
+    """Parse a string referring to an object.
+
+    :param repo: A `Repo` object
+    :param objectish: A string referring to an object
+    :return: A git object
+    :raise KeyError: If the object can not be found
+    """
+    return repo[objectish]
+
+
+def parse_commit_range(repo, committishs):
+    """Parse a string referring to a range of commits.
+
+    :param repo: A `Repo` object
+    :param committishs: A string referring to a range of commits.
+    :return: An iterator over `Commit` objects
+    :raise KeyError: When the reference commits can not be found
+    :raise ValueError: If the range can not be parsed
+    """
+    return iter([repo[committishs]])

+ 1 - 0
dulwich/tests/__init__.py

@@ -122,6 +122,7 @@ def self_test_suite():
         'index',
         'lru_cache',
         'objects',
+        'objectspec',
         'object_store',
         'missing_obj_finder',
         'pack',

+ 70 - 0
dulwich/tests/test_objectspec.py

@@ -0,0 +1,70 @@
+# test_objectspec.py -- tests for objectspec.py
+# Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License or (at your option) any later version of
+# the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.
+
+"""Tests for revision spec parsing."""
+
+# TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
+
+
+from dulwich.objects import (
+    Blob,
+    Commit,
+    Tag,
+    Tree,
+    )
+from dulwich.objectspec import (
+    parse_object,
+    parse_commit_range,
+    )
+from dulwich.repo import MemoryRepo
+from dulwich.tests import (
+    TestCase,
+    )
+from dulwich.tests.utils import (
+    build_commit_graph,
+    make_object,
+    )
+
+
+class ParseObjectTests(TestCase):
+    """Test parse_object."""
+
+    def test_nonexistent(self):
+        r = MemoryRepo()
+        self.assertRaises(KeyError, parse_object, r, "thisdoesnotexist")
+
+    def test_blob_by_sha(self):
+        r = MemoryRepo()
+        b = Blob.from_string("Blah")
+        r.object_store.add_object(b)
+        self.assertEquals(b, parse_object(r, b.id))
+
+
+class ParseCommitRangeTests(TestCase):
+    """Test parse_commit_range."""
+
+    def test_nonexistent(self):
+        r = MemoryRepo()
+        self.assertRaises(KeyError, parse_commit_range, r, "thisdoesnotexist")
+
+    def test_commit_by_sha(self):
+        r = MemoryRepo()
+        c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1],
+            [3, 1, 2]])
+        self.assertEquals([c1], list(parse_commit_range(r, c1.id)))