summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@github.com>2016-07-14 16:23:24 -0400
committerEdward Thomson <ethomson@github.com>2016-08-04 15:12:04 -0400
commit8f09a98e1809dcdfd9d25b8268657bac4d942e6a (patch)
tree25f4b977d9a9055569d95a5ba5b038dfbfb01d6a /tests
parentd2794b0e37e98206b991ba4c8639ddf53c03bdb9 (diff)
downloadlibgit2-8f09a98e1809dcdfd9d25b8268657bac4d942e6a.tar.gz
odb: freshen existing objects when writing
When writing an object, we calculate its OID and see if it exists in the object database. If it does, we need to freshen the file that contains it.
Diffstat (limited to 'tests')
-rw-r--r--tests/odb/freshen.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/odb/freshen.c b/tests/odb/freshen.c
new file mode 100644
index 000000000..0f2e0d017
--- /dev/null
+++ b/tests/odb/freshen.c
@@ -0,0 +1,79 @@
+#include "clar_libgit2.h"
+#include "odb.h"
+#include "posix.h"
+
+static git_repository *repo;
+static git_odb *odb;
+
+void test_odb_freshen__initialize(void)
+{
+ repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_pass(git_repository_odb(&odb, repo));
+}
+
+void test_odb_freshen__cleanup(void)
+{
+ git_odb_free(odb);
+ cl_git_sandbox_cleanup();
+}
+
+#define LOOSE_STR "hey\n"
+#define LOOSE_ID "1385f264afb75a56a5bec74243be9b367ba4ca08"
+#define LOOSE_FN "13/85f264afb75a56a5bec74243be9b367ba4ca08"
+
+void test_odb_freshen__loose_object(void)
+{
+ git_oid expected_id, id;
+ struct stat before, after;
+ struct p_timeval old_times[2];
+
+ cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_ID));
+
+ old_times[0].tv_sec = 1234567890;
+ old_times[0].tv_usec = 0;
+ old_times[1].tv_sec = 1234567890;
+ old_times[1].tv_usec = 0;
+
+ /* set time to way back */
+ cl_must_pass(p_utimes("testrepo.git/objects/" LOOSE_FN, old_times));
+ cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &before));
+
+ cl_git_pass(git_odb_write(&id, odb, LOOSE_STR, CONST_STRLEN(LOOSE_STR),
+ GIT_OBJ_BLOB));
+ cl_assert_equal_oid(&expected_id, &id);
+ cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &after));
+
+ cl_assert(before.st_atime < after.st_atime);
+ cl_assert(before.st_mtime < after.st_mtime);
+}
+
+#define PACKED_STR "Testing a readme.txt\n"
+#define PACKED_ID "6336846bd5c88d32f93ae57d846683e61ab5c530"
+#define PACKED_FN "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack"
+
+void test_odb_freshen__packed_object(void)
+{
+ git_oid expected_id, id;
+ struct stat before, after;
+ struct p_timeval old_times[2];
+
+ cl_git_pass(git_oid_fromstr(&expected_id, PACKED_ID));
+
+ old_times[0].tv_sec = 1234567890;
+ old_times[0].tv_usec = 0;
+ old_times[1].tv_sec = 1234567890;
+ old_times[1].tv_usec = 0;
+
+ /* set time to way back */
+ cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times));
+ cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before));
+
+ cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
+ CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB));
+ cl_assert_equal_oid(&expected_id, &id);
+ cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
+
+ cl_assert(before.st_atime < after.st_atime);
+ cl_assert(before.st_mtime < after.st_mtime);
+}
+