Skip to content

Commit

Permalink
clone: wrap clone_into()
Browse files Browse the repository at this point in the history
This allows the user to prepare the repository and remote with whichever
custom settings they want before performing the "clone" proper.
  • Loading branch information
carlosmn committed May 20, 2014
1 parent 06b7438 commit 19be4b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
23 changes: 23 additions & 0 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,27 @@ def clone_repository(

return Repository(path)

def clone_into(repo, remote, branch=None):
"""Clone into an empty repository from the specified remote
:param Repository repo: The empty repository into which to clone
:param Remote remote: The remote from which to clone
:param str branch: Branch to checkout after the clone. Pass None
to use the remotes's default branch.
This allows you specify arbitrary repository and remote configurations
before performing the clone step itself. E.g. you can replicate git-clone's
'--mirror' option by setting a refspec of '+refs/*:refs/*', 'core.mirror' to true
and calling this function.
"""

err = C.git_clone_into(repo._repo, remote._remote, ffi.NULL, to_str(branch))

if remote._stored_exception:
raise remote._stored_exception

check_error(err)

settings = Settings()
1 change: 1 addition & 0 deletions pygit2/decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ int git_clone(git_repository **out,
const char *local_path,
const git_clone_options *options);

int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_opts *co_opts, const char *branch);

typedef enum {
GIT_CONFIG_LEVEL_SYSTEM = 1,
Expand Down
9 changes: 8 additions & 1 deletion test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

# Import from pygit2
from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT
from pygit2 import init_repository, clone_repository, discover_repository
from pygit2 import init_repository, clone_repository, clone_into, discover_repository
from pygit2 import Oid, Reference, hashfile
import pygit2
from . import utils
Expand Down Expand Up @@ -461,6 +461,13 @@ def test_clone_remote_name(self):
self.assertFalse(repo.is_empty)
self.assertEqual(repo.remotes[0].name, "custom_remote")

def test_clone_into(self):
repo_path = "./test/data/testrepo.git/"
repo = init_repository(os.path.join(self._temp_dir, "clone-into"))
remote = repo.create_remote("origin", 'file://' + os.path.realpath(repo_path))
clone_into(repo, remote)
self.assertTrue('refs/remotes/origin/master' in repo.listall_references())

def test_clone_with_credentials(self):
credentials = pygit2.UserPass("libgit2", "libgit2")
repo = clone_repository(
Expand Down

0 comments on commit 19be4b6

Please sign in to comment.