summaryrefslogtreecommitdiff
path: root/tests/diff/racy.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-06-17 14:34:10 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-06-22 12:47:30 +0200
commitff47537557f0ac1919e77c5cb21f36f2e98425de (patch)
treeb7164edbbc40fcdea677c1d178c1052e7b197ef0 /tests/diff/racy.c
parente96a97f18e8f961c434e4fa4fc2c7d950480b9e9 (diff)
downloadlibgit2-ff47537557f0ac1919e77c5cb21f36f2e98425de.tar.gz
diff: check files with the same or newer timestamps
When a file on the workdir has the same or a newer timestamp than the index, we need to perform a full check of the contents, as the update of the file may have happened just after we wrote the index. The iterator changes are such that we can reach inside the workdir iterator from the diff, though it may be better to have an accessor instead of moving these structs into the header.
Diffstat (limited to 'tests/diff/racy.c')
-rw-r--r--tests/diff/racy.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/diff/racy.c b/tests/diff/racy.c
index a109f8c3b..66630cc4d 100644
--- a/tests/diff/racy.c
+++ b/tests/diff/racy.c
@@ -1,4 +1,5 @@
#include "clar_libgit2.h"
+#include "../checkout/checkout_helpers.h"
#include "buffer.h"
@@ -37,3 +38,35 @@ void test_diff_racy__diff(void)
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
}
+
+void test_diff_racy__write_index_just_after_file(void)
+{
+ git_index *index;
+ git_diff *diff;
+ git_buf path = GIT_BUF_INIT;
+
+ /* Make sure we do have a timestamp */
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_write(index));
+ /* The timestamp will be one second before we change the file */
+ sleep(1);
+
+ cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
+ cl_git_mkfile(path.ptr, "A");
+
+ /*
+ * Put 'A' into the index, the size field will be filled,
+ * because the index' on-disk timestamp does not match the
+ * file's timestamp. Both timestamps will however match after
+ * writing out the index.
+ */
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_add_bypath(index, "A"));
+ cl_git_pass(git_index_write(index));
+
+ /* Change its contents quickly, so we get the same timestamp */
+ cl_git_mkfile(path.ptr, "B");
+
+ cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
+ cl_assert_equal_i(1, git_diff_num_deltas(diff));
+}