summaryrefslogtreecommitdiff
path: root/tests/merge/driver.c
blob: a3fececb4628e4045a4e5ff508ef5c0d679d8eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "buffer.h"
#include "merge.h"

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

#define AUTOMERGEABLE_IDSTR "f2e1550a0c9e53d5811175864a29536642ae3821"

static git_repository *repo;
static git_index *repo_index;
static git_oid automergeable_id;

static void test_drivers_register(void);
static void test_drivers_unregister(void);

void test_merge_driver__initialize(void)
{
    git_config *cfg;

    repo = cl_git_sandbox_init(TEST_REPO_PATH);
    git_repository_index(&repo_index, repo);

	git_oid_fromstr(&automergeable_id, AUTOMERGEABLE_IDSTR);

    /* Ensure that the user's merge.conflictstyle doesn't interfere */
    cl_git_pass(git_repository_config(&cfg, repo));

    cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
    cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false));

	test_drivers_register();

    git_config_free(cfg);
}

void test_merge_driver__cleanup(void)
{
	test_drivers_unregister();

    git_index_free(repo_index);
	cl_git_sandbox_cleanup();
}

struct test_merge_driver {
	git_merge_driver base;
	int initialized;
	int shutdown;
};

static int test_driver_init(git_merge_driver *s)
{
	struct test_merge_driver *self = (struct test_merge_driver *)s;
	self->initialized = 1;
	return 0;
}

static void test_driver_shutdown(git_merge_driver *s)
{
	struct test_merge_driver *self = (struct test_merge_driver *)s;
	self->shutdown = 1;
}

static int test_driver_apply(
	git_merge_driver *s,
	const char **path_out,
	uint32_t *mode_out,
	git_buf *merged_out,
	const char *filter_name,
	const git_merge_driver_source *src)
{
	GIT_UNUSED(s);
	GIT_UNUSED(src);

	*path_out = "applied.txt";
	*mode_out = GIT_FILEMODE_BLOB;

	return git_buf_printf(merged_out, "This is the `%s` driver.\n",
		filter_name);
}

static struct test_merge_driver test_driver_custom = {
	{
		GIT_MERGE_DRIVER_VERSION,
		test_driver_init,
		test_driver_shutdown,
		test_driver_apply,
	},
	0,
	0,
};

static struct test_merge_driver test_driver_wildcard = {
	{
		GIT_MERGE_DRIVER_VERSION,
		test_driver_init,
		test_driver_shutdown,
		test_driver_apply,
	},
	0,
	0,
};

static void test_drivers_register(void)
{
	cl_git_pass(git_merge_driver_register("custom", &test_driver_custom.base));
	cl_git_pass(git_merge_driver_register("*", &test_driver_wildcard.base));
}

static void test_drivers_unregister(void)
{
	cl_git_pass(git_merge_driver_unregister("custom"));
	cl_git_pass(git_merge_driver_unregister("*"));
}

static void set_gitattributes_to(const char *driver)
{
	git_buf line = GIT_BUF_INIT;

	if (driver && strcmp(driver, ""))
		git_buf_printf(&line, "automergeable.txt merge=%s\n", driver);
	else if (driver)
		git_buf_printf(&line, "automergeable.txt merge\n");
	else
		git_buf_printf(&line, "automergeable.txt -merge\n");

	cl_assert(!git_buf_oom(&line));

	cl_git_mkfile(TEST_REPO_PATH "/.gitattributes", line.ptr);
	git_buf_dispose(&line);
}

static void merge_branch(void)
{
	git_oid their_id;
	git_annotated_commit *their_head;

	cl_git_pass(git_oid_fromstr(&their_id, BRANCH_ID));
	cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head,
		1, NULL, NULL));

	git_annotated_commit_free(their_head);
}

void test_merge_driver__custom(void)
{
	const char *expected = "This is the `custom` driver.\n";
	set_gitattributes_to("custom");
	merge_branch();

	cl_assert_equal_file(expected, strlen(expected),
		TEST_REPO_PATH "/applied.txt");
}

void test_merge_driver__wildcard(void)
{
	const char *expected = "This is the `foobar` driver.\n";
	set_gitattributes_to("foobar");
	merge_branch();

	cl_assert_equal_file(expected, strlen(expected),
		TEST_REPO_PATH "/applied.txt");
}

void test_merge_driver__shutdown_is_called(void)
{
    test_driver_custom.initialized = 0;
    test_driver_custom.shutdown = 0;
    test_driver_wildcard.initialized = 0;
    test_driver_wildcard.shutdown = 0;
    
    /* run the merge with the custom driver */
    set_gitattributes_to("custom");
    merge_branch();
    
	/* unregister the drivers, ensure their shutdown function is called */
	test_drivers_unregister();

    /* since the `custom` driver was used, it should have been initialized and
     * shutdown, but the wildcard driver was not used at all and should not
     * have been initialized or shutdown.
     */
	cl_assert(test_driver_custom.initialized);
	cl_assert(test_driver_custom.shutdown);
	cl_assert(!test_driver_wildcard.initialized);
	cl_assert(!test_driver_wildcard.shutdown);

	test_drivers_register();
}

static int defer_driver_apply(
	git_merge_driver *s,
	const char **path_out,
	uint32_t *mode_out,
	git_buf *merged_out,
	const char *filter_name,
	const git_merge_driver_source *src)
{
	GIT_UNUSED(s);
	GIT_UNUSED(path_out);
	GIT_UNUSED(mode_out);
	GIT_UNUSED(merged_out);
	GIT_UNUSED(filter_name);
	GIT_UNUSED(src);

	return GIT_PASSTHROUGH;
}

static struct test_merge_driver test_driver_defer_apply = {
	{
		GIT_MERGE_DRIVER_VERSION,
		test_driver_init,
		test_driver_shutdown,
		defer_driver_apply,
	},
	0,
	0,
};

void test_merge_driver__apply_can_defer(void)
{
	const git_index_entry *idx;

	cl_git_pass(git_merge_driver_register("defer",
		&test_driver_defer_apply.base));

    set_gitattributes_to("defer");
    merge_branch();

	cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0)));
	cl_assert_equal_oid(&automergeable_id, &idx->id);

	git_merge_driver_unregister("defer");
}

static int conflict_driver_apply(
	git_merge_driver *s,
	const char **path_out,
	uint32_t *mode_out,
	git_buf *merged_out,
	const char *filter_name,
	const git_merge_driver_source *src)
{
	GIT_UNUSED(s);
	GIT_UNUSED(path_out);
	GIT_UNUSED(mode_out);
	GIT_UNUSED(merged_out);
	GIT_UNUSED(filter_name);
	GIT_UNUSED(src);

	return GIT_EMERGECONFLICT;
}

static struct test_merge_driver test_driver_conflict_apply = {
	{
		GIT_MERGE_DRIVER_VERSION,
		test_driver_init,
		test_driver_shutdown,
		conflict_driver_apply,
	},
	0,
	0,
};

void test_merge_driver__apply_can_conflict(void)
{
	const git_index_entry *ancestor, *ours, *theirs;

	cl_git_pass(git_merge_driver_register("conflict",
		&test_driver_conflict_apply.base));

    set_gitattributes_to("conflict");
    merge_branch();

	cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs,
		repo_index, "automergeable.txt"));

	git_merge_driver_unregister("conflict");
}

void test_merge_driver__default_can_be_specified(void)
{
	git_oid their_id;
	git_annotated_commit *their_head;
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
	const char *expected = "This is the `custom` driver.\n";

	merge_opts.default_driver = "custom";

	cl_git_pass(git_oid_fromstr(&their_id, BRANCH_ID));
	cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head,
		1, &merge_opts, NULL));

	git_annotated_commit_free(their_head);

	cl_assert_equal_file(expected, strlen(expected),
		TEST_REPO_PATH "/applied.txt");
}

void test_merge_driver__honors_builtin_mergedefault(void)
{
	const git_index_entry *ancestor, *ours, *theirs;

	cl_repo_set_string(repo, "merge.default", "binary");
	merge_branch();

	cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs,
		repo_index, "automergeable.txt"));
}

void test_merge_driver__honors_custom_mergedefault(void)
{
	const char *expected = "This is the `custom` driver.\n";

	cl_repo_set_string(repo, "merge.default", "custom");
	merge_branch();

	cl_assert_equal_file(expected, strlen(expected),
		TEST_REPO_PATH "/applied.txt");
}

void test_merge_driver__mergedefault_deferring_falls_back_to_text(void)
{
	const git_index_entry *idx;

	cl_git_pass(git_merge_driver_register("defer",
		&test_driver_defer_apply.base));

	cl_repo_set_string(repo, "merge.default", "defer");
	merge_branch();

	cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0)));
	cl_assert_equal_oid(&automergeable_id, &idx->id);

	git_merge_driver_unregister("defer");
}

void test_merge_driver__set_forces_text(void)
{
	const git_index_entry *idx;

	/* `merge` without specifying a driver indicates `text` */
	set_gitattributes_to("");
	cl_repo_set_string(repo, "merge.default", "custom");

	merge_branch();

	cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0)));
	cl_assert_equal_oid(&automergeable_id, &idx->id);
}

void test_merge_driver__unset_forces_binary(void)
{
	const git_index_entry *ancestor, *ours, *theirs;

	/* `-merge` without specifying a driver indicates `binary` */
	set_gitattributes_to(NULL);
	cl_repo_set_string(repo, "merge.default", "custom");

	merge_branch();

	cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs,
		repo_index, "automergeable.txt"));
}

void test_merge_driver__not_configured_driver_falls_back(void)
{
	const git_index_entry *idx;

	test_drivers_unregister();

	/* `merge` without specifying a driver indicates `text` */
	set_gitattributes_to("notfound");

	merge_branch();

	cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0)));
	cl_assert_equal_oid(&automergeable_id, &idx->id);

	test_drivers_register();
}