浏览代码

Allow Commit objects to be parsed by parse_commit()

Jelmer Vernooij 1 月之前
父节点
当前提交
68012b9bcb
共有 2 个文件被更改,包括 12 次插入2 次删除
  1. 6 2
      dulwich/objectspec.py
  2. 6 0
      tests/test_objectspec.py

+ 6 - 2
dulwich/objectspec.py

@@ -234,12 +234,12 @@ def scan_for_short_id(object_store, prefix, tp):
     raise AmbiguousShortId(prefix, ret)
 
 
-def parse_commit(repo: "Repo", committish: Union[str, bytes]) -> "Commit":
+def parse_commit(repo: "Repo", committish: Union[str, bytes, Commit]) -> "Commit":
     """Parse a string referring to a single commit.
 
     Args:
       repo: A` Repo` object
-      committish: A string referring to a single commit.
+      committish: A string referring to a single commit, or a Commit object.
     Returns: A Commit object
     Raises:
       KeyError: When the reference commits can not be found
@@ -259,6 +259,10 @@ def parse_commit(repo: "Repo", committish: Union[str, bytes]) -> "Commit":
             raise ValueError(f"Expected commit, got {obj.type_name}")
         return obj
 
+    # If already a Commit object, return it directly
+    if isinstance(committish, Commit):
+        return committish
+
     committish = to_bytes(committish)
     try:
         obj = repo[committish]

+ 6 - 0
tests/test_objectspec.py

@@ -160,6 +160,12 @@ class ParseCommitTests(TestCase):
         # Should raise ValueError as it's not a commit
         self.assertRaises(ValueError, parse_commit, r, tag.id)
 
+    def test_commit_object(self) -> None:
+        r = MemoryRepo()
+        [c1] = build_commit_graph(r.object_store, [[1]])
+        # Test that passing a Commit object directly returns the same object
+        self.assertEqual(c1, parse_commit(r, c1))
+
 
 class ParseRefTests(TestCase):
     def test_nonexistent(self) -> None: