]> arthur.barton.de Git - bup.git/blob - git.py
Add a '-t' option to 'bup split' and make 'bup join' support it.
[bup.git] / git.py
1 import os, errno, zlib
2 from sha import sha
3
4
5 def hash_raw(type, s):
6     header = '%s %d\0' % (type, len(s))
7     sum = sha(header)
8     sum.update(s)
9     hex = sum.hexdigest()
10     dir = '.git/objects/%s' % hex[0:2]
11     fn = '%s/%s' % (dir, hex[2:])
12     if not os.path.exists(fn):
13         #log('creating %s' % fn)
14         try:
15             os.mkdir(dir)
16         except OSError, e:
17             if e.errno != errno.EEXIST:
18                 raise
19         tfn = '%s.%d' % (fn, os.getpid())
20         f = open(tfn, 'w')
21         z = zlib.compressobj(1)
22         f.write(z.compress(header))
23         f.write(z.compress(s))
24         f.write(z.flush())
25         f.close()
26         os.rename(tfn, fn)
27     else:
28         #log('exists %s' % fn)
29         pass
30     return hex
31
32
33 def hash_blob(blob):
34     return hash_raw('blob', blob)
35
36
37 def gen_tree(shalist):
38     l = ['%s %s\0%s' % (mode,name,hex.decode('hex')) 
39          for (mode,name,hex) in shalist]
40     return hash_raw('tree', ''.join(l))