Quellcode durchsuchen

Factor out more formatting functions.

Jelmer Vernooij vor 2 Jahren
Ursprung
Commit
f5f98c539d
2 geänderte Dateien mit 24 neuen und 6 gelöschten Zeilen
  1. 16 0
      dulwich/protocol.py
  2. 8 6
      dulwich/server.py

+ 16 - 0
dulwich/protocol.py

@@ -109,6 +109,8 @@ KNOWN_RECEIVE_CAPABILITIES = set(
 
 DEPTH_INFINITE = 0x7FFFFFFF
 
+NAK_LINE = b"NAK\n"
+
 
 def agent_string():
     return ("dulwich/%d.%d.%d" % dulwich.__version__).encode("ascii")
@@ -599,3 +601,17 @@ def format_ref_line(ref, sha, capabilities=None):
             sha + b" " + ref + b"\0"
             + format_capability_line(capabilities)
             + b"\n")
+
+
+def format_shallow_line(sha):
+    return COMMAND_SHALLOW + b" " + sha
+
+
+def format_unshallow_line(sha):
+    return COMMAND_UNSHALLOW + b" " + sha
+
+
+def format_ack_line(sha, ack_type=b""):
+    if ack_type:
+        ack_type = b" " + ack_type
+    return b"ACK " + sha + ack_type + b"\n"

+ 8 - 6
dulwich/server.py

@@ -109,6 +109,10 @@ from dulwich.protocol import (
     extract_want_line_capabilities,
     symref_capabilities,
     format_ref_line,
+    format_shallow_line,
+    format_unshallow_line,
+    format_ack_line,
+    NAK_LINE,
 )
 from dulwich.refs import (
     ANNOTATED_TAG_SUFFIX,
@@ -704,9 +708,9 @@ class _ProtocolGraphWalker(object):
         unshallow = self.unshallow = not_shallow & self.client_shallow
 
         for sha in sorted(new_shallow):
-            self.proto.write_pkt_line(COMMAND_SHALLOW + b" " + sha)
+            self.proto.write_pkt_line(format_shallow_line(sha))
         for sha in sorted(unshallow):
-            self.proto.write_pkt_line(COMMAND_UNSHALLOW + b" " + sha)
+            self.proto.write_pkt_line(format_unshallow_line(sha))
 
         self.proto.write_pkt_line(None)
 
@@ -715,12 +719,10 @@ class _ProtocolGraphWalker(object):
         self.handler.notify_done()
 
     def send_ack(self, sha, ack_type=b""):
-        if ack_type:
-            ack_type = b" " + ack_type
-        self.proto.write_pkt_line(b"ACK " + sha + ack_type + b"\n")
+        self.proto.write_pkt_line(format_ack_line(sha, ack_type))
 
     def send_nak(self):
-        self.proto.write_pkt_line(b"NAK\n")
+        self.proto.write_pkt_line(NAK_LINE)
 
     def handle_done(self, done_required, done_received):
         # Delegate this to the implementation.