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

Skip to content

Commit 5b05f95

Browse files
committed
merge: work around write-side racy protection when hacking the index
As we attempt to replicate a situation in which an older checkout has put a file on disk with different filtering settings from us, set the timestamp on the entry and file to a second before we're performing the operation so the entry in the index counts as old. This way we can test that we're not looking at the on-disk file when the index has the entry and we detect it as clean.
1 parent 26432a9 commit 5b05f95

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tests/merge/workdir/dirty.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,33 @@ static void hack_index(char *files[])
133133
struct stat statbuf;
134134
git_buf path = GIT_BUF_INIT;
135135
git_index_entry *entry;
136+
struct timeval times[2];
137+
time_t now;
136138
size_t i;
137139

138140
/* Update the index to suggest that checkout placed these files on
139141
* disk, keeping the object id but updating the cache, which will
140142
* emulate a Git implementation's different filter.
143+
*
144+
* We set the file's timestamp to before now to pretend that
145+
* it was an old checkout so we don't trigger the racy
146+
* protections would would check the content.
141147
*/
148+
149+
now = time(NULL);
150+
times[0].tv_sec = now - 5;
151+
times[0].tv_usec = 0;
152+
times[1].tv_sec = now - 5;
153+
times[1].tv_usec = 0;
154+
142155
for (i = 0, filename = files[i]; filename; filename = files[++i]) {
143156
git_buf_clear(&path);
144157

145158
cl_assert(entry = (git_index_entry *)
146159
git_index_get_bypath(repo_index, filename, 0));
147160

148161
cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename));
162+
cl_git_pass(p_utimes(path.ptr, times));
149163
cl_git_pass(p_stat(path.ptr, &statbuf));
150164

151165
entry->ctime.seconds = (git_time_t)statbuf.st_ctime;
@@ -245,7 +259,6 @@ static int merge_differently_filtered_files(char *files[])
245259
write_files(files);
246260
hack_index(files);
247261

248-
repo_index->stamp.mtime = time(NULL) + 1;
249262
cl_git_pass(git_index_write(repo_index));
250263

251264
error = merge_branch();

0 commit comments

Comments
 (0)