2 import sys, os, subprocess, errno, zlib
6 BLOBSIZE = 1 << (BLOBBITS-1)
8 WINDOWSIZE = 1 << (WINDOWBITS-1)
12 sys.stderr.write('%s\n' % s)
15 # FIXME: replace this with a not-stupid rolling checksum algorithm,
16 # such as the one used in rsync (Adler32?)
17 def stupidsum_add(old, drop, add):
18 return (((old<<1) | ((old>>31)&0xffffffff)) & 0xffffffff) ^ drop ^ add
23 for i in range(WINDOWSIZE):
24 sum = stupidsum_add(sum, 0, i%256)
26 for i in range(WINDOWSIZE*5):
27 sum = stupidsum_add(sum, i%256, i%256)
29 for i in range(WINDOWSIZE):
30 sum = stupidsum_add(sum, i%256, 0)
47 while count > 0 and self.list:
50 out.append(self.list[0])
51 self.list = self.list[1:]
54 out.append(self.list[0][:n])
55 self.list[0] = self.list[0][n:]
65 #return buf.get(BLOBSIZE)
66 window = [0] * WINDOWSIZE
74 sum = stupidsum_add(sum, window[i], b)
76 i = (i + 1) % WINDOWSIZE
77 if (sum & (BLOBSIZE-1)) == ((~0) & (BLOBSIZE-1)):
83 header = 'blob %d\0' % len(blob)
87 dir = '.git/objects/%s' % hex[0:2]
88 fn = '%s/%s' % (dir, hex[2:])
92 if e.errno != errno.EEXIST:
94 if not os.path.exists(fn):
95 #log('creating %s' % fn)
96 tfn = '%s.%d' % (fn, os.getpid())
98 z = zlib.compressobj(1)
99 f.write(z.compress(header))
100 f.write(z.compress(blob))
105 #log('exists %s' % fn)
118 while blob or not eof:
119 if not eof and (buf.used() < BLOBSIZE*2 or not blob):
120 bnew = sys.stdin.read(BLOBSIZE*4)
121 if not len(bnew): eof = 1
122 #log('got %d, total %d' % (len(bnew), buf.used()))
127 blob = buf.get(buf.used())
128 if not blob and buf.used() >= BLOBSIZE*8:
129 blob = buf.get(BLOBSIZE*4) # limit max blob size
130 if not blob and not eof:
135 #log('SPLIT @ %-8d size=%-8d (%d/%d)'
136 # % (ofs, len(blob), BLOBSIZE, WINDOWSIZE))
139 nv = (ofs + buf.used())/1000000
145 assert(WINDOWSIZE >= 32)
146 assert(BLOBSIZE >= 32)