From 49f81fa291ca925ec985dc52f51b9a37bb3106ee Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 13 Nov 2008 14:31:49 -0500 Subject: Fixed 0cad bug with smaller fix. Hubert Chathi's fix was confusing for me, so I made a simpler change. Seems to work so far. The problem was that os.path.dirname('filename') returns an empty string ('') if there are no directories in the filename. So when `git rev-parse --git-dir` returned '.git', os returned ''. Later programs didn't recognize '' as a valid directory and crashed. My fix returns '.' in this case, so we don't crash, and avoid having to use full paths. I'm not sure why I don't want to use full paths; they just give me bad vibes... --- libbe/git.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'libbe/git.py') diff --git a/libbe/git.py b/libbe/git.py index 398585f..5c377fd 100644 --- a/libbe/git.py +++ b/libbe/git.py @@ -102,7 +102,10 @@ def git_repo_for_path(path): """Find the root of the deepest repository containing path.""" # Assume that nothing funny is going on; in particular, that we aren't # dealing with a bare repo. - return os.path.dirname(git_dir_for_path(path)) + dirname = os.path.dirname(git_dir_for_path(path)) + if dirname == '' : # os.path.dirname('filename') == '' + dirname = '.' + return dirname def git_dir_for_path(path): """Find the git-dir of the deepest repo containing path.""" -- cgit From f6253f4c6ba301954a5b9beed4e5b41e74bb8004 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 13 Nov 2008 15:27:07 -0500 Subject: Oops... *Now* I've fixed 0cad --- libbe/git.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'libbe/git.py') diff --git a/libbe/git.py b/libbe/git.py index 5c377fd..172c324 100644 --- a/libbe/git.py +++ b/libbe/git.py @@ -22,7 +22,11 @@ def strip_git(filename): # Find the base path of the GIT tree, in order to strip that leading # path from arguments to git -- it doesn't like absolute paths. if os.path.isabs(filename): - filename = filename[len(git_repo_for_path('.'))+1:] + absRepoDir = os.path.abspath(git_repo_for_path('.')) + absRepoSlashedDir = os.path.join(absRepoDir,"") + assert filename.startswith(absRepoSlashedDir), \ + "file %s not in git repo %s" % (filename, absRepoSlashedDir) + filename = filename.lstrip(absRepoSlashedDir) return filename def invoke_client(*args, **kwargs): -- cgit From 2b071a28a2cedab54c713948c6b6f4bd27bb45e2 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 16 Nov 2008 15:37:14 -0500 Subject: Fixed another bug in git.strip_git() (bug 0cad). Also added git mode to test_usage.sh. I'll go through and add modes for the other RCSs... --- libbe/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbe/git.py') diff --git a/libbe/git.py b/libbe/git.py index 172c324..e15d773 100644 --- a/libbe/git.py +++ b/libbe/git.py @@ -26,7 +26,7 @@ def strip_git(filename): absRepoSlashedDir = os.path.join(absRepoDir,"") assert filename.startswith(absRepoSlashedDir), \ "file %s not in git repo %s" % (filename, absRepoSlashedDir) - filename = filename.lstrip(absRepoSlashedDir) + filename = filename[len(absRepoSlashedDir):] return filename def invoke_client(*args, **kwargs): -- cgit From 19b153b9a86377a2b30cc80fa3f475fed892e2fe Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 18 Nov 2008 20:42:50 -0500 Subject: Major rewrite of RCS backends. RCS now represented as a class. Lots of changes and just one commit. This started with bug dac91856-cb6a-4f69-8c03-38ff0b29aab2, when I noticed that new bugs were not being added appropriately with the Git backend. I'd been working with Git trouble before with bug 0cad2ac6-76ef-4a88-abdf-b2e02de76f5c, and decided things would be better off if I just scrapped the current RCS architecture and went to a more object oriented setup. So I did. It's not clear how to add support for an RCS backend: * Create a new module that - defines an inheritor of rsc.RCS, overriding the _rcs_*() methods - provide a new() function for instantizating the new class - defines an inheritor of rcs.RCStestCase, overiding the Class attribute - defines 'suite' a unittest.TestSuite testing the module * Add your new module to the rest in rcs._get_matching_rcs() * Add your new module to the rest in libbe/tests.py Although I'm not sure libbe/tests.py is still usefull. The new framework clears out a bunch of hackery that used to be involved with supporting becommands/diff.py. There's still room for progress though. While implementing the new verision, I moved the testing framework over from doctest to a doctest/unittest combination. Longer tests that don't demonstrate a function's usage should be moved to unittests at the end of the module, since unittest has better support for setup/teardown, etc. The new framework also revealed some underimplented backends, most notably arch. These backends have now been fixed. I also tweaked the test_usage.sh script to run through all the backends if it is called with no arguments. The fix for the dac bug turned out to be an unflushed file write :p. --- libbe/git.py | 218 ++++++++++++++++++++++------------------------------------- 1 file changed, 82 insertions(+), 136 deletions(-) (limited to 'libbe/git.py') diff --git a/libbe/git.py b/libbe/git.py index e15d773..046e72e 100644 --- a/libbe/git.py +++ b/libbe/git.py @@ -14,140 +14,86 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os -import tempfile - -from rcs import invoke - -def strip_git(filename): - # Find the base path of the GIT tree, in order to strip that leading - # path from arguments to git -- it doesn't like absolute paths. - if os.path.isabs(filename): - absRepoDir = os.path.abspath(git_repo_for_path('.')) - absRepoSlashedDir = os.path.join(absRepoDir,"") - assert filename.startswith(absRepoSlashedDir), \ - "file %s not in git repo %s" % (filename, absRepoSlashedDir) - filename = filename[len(absRepoSlashedDir):] - return filename - -def invoke_client(*args, **kwargs): - directory = kwargs['directory'] - expect = kwargs.get('expect', (0, 1)) - cl_args = ["git"] - cl_args.extend(args) - status,output,error = invoke(cl_args, expect, cwd=directory) - return status, output - -def add_id(filename, paranoid=False): - filename = strip_git(filename) - invoke_client("add", filename, directory=git_repo_for_path('.')) - -def delete_id(filename): - filename = strip_git(filename) - invoke_client("rm", filename, directory=git_repo_for_path('.')) - -def mkdir(path, paranoid=False): - os.mkdir(path) - -def set_file_contents(path, contents): - add = not os.path.exists(path) - file(path, "wb").write(contents) - if add: - add_id(path) - -def detect(path): - """Detect whether a directory is revision-controlled using GIT""" - path = os.path.realpath(path) - old_path = None - while True: - if os.path.exists(os.path.join(path, ".git")): +import re +import unittest +import doctest + +from rcs import RCS, RCStestCase, CommandError + +def new(): + return Git() + +class Git(RCS): + name="git" + client="git" + versioned=True + def _rcs_help(self): + status,output,error = self._u_invoke_client("--help") + return output + def _rcs_detect(self, path): + if self._u_search_parent_directories(path, ".git") != None : return True - if path == old_path: - return False - old_path = path - path = os.path.dirname(path) - -def precommit(directory): - pass - -def commit(directory, summary, body=None): - if body is not None: - summary += '\n' + body - descriptor, filename = tempfile.mkstemp() - try: - temp_file = os.fdopen(descriptor, 'wb') - temp_file.write(summary) - temp_file.close() - invoke_client('commit', '-a', '-F', filename, directory=directory) - finally: - os.unlink(filename) - -def postcommit(directory): - pass - - -# In order to diff the bug database, you need a way to check out arbitrary -# previous revisions and a mechanism for locating the bug_dir in the revision -# you've checked out. -# -# Copying the Mercurial implementation, this feature is implemented by four -# functions: -# -# git_dir_for_path : find '.git' for a git tree. -# -# export : check out a commit 'spec' from git-repo 'bug_dir' into a dir -# 'revision_dir' -# -# find_or_make_export : check out a commit 'spec' from git repo 'directory' to -# any location you please and return the path to the checkout -# -# path_in_reference : return a path to the bug_dir of the commit 'spec' - -def git_repo_for_path(path): - """Find the root of the deepest repository containing path.""" - # Assume that nothing funny is going on; in particular, that we aren't - # dealing with a bare repo. - dirname = os.path.dirname(git_dir_for_path(path)) - if dirname == '' : # os.path.dirname('filename') == '' - dirname = '.' - return dirname - -def git_dir_for_path(path): - """Find the git-dir of the deepest repo containing path.""" - return invoke_client("rev-parse", "--git-dir", directory=path)[1].rstrip() - -def export(spec, bug_dir, revision_dir): - """Check out commit 'spec' from the git repo containing bug_dir into - 'revision_dir'.""" - if not os.path.exists(revision_dir): - os.makedirs(revision_dir) - invoke_client("init", directory=revision_dir) - invoke_client("pull", git_dir_for_path(bug_dir), directory=revision_dir) - invoke_client("checkout", '-f', spec, directory=revision_dir) - -def find_or_make_export(spec, directory): - """Checkout 'spec' from the repo at 'directory' by hook or by crook and - return the path to the working copy.""" - home = os.path.expanduser("~") - revision_root = os.path.join(home, ".be_revs") - if not os.path.exists(revision_root): - os.mkdir(revision_root) - revision_dir = os.path.join(revision_root, spec) - if not os.path.exists(revision_dir): - export(spec, directory, revision_dir) - return revision_dir - -def path_in_reference(bug_dir, spec): - """Check out 'spec' and return the path to its bug_dir.""" - spec = spec or 'HEAD' - spec = invoke_client('rev-parse', spec, directory=bug_dir)[1].rstrip() - # This is a really hairy computation. - # The theory is that we can't possibly be working out of a bare repo; - # hence, we get the rel_bug_dir by chopping off dirname(git_dir_for_path(bug_dir)) - # + '/'. - rel_bug_dir = strip_git(bug_dir) - export_root = find_or_make_export(spec, directory=bug_dir) - return os.path.join(export_root, rel_bug_dir) - - -name = "git" - + return False + def _rcs_root(self, path): + """Find the root of the deepest repository containing path.""" + # Assume that nothing funny is going on; in particular, that we aren't + # dealing with a bare repo. + if os.path.isdir(path) != True: + path = os.path.dirname(path) + status,output,error = self._u_invoke_client("rev-parse", "--git-dir", + directory=path) + gitdir = os.path.join(path, output.rstrip('\n')) + dirname = os.path.abspath(os.path.dirname(gitdir)) + return dirname + def _rcs_init(self, path): + self._u_invoke_client("init", directory=path) + def _rcs_get_user_id(self): + status,output,error = self._u_invoke_client("config", "user.name") + name = output.rstrip('\n') + status,output,error = self._u_invoke_client("config", "user.email") + email = output.rstrip('\n') + return self._u_create_id(name, email) + def _rcs_set_user_id(self, value): + name,email = self._u_parse_id(value) + if email != None: + self._u_invoke_client("config", "user.email", email) + self._u_invoke_client("config", "user.name", name) + def _rcs_add(self, path): + if os.path.isdir(path): + return + self._u_invoke_client("add", path) + def _rcs_remove(self, path): + if not os.path.isdir(self._u_abspath(path)): + self._u_invoke_client("rm", "-f", path) + def _rcs_update(self, path): + self._rcs_add(path) + def _rcs_get_file_contents(self, path, revision=None): + if revision == None: + return file(self._u_abspath(path), "rb").read() + else: + arg = "%s:%s" % (revision,path) + status,output,error = self._u_invoke_client("show", arg) + return output + def _rcs_duplicate_repo(self, directory, revision=None): + if revision==None: + RCS._rcs_duplicate_repo(self, directory, revision) + else: + #self._u_invoke_client("archive", revision, directory) # makes tarball + self._u_invoke_client("clone", "--no-checkout",".",directory) + self._u_invoke_client("checkout", revision, directory=directory) + def _rcs_commit(self, commitfile): + status,output,error = self._u_invoke_client('commit', '-a', + '-F', commitfile) + revision = None + revline = re.compile("Created (.*)commit (.*):(.*)") + match = revline.search(output) + assert match != None, output+error + assert len(match.groups()) == 3 + revision = match.groups()[1] + return revision + +class GitTestCase(RCStestCase): + Class = Git + +unitsuite = unittest.TestLoader().loadTestsFromTestCase(GitTestCase) +suite = unittest.TestSuite([unitsuite, doctest.DocTestSuite()]) -- cgit