Skip to content

Commit 4c4ff49

Browse files
Add tests for packbuilder
1 parent c415819 commit 4c4ff49

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
from pathlib import Path
31+
32+
import pytest
33+
34+
import pygit2
35+
from pygit2 import PackBuilder
36+
from . import utils
37+
38+
39+
def test_create_packbuilder(testrepo):
40+
# simple test of PackBuilder creation
41+
packbuilder = PackBuilder(testrepo)
42+
assert len(packbuilder) == 0
43+
44+
45+
def test_add(testrepo):
46+
# Add a few objects and confirm that the count is correct
47+
packbuilder = PackBuilder(testrepo)
48+
objects_to_add = [obj for obj in testrepo]
49+
packbuilder.add(objects_to_add[0])
50+
assert len(packbuilder) == 1
51+
packbuilder.add(objects_to_add[1])
52+
assert len(packbuilder) == 2
53+
54+
55+
def test_add_recursively(testrepo):
56+
# Add the head object and referenced objects recursively and confirm that the count is correct
57+
packbuilder = PackBuilder(testrepo)
58+
packbuilder.add_recur(testrepo.head.target)
59+
60+
#expect a count of 4 made up of the following referenced objects:
61+
# Commit
62+
# Tree
63+
# Blob: hello.txt
64+
# Blob: .gitignore
65+
66+
assert len(packbuilder) == 4
67+
68+
69+
def test_repo_pack(testrepo, tmp_path):
70+
# pack the repo with the default strategy
71+
confirm_same_repo_after_packing(testrepo, tmp_path, None)
72+
73+
74+
def test_pack_with_delegate(testrepo, tmp_path):
75+
# loop through all branches and add each commit to the packbuilder
76+
def pack_delegate(pb):
77+
for branch in pb._repo.branches:
78+
br = pb._repo.branches.get(branch)
79+
for commit in br.log():
80+
pb.add_recur(commit.oid_new)
81+
confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate)
82+
83+
84+
def setup_second_repo(tmp_path):
85+
# helper method to set up a second repo for comparison
86+
tmp_path_2 = os.path.join(tmp_path, 'test_repo2')
87+
with utils.TemporaryRepository('testrepo.tar', tmp_path_2) as path:
88+
testrepo = pygit2.Repository(path)
89+
return testrepo
90+
91+
def confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate):
92+
# Helper method to confirm the contents of two repos before and after packing
93+
pack_repo = setup_second_repo(tmp_path)
94+
95+
objects_dir = os.path.join(pack_repo.path, 'objects')
96+
shutil.rmtree(objects_dir)
97+
pack_path = os.path.join(pack_repo.path, 'objects', 'pack')
98+
os.makedirs(pack_path)
99+
100+
# assert that the number of written objects is the same as the number of objects in the repo
101+
written_objects = testrepo.pack(pack_path, pack_delegate=pack_delegate)
102+
assert written_objects == len([obj for obj in testrepo])
103+
104+
105+
# assert that the number of objects in the pack repo is the same as the original repo
106+
orig_objects = [obj for obj in testrepo.odb]
107+
packed_objects = [obj for obj in pack_repo.odb]
108+
assert len(packed_objects) == len(orig_objects)
109+
110+
# assert that the objects in the packed repo are the same objects as the original repo
111+
for i, obj in enumerate(orig_objects):
112+
assert pack_repo[obj].type == testrepo[obj].type
113+
assert pack_repo[obj].read_raw() == testrepo[obj].read_raw()

0 commit comments

Comments
 (0)