diff --git a/pygit2/repository.py b/pygit2/repository.py index bffc2d8..70acbe7 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -185,21 +185,30 @@ class Repository(_Repository): copts = ffi.new('git_checkout_options *') check_error(C.git_checkout_init_options(copts, 1)) + # References we need to keep to strings and so forth + refs = [] + # pygit2's default is SAFE_CREATE copts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE # and go through the arguments to see what the user wanted - for k, v in kwargs.iteritems(): - if k == 'strategy': - copts.checkout_strategy = v + strategy = kwargs.get('strategy') + if strategy: + copts.checkout_strategy = strategy - return copts + directory = kwargs.get('directory') + if directory: + target_dir = ffi.new('char[]', to_str(directory)) + refs.append(target_dir) + copts.target_directory = target_dir + + return copts, refs def checkout_head(self, **kwargs): """Checkout HEAD For arguments, see Repository.checkout(). """ - copts = Repository._checkout_args_to_options(**kwargs) + copts, refs = Repository._checkout_args_to_options(**kwargs) check_error(C.git_checkout_head(self._repo, copts)) def checkout_index(self, **kwargs): @@ -207,7 +216,7 @@ class Repository(_Repository): For arguments, see Repository.checkout(). """ - copts = Repository._checkout_args_to_options(**kwargs) + copts, refs = Repository._checkout_args_to_options(**kwargs) check_error(C.git_checkout_index(self._repo, ffi.NULL, copts)) def checkout_tree(self, treeish, **kwargs): @@ -215,7 +224,7 @@ class Repository(_Repository): For arguments, see Repository.checkout(). """ - copts = Repository._checkout_args_to_options(**kwargs) + copts, refs = Repository._checkout_args_to_options(**kwargs) cptr = ffi.new('git_object **') ffi.buffer(cptr)[:] = treeish._pointer[:] @@ -242,6 +251,8 @@ class Repository(_Repository): :param int strategy: A ``GIT_CHECKOUT_`` value. The default is ``GIT_CHECKOUT_SAFE_CREATE``. + :param str directory: Alternative checkout path to workdir. + """ diff --git a/test/test_repository.py b/test/test_repository.py index 6f3d4ab..cda846c 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -246,6 +246,14 @@ class RepositoryTest_II(utils.RepoTestCase): self.repo.checkout('HEAD', strategy=pygit2.GIT_CHECKOUT_FORCE) self.assertTrue('bye.txt' not in self.repo.status()) + def test_checkout_alternative_dir(self): + ref_i18n = self.repo.lookup_reference('refs/heads/i18n') + extra_dir = os.path.join(self.repo.workdir, 'extra-dir') + os.mkdir(extra_dir) + self.assertTrue(len(os.listdir(extra_dir)) == 0) + self.repo.checkout(ref_i18n, directory=extra_dir) + self.assertFalse(len(os.listdir(extra_dir)) == 0) + def test_merge_base(self): commit = self.repo.merge_base( '5ebeeebb320790caf276b9fc8b24546d63316533',