Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 565c419

Browse files
author
Edward Thomson
committed
index::nsec: don't expect shit filesystems to not suck
If the underlying filesystem doesn't support better than one second resolution, then don't expect that turning on `GIT_USE_NSEC` does anything magical to change that.
1 parent 6abdf52 commit 565c419

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

tests/index/nsec.c

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,43 @@ void test_index_nsec__cleanup(void)
2424
cl_git_sandbox_cleanup();
2525
}
2626

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+
{
50+
git_buf nsec_path = GIT_BUF_INIT;
51+
bool expect;
52+
53+
git_buf_joinpath(&nsec_path, clar_sandbox_path(), "nsec_test");
54+
55+
expect = try_create_file_with_nsec_timestamp(nsec_path.ptr);
56+
57+
p_unlink(nsec_path.ptr);
58+
59+
git_buf_clear(&nsec_path);
60+
61+
return expect;
62+
}
63+
2764
static bool has_nsecs(void)
2865
{
2966
const git_index_entry *entry;
@@ -50,8 +87,13 @@ void test_index_nsec__has_nanos(void)
5087
void test_index_nsec__staging_maintains_other_nanos(void)
5188
{
5289
const git_index_entry *entry;
90+
bool expect_nsec, test_file_has_nsec;
91+
92+
expect_nsec = should_expect_nsecs();
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);
5396

54-
cl_git_rewritefile("nsecs/a.txt", "This is file A");
5597
cl_git_pass(git_index_add_bypath(repo_index, "a.txt"));
5698
cl_git_pass(git_index_write(repo_index));
5799

@@ -63,15 +105,15 @@ void test_index_nsec__staging_maintains_other_nanos(void)
63105
cl_assert((entry = git_index_get_bypath(repo_index, "a.txt", 0)));
64106

65107
/* if we are writing nanoseconds to the index, expect them to be
66-
* nonzero. if we are *not*, expect that we truncated the entry.
108+
* nonzero.
67109
*/
68-
#ifdef GIT_USE_NSEC
69-
cl_assert(entry->ctime.nanoseconds != 0);
70-
cl_assert(entry->mtime.nanoseconds != 0);
71-
#else
72-
cl_assert_equal_i(0, entry->ctime.nanoseconds);
73-
cl_assert_equal_i(0, entry->mtime.nanoseconds);
74-
#endif
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+
}
75117
}
76118

77119
void test_index_nsec__status_doesnt_clear_nsecs(void)

0 commit comments

Comments
 (0)