2 from bup import git, _hashsplit
3 from bup.helpers import *
9 progress_callback = None
10 max_pack_size = 1000*1000*1000 # larger packs will slow down pruning
11 max_pack_objects = 200*1000 # cache memory usage is about 83 bytes per object
21 self.data = buffer(self.data, self.start) + s
24 def peek(self, count):
25 return buffer(self.data, self.start, count)
31 v = buffer(self.data, self.start, count)
36 return len(self.data) - self.start
40 b = buf.peek(buf.used())
41 (ofs, bits) = _hashsplit.splitbuf(b)
44 return (buffer(b, 0, ofs), bits)
57 def drainbuf(buf, finalize):
59 (blob, bits) = splitbuf(buf)
64 if buf.used() > BLOB_MAX:
66 yield (buf.get(buf.used()), 0)
67 elif finalize and buf.used():
68 yield (buf.get(buf.used()), 0)
71 def hashsplit_iter(files):
72 assert(BLOB_HWM > BLOB_MAX)
76 for i in drainbuf(buf, finalize=False):
78 while buf.used() < BLOB_HWM:
82 for i in drainbuf(buf, finalize=True):
89 def _split_to_blobs(w, files):
91 for (blob, bits) in hashsplit_iter(files):
92 sha = w.new_blob(blob)
93 total_split += len(blob)
94 if w.outbytes >= max_pack_size or w.count >= max_pack_objects:
97 progress_callback(len(blob))
98 yield (sha, len(blob), bits)
101 def _make_shalist(l):
104 for (mode, sha, size) in l:
105 shalist.append((mode, '%016x' % ofs, sha))
108 return (shalist, total)
111 def _squish(w, stacks, n):
113 while i<n or len(stacks[i]) > MAX_PER_TREE:
114 while len(stacks) <= i+1:
116 if len(stacks[i]) == 1:
117 stacks[i+1] += stacks[i]
119 (shalist, size) = _make_shalist(stacks[i])
120 tree = w.new_tree(shalist)
121 stacks[i+1].append(('40000', tree, size))
126 def split_to_shalist(w, files):
127 sl = _split_to_blobs(w, files)
130 for (sha,size,bits) in sl:
131 shal.append(('100644', sha, size))
132 return _make_shalist(shal)[0]
134 base_bits = _hashsplit.blobbits()
135 fanout_bits = int(math.log(fanout, 2))
137 assert(n >= base_bits)
138 return (n - base_bits)/fanout_bits
140 for (sha,size,bits) in sl:
142 stacks[0].append(('100644', sha, size))
144 _squish(w, stacks, bits_to_idx(bits))
145 #log('stacks: %r\n' % [len(i) for i in stacks])
146 _squish(w, stacks, len(stacks)-1)
147 #log('stacks: %r\n' % [len(i) for i in stacks])
148 return _make_shalist(stacks[-1])[0]
151 def split_to_blob_or_tree(w, files):
152 shalist = list(split_to_shalist(w, files))
153 if len(shalist) == 1:
154 return (shalist[0][0], shalist[0][2])
155 elif len(shalist) == 0:
156 return ('100644', w.new_blob(''))
158 return ('40000', w.new_tree(shalist))
161 def open_noatime(name):
162 fd = _hashsplit.open_noatime(name)
164 return os.fdopen(fd, 'rb', 1024*1024)