1 import os, errno, zlib, time, sha, subprocess, struct, mmap, stat
5 repodir = os.environ.get('BUP_DIR', '.git')
9 gd = os.path.join(repodir, '.git')
10 if os.path.exists(gd):
12 return os.path.join(repodir, sub)
16 def __init__(self, filename):
19 self.map = mmap.mmap(f.fileno(), 0,
20 mmap.MAP_SHARED, mmap.PROT_READ)
21 f.close() # map will persist beyond file close
22 assert(str(self.map[0:8]) == '\377tOc\0\0\0\2')
23 self.fanout = list(struct.unpack('!256I',
24 str(buffer(self.map, 8, 256*4))))
25 self.fanout.append(0) # entry "-1"
26 nsha = self.fanout[255]
27 self.ofstable = buffer(self.map,
28 8 + 256*4 + nsha*20 + nsha*4,
30 self.ofs64table = buffer(self.map,
31 8 + 256*4 + nsha*20 + nsha*4 + nsha*4)
33 def _ofs_from_idx(self, idx):
34 ofs = struct.unpack('!I', str(buffer(self.ofstable, idx*4, 4)))[0]
36 idx64 = ofs & 0x7fffffff
37 ofs = struct.unpack('!I',
38 str(buffer(self.ofs64table, idx64*8, 8)))[0]
41 def _idx_from_hash(self, hash):
42 assert(len(hash) == 20)
44 start = self.fanout[b1-1] # range -1..254
45 end = self.fanout[b1] # range 0..255
46 buf = buffer(self.map, 8 + 256*4, end*20)
49 mid = start + (end-start)/2
50 v = buffer(buf, mid*20, 20)
59 def find_offset(self, hash):
60 idx = self._idx_from_hash(hash)
62 return self._ofs_from_idx(idx)
65 def exists(self, hash):
66 return (self._idx_from_hash(hash) != None) and True or None
70 def __init__(self, dir):
73 for f in os.listdir(dir):
74 if f.endswith('.idx'):
75 self.packs.append(PackIndex(os.path.join(dir, f)))
77 def exists(self, hash):
80 for i in range(len(self.packs)):
83 # reorder so most recently used packs are searched first
84 self.packs = [p] + self.packs[:i] + self.packs[i+1:]
95 def calc_hash(type, content):
96 header = '%s %d\0' % (type, len(content))
102 def _shalist_sort_key(ent):
103 (mode, name, id) = ent
104 if stat.S_ISDIR(int(mode, 8)):
110 _typemap = dict(blob=3, tree=2, commit=1, tag=8)
112 def __init__(self, objcache=None):
116 self.objcache = objcache or MultiPackIndex(repo('objects/pack'))
122 assert(not self.file)
123 self.objcache.zap_also()
124 self.filename = repo('objects/bup%d' % os.getpid())
125 self.file = open(self.filename + '.pack', 'w+')
126 self.file.write('PACK\0\0\0\2\0\0\0\0')
128 def _raw_write(self, datalist):
136 def _write(self, bin, type, content):
143 szbits = (sz & 0x0f) | (_typemap[type]<<4)
146 if sz: szbits |= 0x80
147 out.append(chr(szbits))
153 z = zlib.compressobj(1)
154 out.append(z.compress(content))
155 out.append(z.flush())
160 def write(self, type, content):
161 return self._write(calc_hash(type, content), type, content)
163 def maybe_write(self, type, content):
164 bin = calc_hash(type, content)
165 if not self.objcache.exists(bin):
166 self._write(bin, type, content)
167 self.objcache.add(bin)
170 def new_blob(self, blob):
171 return self.maybe_write('blob', blob)
173 def new_tree(self, shalist):
174 shalist = sorted(shalist, key = _shalist_sort_key)
175 l = ['%s %s\0%s' % (mode,name,bin)
176 for (mode,name,bin) in shalist]
177 return self.maybe_write('tree', ''.join(l))
179 def _new_commit(self, tree, parent, author, adate, committer, cdate, msg):
181 if tree: l.append('tree %s' % tree.encode('hex'))
182 if parent: l.append('parent %s' % parent)
183 if author: l.append('author %s %s' % (author, _git_date(adate)))
184 if committer: l.append('committer %s %s' % (committer, _git_date(cdate)))
187 return self.maybe_write('commit', '\n'.join(l))
189 def new_commit(self, ref, tree, msg):
191 userline = '%s <%s@%s>' % (userfullname(), username(), hostname())
192 oldref = ref and _read_ref(ref) or None
193 commit = self._new_commit(tree, oldref,
194 userline, now, userline, now,
197 self.close() # UGLY: needed so _update_ref can see the new objects
198 _update_ref(ref, commit.encode('hex'), oldref)
206 os.unlink(self.filename + '.pack')
210 if not f: return None
213 # update object count
215 cp = struct.pack('!i', self.count)
219 # calculate the pack sha1sum
226 f.write(sum.digest())
230 p = subprocess.Popen(['git', 'index-pack', '-v',
231 self.filename + '.pack'],
232 preexec_fn = _gitenv,
233 stdout = subprocess.PIPE)
234 out = p.stdout.read().strip()
235 if p.wait() or not out:
236 raise Exception('git index-pack returned an error')
237 nameprefix = repo('objects/pack/%s' % out)
238 os.rename(self.filename + '.pack', nameprefix + '.pack')
239 os.rename(self.filename + '.idx', nameprefix + '.idx')
243 class PackWriter_Remote(PackWriter):
244 def __init__(self, conn, objcache=None):
245 PackWriter.__init__(self, objcache)
247 self.filename = 'remote socket'
250 assert(not "can't reopen a PackWriter_Remote")
254 self.file.write('\0\0\0\0')
257 def _raw_write(self, datalist):
259 data = ''.join(datalist)
261 self.file.write(struct.pack('!I', len(data)) + data)
265 return time.strftime('%s %z', time.localtime(date))
269 os.environ['GIT_DIR'] = os.path.abspath(repo())
272 def _read_ref(refname):
273 p = subprocess.Popen(['git', 'show-ref', '--', refname],
274 preexec_fn = _gitenv,
275 stdout = subprocess.PIPE)
276 out = p.stdout.read().strip()
279 return out.split()[0]
284 def _update_ref(refname, newval, oldval):
287 p = subprocess.Popen(['git', 'update-ref', '--', refname, newval, oldval],
288 preexec_fn = _gitenv)
295 if os.path.exists(d) and not os.path.isdir(os.path.join(d, '.')):
296 raise Exception('"%d" exists but is not a directory\n' % d)
297 p = subprocess.Popen(['git', 'init', '--bare'],
298 preexec_fn = _gitenv)
302 def check_repo_or_die():
303 if not os.path.isdir(repo('objects/pack/.')):
304 log('error: %r is not a bup/git repository\n' % repo())