Skip to content

Commit e4bdba5

Browse files
authored
Merge pull request libgit2#5515 from pks-t/pks/flaky-checkout-test
tests: checkout: fix flaky test due to mtime race
2 parents 3b7b4d2 + f88e12d commit e4bdba5

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/checkout.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ static bool checkout_is_workdir_modified(
217217
ie = git_index_get_bypath(data->index, wditem->path, 0);
218218

219219
if (ie != NULL &&
220-
git_index_time_eq(&wditem->mtime, &ie->mtime) &&
221-
wditem->file_size == ie->file_size &&
222-
!is_filemode_changed(wditem->mode, ie->mode, data->respect_filemode)) {
220+
!git_index_entry_newer_than_index(ie, data->index) &&
221+
git_index_time_eq(&wditem->mtime, &ie->mtime) &&
222+
wditem->file_size == ie->file_size &&
223+
!is_filemode_changed(wditem->mode, ie->mode, data->respect_filemode)) {
223224

224225
/* The workdir is modified iff the index entry is modified */
225226
return !is_workdir_base_or_new(&ie->id, baseitem, newitem) ||

tests/checkout/index.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,18 @@ void test_checkout_index__can_remove_untracked_files(void)
9191

9292
void test_checkout_index__can_disable_pathspec_match(void)
9393
{
94-
static git_index *index;
95-
git_oid commit_id;
96-
git_checkout_options g_opts = GIT_CHECKOUT_OPTIONS_INIT;
97-
git_object *g_object;
98-
9994
char *files_to_checkout[] = { "test10.txt", "test11.txt"};
100-
size_t files_to_checkout_size = 2;
95+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
96+
git_object *objects;
97+
git_index *index;
10198

10299
/* reset to beginning of history (i.e. just a README file) */
103-
g_opts.checkout_strategy =
104-
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
100+
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
105101

106-
cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
107-
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
108-
cl_git_pass(
109-
git_repository_set_head_detached(g_repo, git_object_id(g_object)));
110-
git_object_free(g_object);
111-
g_object = NULL;
102+
cl_git_pass(git_revparse_single(&objects, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
103+
cl_git_pass(git_checkout_tree(g_repo, objects, &opts));
104+
cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(objects)));
105+
git_object_free(objects);
112106

113107
cl_git_pass(git_repository_index(&index, g_repo));
114108

@@ -124,7 +118,7 @@ void test_checkout_index__can_disable_pathspec_match(void)
124118
cl_git_pass(git_index_add_bypath(index, "test12.txt"));
125119
cl_git_pass(git_index_write(index));
126120

127-
cl_repo_commit_from_index(&commit_id, g_repo, NULL, 0, "commit our test files");
121+
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "commit our test files");
128122

129123
/* We modify the content of all 4 of our files */
130124
cl_git_rewritefile("testrepo/test9.txt", "modified\n");
@@ -133,19 +127,21 @@ void test_checkout_index__can_disable_pathspec_match(void)
133127
cl_git_rewritefile("testrepo/test12.txt", "modified\n");
134128

135129
/* We checkout only test10.txt and test11.txt */
136-
g_opts.checkout_strategy =
130+
opts.checkout_strategy =
137131
GIT_CHECKOUT_FORCE |
138132
GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
139-
g_opts.paths.strings = files_to_checkout;
140-
g_opts.paths.count = files_to_checkout_size;
141-
cl_git_pass(git_checkout_index(g_repo, NULL, &g_opts));
133+
opts.paths.strings = files_to_checkout;
134+
opts.paths.count = ARRAY_SIZE(files_to_checkout);
135+
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
142136

143137
/* The only files that have been reverted to their original content
144138
should be test10.txt and test11.txt */
145139
check_file_contents("testrepo/test9.txt", "modified\n");
146140
check_file_contents("testrepo/test10.txt", "original\n");
147141
check_file_contents("testrepo/test11.txt", "original\n");
148142
check_file_contents("testrepo/test12.txt", "modified\n");
143+
144+
git_index_free(index);
149145
}
150146

151147
void test_checkout_index__honor_the_specified_pathspecs(void)

0 commit comments

Comments
 (0)