2 import sys, os, subprocess, errno, zlib, time
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)
112 start_time = time.time()
119 while blob or not eof:
120 if not eof and (buf.used() < BLOBSIZE*2 or not blob):
121 bnew = sys.stdin.read(BLOBSIZE*4)
122 if not len(bnew): eof = 1
123 #log('got %d, total %d' % (len(bnew), buf.used()))
128 blob = buf.get(buf.used())
129 if not blob and buf.used() >= BLOBSIZE*8:
130 blob = buf.get(BLOBSIZE*4) # limit max blob size
131 if not blob and not eof:
136 #log('SPLIT @ %-8d size=%-8d (%d/%d)'
137 # % (ofs, len(blob), BLOBSIZE, WINDOWSIZE))
140 nv = (ofs + buf.used())/1000000
144 secs = time.time() - start_time
145 log('\n%.2fkbytes in %.2f secs = %.2f kbytes/sec'
146 % (ofs/1024., secs, ofs/1024./secs))
149 assert(WINDOWSIZE >= 32)
150 assert(BLOBSIZE >= 32)