]> git.proxmox.com Git - libgit2.git/blob - tests/odb/freshen.c
1fe64309d032f4d2e05cba37c10957bd1dde2c80
[libgit2.git] / tests / odb / freshen.c
1 #include "clar_libgit2.h"
2 #include "odb.h"
3 #include "posix.h"
4
5 static git_repository *repo;
6 static git_odb *odb;
7
8 void test_odb_freshen__initialize(void)
9 {
10 repo = cl_git_sandbox_init("testrepo.git");
11 cl_git_pass(git_repository_odb(&odb, repo));
12 }
13
14 void test_odb_freshen__cleanup(void)
15 {
16 git_odb_free(odb);
17 cl_git_sandbox_cleanup();
18 }
19
20 static void set_time_wayback(struct stat *out, const char *fn)
21 {
22 git_buf fullpath = GIT_BUF_INIT;
23 struct p_timeval old[2];
24
25 old[0].tv_sec = 1234567890;
26 old[0].tv_usec = 0;
27 old[1].tv_sec = 1234567890;
28 old[1].tv_usec = 0;
29
30 git_buf_joinpath(&fullpath, "testrepo.git/objects", fn);
31
32 cl_must_pass(p_utimes(git_buf_cstr(&fullpath), old));
33 cl_must_pass(p_lstat(git_buf_cstr(&fullpath), out));
34 git_buf_dispose(&fullpath);
35 }
36
37 #define LOOSE_STR "my new file\n"
38 #define LOOSE_BLOB_ID "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"
39 #define LOOSE_BLOB_FN "a7/1586c1dfe8a71c6cbf6c129f404c5642ff31bd"
40
41 void test_odb_freshen__loose_blob(void)
42 {
43 git_oid expected_id, id;
44 struct stat before, after;
45
46 cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_BLOB_ID));
47 set_time_wayback(&before, LOOSE_BLOB_FN);
48
49 /* make sure we freshen a blob */
50 cl_git_pass(git_blob_create_frombuffer(&id, repo, LOOSE_STR, CONST_STRLEN(LOOSE_STR)));
51 cl_assert_equal_oid(&expected_id, &id);
52 cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_BLOB_FN, &after));
53
54 cl_assert(before.st_atime < after.st_atime);
55 cl_assert(before.st_mtime < after.st_mtime);
56 }
57
58 #define UNIQUE_STR "doesnt exist in the odb yet\n"
59 #define UNIQUE_BLOB_ID "78a87d0b8878c5953b9a63015ff4e22a3d898826"
60 #define UNIQUE_BLOB_FN "78/a87d0b8878c5953b9a63015ff4e22a3d898826"
61
62 void test_odb_freshen__readonly_object(void)
63 {
64 git_oid expected_id, id;
65 struct stat before, after;
66
67 cl_git_pass(git_oid_fromstr(&expected_id, UNIQUE_BLOB_ID));
68
69 cl_git_pass(git_blob_create_frombuffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
70 cl_assert_equal_oid(&expected_id, &id);
71
72 set_time_wayback(&before, UNIQUE_BLOB_FN);
73 cl_assert((before.st_mode & S_IWUSR) == 0);
74
75 cl_git_pass(git_blob_create_frombuffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
76 cl_assert_equal_oid(&expected_id, &id);
77 cl_must_pass(p_lstat("testrepo.git/objects/" UNIQUE_BLOB_FN, &after));
78
79 cl_assert(before.st_atime < after.st_atime);
80 cl_assert(before.st_mtime < after.st_mtime);
81 }
82
83 #define LOOSE_TREE_ID "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
84 #define LOOSE_TREE_FN "94/4c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
85
86 void test_odb_freshen__loose_tree(void)
87 {
88 git_oid expected_id, id;
89 git_tree *tree;
90 struct stat before, after;
91
92 cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_TREE_ID));
93 set_time_wayback(&before, LOOSE_TREE_FN);
94
95 cl_git_pass(git_tree_lookup(&tree, repo, &expected_id));
96 cl_git_pass(git_tree_create_updated(&id, repo, tree, 0, NULL));
97
98 /* make sure we freshen a tree */
99 cl_assert_equal_oid(&expected_id, &id);
100 cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
101
102 cl_assert(before.st_atime < after.st_atime);
103 cl_assert(before.st_mtime < after.st_mtime);
104
105 git_tree_free(tree);
106 }
107
108 void test_odb_freshen__tree_during_commit(void)
109 {
110 git_oid tree_id, parent_id, commit_id;
111 git_tree *tree;
112 git_commit *parent;
113 git_signature *signature;
114 struct stat before, after;
115
116 cl_git_pass(git_oid_fromstr(&tree_id, LOOSE_TREE_ID));
117 cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
118 set_time_wayback(&before, LOOSE_TREE_FN);
119
120 cl_git_pass(git_oid_fromstr(&parent_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
121 cl_git_pass(git_commit_lookup(&parent, repo, &parent_id));
122
123 cl_git_pass(git_signature_new(&signature,
124 "Refresher", "refresher@example.com", 1488547083, 0));
125
126 cl_git_pass(git_commit_create(&commit_id, repo, NULL,
127 signature, signature, NULL, "New commit pointing to old tree",
128 tree, 1, (const git_commit **)&parent));
129
130 /* make sure we freshen the tree the commit points to */
131 cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
132 cl_assert(before.st_atime < after.st_atime);
133 cl_assert(before.st_mtime < after.st_mtime);
134
135 git_signature_free(signature);
136 git_commit_free(parent);
137 git_tree_free(tree);
138 }
139
140 #define PACKED_STR "Testing a readme.txt\n"
141 #define PACKED_ID "6336846bd5c88d32f93ae57d846683e61ab5c530"
142 #define PACKED_FN "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack"
143
144 void test_odb_freshen__packed_object(void)
145 {
146 git_oid expected_id, id;
147 struct stat before, after;
148 struct p_timeval old_times[2];
149
150 cl_git_pass(git_oid_fromstr(&expected_id, PACKED_ID));
151
152 old_times[0].tv_sec = 1234567890;
153 old_times[0].tv_usec = 0;
154 old_times[1].tv_sec = 1234567890;
155 old_times[1].tv_usec = 0;
156
157 /* set time to way back */
158 cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times));
159 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before));
160
161 /* ensure that packfile is freshened */
162 cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
163 CONST_STRLEN(PACKED_STR), GIT_OBJECT_BLOB));
164 cl_assert_equal_oid(&expected_id, &id);
165 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
166
167 cl_assert(before.st_atime < after.st_atime);
168 cl_assert(before.st_mtime < after.st_mtime);
169
170 memcpy(&before, &after, sizeof(struct stat));
171
172 /* ensure that the pack file is not freshened again immediately */
173 cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
174 CONST_STRLEN(PACKED_STR), GIT_OBJECT_BLOB));
175 cl_assert_equal_oid(&expected_id, &id);
176 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
177
178 cl_assert(before.st_atime == after.st_atime);
179 cl_assert(before.st_atime_nsec == after.st_atime_nsec);
180 cl_assert(before.st_mtime == after.st_mtime);
181 cl_assert(before.st_mtime_nsec == after.st_mtime_nsec);
182 }
183