]> arthur.barton.de Git - bup.git/blob - git.py
'bup split' now has a -c option to generate a git commit object.
[bup.git] / git.py
1 import os, errno, zlib, time, sha
2
3
4 def hash_raw(type, s):
5     header = '%s %d\0' % (type, len(s))
6     sum = sha.sha(header)
7     sum.update(s)
8     hex = sum.hexdigest()
9     dir = '.git/objects/%s' % hex[0:2]
10     fn = '%s/%s' % (dir, hex[2:])
11     if not os.path.exists(fn):
12         #log('creating %s' % fn)
13         try:
14             os.mkdir(dir)
15         except OSError, e:
16             if e.errno != errno.EEXIST:
17                 raise
18         tfn = '%s.%d' % (fn, os.getpid())
19         f = open(tfn, 'w')
20         z = zlib.compressobj(1)
21         f.write(z.compress(header))
22         f.write(z.compress(s))
23         f.write(z.flush())
24         f.close()
25         os.rename(tfn, fn)
26     else:
27         #log('exists %s' % fn)
28         pass
29     return hex
30
31
32 def hash_blob(blob):
33     return hash_raw('blob', blob)
34
35
36 def gen_tree(shalist):
37     l = ['%s %s\0%s' % (mode,name,hex.decode('hex')) 
38          for (mode,name,hex) in shalist]
39     return hash_raw('tree', ''.join(l))
40
41
42 def _git_date(date):
43     return time.strftime('%s %z', time.localtime(date))
44
45
46 def gen_commit(tree, parent, author, adate, committer, cdate, msg):
47     l = []
48     if tree: l.append('tree %s' % tree)
49     if parent: l.append('parent %s' % parent)
50     if author: l.append('author %s %s' % (author, _git_date(adate)))
51     if committer: l.append('committer %s %s' % (committer, _git_date(cdate)))
52     l.append('')
53     l.append(msg)
54     return hash_raw('commit', '\n'.join(l))