Skip to content

Commit 402fa4e

Browse files
Add tests for packbuilder
1 parent c415819 commit 402fa4e

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

pygit2/packbuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def add(self, git_object):
6363
err = C.git_packbuilder_insert(self._packbuilder, oid, ffi.NULL)
6464
check_error(err)
6565

66-
def add_recursive(self, git_object):
66+
def add_recur(self, git_object):
6767
oid = self.convert_object_to_oid(git_object)
6868
err = C.git_packbuilder_insert_recur(self._packbuilder, oid, ffi.NULL)
6969
check_error(err)

src/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef struct {
5050
int owned; /* _from_c() sometimes means we don't own the C pointer */
5151
} Repository;
5252

53+
5354
typedef struct {
5455
PyObject_HEAD
5556
git_oid oid;

test/test_packbuilder.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright 2010-2020 The pygit2 contributors
2+
#
3+
# This file is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License, version 2,
5+
# as published by the Free Software Foundation.
6+
#
7+
# In addition to the permissions in the GNU General Public License,
8+
# the authors give you unlimited permission to link the compiled
9+
# version of this file into combinations with other programs,
10+
# and to distribute those combinations without any restriction
11+
# coming from the use of this file. (The General Public License
12+
# restrictions do apply in other respects; for example, they cover
13+
# modification of the file, and distribution when not linked into
14+
# a combined executable.)
15+
#
16+
# This file is distributed in the hope that it will be useful, but
17+
# WITHOUT ANY WARRANTY; without even the implied warranty of
18+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
# General Public License for more details.
20+
#
21+
# You should have received a copy of the GNU General Public License
22+
# along with this program; see the file COPYING. If not, write to
23+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24+
# Boston, MA 02110-1301, USA.
25+
26+
"""Tests for Index files."""
27+
28+
import os
29+
import shutil
30+
31+
import pytest
32+
33+
import pygit2
34+
from pygit2 import PackBuilder
35+
from . import utils
36+
37+
38+
def test_create_packbuilder(testrepo):
39+
# simple test of PackBuilder creation
40+
packbuilder = PackBuilder(testrepo)
41+
assert len(packbuilder) == 0
42+
43+
44+
def test_add(testrepo):
45+
# Add a few objects and confirm that the count is correct
46+
packbuilder = PackBuilder(testrepo)
47+
objects_to_add = [obj for obj in testrepo]
48+
packbuilder.add(objects_to_add[0])
49+
assert len(packbuilder) == 1
50+
packbuilder.add(objects_to_add[1])
51+
assert len(packbuilder) == 2
52+
53+
54+
def test_add_recursively(testrepo):
55+
# Add the head object and referenced objects recursively and confirm that the count is correct
56+
packbuilder = PackBuilder(testrepo)
57+
packbuilder.add_recur(testrepo.head.target)
58+
59+
#expect a count of 4 made up of the following referenced objects:
60+
# Commit
61+
# Tree
62+
# Blob: hello.txt
63+
# Blob: .gitignore
64+
65+
assert len(packbuilder) == 4
66+
67+
68+
def test_repo_pack(testrepo, tmp_path):
69+
# pack the repo with the default strategy
70+
confirm_same_repo_after_packing(testrepo, tmp_path, None)
71+
72+
73+
def test_pack_with_delegate(testrepo, tmp_path):
74+
# loop through all branches and add each commit to the packbuilder
75+
def pack_delegate(pb):
76+
for branch in pb._repo.branches:
77+
br = pb._repo.branches.get(branch)
78+
for commit in br.log():
79+
pb.add_recur(commit.oid_new)
80+
confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate)
81+
82+
83+
def setup_second_repo(tmp_path):
84+
# helper method to set up a second repo for comparison
85+
tmp_path_2 = os.path.join(tmp_path, 'test_repo2')
86+
with utils.TemporaryRepository('testrepo.tar', tmp_path_2) as path:
87+
testrepo = pygit2.Repository(path)
88+
return testrepo
89+
90+
def confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate):
91+
# Helper method to confirm the contents of two repos before and after packing
92+
pack_repo = setup_second_repo(tmp_path)
93+
94+
objects_dir = os.path.join(pack_repo.path, 'objects')
95+
shutil.rmtree(objects_dir)
96+
pack_path = os.path.join(pack_repo.path, 'objects', 'pack')
97+
os.makedirs(pack_path)
98+
99+
# assert that the number of written objects is the same as the number of objects in the repo
100+
written_objects = testrepo.pack(pack_path, pack_delegate=pack_delegate)
101+
assert written_objects == len([obj for obj in testrepo])
102+
103+
104+
# assert that the number of objects in the pack repo is the same as the original repo
105+
orig_objects = [obj for obj in testrepo.odb]
106+
packed_objects = [obj for obj in pack_repo.odb]
107+
assert len(packed_objects) == len(orig_objects)
108+
109+
# assert that the objects in the packed repo are the same objects as the original repo
110+
for i, obj in enumerate(orig_objects):
111+
assert pack_repo[obj].type == testrepo[obj].type
112+
assert pack_repo[obj].read_raw() == testrepo[obj].read_raw()

0 commit comments

Comments
 (0)