From 509b1985807a308e31bdff595c939503b6d26a40 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 4 Nov 2014 12:56:53 +0000 Subject: Only search for repository root in GitDirectory constructor if told to The GitDirectory() constructor, if passed a 'dirname' that doesn't contain a '.git' subdirectory, can search upwards to find the real root of the repository. This is used by the `add-binary`, `push`, and `pull` commands. This causes very confusing behaviour in the case that 'dirname' points to a directory that should be a Git repository, but isn't, and that directory is a path inside the working tree of another Git repository. Rather than raising an error, in this case the GitDirectory class would perform operations on a different repository to the one the caller expected. This 'search_for_root' behaviour is now opt-in, to avoid confusion. --- morphlib/gitdir.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'morphlib/gitdir.py') diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index 9fef4f1e..a9837030 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -352,12 +352,19 @@ class GitDirectory(object): ''' - def __init__(self, dirname): - self.dirname = morphlib.util.find_root(dirname, '.git') - # if we are in a bare repo, self.dirname will now be None - # so we just use the provided dirname - if not self.dirname: - self.dirname = dirname + def __init__(self, dirname, search_for_root=False): + '''Set up a GitDirectory instance for the repository at 'dirname'. + + If 'search_for_root' is set to True, 'dirname' may point to a + subdirectory inside the working tree of repository. Otherwise 'dirname' + must be the top directory. + + ''' + + if search_for_root: + dirname = morphlib.util.find_root(dirname, '.git') + + self.dirname = dirname self._config = {} def _runcmd(self, argv, **kwargs): -- cgit v1.2.1 From 1a86803340081b9e929bb77491dfe01020516164 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 4 Nov 2014 13:03:06 +0000 Subject: Improve documentation of GitDirectory class Clarify that bare repositories are supported, and other fixes. --- morphlib/gitdir.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'morphlib/gitdir.py') diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index a9837030..94cc5cb9 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -343,12 +343,20 @@ class Remote(object): class GitDirectory(object): - '''Represent a git working tree + .git directory. + '''Represents a local Git repository. - This class represents a directory that is the result of a - "git clone". It includes both the .git subdirectory and - the working tree. It is a thin abstraction, meant to make - it easier to do certain git operations. + This class represents a directory that is the result of a `git clone` or + `git init`. It includes both the .git subdirectory and the working tree. + It is a thin abstraction, meant to make it easier to do certain git + operations. + + In the case of bare repositories, there is no working tree. These are + supported, but a NoWorkingTree exception will be raised if you try to + perform any operations that require a working tree. + + The repository must already exist on disk. Use gitdir.init() to create a + new repo, or gitdir.clone_from_cached_repo() if you want to clone a repo + that Morph has cached locally. ''' -- cgit v1.2.1