diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-06 16:31:34 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-11-06 16:31:34 +0000 |
commit | 6779e46e880eec757a6923441accef2442007677 (patch) | |
tree | ad0dd5c28927cfcb5410760930b5bdfafaf5e948 /morphlib | |
parent | a04cb6cf9e3ad745af8b3c4d4675cfd4c3b12df1 (diff) | |
parent | 1a86803340081b9e929bb77491dfe01020516164 (diff) | |
download | morph-6779e46e880eec757a6923441accef2442007677.tar.gz |
Merge branch 'sam/gitdir-fixes-v2'
Reviewed-By: Pedro Alvarez <pedro.alvarez@codethink.co.uk>
Reviewed-By: Adam Coldrick <adam.coldrick@codethink.co.uk>
Diffstat (limited to 'morphlib')
-rw-r--r-- | morphlib/gitdir.py | 37 | ||||
-rw-r--r-- | morphlib/gitdir_tests.py | 10 | ||||
-rw-r--r-- | morphlib/plugins/add_binary_plugin.py | 2 | ||||
-rw-r--r-- | morphlib/plugins/push_pull_plugin.py | 4 |
4 files changed, 39 insertions, 14 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index fd7f3808..a0f35eef 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -343,21 +343,36 @@ 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. ''' - 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): diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 10b3b7e5..55b3caa5 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -43,6 +43,16 @@ class GitDirectoryTests(unittest.TestCase): gitdir = morphlib.gitdir.GitDirectory(self.dirname) self.assertEqual(gitdir.dirname, self.dirname) + def test_can_search_for_top_directory(self): + self.fake_git_clone() + + path_inside_working_tree = os.path.join(self.dirname, 'a', 'b', 'c') + os.makedirs(path_inside_working_tree) + + gitdir = morphlib.gitdir.GitDirectory( + path_inside_working_tree, search_for_root=True) + self.assertEqual(gitdir.dirname, self.dirname) + def test_runs_command_in_right_directory(self): self.fake_git_clone() gitdir = morphlib.gitdir.GitDirectory(self.dirname) diff --git a/morphlib/plugins/add_binary_plugin.py b/morphlib/plugins/add_binary_plugin.py index a192f792..dee1d9c4 100644 --- a/morphlib/plugins/add_binary_plugin.py +++ b/morphlib/plugins/add_binary_plugin.py @@ -56,7 +56,7 @@ class AddBinaryPlugin(cliapp.Plugin): if not binaries: raise morphlib.Error('add-binary must get at least one argument') - gd = morphlib.gitdir.GitDirectory(os.getcwd()) + gd = morphlib.gitdir.GitDirectory(os.getcwd(), search_for_root=True) gd.fat_init() if not gd.has_fat(): self._make_gitfat(gd) diff --git a/morphlib/plugins/push_pull_plugin.py b/morphlib/plugins/push_pull_plugin.py index 843de1a6..ddc9a8af 100644 --- a/morphlib/plugins/push_pull_plugin.py +++ b/morphlib/plugins/push_pull_plugin.py @@ -52,7 +52,7 @@ class PushPullPlugin(cliapp.Plugin): if len(args) != 2: raise morphlib.Error('push must get exactly two arguments') - gd = morphlib.gitdir.GitDirectory(os.getcwd()) + gd = morphlib.gitdir.GitDirectory(os.getcwd(), search_for_root=True) remote, branch = args rs = morphlib.gitdir.RefSpec(branch) gd.get_remote(remote).push(rs) @@ -81,7 +81,7 @@ class PushPullPlugin(cliapp.Plugin): if len(args) > 1: raise morphlib.Error('pull takes at most one argument') - gd = morphlib.gitdir.GitDirectory(os.getcwd()) + gd = morphlib.gitdir.GitDirectory(os.getcwd(), search_for_root=True) remote = gd.get_remote('origin') if args: branch = args[0] |