Commit c4e6ab5f23819c35dc0c1a0fd7f50e5a194f0d5a

Carlos Martín Nieto 2015-06-15T14:32:08

crlf: tick the index forward to work around racy-git behaviour In order to avoid racy-git, we zero out the file size for entries with the same timestamp as the index (or during the initial checkout). This is the case in a couple of crlf tests, as the code is fast enough to do everything in the same second. As we know that we do not perform the modification just after writing out the index, which is what this is designed to work around, tick the mtime of the index file such that it doesn't agree with the files anymore, and we do not zero out these entries.

diff --git a/src/unix/posix.h b/src/unix/posix.h
index 8b4f427..35bd431 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -26,6 +26,7 @@ typedef int GIT_SOCKET;
 #define p_mkdir(p,m) mkdir(p, m)
 #define p_fsync(fd) fsync(fd)
 extern char *p_realpath(const char *, char *);
+#define p_utimensat(fd, path, times, flags) utimensat(fd, path, times, flags)
 
 #define p_recv(s,b,l,f) recv(s,b,l,f)
 #define p_send(s,b,l,f) send(s,b,l,f)
diff --git a/tests/checkout/crlf.c b/tests/checkout/crlf.c
index 381b040..e0d94e7 100644
--- a/tests/checkout/crlf.c
+++ b/tests/checkout/crlf.c
@@ -4,6 +4,7 @@
 
 #include "git2/checkout.h"
 #include "repository.h"
+#include "index.h"
 #include "posix.h"
 
 static git_repository *g_repo;
@@ -31,6 +32,25 @@ void test_checkout_crlf__detect_crlf_autocrlf_false(void)
 	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
 }
 
+static void tick_index(git_index *index)
+{
+	git_time_t ts;
+	struct timespec times[2];
+
+	cl_assert(index->on_disk);
+	cl_assert(git_index_path(index));
+
+	cl_git_pass(git_index_read(index, true));
+	ts = index->stamp.mtime;
+
+	times[0].tv_sec  = UTIME_OMIT; /* dont' change the atime */
+	times[0].tv_nsec = UTIME_OMIT; /* dont' change the atime */
+	times[1].tv_sec  = ts + 1;
+	times[1].tv_nsec = 0;
+	cl_git_pass(p_utimensat(AT_FDCWD, git_index_path(index), times, 0));
+	cl_git_pass(git_index_read(index, true));
+}
+
 void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
 {
 	git_index *index;
@@ -40,9 +60,10 @@ void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
 
 	cl_repo_set_bool(g_repo, "core.autocrlf", false);
 
-	git_checkout_head(g_repo, &opts);
-
 	git_repository_index(&index, g_repo);
+	tick_index(index);
+
+	git_checkout_head(g_repo, &opts);
 
 	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
 	cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
@@ -140,9 +161,10 @@ void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
 
 	cl_repo_set_bool(g_repo, "core.autocrlf", true);
 
-	git_checkout_head(g_repo, &opts);
-
 	git_repository_index(&index, g_repo);
+	tick_index(index);
+
+	git_checkout_head(g_repo, &opts);
 
 	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);