1 #include "clar_libgit2.h"
2 #include "git2/merge.h"
5 #include "../merge_helpers.h"
8 #define TEST_REPO_PATH "merge-resolve"
9 #define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520"
11 #define AUTOMERGEABLE_MERGED_FILE \
12 "this file is changed in master\n" \
13 "this file is automergeable\n" \
14 "this file is automergeable\n" \
15 "this file is automergeable\n" \
16 "this file is automergeable\n" \
17 "this file is automergeable\n" \
18 "this file is automergeable\n" \
19 "this file is automergeable\n" \
20 "this file is changed in branch\n"
22 #define CHANGED_IN_BRANCH_FILE \
25 static git_repository
*repo
;
26 static git_index
*repo_index
;
28 static char *unaffected
[][4] = {
29 { "added-in-master.txt", NULL
},
30 { "changed-in-master.txt", NULL
},
31 { "unchanged.txt", NULL
},
32 { "added-in-master.txt", "changed-in-master.txt", NULL
},
33 { "added-in-master.txt", "unchanged.txt", NULL
},
34 { "changed-in-master.txt", "unchanged.txt", NULL
},
35 { "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL
},
36 { "new_file.txt", NULL
},
37 { "new_file.txt", "unchanged.txt", NULL
},
41 static char *affected
[][5] = {
42 { "automergeable.txt", NULL
},
43 { "changed-in-branch.txt", NULL
},
44 { "conflicting.txt", NULL
},
45 { "removed-in-branch.txt", NULL
},
46 { "automergeable.txt", "changed-in-branch.txt", NULL
},
47 { "automergeable.txt", "conflicting.txt", NULL
},
48 { "automergeable.txt", "removed-in-branch.txt", NULL
},
49 { "changed-in-branch.txt", "conflicting.txt", NULL
},
50 { "changed-in-branch.txt", "removed-in-branch.txt", NULL
},
51 { "conflicting.txt", "removed-in-branch.txt", NULL
},
52 { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL
},
53 { "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL
},
54 { "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
55 { "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
56 { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
60 static char *result_contents
[4][6] = {
61 { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE
, NULL
, NULL
},
62 { "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE
, NULL
, NULL
},
63 { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE
, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE
, NULL
, NULL
},
67 void test_merge_workdir_dirty__initialize(void)
69 repo
= cl_git_sandbox_init(TEST_REPO_PATH
);
70 git_repository_index(&repo_index
, repo
);
73 void test_merge_workdir_dirty__cleanup(void)
75 git_index_free(repo_index
);
76 cl_git_sandbox_cleanup();
79 static void set_core_autocrlf_to(git_repository
*repo
, bool value
)
83 cl_git_pass(git_repository_config(&cfg
, repo
));
84 cl_git_pass(git_config_set_bool(cfg
, "core.autocrlf", value
));
89 static int merge_branch(void)
91 git_oid their_oids
[1];
92 git_annotated_commit
*their_head
;
93 git_merge_options merge_opts
= GIT_MERGE_OPTIONS_INIT
;
94 git_checkout_options checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
97 cl_git_pass(git_oid_fromstr(&their_oids
[0], MERGE_BRANCH_OID
));
98 cl_git_pass(git_annotated_commit_lookup(&their_head
, repo
, &their_oids
[0]));
100 checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
101 error
= git_merge(repo
, (const git_annotated_commit
**)&their_head
, 1, &merge_opts
, &checkout_opts
);
103 git_annotated_commit_free(their_head
);
108 static void write_files(char *files
[])
111 git_str path
= GIT_STR_INIT
, content
= GIT_STR_INIT
;
114 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
]) {
115 git_str_clear(&path
);
116 git_str_clear(&content
);
118 git_str_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
);
119 git_str_printf(&content
, "This is a dirty file in the working directory!\n\n"
120 "It will not be staged! Its filename is %s.\n", filename
);
122 cl_git_mkfile(path
.ptr
, content
.ptr
);
125 git_str_dispose(&path
);
126 git_str_dispose(&content
);
129 static void hack_index(char *files
[])
133 git_str path
= GIT_STR_INIT
;
134 git_index_entry
*entry
;
135 struct p_timeval times
[2];
139 /* Update the index to suggest that checkout placed these files on
140 * disk, keeping the object id but updating the cache, which will
141 * emulate a Git implementation's different filter.
143 * We set the file's timestamp to before now to pretend that
144 * it was an old checkout so we don't trigger the racy
145 * protections would would check the content.
149 times
[0].tv_sec
= now
- 5;
150 times
[0].tv_usec
= 0;
151 times
[1].tv_sec
= now
- 5;
152 times
[1].tv_usec
= 0;
154 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
]) {
155 git_str_clear(&path
);
157 cl_assert(entry
= (git_index_entry
*)
158 git_index_get_bypath(repo_index
, filename
, 0));
160 cl_git_pass(git_str_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
));
161 cl_git_pass(p_utimes(path
.ptr
, times
));
162 cl_git_pass(p_stat(path
.ptr
, &statbuf
));
164 entry
->ctime
.seconds
= (int32_t)statbuf
.st_ctime
;
165 entry
->mtime
.seconds
= (int32_t)statbuf
.st_mtime
;
166 #if defined(GIT_USE_NSEC)
167 entry
->ctime
.nanoseconds
= statbuf
.st_ctime_nsec
;
168 entry
->mtime
.nanoseconds
= statbuf
.st_mtime_nsec
;
170 entry
->ctime
.nanoseconds
= 0;
171 entry
->mtime
.nanoseconds
= 0;
173 entry
->dev
= statbuf
.st_dev
;
174 entry
->ino
= statbuf
.st_ino
;
175 entry
->uid
= statbuf
.st_uid
;
176 entry
->gid
= statbuf
.st_gid
;
177 entry
->file_size
= (uint32_t)statbuf
.st_size
;
180 git_str_dispose(&path
);
183 static void stage_random_files(char *files
[])
190 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
])
191 cl_git_pass(git_index_add_bypath(repo_index
, filename
));
194 static void stage_content(char *content
[])
197 git_object
*head_object
;
198 git_str path
= GIT_STR_INIT
;
199 char *filename
, *text
;
202 cl_git_pass(git_repository_head(&head
, repo
));
203 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
204 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
206 for (i
= 0, filename
= content
[i
], text
= content
[++i
];
208 filename
= content
[++i
], text
= content
[++i
]) {
210 git_str_clear(&path
);
212 cl_git_pass(git_str_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
));
214 cl_git_mkfile(path
.ptr
, text
);
215 cl_git_pass(git_index_add_bypath(repo_index
, filename
));
218 git_object_free(head_object
);
219 git_reference_free(head
);
220 git_str_dispose(&path
);
223 static int merge_dirty_files(char *dirty_files
[])
226 git_object
*head_object
;
229 cl_git_pass(git_repository_head(&head
, repo
));
230 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
231 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
233 write_files(dirty_files
);
235 error
= merge_branch();
237 git_object_free(head_object
);
238 git_reference_free(head
);
243 static int merge_differently_filtered_files(char *files
[])
246 git_object
*head_object
;
249 cl_git_pass(git_repository_head(&head
, repo
));
250 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
251 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
253 /* Emulate checkout with a broken or misconfigured filter: modify some
254 * files on-disk and then update the index with the updated file size
255 * and time, as if some filter applied them. These files should not be
256 * treated as dirty since we created them.
258 * (Make sure to update the index stamp to defeat racy-git protections
259 * trying to sanity check the files in the index; those would rehash the
260 * files, showing them as dirty, the exact mechanism we're trying to avoid.)
266 cl_git_pass(git_index_write(repo_index
));
268 error
= merge_branch();
270 git_object_free(head_object
);
271 git_reference_free(head
);
276 static int merge_staged_files(char *staged_files
[])
278 stage_random_files(staged_files
);
279 return merge_branch();
282 void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void)
287 for (i
= 0, files
= unaffected
[i
]; files
[0]; files
= unaffected
[++i
])
288 cl_git_pass(merge_dirty_files(files
));
291 void test_merge_workdir_dirty__unstaged_deletes_maintained(void)
294 git_object
*head_object
;
296 cl_git_pass(git_repository_head(&head
, repo
));
297 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
298 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
300 cl_git_pass(p_unlink("merge-resolve/unchanged.txt"));
302 cl_git_pass(merge_branch());
304 git_object_free(head_object
);
305 git_reference_free(head
);
308 void test_merge_workdir_dirty__affected_dirty_files_disallowed(void)
313 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
314 cl_git_fail(merge_dirty_files(files
));
317 void test_merge_workdir_dirty__staged_files_in_index_disallowed(void)
322 for (i
= 0, files
= unaffected
[i
]; files
[0]; files
= unaffected
[++i
])
323 cl_git_fail(merge_staged_files(files
));
325 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
326 cl_git_fail(merge_staged_files(files
));
329 void test_merge_workdir_dirty__identical_staged_files_allowed(void)
334 set_core_autocrlf_to(repo
, false);
336 for (i
= 0, content
= result_contents
[i
]; content
[0]; content
= result_contents
[++i
]) {
337 stage_content(content
);
339 cl_git_pass(git_index_write(repo_index
));
340 cl_git_pass(merge_branch());
344 void test_merge_workdir_dirty__honors_cache(void)
349 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
350 cl_git_pass(merge_differently_filtered_files(files
));