1 import os, errno, zlib, time, sha, subprocess, struct, mmap, stat, re
5 home_repodir = os.path.expanduser('~/.bup')
9 class GitError(Exception):
16 raise GitError('You should call check_repo_or_die()')
17 gd = os.path.join(repodir, '.git')
18 if os.path.exists(gd):
20 return os.path.join(repodir, sub)
24 def __init__(self, filename):
27 self.map = mmap.mmap(f.fileno(), 0,
28 mmap.MAP_SHARED, mmap.PROT_READ)
29 f.close() # map will persist beyond file close
30 assert(str(self.map[0:8]) == '\377tOc\0\0\0\2')
31 self.fanout = list(struct.unpack('!256I',
32 str(buffer(self.map, 8, 256*4))))
33 self.fanout.append(0) # entry "-1"
34 nsha = self.fanout[255]
35 self.ofstable = buffer(self.map,
36 8 + 256*4 + nsha*20 + nsha*4,
38 self.ofs64table = buffer(self.map,
39 8 + 256*4 + nsha*20 + nsha*4 + nsha*4)
41 def _ofs_from_idx(self, idx):
42 ofs = struct.unpack('!I', str(buffer(self.ofstable, idx*4, 4)))[0]
44 idx64 = ofs & 0x7fffffff
45 ofs = struct.unpack('!I',
46 str(buffer(self.ofs64table, idx64*8, 8)))[0]
49 def _idx_from_hash(self, hash):
50 assert(len(hash) == 20)
52 start = self.fanout[b1-1] # range -1..254
53 end = self.fanout[b1] # range 0..255
54 buf = buffer(self.map, 8 + 256*4, end*20)
57 mid = start + (end-start)/2
58 v = buffer(buf, mid*20, 20)
67 def find_offset(self, hash):
68 idx = self._idx_from_hash(hash)
70 return self._ofs_from_idx(idx)
73 def exists(self, hash):
74 return (self._idx_from_hash(hash) != None) and True or None
78 def __init__(self, dir):
81 for f in os.listdir(dir):
82 if f.endswith('.idx'):
83 self.packs.append(PackIndex(os.path.join(dir, f)))
85 def exists(self, hash):
88 for i in range(len(self.packs)):
91 # reorder so most recently used packs are searched first
92 self.packs = [p] + self.packs[:i] + self.packs[i+1:]
103 def calc_hash(type, content):
104 header = '%s %d\0' % (type, len(content))
105 sum = sha.sha(header)
110 def _shalist_sort_key(ent):
111 (mode, name, id) = ent
112 if stat.S_ISDIR(int(mode, 8)):
118 _typemap = dict(blob=3, tree=2, commit=1, tag=8)
120 def __init__(self, objcache=None):
124 self.objcache = objcache or MultiPackIndex(repo('objects/pack'))
130 assert(not self.file)
131 self.objcache.zap_also()
132 self.filename = repo('objects/bup%d' % os.getpid())
133 self.file = open(self.filename + '.pack', 'w+')
134 self.file.write('PACK\0\0\0\2\0\0\0\0')
136 def _raw_write(self, datalist):
144 def _write(self, bin, type, content):
151 szbits = (sz & 0x0f) | (_typemap[type]<<4)
154 if sz: szbits |= 0x80
155 out.append(chr(szbits))
161 z = zlib.compressobj(1)
162 out.append(z.compress(content))
163 out.append(z.flush())
168 def write(self, type, content):
169 return self._write(calc_hash(type, content), type, content)
171 def maybe_write(self, type, content):
172 bin = calc_hash(type, content)
173 if not self.objcache.exists(bin):
174 self._write(bin, type, content)
175 self.objcache.add(bin)
178 def new_blob(self, blob):
179 return self.maybe_write('blob', blob)
181 def new_tree(self, shalist):
182 shalist = sorted(shalist, key = _shalist_sort_key)
183 l = ['%s %s\0%s' % (mode,name,bin)
184 for (mode,name,bin) in shalist]
185 return self.maybe_write('tree', ''.join(l))
187 def _new_commit(self, tree, parent, author, adate, committer, cdate, msg):
189 if tree: l.append('tree %s' % tree.encode('hex'))
190 if parent: l.append('parent %s' % parent.encode('hex'))
191 if author: l.append('author %s %s' % (author, _git_date(adate)))
192 if committer: l.append('committer %s %s' % (committer, _git_date(cdate)))
195 return self.maybe_write('commit', '\n'.join(l))
197 def new_commit(self, parent, tree, msg):
199 userline = '%s <%s@%s>' % (userfullname(), username(), hostname())
200 commit = self._new_commit(tree, parent,
201 userline, now, userline, now,
210 os.unlink(self.filename + '.pack')
214 if not f: return None
217 # update object count
219 cp = struct.pack('!i', self.count)
223 # calculate the pack sha1sum
230 f.write(sum.digest())
234 p = subprocess.Popen(['git', 'index-pack', '-v',
236 self.filename + '.pack'],
237 preexec_fn = _gitenv,
238 stdout = subprocess.PIPE)
239 out = p.stdout.read().strip()
240 _git_wait('git index-pack', p)
242 raise GitError('git index-pack produced no output')
243 nameprefix = repo('objects/pack/%s' % out)
244 os.rename(self.filename + '.pack', nameprefix + '.pack')
245 os.rename(self.filename + '.idx', nameprefix + '.idx')
249 class PackWriter_Remote(PackWriter):
250 def __init__(self, conn, objcache=None, onclose=None):
251 PackWriter.__init__(self, objcache)
253 self.filename = 'remote socket'
254 self.onclose = onclose
257 assert(not "can't reopen a PackWriter_Remote")
261 self.file.write('\0\0\0\0')
267 raise GitError("don't know how to abort remote pack writing")
269 def _raw_write(self, datalist):
271 data = ''.join(datalist)
273 self.file.write(struct.pack('!I', len(data)) + data)
277 return time.strftime('%s %z', time.localtime(date))
281 os.environ['GIT_DIR'] = os.path.abspath(repo())
284 def read_ref(refname):
285 p = subprocess.Popen(['git', 'show-ref', '--', refname],
286 preexec_fn = _gitenv,
287 stdout = subprocess.PIPE)
288 out = p.stdout.read().strip()
289 rv = p.wait() # not fatal
293 return out.split()[0].decode('hex')
298 def update_ref(refname, newval, oldval):
301 p = subprocess.Popen(['git', 'update-ref', '--', refname,
302 newval.encode('hex'), oldval.encode('hex')],
303 preexec_fn = _gitenv)
304 _git_wait('git update-ref', p)
307 def guess_repo(path=None):
312 repodir = os.environ.get('BUP_DIR')
314 repodir = os.path.expanduser('~/.bup')
317 def init_repo(path=None):
320 if os.path.exists(d) and not os.path.isdir(os.path.join(d, '.')):
321 raise GitError('"%d" exists but is not a directory\n' % d)
322 p = subprocess.Popen(['git', '--bare', 'init'], stdout=sys.stderr,
323 preexec_fn = _gitenv)
324 _git_wait('git init', p)
325 p = subprocess.Popen(['git', 'config', 'pack.indexVersion', '2'],
326 stdout=sys.stderr, preexec_fn = _gitenv)
327 _git_wait('git config', p)
330 def check_repo_or_die(path=None):
332 if not os.path.isdir(repo('objects/pack/.')):
333 if repodir == home_repodir:
336 log('error: %r is not a bup/git repository\n' % repo())
342 while ofs < len(buf):
343 z = buf[ofs:].find('\0')
345 spl = buf[ofs:ofs+z].split(' ', 1)
346 assert(len(spl) == 2)
347 sha = buf[ofs+z+1:ofs+z+1+20]
349 yield (spl[0], spl[1], sha)
355 p = subprocess.Popen(['git', '--version'],
356 stdout=subprocess.PIPE)
357 gvs = p.stdout.read()
358 _git_wait('git --version', p)
359 m = re.match(r'git version (\S+.\S+)', gvs)
361 raise GitError('git --version weird output: %r' % gvs)
362 _ver = tuple(m.group(1).split('.'))
363 needed = ('1','5','4')
365 raise GitError('git version %s or higher is required; you have %s'
366 % ('.'.join(needed), '.'.join(_ver)))
370 def _git_wait(cmd, p):
373 raise GitError('%s returned %d' % (cmd, rv))
376 def _git_capture(argv):
377 p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn = _gitenv)
379 _git_wait(repr(argv), p)
387 wanted = ('1','5','6')
390 log('warning: git version < %s; bup will be slow.\n'
393 self.get = self._slow_get
395 self.p = subprocess.Popen(['git', 'cat-file', '--batch'],
396 stdin=subprocess.PIPE,
397 stdout=subprocess.PIPE,
398 preexec_fn = _gitenv)
399 self.get = self._fast_get
401 def _fast_get(self, id):
402 assert(id.find('\n') < 0)
403 assert(id.find('\r') < 0)
405 self.p.stdin.write('%s\n' % id)
406 hdr = self.p.stdout.readline()
408 assert(len(spl) == 3)
409 assert(len(spl[0]) == 40)
410 (hex, type, size) = spl
412 for blob in chunkyreader(self.p.stdout, int(spl[2])):
414 assert(self.p.stdout.readline() == '\n')
416 def _slow_get(self, id):
417 assert(id.find('\n') < 0)
418 assert(id.find('\r') < 0)
420 type = _git_capture(['git', 'cat-file', '-t', id]).strip()
423 p = subprocess.Popen(['git', 'cat-file', type, id],
424 stdout=subprocess.PIPE,
425 preexec_fn = _gitenv)
426 for blob in chunkyreader(p.stdout):
428 _git_wait('git cat-file', p)
436 treefile = ''.join(it)
437 for (mode, name, sha) in _treeparse(treefile):
438 for blob in self.join(sha.encode('hex')):
440 elif type == 'commit':
441 treeline = ''.join(it).split('\n')[0]
442 assert(treeline.startswith('tree '))
443 for blob in self.join(treeline[5:]):
446 raise GitError('unknown object type %r' % type)
449 for d in self._join(self.get(id)):