diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2010-07-07 23:02:25 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2010-07-07 23:45:47 +0200 |
commit | b425301ad16f265157abdaf47f7af1c1ea879068 (patch) | |
tree | 22eef12f33cec4cff9ef12e39a59ce32351b2bf1 | |
parent | ca288d443f4fc9d790eecb6e1cdf82b6cdd8dc0d (diff) | |
download | gitpython-b425301ad16f265157abdaf47f7af1c1ea879068.tar.gz |
Adjusted clone method to allow static classmethod clone ( using clone_from ) as well as the previous instance method clone to keep it compatible
Fixed small bug in test code
-rw-r--r-- | lib/git/repo/base.py | 47 | ||||
-rw-r--r-- | test/git/test_repo.py | 16 |
2 files changed, 44 insertions, 19 deletions
diff --git a/lib/git/repo/base.py b/lib/git/repo/base.py index ed805991..d45dd713 100644 --- a/lib/git/repo/base.py +++ b/lib/git/repo/base.py @@ -606,26 +606,15 @@ class Repo(object): output = git.init(**kwargs) return Repo(path) - def clone(self, path, **kwargs): - """Create a clone from this repository. - :param path: - is the full path of the new repo (traditionally ends with ./<name>.git). - - :param kwargs: - odbt = ObjectDatabase Type, allowing to determine the object database - implementation used by the returned Repo instance - - All remaining keyword arguments are given to the git-clone command - - :return: - ``git.Repo`` (the newly cloned repo)""" + @classmethod + def _clone(cls, git, url, path, odb_default_type, **kwargs): # special handling for windows for path at which the clone should be # created. # tilde '~' will be expanded to the HOME no matter where the ~ occours. Hence # we at least give a proper error instead of letting git fail prev_cwd = None prev_path = None - odbt = kwargs.pop('odbt', type(self.odb)) + odbt = kwargs.pop('odbt', odb_default_type) if os.name == 'nt': if '~' in path: raise OSError("Git cannot handle the ~ character in path %r correctly" % path) @@ -645,7 +634,7 @@ class Repo(object): # END windows handling try: - self.git.clone(self.git_dir, path, **kwargs) + git.clone(url, path, **kwargs) finally: if prev_cwd is not None: os.chdir(prev_cwd) @@ -655,10 +644,32 @@ class Repo(object): # our git command could have a different working dir than our actual # environment, hence we prepend its working dir if required - if not os.path.isabs(path) and self.git.working_dir: - path = join(self.git._working_dir, path) - return Repo(os.path.abspath(path), odbt = odbt) + if not os.path.isabs(path) and git.working_dir: + path = join(git._working_dir, path) + return cls(os.path.abspath(path), odbt = odbt) + def clone(self, path, **kwargs): + """Create a clone from this repository. + :param path: + is the full path of the new repo (traditionally ends with ./<name>.git). + + :param kwargs: + odbt = ObjectDatabase Type, allowing to determine the object database + implementation used by the returned Repo instance + + All remaining keyword arguments are given to the git-clone command + + :return: ``git.Repo`` (the newly cloned repo)""" + return self._clone(self.git, self.git_dir, path, type(self.odb), **kwargs) + + @classmethod + def clone_from(cls, url, to_path, **kwargs): + """Create a clone from the given URL + :param url: valid git url, see http://www.kernel.org/pub/software/scm/git/docs/git-clone.html#URLS + :param to_path: Path to which the repository should be cloned to + :param **kwargs: see the ``clone`` method + :return: Repo instance pointing to the cloned directory""" + return cls._clone(Git(os.getcwd()), url, to_path, GitCmdObjectDB, **kwargs) def archive(self, ostream, treeish=None, prefix=None, **kwargs): """Archive the tree at the given revision. diff --git a/test/git/test_repo.py b/test/git/test_repo.py index 53829556..88d8c8c1 100644 --- a/test/git/test_repo.py +++ b/test/git/test_repo.py @@ -145,6 +145,19 @@ class TestRepo(TestBase): rc = r.clone(clone_path) self._assert_empty_repo(rc) + + try: + shutil.rmtree(clone_path) + except OSError: + # when relative paths are used, the clone may actually be inside + # of the parent directory + pass + # END exception handling + + # try again, this time with the absolute version + rc = Repo.clone_from(r.git_dir, clone_path) + self._assert_empty_repo(rc) + shutil.rmtree(git_dir_abs) try: shutil.rmtree(clone_path) @@ -153,6 +166,7 @@ class TestRepo(TestBase): # of the parent directory pass # END exception handling + # END for each path os.makedirs(git_dir_rela) @@ -441,7 +455,7 @@ class TestRepo(TestBase): for pn, parent in enumerate(obj.parents): rev = name + "^%i" % (pn+1) assert rev_parse(rev) == parent - self._assert_rev_parse_types(rev, obj2) + self._assert_rev_parse_types(rev, parent) # END for each parent return orig_obj |