]> git.proxmox.com Git - libgit2.git/blob - tests/odb/freshen.c
Merge branch 'pr/3957'
[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_free(&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 LOOSE_TREE_ID "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
59 #define LOOSE_TREE_FN "94/4c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
60
61 void test_odb_freshen__loose_tree(void)
62 {
63 git_oid expected_id, id;
64 git_tree *tree;
65 struct stat before, after;
66
67 cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_TREE_ID));
68 set_time_wayback(&before, LOOSE_TREE_FN);
69
70 cl_git_pass(git_tree_lookup(&tree, repo, &expected_id));
71 cl_git_pass(git_tree_create_updated(&id, repo, tree, 0, NULL));
72
73 /* make sure we freshen a tree */
74 cl_assert_equal_oid(&expected_id, &id);
75 cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
76
77 cl_assert(before.st_atime < after.st_atime);
78 cl_assert(before.st_mtime < after.st_mtime);
79
80 git_tree_free(tree);
81 }
82
83 void test_odb_freshen__tree_during_commit(void)
84 {
85 git_oid tree_id, parent_id, commit_id;
86 git_tree *tree;
87 git_commit *parent;
88 git_signature *signature;
89 struct stat before, after;
90
91 cl_git_pass(git_oid_fromstr(&tree_id, LOOSE_TREE_ID));
92 cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
93 set_time_wayback(&before, LOOSE_TREE_FN);
94
95 cl_git_pass(git_oid_fromstr(&parent_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
96 cl_git_pass(git_commit_lookup(&parent, repo, &parent_id));
97
98 cl_git_pass(git_signature_new(&signature,
99 "Refresher", "refresher@example.com", 1488547083, 0));
100
101 cl_git_pass(git_commit_create(&commit_id, repo, NULL,
102 signature, signature, NULL, "New commit pointing to old tree",
103 tree, 1, (const git_commit **)&parent));
104
105 /* make sure we freshen the tree the commit points to */
106 cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
107 cl_assert(before.st_atime < after.st_atime);
108 cl_assert(before.st_mtime < after.st_mtime);
109
110 git_signature_free(signature);
111 git_commit_free(parent);
112 git_tree_free(tree);
113 }
114
115 #define PACKED_STR "Testing a readme.txt\n"
116 #define PACKED_ID "6336846bd5c88d32f93ae57d846683e61ab5c530"
117 #define PACKED_FN "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack"
118
119 void test_odb_freshen__packed_object(void)
120 {
121 git_oid expected_id, id;
122 struct stat before, after;
123 struct p_timeval old_times[2];
124
125 cl_git_pass(git_oid_fromstr(&expected_id, PACKED_ID));
126
127 old_times[0].tv_sec = 1234567890;
128 old_times[0].tv_usec = 0;
129 old_times[1].tv_sec = 1234567890;
130 old_times[1].tv_usec = 0;
131
132 /* set time to way back */
133 cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times));
134 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before));
135
136 /* ensure that packfile is freshened */
137 cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
138 CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB));
139 cl_assert_equal_oid(&expected_id, &id);
140 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
141
142 cl_assert(before.st_atime < after.st_atime);
143 cl_assert(before.st_mtime < after.st_mtime);
144
145 memcpy(&before, &after, sizeof(struct stat));
146
147 /* ensure that the pack file is not freshened again immediately */
148 cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
149 CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB));
150 cl_assert_equal_oid(&expected_id, &id);
151 cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
152
153 cl_assert(before.st_atime == after.st_atime);
154 cl_assert(before.st_atime_nsec == after.st_atime_nsec);
155 cl_assert(before.st_mtime == after.st_mtime);
156 cl_assert(before.st_mtime_nsec == after.st_mtime_nsec);
157 }
158