diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-10 19:20:22 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-10 19:20:22 +0100 |
commit | 6e8aa9b0aefc30ee5807c2b2cf433a38555b7e0b (patch) | |
tree | 21085a6a7f94d6362e995bd3fe1cb77e28be36ed | |
parent | 96c6ab4f7572fd5ca8638f3cb6e342d5000955e7 (diff) | |
download | gitpython-6e8aa9b0aefc30ee5807c2b2cf433a38555b7e0b.tar.gz |
Repo.init() now supports paths with a '~' in it, or environment variables in general.
Fixes #83
-rw-r--r-- | git/repo/base.py | 11 | ||||
-rw-r--r-- | git/repo/fun.py | 8 | ||||
-rw-r--r-- | git/test/test_repo.py | 10 |
3 files changed, 23 insertions, 6 deletions
diff --git a/git/repo/base.py b/git/repo/base.py index 97e49aa2..1dcb80ed 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -66,6 +66,10 @@ if sys.version_info[:2] < (2, 5): # python 2.4 compatiblity __all__ = ('Repo', ) +def _expand_path(p): + return os.path.abspath(os.path.expandvars(os.path.expanduser(p))) + + class Repo(object): """Represents a git repository and allows you to query references, @@ -111,7 +115,7 @@ class Repo(object): :raise InvalidGitRepositoryError: :raise NoSuchPathError: :return: git.Repo """ - epath = os.path.abspath(os.path.expandvars(os.path.expanduser(path or os.getcwd()))) + epath = _expand_path(path or os.getcwd()) self.git = None # should be set for __del__ not to fail in case we raise if not os.path.exists(epath): raise NoSuchPathError(epath) @@ -133,7 +137,7 @@ class Repo(object): self.git_dir = gitpath self._working_tree_dir = curpath break - + if not search_parent_directories: break curpath, dummy = os.path.split(curpath) @@ -700,7 +704,8 @@ class Repo(object): keyword arguments serving as additional options to the git-init command :return: ``git.Repo`` (the newly created repo)""" - + if path: + path = _expand_path(path) if mkdir and path and not os.path.exists(path): os.makedirs(path, 0o755) diff --git a/git/repo/fun.py b/git/repo/fun.py index a0dc5ce9..1ee11ffc 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -19,7 +19,7 @@ from gitdb.util import ( from git.compat import xrange -__all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', +__all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', 'to_commit') @@ -53,8 +53,10 @@ def find_git_dir(d): pass else: if content.startswith('gitdir: '): - d = join(dirname(d), content[8:]) - return find_git_dir(d) + path = content[8:] + if not os.path.isabs(path): + path = join(dirname(d), path) + return find_git_dir(path) # end handle exception return None diff --git a/git/test/test_repo.py b/git/test/test_repo.py index 25acbec9..0153d886 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -383,6 +383,16 @@ class TestRepo(TestBase): assert self.rorepo == self.rorepo and not (self.rorepo != self.rorepo) assert len(set((self.rorepo, self.rorepo))) == 1 + @with_rw_directory + def test_tilde_in_repo_path(self, rw_dir): + ph = os.environ['HOME'] + try: + os.environ['HOME'] = rw_dir + Repo.init(os.path.join(rw_dir, 'test.git'), bare=True) + finally: + os.environ['HOME'] = ph + # end assure HOME gets reset to what it was + def test_git_cmd(self): # test CatFileContentStream, just to be very sure we have no fencepost errors # last \n is the terminating newline that it expects |