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

Skip to content

Commit eeff96c

Browse files
committed
Merge pull request libgit2#3655 from ethomson/nanosecond_defaults
Enable nanosecond resolution by default
2 parents eee1799 + 53fb823 commit eeff96c

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ELSE ()
109109
ENDIF()
110110

111111
IF (HAVE_STRUCT_STAT_NSEC OR WIN32)
112-
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" OFF )
112+
OPTION( USE_NSEC "Care about sub-second file mtimes and ctimes" ON )
113113
ENDIF()
114114

115115
# This variable will contain the libraries we need to put into

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)

tests/index/racy.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,29 @@ static void setup_race(void)
105105
{
106106
git_buf path = GIT_BUF_INIT;
107107
git_index *index;
108-
const git_index_entry *entry;
109-
int i, found_race = 0;
108+
git_index_entry *entry;
109+
struct stat st;
110110

111111
/* Make sure we do have a timestamp */
112112
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
113113
cl_git_pass(git_index_write(index));
114114

115115
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
116116

117-
/* Make sure writing the file, adding and rewriting happen in the same second */
118-
for (i = 0; i < 10; i++) {
119-
struct stat st;
120-
cl_git_mkfile(path.ptr, "A");
117+
cl_git_mkfile(path.ptr, "A");
118+
cl_git_pass(git_index_add_bypath(index, "A"));
121119

122-
cl_git_pass(git_index_add_bypath(index, "A"));
123-
cl_git_mkfile(path.ptr, "B");
124-
cl_git_pass(git_index_write(index));
120+
cl_git_mkfile(path.ptr, "B");
121+
cl_git_pass(git_index_write(index));
125122

126-
cl_git_mkfile(path.ptr, "");
123+
cl_git_mkfile(path.ptr, "");
127124

128-
cl_git_pass(p_stat(path.ptr, &st));
129-
cl_assert(entry = git_index_get_bypath(index, "A", 0));
130-
if (entry->mtime.seconds == (int32_t) st.st_mtime) {
131-
found_race = 1;
132-
break;
133-
}
134-
}
125+
cl_git_pass(p_stat(path.ptr, &st));
126+
cl_assert(entry = (git_index_entry *)git_index_get_bypath(index, "A", 0));
135127

136-
if (!found_race)
137-
cl_fail("failed to find race after 10 attempts");
128+
/* force a race */
129+
entry->mtime.seconds = st.st_mtime;
130+
entry->mtime.nanoseconds = st.st_mtime_nsec;
138131

139132
git_buf_free(&path);
140133
}

tests/merge/workdir/dirty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ static void hack_index(char *files[])
165165
entry->ctime.seconds = (int32_t)statbuf.st_ctime;
166166
entry->mtime.seconds = (int32_t)statbuf.st_mtime;
167167
#if defined(GIT_USE_NSEC)
168-
entry->ctime.nanoseconds = statbuf.st_ctim.tv_nsec;
169-
entry->mtime.nanoseconds = statbuf.st_mtim.tv_nsec;
168+
entry->ctime.nanoseconds = statbuf.st_ctime_nsec;
169+
entry->mtime.nanoseconds = statbuf.st_mtime_nsec;
170170
#else
171171
entry->ctime.nanoseconds = 0;
172172
entry->mtime.nanoseconds = 0;

0 commit comments

Comments
 (0)