]>
git.proxmox.com Git - libgit2.git/blob - tests/index/nsec.c
1 #include "clar_libgit2.h"
3 #include "git2/sys/index.h"
4 #include "git2/repository.h"
5 #include "../reset/reset_helpers.h"
7 static git_repository
*repo
;
8 static git_index
*repo_index
;
10 #define TEST_REPO_PATH "nsecs"
12 /* Fixture setup and teardown */
13 void test_index_nsec__initialize(void)
15 repo
= cl_git_sandbox_init("nsecs");
16 git_repository_index(&repo_index
, repo
);
19 void test_index_nsec__cleanup(void)
21 git_index_free(repo_index
);
24 cl_git_sandbox_cleanup();
27 static bool try_create_file_with_nsec_timestamp(const char *path
)
32 /* retry a few times to avoid nanos *actually* equal 0 race condition */
33 for (try = 0; try < 3; try++) {
34 cl_git_mkfile(path
, "This is hopefully a file with nanoseconds!");
36 cl_must_pass(p_stat(path
, &st
));
38 if (st
.st_ctime_nsec
&& st
.st_mtime_nsec
)
45 /* try to determine if the underlying filesystem supports a resolution
46 * higher than a single second. (i'm looking at you, hfs+)
48 static bool should_expect_nsecs(void)
50 git_buf nsec_path
= GIT_BUF_INIT
;
53 git_buf_joinpath(&nsec_path
, clar_sandbox_path(), "nsec_test");
55 expect
= try_create_file_with_nsec_timestamp(nsec_path
.ptr
);
57 cl_must_pass(p_unlink(nsec_path
.ptr
));
59 git_buf_dispose(&nsec_path
);
64 static bool has_nsecs(void)
66 const git_index_entry
*entry
;
68 bool has_nsecs
= false;
70 for (i
= 0; i
< git_index_entrycount(repo_index
); i
++) {
71 entry
= git_index_get_byindex(repo_index
, i
);
73 if (entry
->ctime
.nanoseconds
|| entry
->mtime
.nanoseconds
) {
82 void test_index_nsec__has_nanos(void)
84 cl_assert_equal_b(true, has_nsecs());
87 void test_index_nsec__staging_maintains_other_nanos(void)
89 const git_index_entry
*entry
;
90 bool expect_nsec
, test_file_has_nsec
;
92 expect_nsec
= should_expect_nsecs();
93 test_file_has_nsec
= try_create_file_with_nsec_timestamp("nsecs/a.txt");
95 cl_assert_equal_b(expect_nsec
, test_file_has_nsec
);
97 cl_git_pass(git_index_add_bypath(repo_index
, "a.txt"));
98 cl_git_pass(git_index_write(repo_index
));
100 cl_git_pass(git_index_write(repo_index
));
102 git_index_read(repo_index
, 1);
103 cl_assert_equal_b(true, has_nsecs());
105 cl_assert((entry
= git_index_get_bypath(repo_index
, "a.txt", 0)));
107 /* if we are writing nanoseconds to the index, expect them to be
111 cl_assert(entry
->ctime
.nanoseconds
!= 0);
112 cl_assert(entry
->mtime
.nanoseconds
!= 0);
114 cl_assert_equal_i(0, entry
->ctime
.nanoseconds
);
115 cl_assert_equal_i(0, entry
->mtime
.nanoseconds
);
119 void test_index_nsec__status_doesnt_clear_nsecs(void)
121 git_status_list
*statuslist
;
123 cl_git_pass(git_status_list_new(&statuslist
, repo
, NULL
));
125 git_index_read(repo_index
, 1);
126 cl_assert_equal_b(true, has_nsecs());
128 git_status_list_free(statuslist
);