Commits

Andrew Godwin committed c8b6fcd

Working cheap diff

  • Participants
  • Parent commits b2fae95

Comments (0)

Files changed (1)

             return number_so_far
 
 
-def read_section(handle, current_length):
+def make_integer(integer):
+    "Encodes an integer in the svndiff variable-length format"
+    output = ""
+    
+    was_shift = False
+    while True:
+        # Encode the lower 7 bits, then shift
+        lower = integer & 127
+        integer >>= 7
+        # Is there more?
+        if was_shift:
+            lower |= 128
+        output = chr(lower) + output
+        # If there was more, remember that
+        if integer:
+            was_shift = True
+        # If not, we're done
+        else:
+            return output
+
+
+def read_section(handle, current_length, use_zlib):
     "Reads a section from the handle and possibly decompresses it."
-    offset_before = handle.tell()
-    original_length = read_integer(handle)
-    integer_length = handle.tell() - offset_before
+    if use_zlib:
+        offset_before = handle.tell()
+        original_length = read_integer(handle)
+        integer_length = handle.tell() - offset_before
+    else:
+        integer_length = 0
     # Read the data and possible decode it
     data = handle.read(current_length - integer_length)
     #print repr(data), len(data), original_length
     # If the length is different, zlib decode it
-    if len(data) != original_length:
+    if use_zlib and len(data) != original_length:
         data = zlib.decompress(data[1:])
     return data
 
     
     # Read the instructions and newdata into their own stringios.
     # First, get the "original length" header, and see how many bits it was
-    instruction_data = read_section(diff, instructions_length)
-    newdata_data = read_section(diff, newdata_length)
+    instruction_data = read_section(diff, instructions_length, use_zlib)
+    newdata_data = read_section(diff, newdata_length, use_zlib)
     
     instructions = StringIO(instruction_data)
     newdata = StringIO(newdata_data)
         else:
             raise ValueError("Unknown op")
         
-    return target.getvalue()
+    return target.getvalue()
+
+
+def make_cheap_diff(text):
+    "Makes a completely uncompressed svndiff of the given text."
+    
+    instructions = chr(OP_NEW_DATA << 6) + make_integer(len(text))
+    
+    return ("SVN\0" +
+           "\0" + # Source offset
+           "\0" + # Source length
+           make_integer(len(text)) + # Target length
+           make_integer(len(instructions)) + # Instructions length
+           make_integer(len(text)) + # New data length
+           instructions +
+           text)