1 import os, errno, zlib, time, sha, subprocess
6 header = '%s %d\0' % (type, len(s))
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)
17 if e.errno != errno.EEXIST:
19 tfn = '%s.%d' % (fn, os.getpid())
21 z = zlib.compressobj(1)
22 f.write(z.compress(header))
23 f.write(z.compress(s))
28 #log('exists %s' % fn)
34 return hash_raw('blob', blob)
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))
44 return time.strftime('%s %z', time.localtime(date))
48 os.environ['GIT_DIR'] = os.path.abspath(repo)
51 def _read_ref(repo, refname):
52 p = subprocess.Popen(['git', 'show-ref', '--', refname],
53 preexec_fn = lambda: _gitenv(repo),
54 stdout = subprocess.PIPE)
55 out = p.stdout.read().strip()
63 def _update_ref(repo, refname, newval, oldval):
66 p = subprocess.Popen(['git', 'update-ref', '--', refname, newval, oldval],
67 preexec_fn = lambda: _gitenv(repo))
72 def gen_commit(tree, parent, author, adate, committer, cdate, msg):
74 if tree: l.append('tree %s' % tree)
75 if parent: l.append('parent %s' % parent)
76 if author: l.append('author %s %s' % (author, _git_date(adate)))
77 if committer: l.append('committer %s %s' % (committer, _git_date(cdate)))
80 return hash_raw('commit', '\n'.join(l))
83 def gen_commit_easy(ref, tree, msg):
85 userline = '%s <%s@%s>' % (userfullname(), username(), hostname())
86 oldref = ref and _read_ref('.git', ref) or None
87 commit = gen_commit(tree, oldref, userline, now, userline, now, msg)
89 _update_ref('.git', ref, commit, oldref)