server: Factor out _split_proto_line as a global.
authorDave Borowitz <dborowitz@google.com>
Sun, 8 Aug 2010 15:12:48 +0000 (17:12 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sun, 8 Aug 2010 15:12:48 +0000 (17:12 +0200)
dulwich/server.py
dulwich/tests/test_server.py

index caf5cf52ab6725f0b69b82d63e8858310d907427..28331e66f1d53de058b2e4e609863a8d0ff5f1c9 100644 (file)
@@ -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:
index 002f21dae443f4985b93d17dfed9a7718af6db55..e66ab7446045feb3bb237c076edf1aed55499566 100644 (file)
@@ -29,6 +29,7 @@ from dulwich.server import (
     Handler,
     MultiAckGraphWalkerImpl,
     MultiAckDetailedGraphWalkerImpl,
+    _split_proto_line,
     ProtocolGraphWalker,
     SingleAckGraphWalkerImpl,
     UploadPackHandler,
@@ -270,22 +271,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, {})