|
2 | 2 | #include "../checkout/checkout_helpers.h"
|
3 | 3 |
|
4 | 4 | #include "buffer.h"
|
| 5 | +#include "index.h" |
5 | 6 |
|
6 | 7 | static git_repository *g_repo;
|
7 | 8 |
|
@@ -52,29 +53,48 @@ void test_diff_racy__write_index_just_after_file(void)
|
52 | 53 | git_index *index;
|
53 | 54 | git_diff *diff;
|
54 | 55 | git_buf path = GIT_BUF_INIT;
|
| 56 | + struct timeval times[2]; |
55 | 57 |
|
56 | 58 | /* Make sure we do have a timestamp */
|
57 | 59 | cl_git_pass(git_repository_index(&index, g_repo));
|
58 | 60 | cl_git_pass(git_index_write(index));
|
59 |
| - /* The timestamp will be one second before we change the file */ |
60 |
| - sleep(1); |
61 | 61 |
|
62 | 62 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
|
63 | 63 | cl_git_mkfile(path.ptr, "A");
|
| 64 | + /* Force the file's timestamp to be a second after we wrote the index */ |
| 65 | + times[0].tv_sec = index->stamp.mtime + 1; |
| 66 | + times[0].tv_usec = 0; |
| 67 | + times[1].tv_sec = index->stamp.mtime + 1; |
| 68 | + times[1].tv_usec = 0; |
| 69 | + cl_git_pass(p_utimes(path.ptr, times)); |
64 | 70 |
|
65 | 71 | /*
|
66 | 72 | * Put 'A' into the index, the size field will be filled,
|
67 | 73 | * because the index' on-disk timestamp does not match the
|
68 |
| - * file's timestamp. Both timestamps will however match after |
69 |
| - * writing out the index. |
| 74 | + * file's timestamp. |
70 | 75 | */
|
71 |
| - cl_git_pass(git_repository_index(&index, g_repo)); |
72 | 76 | cl_git_pass(git_index_add_bypath(index, "A"));
|
73 | 77 | cl_git_pass(git_index_write(index));
|
74 | 78 |
|
75 |
| - /* Change its contents quickly, so we get the same timestamp */ |
76 | 79 | cl_git_mkfile(path.ptr, "B");
|
| 80 | + /* |
| 81 | + * Pretend this index' modification happend a second after the |
| 82 | + * file update, and rewrite the file in that same second. |
| 83 | + */ |
| 84 | + times[0].tv_sec = index->stamp.mtime + 2; |
| 85 | + times[0].tv_usec = 0; |
| 86 | + times[1].tv_sec = index->stamp.mtime + 2; |
| 87 | + times[0].tv_usec = 0; |
| 88 | + |
| 89 | + cl_git_pass(p_utimes(git_index_path(index), times)); |
| 90 | + cl_git_pass(p_utimes(path.ptr, times)); |
| 91 | + |
| 92 | + cl_git_pass(git_index_read(index, true)); |
77 | 93 |
|
78 | 94 | cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
|
79 | 95 | cl_assert_equal_i(1, git_diff_num_deltas(diff));
|
| 96 | + |
| 97 | + git_buf_free(&path); |
| 98 | + git_diff_free(diff); |
| 99 | + git_index_free(index); |
80 | 100 | }
|
0 commit comments