← Back to team overview

dulwich-users team mailing list archive

[PATCH 06/10] server: Factor out _split_proto_line as a global.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: Ibf08abd557852ed95a839e2cf927a23c42859488
---
 dulwich/server.py            |   58 +++++++++++++++++++++++------------------
 dulwich/tests/test_server.py |   27 ++++++++-----------
 2 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/dulwich/server.py b/dulwich/server.py
index caf5cf5..28331e6 100644
--- a/dulwich/server.py
+++ b/dulwich/server.py
@@ -276,6 +276,33 @@ class UploadPackHandler(Handler):
         self.proto.write("0000")
 
 
+def _split_proto_line(line):
+    """Split a line read from the wire.
+
+    :return: a tuple having one of the following forms:
+        ('want', obj_id)
+        ('have', obj_id)
+        ('done', None)
+        (None, None)  (for a flush-pkt)
+
+    :raise GitProtocolError: if the line cannot be parsed into one of the
+        possible return values.
+    """
+    if not line:
+        fields = [None]
+    else:
+        fields = line.rstrip('\n').split(' ', 1)
+    if len(fields) == 1 and fields[0] in ('done', None):
+        return (fields[0], None)
+    elif len(fields) == 2 and fields[0] in ('want', 'have'):
+        try:
+            hex_to_sha(fields[1])
+            return tuple(fields)
+        except (TypeError, AssertionError), e:
+            raise GitProtocolError(e)
+    raise GitProtocolError('Received invalid line from client:\n%s' % line)
+
+
 class ProtocolGraphWalker(object):
     """A graph walker that knows the git protocol.
 
@@ -340,7 +367,7 @@ class ProtocolGraphWalker(object):
         line, caps = extract_want_line_capabilities(want)
         self.handler.set_client_capabilities(caps)
         self.set_ack_type(ack_type(caps))
-        command, sha = self._split_proto_line(line)
+        command, sha = _split_proto_line(line)
 
         want_revs = []
         while command != None:
@@ -373,34 +400,13 @@ class ProtocolGraphWalker(object):
             return None
         return self._cache[self._cache_index]
 
-    def _split_proto_line(self, line):
-        fields = line.rstrip('\n').split(' ', 1)
-        if len(fields) == 1 and fields[0] == 'done':
-            return ('done', None)
-        elif len(fields) == 2 and fields[0] in ('want', 'have'):
-            try:
-                hex_to_sha(fields[1])
-                return tuple(fields)
-            except (TypeError, AssertionError), e:
-                raise GitProtocolError(e)
-        raise GitProtocolError('Received invalid line from client:\n%s' % line)
-
     def read_proto_line(self):
-        """Read a line from the wire.
-
-        :return: a tuple having one of the following forms:
-            ('want', obj_id)
-            ('have', obj_id)
-            ('done', None)
-            (None, None)  (for a flush-pkt)
+        """Read and split a line from the wire.
 
-        :raise GitProtocolError: if the line cannot be parsed into one of the
-            possible return values.
+        :return: A tuple of (command, value); see _split_proto_line.
+        :raise GitProtocolError: If an error occurred reading the line.
         """
-        line = self.proto.read_pkt_line()
-        if not line:
-            return (None, None)
-        return self._split_proto_line(line)
+        return _split_proto_line(self.proto.read_pkt_line())
 
     def send_ack(self, sha, ack_type=''):
         if ack_type:
diff --git a/dulwich/tests/test_server.py b/dulwich/tests/test_server.py
index d018cd1..e322797 100644
--- a/dulwich/tests/test_server.py
+++ b/dulwich/tests/test_server.py
@@ -32,6 +32,7 @@ from dulwich.server import (
     Handler,
     MultiAckGraphWalkerImpl,
     MultiAckDetailedGraphWalkerImpl,
+    _split_proto_line,
     ProtocolGraphWalker,
     SingleAckGraphWalkerImpl,
     UploadPackHandler,
@@ -240,22 +241,16 @@ class ProtocolGraphWalkerTestCase(TestCase):
         self.assertFalse(self._walker.all_wants_satisfied([THREE]))
         self.assertTrue(self._walker.all_wants_satisfied([TWO, THREE]))
 
-    def test_read_proto_line(self):
-        self._walker.proto.set_output([
-          'want %s' % ONE,
-          'want %s' % TWO,
-          'have %s' % THREE,
-          'foo %s' % FOUR,
-          'bar',
-          'done',
-          ])
-        self.assertEquals(('want', ONE), self._walker.read_proto_line())
-        self.assertEquals(('want', TWO), self._walker.read_proto_line())
-        self.assertEquals(('have', THREE), self._walker.read_proto_line())
-        self.assertRaises(GitProtocolError, self._walker.read_proto_line)
-        self.assertRaises(GitProtocolError, self._walker.read_proto_line)
-        self.assertEquals(('done', None), self._walker.read_proto_line())
-        self.assertEquals((None, None), self._walker.read_proto_line())
+    def test_split_proto_line(self):
+        self.assertEquals(('want', ONE), _split_proto_line('want %s\n' % ONE))
+        self.assertEquals(('want', TWO), _split_proto_line('want %s\n' % TWO))
+        self.assertEquals(('have', THREE),
+                          _split_proto_line('have %s\n' % THREE))
+        self.assertRaises(GitProtocolError,
+                          _split_proto_line, 'foo %s\n' % FOUR)
+        self.assertRaises(GitProtocolError, _split_proto_line, 'bar')
+        self.assertEquals(('done', None), _split_proto_line('done\n'))
+        self.assertEquals((None, None), _split_proto_line(''))
 
     def test_determine_wants(self):
         self.assertRaises(GitProtocolError, self._walker.determine_wants, {})
-- 
1.7.1




References