Browse Source

Split out parsing/formatting.

Jelmer Vernooij 4 years ago
parent
commit
ab6d63110d
1 changed files with 13 additions and 5 deletions
  1. 13 5
      dulwich/protocol.py

+ 13 - 5
dulwich/protocol.py

@@ -150,6 +150,17 @@ class ProtocolFile(object):
         pass
 
 
+def format_cmd_pkt(cmd, *args):
+    return cmd + b" " + b"".join([(a + b"\0") for a in args])
+
+
+def parse_cmd_pkt(line):
+    splice_at = line.find(b" ")
+    cmd, args = line[:splice_at], line[splice_at+1:]
+    assert args[-1:] == b"\x00"
+    return cmd, args[:-1].split(b"\0")
+
+
 def pkt_line(data):
     """Wrap data in a pkt-line.
 
@@ -327,7 +338,7 @@ class Protocol(object):
           cmd: The remote service to access.
           args: List of arguments to send to remove service.
         """
-        self.write_pkt_line(cmd + b" " + b"".join([(a + b"\0") for a in args]))
+        self.write_pkt_line(format_cmd_pkt(cmd, *args))
 
     def read_cmd(self):
         """Read a command and some arguments from the git client
@@ -337,10 +348,7 @@ class Protocol(object):
         Returns: A tuple of (command, [list of arguments]).
         """
         line = self.read_pkt_line()
-        splice_at = line.find(b" ")
-        cmd, args = line[:splice_at], line[splice_at+1:]
-        assert args[-1:] == b"\x00"
-        return cmd, args[:-1].split(b"\0")
+        return parse_cmd_pkt(line)
 
 
 _RBUFSIZE = 8192  # Default read buffer size.