Skip to content

Commit b550027

Browse files
committed
Fix cloning a repository with a particular branch to checkout
We were missing a cast to bytes. Add a test for this option as well and remove the old commented-out test for checkout_branch, which is superseded by this one and whose last bit seemed confused about what the option means. This fixes #399
1 parent 82b3429 commit b550027

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

pygit2/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def clone_repository(
154154
# We need to keep the ref alive ourselves
155155
checkout_branch_ref = None
156156
if branch:
157-
checkout_branch_ref = ffi.new('char []', branch)
157+
checkout_branch_ref = ffi.new('char []', to_bytes(branch))
158158
opts.checkout_branch = checkout_branch_ref
159159

160160
remote_name_ref = ffi.new('char []', to_bytes(remote_name))

test/test_repository.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,17 @@ def test_clone_with_credentials(self):
428428

429429
self.assertFalse(repo.is_empty)
430430

431+
def test_clone_with_checkout_branch(self):
432+
# create a test case which isolates the remote
433+
test_repo = clone_repository('./test/data/testrepo.git',
434+
os.path.join(self._temp_dir, 'testrepo-orig.git'),
435+
bare=True)
436+
test_repo.create_branch('test', test_repo[test_repo.head.target])
437+
repo = clone_repository(test_repo.path,
438+
os.path.join(self._temp_dir, 'testrepo.git'),
439+
checkout_branch='test', bare=True)
440+
self.assertEqual(repo.lookup_reference('HEAD').target, 'refs/heads/test')
441+
431442
# FIXME The tests below are commented because they are broken:
432443
#
433444
# - test_clone_push_url: Passes, but does nothing useful.
@@ -437,8 +448,6 @@ def test_clone_with_credentials(self):
437448
#
438449
# - test_clone_push_spec: Passes, but does nothing useful.
439450
#
440-
# - test_clone_checkout_branch: Fails, because the test fixture does not
441-
# have any branch named "test"
442451

443452
# def test_clone_push_url(self):
444453
# repo_path = "./test/data/testrepo.git/"
@@ -470,15 +479,5 @@ def test_clone_with_credentials(self):
470479
# # not sure how to test this either... couldn't find pushspec
471480
# # self.assertEqual(repo.remotes[0].fetchspec, "refs/heads/test")
472481

473-
# def test_clone_checkout_branch(self):
474-
# repo_path = "./test/data/testrepo.git/"
475-
# repo = clone_repository(repo_path, self._temp_dir,
476-
# checkout_branch="test")
477-
# self.assertFalse(repo.is_empty)
478-
# # FIXME: When pygit2 supports retrieving the current branch,
479-
# # enable this test
480-
# # self.assertEqual(repo.remotes[0].current_branch, "test")
481-
482-
483482
if __name__ == '__main__':
484483
unittest.main()

0 commit comments

Comments
 (0)