2
0
Эх сурвалжийг харах

Add dulwich.objectspec.parse_refspec.

Jelmer Vernooij 9 жил өмнө
parent
commit
4d9713c986

+ 1 - 0
NEWS

@@ -15,6 +15,7 @@
   * Add `porcelain.ls_remote` and `GitClient.get_refs`.
     (Michael Edgar)
   * Add `Repo.discover` method. (B. M. Corser)
+  * Add `dulwich.objectspec.parse_refspec`. (Jelmer Vernooij)
 
  BUG FIXES
 

+ 17 - 0
dulwich/objectspec.py

@@ -32,6 +32,23 @@ def parse_object(repo, objectish):
     return repo[objectish]
 
 
+def parse_refspec(container, refspec):
+    """Parse a string referring to an reference.
+
+    :param container: A RefsContainer object
+    :param refspec: A string referring to a ref
+    :return: A ref
+    :raise KeyError: If the ref can not be found
+    """
+    if getattr(refspec, "encode", None) is not None:
+        refspec = refspec.encode('ascii')
+    for ref in [refspec, b"refs/heads/" + refspec]:
+        if ref in container:
+            return ref
+    else:
+        raise KeyError(refspec)
+
+
 def parse_commit_range(repo, committishs):
     """Parse a string referring to a range of commits.
 

+ 6 - 6
dulwich/porcelain.py

@@ -519,13 +519,13 @@ def reset(repo, mode, committish="HEAD"):
         r.reset_index()
 
 
-def push(repo, remote_location, refs_path,
+def push(repo, remote_location, refspec,
          outstream=sys.stdout, errstream=sys.stderr):
     """Remote push with dulwich via dulwich.client
 
     :param repo: Path to repository
     :param remote_location: Location of the remote
-    :param refs_path: relative path to the refs to push to remote
+    :param refspec: relative path to the refs to push to remote
     :param outstream: A stream file to write output
     :param errstream: A stream file to write errors
     """
@@ -538,7 +538,7 @@ def push(repo, remote_location, refs_path,
 
         def update_refs(refs):
             new_refs = r.get_refs()
-            refs[refs_path] = new_refs[b'HEAD']
+            refs[refspec] = new_refs[b'HEAD']
             del new_refs[b'HEAD']
             return refs
 
@@ -552,13 +552,13 @@ def push(repo, remote_location, refs_path,
             errstream.write(b"Push to " + remote_location_bytes + b" failed -> " + e.message.encode(err_encoding) + b"\n")
 
 
-def pull(repo, remote_location, refs_path,
+def pull(repo, remote_location, refspec,
          outstream=sys.stdout, errstream=sys.stderr):
     """Pull from remote via dulwich.client
 
     :param repo: Path to repository
     :param remote_location: Location of the remote
-    :param refs_path: relative path to the fetched refs
+    :param refspec: relative path to the fetched refs
     :param outstream: A stream file to write to output
     :param errstream: A stream file to write to errors
     """
@@ -567,7 +567,7 @@ def pull(repo, remote_location, refs_path,
     with open_repo_closing(repo) as r:
         client, path = get_transport_and_path(remote_location)
         remote_refs = client.fetch(path, r, progress=errstream.write)
-        r[b'HEAD'] = remote_refs[refs_path]
+        r[b'HEAD'] = remote_refs[refspec]
 
         # Perform 'git checkout .' - syncs staged changes
         tree = r[b"HEAD"].tree

+ 16 - 0
dulwich/tests/test_objectspec.py

@@ -28,6 +28,7 @@ from dulwich.objects import (
 from dulwich.objectspec import (
     parse_object,
     parse_commit_range,
+    parse_refspec,
     )
 from dulwich.repo import MemoryRepo
 from dulwich.tests import (
@@ -64,3 +65,18 @@ class ParseCommitRangeTests(TestCase):
         c1, c2, c3 = build_commit_graph(r.object_store, [[1], [2, 1],
             [3, 1, 2]])
         self.assertEqual([c1], list(parse_commit_range(r, c1.id)))
+
+
+class ParseRefspecTests(TestCase):
+
+    def test_nonexistent(self):
+        r = {}
+        self.assertRaises(KeyError, parse_refspec, r, "thisdoesnotexist")
+
+    def test_head(self):
+        r = {"refs/heads/foo": "bla"}
+        self.assertEquals("refs/heads/foo", parse_refspec(r, "foo"))
+
+    def test_full(self):
+        r = {"refs/heads/foo": "bla"}
+        self.assertEquals("refs/heads/foo", parse_refspec(r, "refs/heads/foo"))