dirty.c 9.59 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "git2/merge.h"
#include "buffer.h"
#include "merge.h"
5
#include "index.h"
6
#include "../merge_helpers.h"
7
#include "posix.h"
8 9 10 11

#define TEST_REPO_PATH "merge-resolve"
#define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520"

12 13 14 15 16 17 18 19 20 21 22 23 24 25
#define AUTOMERGEABLE_MERGED_FILE \
	"this file is changed in master\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is automergeable\n" \
	"this file is changed in branch\n"

#define CHANGED_IN_BRANCH_FILE \
	"changed in branch\n"

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static git_repository *repo;
static git_index *repo_index;

static char *unaffected[][4] = {
	{ "added-in-master.txt", NULL },
	{ "changed-in-master.txt", NULL },
	{ "unchanged.txt", NULL },
	{ "added-in-master.txt", "changed-in-master.txt", NULL },
	{ "added-in-master.txt", "unchanged.txt", NULL },
	{ "changed-in-master.txt", "unchanged.txt", NULL },
	{ "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL },
	{ "new_file.txt", NULL },
	{ "new_file.txt", "unchanged.txt", NULL },
	{ NULL },
};

static char *affected[][5] = {
	{ "automergeable.txt", NULL },
	{ "changed-in-branch.txt", NULL },
	{ "conflicting.txt", NULL },
	{ "removed-in-branch.txt", NULL },
	{ "automergeable.txt", "changed-in-branch.txt", NULL },
	{ "automergeable.txt", "conflicting.txt", NULL },
	{ "automergeable.txt", "removed-in-branch.txt", NULL },
	{ "changed-in-branch.txt", "conflicting.txt", NULL },
	{ "changed-in-branch.txt", "removed-in-branch.txt", NULL },
	{ "conflicting.txt", "removed-in-branch.txt", NULL },
	{ "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL },
	{ "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL },
	{ "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
	{ "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
	{ "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
	{ NULL },
};

61 62 63 64 65 66 67
static char *result_contents[4][6] = {
	{ "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, NULL, NULL },
	{ "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL },
	{ "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL },
	{ NULL }
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
void test_merge_workdir_dirty__initialize(void)
{
	repo = cl_git_sandbox_init(TEST_REPO_PATH);
	git_repository_index(&repo_index, repo);
}

void test_merge_workdir_dirty__cleanup(void)
{
	git_index_free(repo_index);
	cl_git_sandbox_cleanup();
}

static void set_core_autocrlf_to(git_repository *repo, bool value)
{
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value));

	git_config_free(cfg);
}

90
static int merge_branch(void)
91 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;
96 97 98
	int error;

	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]));
100

101
	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
102
	error = git_merge(repo, (const git_annotated_commit **)&their_head, 1, &merge_opts, &checkout_opts);
103

104
	git_annotated_commit_free(their_head);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

	return error;
}

static void write_files(char *files[])
{
	char *filename;
	git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT;
	size_t i;

	for (i = 0, filename = files[i]; filename; filename = files[++i]) {
		git_buf_clear(&path);
		git_buf_clear(&content);

		git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename);
		git_buf_printf(&content, "This is a dirty file in the working directory!\n\n"
			"It will not be staged!  Its filename is %s.\n", filename);

		cl_git_mkfile(path.ptr, content.ptr);
	}

126 127
	git_buf_dispose(&path);
	git_buf_dispose(&content);
128 129
}

130 131 132 133 134 135
static void hack_index(char *files[])
{
	char *filename;
	struct stat statbuf;
	git_buf path = GIT_BUF_INIT;
	git_index_entry *entry;
136
	struct p_timeval times[2];
137
	time_t now;
138 139 140 141 142
	size_t i;

	/* Update the index to suggest that checkout placed these files on
	 * disk, keeping the object id but updating the cache, which will
	 * emulate a Git implementation's different filter.
143 144 145 146
	 *
	 * We set the file's timestamp to before now to pretend that
	 * it was an old checkout so we don't trigger the racy
	 * protections would would check the content.
147
	 */
148 149 150 151 152 153 154

	now = time(NULL);
	times[0].tv_sec  = now - 5;
	times[0].tv_usec = 0;
	times[1].tv_sec  = now - 5;
	times[1].tv_usec = 0;

155 156 157 158 159 160 161
	for (i = 0, filename = files[i]; filename; filename = files[++i]) {
		git_buf_clear(&path);

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

		cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename));
162
		cl_git_pass(p_utimes(path.ptr, times));
163 164
		cl_git_pass(p_stat(path.ptr, &statbuf));

165 166
		entry->ctime.seconds = (int32_t)statbuf.st_ctime;
		entry->mtime.seconds = (int32_t)statbuf.st_mtime;
167
#if defined(GIT_USE_NSEC)
168 169
		entry->ctime.nanoseconds = statbuf.st_ctime_nsec;
		entry->mtime.nanoseconds = statbuf.st_mtime_nsec;
170 171
#else
		entry->ctime.nanoseconds = 0;
172
		entry->mtime.nanoseconds = 0;
173
#endif
174 175 176 177
		entry->dev = statbuf.st_dev;
		entry->ino = statbuf.st_ino;
		entry->uid  = statbuf.st_uid;
		entry->gid  = statbuf.st_gid;
178
		entry->file_size = (uint32_t)statbuf.st_size;
179 180
	}

181
	git_buf_dispose(&path);
182 183
}

184 185 186 187 188 189 190 191 192 193 194
static void stage_random_files(char *files[])
{
	char *filename;
	size_t i;

	write_files(files);

	for (i = 0, filename = files[i]; filename; filename = files[++i])
		cl_git_pass(git_index_add_bypath(repo_index, filename));
}

195 196 197 198 199 200 201 202 203
static void stage_content(char *content[])
{
	git_reference *head;
	git_object *head_object;
	git_buf path = GIT_BUF_INIT;
	char *filename, *text;
	size_t i;

	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));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	for (i = 0, filename = content[i], text = content[++i];
		filename && text;
		filename = content[++i], text = content[++i]) {

		git_buf_clear(&path);

		cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename));

		cl_git_mkfile(path.ptr, text);
		cl_git_pass(git_index_add_bypath(repo_index, filename));
	}

	git_object_free(head_object);
	git_reference_free(head);
221
	git_buf_dispose(&path);
222 223
}

224 225 226 227 228 229 230
static int merge_dirty_files(char *dirty_files[])
{
	git_reference *head;
	git_object *head_object;
	int error;

	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));
233 234 235

	write_files(dirty_files);

236
	error = merge_branch();
237 238 239 240 241 242 243

	git_object_free(head_object);
	git_reference_free(head);

	return error;
}

244 245 246 247 248 249 250
static int merge_differently_filtered_files(char *files[])
{
	git_reference *head;
	git_object *head_object;
	int error;

	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));
253

254 255 256 257 258 259 260 261 262 263
	/* Emulate checkout with a broken or misconfigured filter:  modify some
	 * files on-disk and then update the index with the updated file size
	 * and time, as if some filter applied them.  These files should not be
	 * treated as dirty since we created them.
	 *
	 * (Make sure to update the index stamp to defeat racy-git protections
	 * trying to sanity check the files in the index; those would rehash the
	 * files, showing them as dirty, the exact mechanism we're trying to avoid.)
	 */

264 265 266 267 268
	write_files(files);
	hack_index(files);

	cl_git_pass(git_index_write(repo_index));

269
	error = merge_branch();
270 271 272 273 274 275 276

	git_object_free(head_object);
	git_reference_free(head);

	return error;
}

277
static int merge_staged_files(char *staged_files[])
278
{
279
	stage_random_files(staged_files);
280
	return merge_branch();
281 282 283 284 285 286 287 288 289 290 291
}

void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void)
{
	char **files;
	size_t i;

	for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i])
		cl_git_pass(merge_dirty_files(files));
}

292 293 294 295 296 297
void test_merge_workdir_dirty__unstaged_deletes_maintained(void)
{
	git_reference *head;
	git_object *head_object;

	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));
300 301 302

	cl_git_pass(p_unlink("merge-resolve/unchanged.txt"));

303
	cl_git_pass(merge_branch());
304 305 306 307 308

	git_object_free(head_object);
	git_reference_free(head);
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void test_merge_workdir_dirty__affected_dirty_files_disallowed(void)
{
	char **files;
	size_t i;

	for (i = 0, files = affected[i]; files[0]; files = affected[++i])
		cl_git_fail(merge_dirty_files(files));
}

void test_merge_workdir_dirty__staged_files_in_index_disallowed(void)
{
	char **files;
	size_t i;

	for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i])
		cl_git_fail(merge_staged_files(files));

	for (i = 0, files = affected[i]; files[0]; files = affected[++i])
		cl_git_fail(merge_staged_files(files));
}
329 330 331 332 333 334 335

void test_merge_workdir_dirty__identical_staged_files_allowed(void)
{
	char **content;
	size_t i;

	set_core_autocrlf_to(repo, false);
336

337 338 339
	for (i = 0, content = result_contents[i]; content[0]; content = result_contents[++i]) {
		stage_content(content);

340
		cl_git_pass(git_index_write(repo_index));
341
		cl_git_pass(merge_branch());
342 343
	}
}
344 345 346 347 348 349 350 351 352

void test_merge_workdir_dirty__honors_cache(void)
{
	char **files;
	size_t i;

	for (i = 0, files = affected[i]; files[0]; files = affected[++i])
		cl_git_pass(merge_differently_filtered_files(files));
}