Skip to content

Commit 763b571

Browse files
committed
checkout: add support for overriding the target directory
1 parent af38211 commit 763b571

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pygit2/repository.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -185,37 +185,46 @@ def _checkout_args_to_options(**kwargs):
185185
copts = ffi.new('git_checkout_options *')
186186
check_error(C.git_checkout_init_options(copts, 1))
187187

188+
# References we need to keep to strings and so forth
189+
refs = []
190+
188191
# pygit2's default is SAFE_CREATE
189192
copts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE
190193
# and go through the arguments to see what the user wanted
191-
for k, v in kwargs.iteritems():
192-
if k == 'strategy':
193-
copts.checkout_strategy = v
194+
strategy = kwargs.get('strategy')
195+
if strategy:
196+
copts.checkout_strategy = strategy
197+
198+
directory = kwargs.get('directory')
199+
if directory:
200+
target_dir = ffi.new('char[]', to_str(directory))
201+
refs.append(target_dir)
202+
copts.target_directory = target_dir
194203

195-
return copts
204+
return copts, refs
196205

197206
def checkout_head(self, **kwargs):
198207
"""Checkout HEAD
199208
200209
For arguments, see Repository.checkout().
201210
"""
202-
copts = Repository._checkout_args_to_options(**kwargs)
211+
copts, refs = Repository._checkout_args_to_options(**kwargs)
203212
check_error(C.git_checkout_head(self._repo, copts))
204213

205214
def checkout_index(self, **kwargs):
206215
"""Checkout the repository's index
207216
208217
For arguments, see Repository.checkout().
209218
"""
210-
copts = Repository._checkout_args_to_options(**kwargs)
219+
copts, refs = Repository._checkout_args_to_options(**kwargs)
211220
check_error(C.git_checkout_index(self._repo, ffi.NULL, copts))
212221

213222
def checkout_tree(self, treeish, **kwargs):
214223
"""Checkout the given treeish
215224
216225
For arguments, see Repository.checkout().
217226
"""
218-
copts = Repository._checkout_args_to_options(**kwargs)
227+
copts, refs = Repository._checkout_args_to_options(**kwargs)
219228
cptr = ffi.new('git_object **')
220229
ffi.buffer(cptr)[:] = treeish._pointer[:]
221230

@@ -242,6 +251,8 @@ def checkout(self, refname=None, **kwargs):
242251
:param int strategy: A ``GIT_CHECKOUT_`` value. The default is
243252
``GIT_CHECKOUT_SAFE_CREATE``.
244253
254+
:param str directory: Alternative checkout path to workdir.
255+
245256
"""
246257

247258

test/test_repository.py

+8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ def test_checkout_head(self):
246246
self.repo.checkout('HEAD', strategy=pygit2.GIT_CHECKOUT_FORCE)
247247
self.assertTrue('bye.txt' not in self.repo.status())
248248

249+
def test_checkout_alternative_dir(self):
250+
ref_i18n = self.repo.lookup_reference('refs/heads/i18n')
251+
extra_dir = os.path.join(self.repo.workdir, 'extra-dir')
252+
os.mkdir(extra_dir)
253+
self.assertTrue(len(os.listdir(extra_dir)) == 0)
254+
self.repo.checkout(ref_i18n, directory=extra_dir)
255+
self.assertFalse(len(os.listdir(extra_dir)) == 0)
256+
249257
def test_merge_base(self):
250258
commit = self.repo.merge_base(
251259
'5ebeeebb320790caf276b9fc8b24546d63316533',

0 commit comments

Comments
 (0)