diff options
author | Sascha Silbe <sascha-org-sugar-git@silbe.org> | 2009-06-22 09:18:00 (GMT) |
---|---|---|
committer | Sascha Silbe <sascha-org-sugar-git@silbe.org> | 2009-06-22 09:18:00 (GMT) |
commit | 6f44217f7e5c17e23e99f8fb5f5660e89e21a236 (patch) | |
tree | aa49771b7a3009f3cd728788425c197271b77556 | |
parent | 7c81d00ea55f25457756f090b24325238a139012 (diff) | |
parent | 9b4dcbfa9cfa31db212bdd4fa62878f628cf073b (diff) |
Merge branch 'master' of gitorious@git.sugarlabs.org:versionsupport-project/mainline
-rw-r--r-- | backends/git.py | 47 |
1 files changed, 18 insertions, 29 deletions
diff --git a/backends/git.py b/backends/git.py index 5af75fb..5bf16a4 100644 --- a/backends/git.py +++ b/backends/git.py @@ -8,10 +8,16 @@ import subprocess # TODO: PERF: use hardlinking / reduce copying, including ownership transfer class git : - def __init__(self, repodir, tmpdir) : - # TODO: add name of first branch + def __init__(self, repodir, tmpdir, firstBranch='first') : + """ + Creates repository (in <repodir>) if it does not exist yet. + Temporary files will be created inside <tmpdir>. + <firstBranch> must be a valid branch name (see createBranch()) and is the name of the initial branch + for new entries. + """ self._base = repodir self._tmpdir = tmpdir + self._firstBranch = 'first' self._env = dict(os.environ) self._env['HOME'] = self._base self._globalInit() @@ -26,38 +32,19 @@ class git : entryDir = os.path.join(self._base, name) os.makedirs(entryDir) self._runGit(["init"], cwd=entryDir) - # TODO: use self.commit() (need master special casing first) - destPath = os.path.join(entryDir, name) - if isinstance(content, str) : - # path - if os.path.isdir(content) : - shutil.copytree(content, destPath, symlinks=True) - else : - shutil.copyfile(content, destPath) + if (self._firstBranch != "master") : + self._runGit(["branch", "-m", "master", self._firstBranch], cwd=entryDir) - else : - # file-like object - destF=file(destPath, "w") - shutil.copyfileobj(content, destF) - destF.close() - - self._runGit(["add", name], cwd=entryDir) - self._runGit(["commit", "-m", "create repo"], cwd=entryDir) - # TODO: PERF: special case branch name first -> master - self._runGit(["branch", "first"], cwd=entryDir) - self._runGit(["checkout", "first"], cwd=entryDir) - self._runGit(["gc"], cwd=entryDir) - # TODO: PERF: parse commit output instead - return self.getVersions(name, "first")[0] + return self._commit(name, self._firstBranch, content, message="create repo", runCheckout=False) : def getBranches(self, name) : """ Return a list of branch names. """ entryDir = os.path.join(self._base, name) - return [line.split(" ")[-1] - for line in self._runGit(["branch"], cwd=entryDir).split("\n") - if line not in ['',' master']] + return [line + for line in self._runGit(["branch"], cwd=entryDir).split("\n").lstrip("*").strip() + if line] def getVersions(self, name, branch) : """ @@ -85,7 +72,7 @@ class git : else : shutil.copyfile(sourcePath, dest) - def commit(self, name, branch, content, message=None) : + def commit(self, name, branch, content, message=None, runCheckout=True) : """ Create new version in given branch with content given by <content>. <content> must either be a valid path or a file-like object. @@ -93,7 +80,9 @@ class git : Returns: version id """ entryDir = os.path.join(self._base, name) - self._runGit(["checkout", branch], cwd=entryDir) + if runCheckout : + self._runGit(["checkout", branch], cwd=entryDir) + destPath = os.path.join(entryDir, name) if isinstance(content, str) : if os.path.isdir(destPath) : |