]> git.proxmox.com Git - libgit2.git/blame - tests/pack/indexer.c
More improvements to callback return value tests
[libgit2.git] / tests / pack / indexer.c
CommitLineData
cf0582b4 1#include "clar_libgit2.h"
0b33fca0 2#include <git2.h>
cf0582b4
CMN
3#include "fileops.h"
4#include "hash.h"
5#include "iterator.h"
6#include "vector.h"
7#include "posix.h"
8
0b33fca0 9
cf0582b4
CMN
10/*
11 * This is a packfile with three objects. The second is a delta which
12 * depends on the third, which is also a delta.
13 */
14unsigned char out_of_order_pack[] = {
15 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
16 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
17 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
18 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62,
19 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01,
20 0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c,
21 0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62,
22 0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4,
23 0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92,
24 0x6f, 0xae, 0x66, 0x75
25};
26unsigned int out_of_order_pack_len = 112;
27
0b33fca0
CMN
28/*
29 * Packfile with two objects. The second is a delta against an object
30 * which is not in the packfile
31 */
32unsigned char thin_pack[] = {
33 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
34 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76,
35 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10,
36 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62,
37 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x42, 0x52,
38 0x3a, 0x6f, 0x39, 0xd1, 0xfe, 0x66, 0x68, 0x6b, 0xa5, 0xe5, 0xe2, 0x97,
39 0xac, 0x94, 0x6c, 0x76, 0x0b, 0x04
40};
41unsigned int thin_pack_len = 78;
42
43unsigned char base_obj[] = { 07, 076 };
44unsigned int base_obj_len = 2;
45
cf0582b4
CMN
46void test_pack_indexer__out_of_order(void)
47{
a6154f21 48 git_indexer *idx;
cf0582b4
CMN
49 git_transfer_progress stats;
50
1e60e5f4 51 cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
a6154f21
CMN
52 cl_git_pass(git_indexer_append(idx, out_of_order_pack, out_of_order_pack_len, &stats));
53 cl_git_pass(git_indexer_commit(idx, &stats));
cf0582b4
CMN
54
55 cl_assert_equal_i(stats.total_objects, 3);
56 cl_assert_equal_i(stats.received_objects, 3);
57 cl_assert_equal_i(stats.indexed_objects, 3);
58
a6154f21 59 git_indexer_free(idx);
cf0582b4 60}
0b33fca0
CMN
61
62void test_pack_indexer__fix_thin(void)
63{
a6154f21 64 git_indexer *idx;
0b33fca0
CMN
65 git_transfer_progress stats;
66 git_repository *repo;
67 git_odb *odb;
68 git_oid id, should_id;
69
70 cl_git_pass(git_repository_init(&repo, "thin.git", true));
71 cl_git_pass(git_repository_odb(&odb, repo));
72
73 /* Store the missing base into your ODB so the indexer can fix the pack */
74 cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJ_BLOB));
75 git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18");
76 cl_assert(!git_oid_cmp(&id, &should_id));
77
1e60e5f4 78 cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, NULL));
a6154f21
CMN
79 cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats));
80 cl_git_pass(git_indexer_commit(idx, &stats));
0b33fca0 81
893055f2 82 cl_assert_equal_i(stats.total_objects, 2);
0b33fca0
CMN
83 cl_assert_equal_i(stats.received_objects, 2);
84 cl_assert_equal_i(stats.indexed_objects, 2);
85 cl_assert_equal_i(stats.local_objects, 1);
86
87 git_oid_fromstr(&should_id, "11f0f69b334728fdd8bc86b80499f22f29d85b15");
a6154f21 88 cl_assert(!git_oid_cmp(git_indexer_hash(idx), &should_id));
0b33fca0 89
a6154f21 90 git_indexer_free(idx);
0b33fca0
CMN
91 git_odb_free(odb);
92 git_repository_free(repo);
93
94 /*
95 * The pack's name/hash only tells us what objects there are,
96 * so we need to go through the packfile again in order to
97 * figure out whether we calculated the trailer correctly.
98 */
99 {
100 unsigned char buffer[128];
101 int fd;
102 ssize_t read;
0b33fca0
CMN
103 struct stat st;
104 const char *name = "pack-11f0f69b334728fdd8bc86b80499f22f29d85b15.pack";
105
106 fd = p_open(name, O_RDONLY);
107 cl_assert(fd != -1);
108
109 cl_git_pass(p_stat(name, &st));
0b33fca0 110
1e60e5f4 111 cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
0b33fca0
CMN
112 read = p_read(fd, buffer, sizeof(buffer));
113 cl_assert(read != -1);
114 p_close(fd);
115
a6154f21
CMN
116 cl_git_pass(git_indexer_append(idx, buffer, read, &stats));
117 cl_git_pass(git_indexer_commit(idx, &stats));
0b33fca0
CMN
118
119 cl_assert_equal_i(stats.total_objects, 3);
120 cl_assert_equal_i(stats.received_objects, 3);
121 cl_assert_equal_i(stats.indexed_objects, 3);
122 cl_assert_equal_i(stats.local_objects, 0);
123
a6154f21 124 git_indexer_free(idx);
0b33fca0
CMN
125 }
126}