1 #include "clar_libgit2.h"
2 #include "git2/merge.h"
6 #include "../merge_helpers.h"
9 #define TEST_REPO_PATH "merge-resolve"
10 #define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520"
12 #define AUTOMERGEABLE_MERGED_FILE \
13 "this file is changed in master\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 automergeable\n" \
21 "this file is changed in branch\n"
23 #define CHANGED_IN_BRANCH_FILE \
26 static git_repository
*repo
;
27 static git_index
*repo_index
;
29 static char *unaffected
[][4] = {
30 { "added-in-master.txt", NULL
},
31 { "changed-in-master.txt", NULL
},
32 { "unchanged.txt", NULL
},
33 { "added-in-master.txt", "changed-in-master.txt", NULL
},
34 { "added-in-master.txt", "unchanged.txt", NULL
},
35 { "changed-in-master.txt", "unchanged.txt", NULL
},
36 { "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL
},
37 { "new_file.txt", NULL
},
38 { "new_file.txt", "unchanged.txt", NULL
},
42 static char *affected
[][5] = {
43 { "automergeable.txt", NULL
},
44 { "changed-in-branch.txt", NULL
},
45 { "conflicting.txt", NULL
},
46 { "removed-in-branch.txt", NULL
},
47 { "automergeable.txt", "changed-in-branch.txt", NULL
},
48 { "automergeable.txt", "conflicting.txt", NULL
},
49 { "automergeable.txt", "removed-in-branch.txt", NULL
},
50 { "changed-in-branch.txt", "conflicting.txt", NULL
},
51 { "changed-in-branch.txt", "removed-in-branch.txt", NULL
},
52 { "conflicting.txt", "removed-in-branch.txt", NULL
},
53 { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL
},
54 { "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL
},
55 { "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
56 { "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
57 { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL
},
61 static char *result_contents
[4][6] = {
62 { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE
, NULL
, NULL
},
63 { "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE
, NULL
, NULL
},
64 { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE
, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE
, NULL
, NULL
},
68 void test_merge_workdir_dirty__initialize(void)
70 repo
= cl_git_sandbox_init(TEST_REPO_PATH
);
71 git_repository_index(&repo_index
, repo
);
74 void test_merge_workdir_dirty__cleanup(void)
76 git_index_free(repo_index
);
77 cl_git_sandbox_cleanup();
80 static void set_core_autocrlf_to(git_repository
*repo
, bool value
)
84 cl_git_pass(git_repository_config(&cfg
, repo
));
85 cl_git_pass(git_config_set_bool(cfg
, "core.autocrlf", value
));
90 static int merge_branch(void)
92 git_oid their_oids
[1];
93 git_annotated_commit
*their_head
;
94 git_merge_options merge_opts
= GIT_MERGE_OPTIONS_INIT
;
95 git_checkout_options checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
98 cl_git_pass(git_oid_fromstr(&their_oids
[0], MERGE_BRANCH_OID
));
99 cl_git_pass(git_annotated_commit_lookup(&their_head
, repo
, &their_oids
[0]));
101 checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
102 error
= git_merge(repo
, (const git_annotated_commit
**)&their_head
, 1, &merge_opts
, &checkout_opts
);
104 git_annotated_commit_free(their_head
);
109 static void write_files(char *files
[])
112 git_buf path
= GIT_BUF_INIT
, content
= GIT_BUF_INIT
;
115 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
]) {
116 git_buf_clear(&path
);
117 git_buf_clear(&content
);
119 git_buf_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
);
120 git_buf_printf(&content
, "This is a dirty file in the working directory!\n\n"
121 "It will not be staged! Its filename is %s.\n", filename
);
123 cl_git_mkfile(path
.ptr
, content
.ptr
);
126 git_buf_dispose(&path
);
127 git_buf_dispose(&content
);
130 static void hack_index(char *files
[])
134 git_buf path
= GIT_BUF_INIT
;
135 git_index_entry
*entry
;
136 struct p_timeval times
[2];
140 /* Update the index to suggest that checkout placed these files on
141 * disk, keeping the object id but updating the cache, which will
142 * emulate a Git implementation's different filter.
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.
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;
155 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
]) {
156 git_buf_clear(&path
);
158 cl_assert(entry
= (git_index_entry
*)
159 git_index_get_bypath(repo_index
, filename
, 0));
161 cl_git_pass(git_buf_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
));
162 cl_git_pass(p_utimes(path
.ptr
, times
));
163 cl_git_pass(p_stat(path
.ptr
, &statbuf
));
165 entry
->ctime
.seconds
= (int32_t)statbuf
.st_ctime
;
166 entry
->mtime
.seconds
= (int32_t)statbuf
.st_mtime
;
167 #if defined(GIT_USE_NSEC)
168 entry
->ctime
.nanoseconds
= statbuf
.st_ctime_nsec
;
169 entry
->mtime
.nanoseconds
= statbuf
.st_mtime_nsec
;
171 entry
->ctime
.nanoseconds
= 0;
172 entry
->mtime
.nanoseconds
= 0;
174 entry
->dev
= statbuf
.st_dev
;
175 entry
->ino
= statbuf
.st_ino
;
176 entry
->uid
= statbuf
.st_uid
;
177 entry
->gid
= statbuf
.st_gid
;
178 entry
->file_size
= (uint32_t)statbuf
.st_size
;
181 git_buf_dispose(&path
);
184 static void stage_random_files(char *files
[])
191 for (i
= 0, filename
= files
[i
]; filename
; filename
= files
[++i
])
192 cl_git_pass(git_index_add_bypath(repo_index
, filename
));
195 static void stage_content(char *content
[])
198 git_object
*head_object
;
199 git_buf path
= GIT_BUF_INIT
;
200 char *filename
, *text
;
203 cl_git_pass(git_repository_head(&head
, repo
));
204 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
205 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
207 for (i
= 0, filename
= content
[i
], text
= content
[++i
];
209 filename
= content
[++i
], text
= content
[++i
]) {
211 git_buf_clear(&path
);
213 cl_git_pass(git_buf_printf(&path
, "%s/%s", TEST_REPO_PATH
, filename
));
215 cl_git_mkfile(path
.ptr
, text
);
216 cl_git_pass(git_index_add_bypath(repo_index
, filename
));
219 git_object_free(head_object
);
220 git_reference_free(head
);
221 git_buf_dispose(&path
);
224 static int merge_dirty_files(char *dirty_files
[])
227 git_object
*head_object
;
230 cl_git_pass(git_repository_head(&head
, repo
));
231 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
232 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
234 write_files(dirty_files
);
236 error
= merge_branch();
238 git_object_free(head_object
);
239 git_reference_free(head
);
244 static int merge_differently_filtered_files(char *files
[])
247 git_object
*head_object
;
250 cl_git_pass(git_repository_head(&head
, repo
));
251 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
252 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
254 /* Emulate checkout with a broken or misconfigured filter: modify some
255 * files on-disk and then update the index with the updated file size
256 * and time, as if some filter applied them. These files should not be
257 * treated as dirty since we created them.
259 * (Make sure to update the index stamp to defeat racy-git protections
260 * trying to sanity check the files in the index; those would rehash the
261 * files, showing them as dirty, the exact mechanism we're trying to avoid.)
267 cl_git_pass(git_index_write(repo_index
));
269 error
= merge_branch();
271 git_object_free(head_object
);
272 git_reference_free(head
);
277 static int merge_staged_files(char *staged_files
[])
279 stage_random_files(staged_files
);
280 return merge_branch();
283 void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void)
288 for (i
= 0, files
= unaffected
[i
]; files
[0]; files
= unaffected
[++i
])
289 cl_git_pass(merge_dirty_files(files
));
292 void test_merge_workdir_dirty__unstaged_deletes_maintained(void)
295 git_object
*head_object
;
297 cl_git_pass(git_repository_head(&head
, repo
));
298 cl_git_pass(git_reference_peel(&head_object
, head
, GIT_OBJECT_COMMIT
));
299 cl_git_pass(git_reset(repo
, head_object
, GIT_RESET_HARD
, NULL
));
301 cl_git_pass(p_unlink("merge-resolve/unchanged.txt"));
303 cl_git_pass(merge_branch());
305 git_object_free(head_object
);
306 git_reference_free(head
);
309 void test_merge_workdir_dirty__affected_dirty_files_disallowed(void)
314 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
315 cl_git_fail(merge_dirty_files(files
));
318 void test_merge_workdir_dirty__staged_files_in_index_disallowed(void)
323 for (i
= 0, files
= unaffected
[i
]; files
[0]; files
= unaffected
[++i
])
324 cl_git_fail(merge_staged_files(files
));
326 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
327 cl_git_fail(merge_staged_files(files
));
330 void test_merge_workdir_dirty__identical_staged_files_allowed(void)
335 set_core_autocrlf_to(repo
, false);
337 for (i
= 0, content
= result_contents
[i
]; content
[0]; content
= result_contents
[++i
]) {
338 stage_content(content
);
340 cl_git_pass(git_index_write(repo_index
));
341 cl_git_pass(merge_branch());
345 void test_merge_workdir_dirty__honors_cache(void)
350 for (i
= 0, files
= affected
[i
]; files
[0]; files
= affected
[++i
])
351 cl_git_pass(merge_differently_filtered_files(files
));