|
| 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 | +#define LOOSE_STR "hey\n" |
| 21 | +#define LOOSE_ID "1385f264afb75a56a5bec74243be9b367ba4ca08" |
| 22 | +#define LOOSE_FN "13/85f264afb75a56a5bec74243be9b367ba4ca08" |
| 23 | + |
| 24 | +void test_odb_freshen__loose_object(void) |
| 25 | +{ |
| 26 | + git_oid expected_id, id; |
| 27 | + struct stat before, after; |
| 28 | + struct p_timeval old_times[2]; |
| 29 | + |
| 30 | + cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_ID)); |
| 31 | + |
| 32 | + old_times[0].tv_sec = 1234567890; |
| 33 | + old_times[0].tv_usec = 0; |
| 34 | + old_times[1].tv_sec = 1234567890; |
| 35 | + old_times[1].tv_usec = 0; |
| 36 | + |
| 37 | + /* set time to way back */ |
| 38 | + cl_must_pass(p_utimes("testrepo.git/objects/" LOOSE_FN, old_times)); |
| 39 | + cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &before)); |
| 40 | + |
| 41 | + cl_git_pass(git_odb_write(&id, odb, LOOSE_STR, CONST_STRLEN(LOOSE_STR), |
| 42 | + GIT_OBJ_BLOB)); |
| 43 | + cl_assert_equal_oid(&expected_id, &id); |
| 44 | + cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &after)); |
| 45 | + |
| 46 | + cl_assert(before.st_atime < after.st_atime); |
| 47 | + cl_assert(before.st_mtime < after.st_mtime); |
| 48 | +} |
| 49 | + |
| 50 | +#define PACKED_STR "Testing a readme.txt\n" |
| 51 | +#define PACKED_ID "6336846bd5c88d32f93ae57d846683e61ab5c530" |
| 52 | +#define PACKED_FN "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack" |
| 53 | + |
| 54 | +void test_odb_freshen__packed_object(void) |
| 55 | +{ |
| 56 | + git_oid expected_id, id; |
| 57 | + struct stat before, after; |
| 58 | + struct p_timeval old_times[2]; |
| 59 | + |
| 60 | + cl_git_pass(git_oid_fromstr(&expected_id, PACKED_ID)); |
| 61 | + |
| 62 | + old_times[0].tv_sec = 1234567890; |
| 63 | + old_times[0].tv_usec = 0; |
| 64 | + old_times[1].tv_sec = 1234567890; |
| 65 | + old_times[1].tv_usec = 0; |
| 66 | + |
| 67 | + /* set time to way back */ |
| 68 | + cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times)); |
| 69 | + cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before)); |
| 70 | + |
| 71 | + /* ensure that packfile is freshened */ |
| 72 | + cl_git_pass(git_odb_write(&id, odb, PACKED_STR, |
| 73 | + CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB)); |
| 74 | + cl_assert_equal_oid(&expected_id, &id); |
| 75 | + cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after)); |
| 76 | + |
| 77 | + cl_assert(before.st_atime < after.st_atime); |
| 78 | + cl_assert(before.st_mtime < after.st_mtime); |
| 79 | + |
| 80 | + memcpy(&before, &after, sizeof(struct stat)); |
| 81 | + |
| 82 | + /* ensure that the pack file is not freshened again immediately */ |
| 83 | + cl_git_pass(git_odb_write(&id, odb, PACKED_STR, |
| 84 | + CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB)); |
| 85 | + cl_assert_equal_oid(&expected_id, &id); |
| 86 | + cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after)); |
| 87 | + |
| 88 | + cl_assert(before.st_atime == after.st_atime); |
| 89 | + cl_assert(before.st_atime_nsec == after.st_atime_nsec); |
| 90 | + cl_assert(before.st_mtime == after.st_mtime); |
| 91 | + cl_assert(before.st_mtime_nsec == after.st_mtime_nsec); |
| 92 | +} |
| 93 | + |
0 commit comments