diff options
author | Guido Günther <agx@sigxcpu.org> | 2011-11-20 14:10:41 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2011-11-21 20:52:30 +0100 |
commit | 2881b9bc1555d5d9fa45aed390a4204704a073b2 (patch) | |
tree | 37b7b56a7dcb6d3eab73473ae491b719cd31ce84 /gbp/git | |
parent | 4aa178022def247834f7ecfc45b5defbe9270d09 (diff) |
FastImport: add test and cleanup api a bit
Diffstat (limited to 'gbp/git')
-rw-r--r-- | gbp/git/fastimport.py | 101 |
1 files changed, 77 insertions, 24 deletions
diff --git a/gbp/git/fastimport.py b/gbp/git/fastimport.py index 4093e208..435d5e0e 100644 --- a/gbp/git/fastimport.py +++ b/gbp/git/fastimport.py @@ -18,24 +18,32 @@ """Git fast import class""" import subprocess +import time from gbp.errors import GbpError class FastImport(object): - """Invoke git-fast-import""" + """Add data to a git repository using I{git fast-import}""" _bufsize = 1024 m_regular = 644 m_exec = 755 m_symlink = 120000 - def __init__(self): + def __init__(self, repo): + """ + @param repo: the git repository L{FastImport} acts on + @type repo: L{GitRepository} + """ + self._repo = repo try: - self._fi = subprocess.Popen([ 'git', 'fast-import', '--quiet'], stdin=subprocess.PIPE) + self._fi = subprocess.Popen([ 'git', 'fast-import', '--quiet'], + stdin=subprocess.PIPE, cwd=repo.path) self._out = self._fi.stdin except OSError as err: raise GbpError("Error spawning git fast-import: %s" % err) except ValueError as err: - raise GbpError("Invalid argument when spawning git fast-import: %s" % 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) @@ -51,31 +59,78 @@ class FastImport(object): 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_file(self, filename, fd, size, mode=m_regular): + """ + Add a file + + @param filename: the name of the file to add + @type filename: C{str} + @param fd: stream to read data from + @type fd: C{File} like object + @param size: size of the file to add + @type size: C{int} + @param mode: file mode, default is L{FastImport.m_regular}. + @type mode: C{int} + """ + self._do_file(filename, mode, fd, size) + + def add_symlink(self, linkname, linktarget): + """ + Add a symlink + + @param linkname: the symbolic link's name + @param linkname: C{str} + @param linktarget: the target the symlink points to + @type linktarget: C{str} + """ + self._out.write("M %d inline %s\n" % (self.m_symlink, linkname)) + self._out.write("data %s\n" % len(linktarget)) + self._out.write("%s\n" % linktarget) + + def start_commit(self, branch, committer, msg): + """ + Start a fast import commit + + @param branch: branch to commit on + @type branch: C{str} + @param committer: the committer information + @type committer: L{GitModifier} + @param msg: the commit message + @type msg: C{str} + """ + length = len(msg) + if not committer.date: + committer.date = "%d %s" % (time.time(), + time.strftime("%z")) - 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) + if self._repo.has_branch(branch): + from_ = "from refs/heads/%(branch)s^0\n" + else: + from_ = '' - 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 +committer %(name)s <%(email)s> %(time)s data %(length)s -%(msg)s -from refs/heads/%(branch)s^0 -""" % locals()) - - def do_deleteall(self): +%(msg)s%(from)s""" % + { 'branch': branch, + 'name': committer.name, + 'email': committer.email, + 'time': committer.date, + 'length': length, + 'msg': msg, + 'from': from_, + }) + + def deleteall(self): + """ + Issue I{deleteall} to fastimport so we start from a empty tree + """ self._out.write("deleteall\n") def close(self): + """ + Close fast-import issuing all pending actions + """ if self._out: self._out.close() if self._fi: @@ -83,5 +138,3 @@ from refs/heads/%(branch)s^0 def __del__(self): self.close() - - |