summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-06-15 14:32:08 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-06-16 08:40:45 +0200
commitc4e6ab5f23819c35dc0c1a0fd7f50e5a194f0d5a (patch)
treefb858ad324aa607a9a81013c0a4066518361ba06
parent316b820b6fed712d4b6fae886ba1e6c44afc6c81 (diff)
downloadlibgit2-c4e6ab5f23819c35dc0c1a0fd7f50e5a194f0d5a.tar.gz
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.
-rw-r--r--src/unix/posix.h1
-rw-r--r--tests/checkout/crlf.c30
2 files changed, 27 insertions, 4 deletions
diff --git a/src/unix/posix.h b/src/unix/posix.h
index 8b4f427f7..35bd431b8 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 381b04013..e0d94e79f 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);