summaryrefslogtreecommitdiff
path: root/gbp/git.py
diff options
context:
space:
mode:
Diffstat (limited to 'gbp/git.py')
-rw-r--r--gbp/git.py67
1 files changed, 66 insertions, 1 deletions
diff --git a/gbp/git.py b/gbp/git.py
index 6533b456..60914885 100644
--- a/gbp/git.py
+++ b/gbp/git.py
@@ -1,7 +1,7 @@
# vim: set fileencoding=utf-8 :
#
# (C) 2006,2007,2008 Guido Guenther <agx@sigxcpu.org>
-"""provides some git repository related helpers"""
+"""provides git repository related helpers"""
import subprocess
import os.path
@@ -222,6 +222,71 @@ class GitRepository(object):
return (name, email)
+class FastImport(object):
+ """Invoke git-fast-import"""
+ _bufsize = 1024
+
+ m_regular = 644
+ m_exec = 755
+ m_symlink = 120000
+
+ def __init__(self):
+ try:
+ self._fi = subprocess.Popen([ 'git', 'fast-import', '--quiet'], stdin=subprocess.PIPE)
+ self._out = self._fi.stdin
+ except OSError, err:
+ raise GbpError, "Error spawning git fast-import: %s", err
+ except ValueError, err:
+ raise GbpError, "Invalid argument when spawning git fast-import: %s", err
+
+ def _do_data(self, fd, size):
+ self._out.write("data %s\n" % size)
+ while True:
+ data = fd.read(self._bufsize)
+ self._out.write(data)
+ if len(data) != self._bufsize:
+ break
+ self._out.write("\n")
+
+ def _do_file(self, filename, mode, fd, size):
+ name = "/".join(filename.split('/')[1:])
+ self._out.write("M %d inline %s\n" % (mode, name))
+ self._do_data(fd, size)
+
+ def add_file(self, filename, fd, size):
+ self._do_file(filename, self.m_regular, fd, size)
+
+ def add_executable(self, filename, fd, size):
+ self._do_file(filename, self.m_exec, fd, size)
+
+ def add_symlink(self, filename, linkname):
+ name = "/".join(filename.split('/')[1:])
+ self._out.write("M %d inline %s\n" % (self.m_symlink, name))
+ self._out.write("data %s\n" % len(linkname))
+ self._out.write("%s\n" % linkname)
+
+ def start_commit(self, branch, committer, email, time, msg):
+ length = len(msg)
+ self._out.write("""commit refs/heads/%(branch)s
+committer %(committer)s <%(email)s> %(time)s
+data %(length)s
+%(msg)s
+from refs/heads/%(branch)s^0
+""" % locals())
+
+ def do_deleteall(self):
+ self._out.write("deleteall\n")
+
+ def close(self):
+ if self._out:
+ self._out.close()
+ if self._fi:
+ self._fi.wait()
+
+ def __del__(self):
+ self.close()
+
+
def create_repo(path):
"""create a repository at path"""
abspath = os.path.abspath(path)