]>
Commit | Line | Data |
---|---|---|
99a09f7f ET |
1 | #include "clar_libgit2.h" |
2 | #include "index.h" | |
3 | #include "git2/sys/index.h" | |
4 | #include "git2/repository.h" | |
5 | #include "../reset/reset_helpers.h" | |
6 | ||
7 | static git_repository *repo; | |
8 | static git_index *repo_index; | |
9 | ||
10 | #define TEST_REPO_PATH "nsecs" | |
11 | ||
ac3d33df | 12 | /* Fixture setup and teardown */ |
99a09f7f ET |
13 | void test_index_nsec__initialize(void) |
14 | { | |
15 | repo = cl_git_sandbox_init("nsecs"); | |
16 | git_repository_index(&repo_index, repo); | |
17 | } | |
18 | ||
19 | void test_index_nsec__cleanup(void) | |
20 | { | |
21 | git_index_free(repo_index); | |
22 | repo_index = NULL; | |
23 | ||
24 | cl_git_sandbox_cleanup(); | |
25 | } | |
26 | ||
565c4199 ET |
27 | static bool try_create_file_with_nsec_timestamp(const char *path) |
28 | { | |
29 | struct stat st; | |
30 | int try; | |
31 | ||
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!"); | |
35 | ||
36 | cl_must_pass(p_stat(path, &st)); | |
37 | ||
38 | if (st.st_ctime_nsec && st.st_mtime_nsec) | |
39 | return true; | |
40 | } | |
41 | ||
42 | return false; | |
43 | } | |
44 | ||
45 | /* try to determine if the underlying filesystem supports a resolution | |
46 | * higher than a single second. (i'm looking at you, hfs+) | |
47 | */ | |
48 | static bool should_expect_nsecs(void) | |
49 | { | |
e579e0f7 | 50 | git_str nsec_path = GIT_STR_INIT; |
565c4199 ET |
51 | bool expect; |
52 | ||
e579e0f7 | 53 | git_str_joinpath(&nsec_path, clar_sandbox_path(), "nsec_test"); |
565c4199 ET |
54 | |
55 | expect = try_create_file_with_nsec_timestamp(nsec_path.ptr); | |
56 | ||
22a2d3d5 | 57 | cl_must_pass(p_unlink(nsec_path.ptr)); |
565c4199 | 58 | |
e579e0f7 | 59 | git_str_dispose(&nsec_path); |
565c4199 ET |
60 | |
61 | return expect; | |
62 | } | |
63 | ||
99a09f7f ET |
64 | static bool has_nsecs(void) |
65 | { | |
66 | const git_index_entry *entry; | |
67 | size_t i; | |
68 | bool has_nsecs = false; | |
69 | ||
70 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
71 | entry = git_index_get_byindex(repo_index, i); | |
72 | ||
73 | if (entry->ctime.nanoseconds || entry->mtime.nanoseconds) { | |
74 | has_nsecs = true; | |
75 | break; | |
76 | } | |
77 | } | |
78 | ||
79 | return has_nsecs; | |
80 | } | |
81 | ||
82 | void test_index_nsec__has_nanos(void) | |
83 | { | |
84 | cl_assert_equal_b(true, has_nsecs()); | |
85 | } | |
86 | ||
87 | void test_index_nsec__staging_maintains_other_nanos(void) | |
88 | { | |
89 | const git_index_entry *entry; | |
565c4199 ET |
90 | bool expect_nsec, test_file_has_nsec; |
91 | ||
e756877d | 92 | expect_nsec = should_expect_nsecs(); |
565c4199 ET |
93 | test_file_has_nsec = try_create_file_with_nsec_timestamp("nsecs/a.txt"); |
94 | ||
95 | cl_assert_equal_b(expect_nsec, test_file_has_nsec); | |
99a09f7f | 96 | |
99a09f7f ET |
97 | cl_git_pass(git_index_add_bypath(repo_index, "a.txt")); |
98 | cl_git_pass(git_index_write(repo_index)); | |
99 | ||
100 | cl_git_pass(git_index_write(repo_index)); | |
101 | ||
102 | git_index_read(repo_index, 1); | |
103 | cl_assert_equal_b(true, has_nsecs()); | |
104 | ||
105 | cl_assert((entry = git_index_get_bypath(repo_index, "a.txt", 0))); | |
a4c55069 ET |
106 | |
107 | /* if we are writing nanoseconds to the index, expect them to be | |
565c4199 | 108 | * nonzero. |
a4c55069 | 109 | */ |
565c4199 ET |
110 | if (expect_nsec) { |
111 | cl_assert(entry->ctime.nanoseconds != 0); | |
112 | cl_assert(entry->mtime.nanoseconds != 0); | |
113 | } else { | |
114 | cl_assert_equal_i(0, entry->ctime.nanoseconds); | |
115 | cl_assert_equal_i(0, entry->mtime.nanoseconds); | |
116 | } | |
99a09f7f ET |
117 | } |
118 | ||
119 | void test_index_nsec__status_doesnt_clear_nsecs(void) | |
120 | { | |
121 | git_status_list *statuslist; | |
122 | ||
123 | cl_git_pass(git_status_list_new(&statuslist, repo, NULL)); | |
124 | ||
125 | git_index_read(repo_index, 1); | |
126 | cl_assert_equal_b(true, has_nsecs()); | |
127 | ||
128 | git_status_list_free(statuslist); | |
129 | } |