← Back to team overview

dulwich-users team mailing list archive

[PATCH 2/7] Factor out a function to convert a line to a pkt-line.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I11bd6c47a2434d371d7e15560fae94268cba260a
---
 NEWS                |    2 ++
 dulwich/protocol.py |   24 ++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index 350255b..1f815ff 100644
--- a/NEWS
+++ b/NEWS
@@ -89,6 +89,8 @@
 
   * Factor out _report_status in ReceivePackHandler. (Dave Borowitz)
 
+  * Factor out a function to convert a line to a pkt-line. (Dave Borowitz)
+
 
 0.6.0	2010-05-22
 
diff --git a/dulwich/protocol.py b/dulwich/protocol.py
index 8bd7842..5a5b6b8 100644
--- a/dulwich/protocol.py
+++ b/dulwich/protocol.py
@@ -57,6 +57,18 @@ class ProtocolFile(object):
         pass
 
 
+def pkt_line(data):
+    """Wrap data in a pkt-line.
+
+    :param data: The data to wrap, as a str or None.
+    :return: The data prefixed with its length in pkt-line format; if data was
+        None, returns the flush-pkt ('0000')
+    """
+    if data is None:
+        return '0000'
+    return '%04x%s' % (len(data) + 4, data)
+
+
 class Protocol(object):
 
     def __init__(self, read, write, report_activity=None):
@@ -98,14 +110,10 @@ class Protocol(object):
         :param line: A string containing the data to send
         """
         try:
-            if line is None:
-                self.write("0000")
-                if self.report_activity:
-                    self.report_activity(4, 'write')
-            else:
-                self.write("%04x%s" % (len(line)+4, line))
-                if self.report_activity:
-                    self.report_activity(4+len(line), 'write')
+            line = pkt_line(line)
+            self.write(line)
+            if self.report_activity:
+                self.report_activity(len(line), 'write')
         except socket.error, e:
             raise GitProtocolError(e)
 
-- 
1.7.1




References