Commit 05d47768caf6fec51fa85cb6275c9ba8324aead6

Edward Thomson 2014-03-10T22:30:41

Introduce git_merge_file for consumers

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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
diff --git a/include/git2/merge.h b/include/git2/merge.h
index cfec32f..38e3408 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -23,6 +23,43 @@
 GIT_BEGIN_DECL
 
 /**
+ * The file inputs to `git_merge_file`.  Callers should populate the
+ * `git_merge_file_input` structure with descriptions of the files in
+ * each side of the conflict for use in producing the merge file.
+ */
+typedef struct {
+	unsigned int version;
+
+	/** Pointer to the contents of the file. */
+	const char *ptr;
+
+	/** Size of the contents pointed to in `ptr`. */
+	size_t size;
+
+	/** File name of the conflicted file, or `NULL` to not merge the path. */
+	const char *path;
+
+	/** File mode of the conflicted file, or `0` to not merge the mode. */
+	unsigned int mode;
+} git_merge_file_input;
+
+#define GIT_MERGE_FILE_INPUT_VERSION 1
+#define GIT_MERGE_FILE_INPUT_INIT {GIT_MERGE_FILE_INPUT_VERSION}
+
+/**
+ * Initializes a `git_merge_file_input` with default values. Equivalent to
+ * creating an instance with GIT_MERGE_FILE_INPUT_INIT.
+ *
+ * @param opts the `git_merge_file_input` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_MERGE_FILE_INPUT_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_merge_file_init_input(
+	git_merge_file_input *opts,
+	int version);
+
+/**
  * Flags for `git_merge_tree` options.  A combination of these flags can be
  * passed in via the `flags` value in the `git_merge_tree_opts`.
  */
@@ -71,6 +108,86 @@ typedef enum {
 	GIT_MERGE_FILE_FAVOR_UNION = 3,
 } git_merge_file_favor_t;
 
+typedef enum {
+	/* Defaults */
+	GIT_MERGE_FILE_DEFAULT = 0,
+
+	/* Create standard conflicted merge files */
+	GIT_MERGE_FILE_STYLE_MERGE = (1 << 0),
+
+	/* Create diff3-style files */
+	GIT_MERGE_FILE_STYLE_DIFF3 = (1 << 1),
+
+	/* Condense non-alphanumeric regions for simplified diff file */
+	GIT_MERGE_FILE_SIMPLIFY_ALNUM = (1 << 2),
+} git_merge_file_flags_t;
+
+typedef struct {
+	unsigned int version;
+
+	/**
+	 * Label for the ancestor file side of the conflict which will be prepended
+	 * to labels in diff3-format merge files.
+	 */
+	const char *ancestor_label;
+
+	/**
+	 * Label for our file side of the conflict which will be prepended
+	 * to labels in merge files.
+	 */
+	const char *our_label;
+
+	/**
+	 * Label for their file side of the conflict which will be prepended
+	 * to labels in merge files.
+	 */
+	const char *their_label;
+
+	/** The file to favor in region conflicts. */
+	git_merge_file_favor_t favor;
+
+	/** Merge file flags. */
+	git_merge_file_flags_t flags;
+} git_merge_file_options;
+
+#define GIT_MERGE_FILE_OPTIONS_VERSION 1
+#define GIT_MERGE_FILE_OPTIONS_INIT {GIT_MERGE_FILE_OPTIONS_VERSION}
+
+/**
+ * Initializes a `git_merge_file_options` with default values. Equivalent to
+ * creating an instance with GIT_MERGE_FILE_OPTIONS_INIT.
+ *
+ * @param opts the `git_merge_file_options` instance to initialize.
+ * @param version the version of the struct; you should pass
+ *        `GIT_MERGE_FILE_OPTIONS_VERSION` here.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_merge_file_init_options(
+	git_merge_file_options *opts,
+	int version);
+
+typedef struct {
+	/**
+	 * True if the output was automerged, false if the output contains
+	 * conflict markers.
+	 */
+	unsigned int automergeable;
+
+	/**
+	 * The path that the resultant merge file should use, or NULL if a
+	 * filename conflict would occur.
+	 */
+	char *path;
+
+	/** The mode that the resultant merge file should use.  */
+	unsigned int mode;
+
+	/** The contents of the merge. */
+	unsigned char *ptr;
+
+	/** The length of the merge contents. */
+	size_t len;
+} git_merge_file_result;
 
 typedef struct {
 	unsigned int version;
@@ -269,6 +386,58 @@ GIT_EXTERN(void) git_merge_head_free(
 	git_merge_head *head);
 
 /**
+ * Merge two files as they exist in the in-memory data structures, using
+ * the given common ancestor as the baseline, producing a
+ * `git_merge_file_result` that reflects the merge result.  The
+ * `git_merge_file_result` must be freed with `git_merge_file_result_free`.
+ *
+ * Note that this function does not reference a repository and any
+ * configuration must be passed as `git_merge_file_options`.
+ *
+ * @param out The git_merge_file_result to be filled in
+ * @param ancestor The contents of the ancestor file
+ * @param ours The contents of the file in "our" side
+ * @param theirs The contents of the file in "their" side
+ * @param opts The merge file options or `NULL` for defaults
+ * @return 0 on success or error code
+ */
+GIT_EXTERN(int) git_merge_file(
+	git_merge_file_result *out,
+	const git_merge_file_input *ancestor,
+	const git_merge_file_input *ours,
+	const git_merge_file_input *theirs,
+	const git_merge_file_options *opts);
+
+/**
+ * Merge two files as they exist in the index, using the given common
+ * ancestor as the baseline, producing a `git_merge_file_result` that
+ * reflects the merge result.  The `git_merge_file_result` must be freed with
+ * `git_merge_file_result_free`.
+ *
+ * @param out The git_merge_file_result to be filled in
+ * @param repo The repository
+ * @param ancestor The index entry for the ancestor file (stage level 1)
+ * @param our_path The index entry for our file (stage level 2)
+ * @param their_path The index entry for their file (stage level 3)
+ * @param opts The merge file options or NULL
+ * @return 0 on success or error code
+ */
+GIT_EXTERN(int) git_merge_file_from_index(
+	git_merge_file_result *out,
+	git_repository *repo,
+	const git_index_entry *ancestor,
+	const git_index_entry *ours,
+	const git_index_entry *theirs,
+	const git_merge_file_options *opts);
+
+/**
+ * Frees a `git_merge_file_result`.
+ *
+ * @param result The result to free or `NULL`
+ */
+GIT_EXTERN(void) git_merge_file_result_free(git_merge_file_result *result);
+
+/**
  * Merge two trees, producing a `git_index` that reflects the result of
  * the merge.  The index may be written as-is to the working directory
  * or checked out.  If the index is to be converted to a tree, the caller
diff --git a/src/checkout.c b/src/checkout.c
index 5dd4ec7..f882f35 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1681,29 +1681,20 @@ static int checkout_write_merge(
 {
 	git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
 		path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT;
-	git_merge_file_options merge_file_opts = GIT_MERGE_FILE_OPTIONS_INIT;
-	git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
-		ours = GIT_MERGE_FILE_INPUT_INIT,
-		theirs = GIT_MERGE_FILE_INPUT_INIT;
-	git_merge_file_result result = GIT_MERGE_FILE_RESULT_INIT;
+	git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
+	git_merge_file_result result = {0};
 	git_filebuf output = GIT_FILEBUF_INIT;
 	int error = 0;
 
 	if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)
-		merge_file_opts.style = GIT_MERGE_FILE_STYLE_DIFF3;
-
-	if ((conflict->ancestor &&
-		(error = git_merge_file_input_from_index_entry(
-		&ancestor, data->repo, conflict->ancestor)) < 0) ||
-		(error = git_merge_file_input_from_index_entry(
-		&ours, data->repo, conflict->ours)) < 0 ||
-		(error = git_merge_file_input_from_index_entry(
-		&theirs, data->repo, conflict->theirs)) < 0)
-		goto done;
+		opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3;
 
-	ancestor.label = data->opts.ancestor_label ? data->opts.ancestor_label : "ancestor";
-	ours.label = data->opts.our_label ? data->opts.our_label : "ours";
-	theirs.label = data->opts.their_label ? data->opts.their_label : "theirs";
+	opts.ancestor_label = data->opts.ancestor_label ?
+		data->opts.ancestor_label : "ancestor";
+	opts.our_label = data->opts.our_label ?
+		data->opts.our_label : "ours";
+	opts.their_label = data->opts.their_label ?
+		data->opts.their_label : "theirs";
 
 	/* If all the paths are identical, decorate the diff3 file with the branch
 	 * names.  Otherwise, append branch_name:path.
@@ -1712,16 +1703,17 @@ static int checkout_write_merge(
 		strcmp(conflict->ours->path, conflict->theirs->path) != 0) {
 
 		if ((error = conflict_entry_name(
-			&our_label, ours.label, conflict->ours->path)) < 0 ||
+			&our_label, opts.our_label, conflict->ours->path)) < 0 ||
 			(error = conflict_entry_name(
-			&their_label, theirs.label, conflict->theirs->path)) < 0)
+			&their_label, opts.their_label, conflict->theirs->path)) < 0)
 			goto done;
 
-		ours.label = git_buf_cstr(&our_label);
-		theirs.label = git_buf_cstr(&their_label);
+		opts.our_label = git_buf_cstr(&our_label);
+		opts.their_label = git_buf_cstr(&their_label);
 	}
 
-	if ((error = git_merge_files(&result, &ancestor, &ours, &theirs, &merge_file_opts)) < 0)
+	if ((error = git_merge_file_from_index(&result, data->repo,
+		conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0)
 		goto done;
 
 	if (result.path == NULL || result.mode == 0) {
@@ -1739,7 +1731,7 @@ static int checkout_write_merge(
 
 	if ((error = git_futils_mkpath2file(path_workdir.ptr, 0755)) < 0 ||
 		(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
-		(error = git_filebuf_write(&output, result.data, result.len)) < 0 ||
+		(error = git_filebuf_write(&output, result.ptr, result.len)) < 0 ||
 		(error = git_filebuf_commit(&output)) < 0)
 		goto done;
 
@@ -1747,9 +1739,6 @@ done:
 	git_buf_free(&our_label);
 	git_buf_free(&their_label);
 
-	git_merge_file_input_free(&ancestor);
-	git_merge_file_input_free(&ours);
-	git_merge_file_input_free(&theirs);
 	git_merge_file_result_free(&result);
 	git_buf_free(&path_workdir);
 	git_buf_free(&path_suffixed);
diff --git a/src/index.c b/src/index.c
index 0d7d506..ea0815e 100644
--- a/src/index.c
+++ b/src/index.c
@@ -300,7 +300,7 @@ static void index_entry_free(git_index_entry *entry)
 	git__free(entry);
 }
 
-static unsigned int index_create_mode(unsigned int mode)
+unsigned int git_index__create_mode(unsigned int mode)
 {
 	if (S_ISLNK(mode))
 		return S_IFLNK;
@@ -320,9 +320,9 @@ static unsigned int index_merge_mode(
 
 	if (index->distrust_filemode && S_ISREG(mode))
 		return (existing && S_ISREG(existing->mode)) ?
-			existing->mode : index_create_mode(0666);
+			existing->mode : git_index__create_mode(0666);
 
-	return index_create_mode(mode);
+	return git_index__create_mode(mode);
 }
 
 void git_index__set_ignore_case(git_index *index, bool ignore_case)
@@ -619,7 +619,7 @@ void git_index_entry__init_from_stat(
 	entry->dev  = st->st_rdev;
 	entry->ino  = st->st_ino;
 	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
-		index_create_mode(0666) : index_create_mode(st->st_mode);
+		git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
 	entry->uid  = st->st_uid;
 	entry->gid  = st->st_gid;
 	entry->file_size = st->st_size;
diff --git a/src/index.h b/src/index.h
index f88d110..259a314 100644
--- a/src/index.h
+++ b/src/index.h
@@ -60,4 +60,6 @@ extern int git_index__find(
 
 extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
 
+extern unsigned int git_index__create_mode(unsigned int mode);
+
 #endif
diff --git a/src/merge.c b/src/merge.c
index 0b11c0d..8a33edb 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -539,11 +539,9 @@ static int merge_conflict_resolve_automerge(
 	const git_merge_diff *conflict,
 	unsigned int merge_file_favor)
 {
-	git_merge_file_options merge_file_opts = GIT_MERGE_FILE_OPTIONS_INIT;
-	git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
-		ours = GIT_MERGE_FILE_INPUT_INIT,
-		theirs = GIT_MERGE_FILE_INPUT_INIT;
-	git_merge_file_result result = GIT_MERGE_FILE_RESULT_INIT;
+	const git_index_entry *ancestor = NULL, *ours = NULL, *theirs = NULL;
+	git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
+	git_merge_file_result result = {0};
 	git_index_entry *index_entry;
 	git_odb *odb = NULL;
 	git_oid automerge_oid;
@@ -553,7 +551,9 @@ static int merge_conflict_resolve_automerge(
 
 	*resolved = 0;
 
-	merge_file_opts.favor = merge_file_favor;
+	if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
+		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
+		return 0;
 
 	/* Reject D/F conflicts */
 	if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE)
@@ -584,13 +584,19 @@ static int merge_conflict_resolve_automerge(
 	if (conflict->binary)
 		return 0;
 
+	ancestor = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ?
+		&conflict->ancestor_entry : NULL;
+	ours = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
+		&conflict->our_entry : NULL;
+	theirs = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
+		&conflict->their_entry : NULL;
+
+	opts.favor = merge_file_favor;
+
 	if ((error = git_repository_odb(&odb, diff_list->repo)) < 0 ||
-		(error = git_merge_file_input_from_index_entry(&ancestor, diff_list->repo, &conflict->ancestor_entry)) < 0 ||
-		(error = git_merge_file_input_from_index_entry(&ours, diff_list->repo, &conflict->our_entry)) < 0 ||
-		(error = git_merge_file_input_from_index_entry(&theirs, diff_list->repo, &conflict->their_entry)) < 0 ||
-		(error = git_merge_files(&result, &ancestor, &ours, &theirs, &merge_file_opts)) < 0 ||
+		(error = git_merge_file_from_index(&result, diff_list->repo, ancestor, ours, theirs, &opts)) < 0 ||
 		!result.automergeable ||
-		(error = git_odb_write(&automerge_oid, odb, result.data, result.len, GIT_OBJ_BLOB)) < 0)
+		(error = git_odb_write(&automerge_oid, odb, result.ptr, result.len, GIT_OBJ_BLOB)) < 0)
 		goto done;
 
 	if ((index_entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
@@ -609,9 +615,6 @@ static int merge_conflict_resolve_automerge(
 	*resolved = 1;
 
 done:
-	git_merge_file_input_free(&ancestor);
-	git_merge_file_input_free(&ours);
-	git_merge_file_input_free(&theirs);
 	git_merge_file_result_free(&result);
 	git_odb_free(odb);
 
@@ -2793,3 +2796,27 @@ int git_merge_tree_init_opts(git_merge_tree_opts* opts, int version)
 		return 0;
 	}
 }
+
+int git_merge_file_init_input(git_merge_file_input *input, int version)
+{
+	if (version != GIT_MERGE_FILE_INPUT_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_file_input", version);
+		return -1;
+	} else {
+		git_merge_file_input i = GIT_MERGE_FILE_INPUT_INIT;
+		memcpy(input, &i, sizeof(i));
+		return 0;
+	}
+}
+
+int git_merge_file_init_options(git_merge_file_options *opts, int version)
+{
+	if (version != GIT_MERGE_FILE_OPTIONS_VERSION) {
+		giterr_set(GITERR_INVALID, "Invalid version %d for git_merge_file_options", version);
+		return -1;
+	} else {
+		git_merge_file_options o = GIT_MERGE_FILE_OPTIONS_INIT;
+		memcpy(opts, &o, sizeof(o));
+		return 0;
+	}
+}
diff --git a/src/merge_file.c b/src/merge_file.c
index 986fbf9..fc45cbf 100644
--- a/src/merge_file.c
+++ b/src/merge_file.c
@@ -8,6 +8,9 @@
 #include "common.h"
 #include "repository.h"
 #include "merge_file.h"
+#include "posix.h"
+#include "fileops.h"
+#include "index.h"
 
 #include "git2/repository.h"
 #include "git2/object.h"
@@ -22,17 +25,17 @@ GIT_INLINE(const char *) merge_file_best_path(
 	const git_merge_file_input *ours,
 	const git_merge_file_input *theirs)
 {
-	if (!GIT_MERGE_FILE_SIDE_EXISTS(ancestor)) {
-		if (strcmp(ours->path, theirs->path) == 0)
+	if (!ancestor) {
+		if (ours && theirs && strcmp(ours->path, theirs->path) == 0)
 			return ours->path;
 
 		return NULL;
 	}
 
-	if (strcmp(ancestor->path, ours->path) == 0)
-		return theirs->path;
-	else if(strcmp(ancestor->path, theirs->path) == 0)
-		return ours->path;
+	if (ours && strcmp(ancestor->path, ours->path) == 0)
+		return theirs ? theirs->path : NULL;
+	else if(theirs && strcmp(ancestor->path, theirs->path) == 0)
+		return ours ? ours->path : NULL;
 
 	return NULL;
 }
@@ -47,136 +50,230 @@ GIT_INLINE(int) merge_file_best_mode(
 	 * assume executable.  Otherwise, if any mode changed from the ancestor,
 	 * use that one.
 	 */
-	if (!GIT_MERGE_FILE_SIDE_EXISTS(ancestor)) {
-		if (ours->mode == GIT_FILEMODE_BLOB_EXECUTABLE ||
-			theirs->mode == GIT_FILEMODE_BLOB_EXECUTABLE)
+	if (!ancestor) {
+		if ((ours && ours->mode == GIT_FILEMODE_BLOB_EXECUTABLE) ||
+			(theirs && theirs->mode == GIT_FILEMODE_BLOB_EXECUTABLE))
 			return GIT_FILEMODE_BLOB_EXECUTABLE;
 
 		return GIT_FILEMODE_BLOB;
-	}
+	} else if (ours && theirs) {
+		if (ancestor->mode == ours->mode)
+			return theirs->mode;
 
-	if (ancestor->mode == ours->mode)
-		return theirs->mode;
-	else if(ancestor->mode == theirs->mode)
 		return ours->mode;
+	}
 
 	return 0;
 }
 
-int git_merge_file_input_from_index_entry(
-	git_merge_file_input *input,
-	git_repository *repo,
+int git_merge_file__input_from_index(
+	git_merge_file_input *input_out,
+	git_odb_object **odb_object_out,
+	git_odb *odb,
 	const git_index_entry *entry)
 {
-	git_odb *odb = NULL;
 	int error = 0;
 
-	assert(input && repo && entry);
-
-	if (entry->mode == 0)
-		return 0;
+	assert(input_out && odb_object_out && odb && entry);
 
-	if ((error = git_repository_odb(&odb, repo)) < 0 ||
-		(error = git_odb_read(&input->odb_object, odb, &entry->id)) < 0)
+	if ((error = git_odb_read(odb_object_out, odb, &entry->id)) < 0)
 		goto done;
 
-	input->mode = entry->mode;
-	input->path = git__strdup(entry->path);
-	input->mmfile.size = git_odb_object_size(input->odb_object);
-	input->mmfile.ptr = (char *)git_odb_object_data(input->odb_object);
-
-	if (input->label == NULL)
-		input->label = entry->path;
+	input_out->path = entry->path;
+	input_out->mode = entry->mode;
+	input_out->ptr = (char *)git_odb_object_data(*odb_object_out);
+	input_out->size = git_odb_object_size(*odb_object_out);
 
 done:
-	git_odb_free(odb);
-
 	return error;
 }
 
-int git_merge_file_input_from_diff_file(
-	git_merge_file_input *input,
-	git_repository *repo,
-	const git_diff_file *file)
+static void merge_file_normalize_opts(
+	git_merge_file_options *out,
+	const git_merge_file_options *given_opts)
 {
-	git_odb *odb = NULL;
-	int error = 0;
-
-	assert(input && repo && file);
-
-	if (file->mode == 0)
-		return 0;
-
-	if ((error = git_repository_odb(&odb, repo)) < 0 ||
-		(error = git_odb_read(&input->odb_object, odb, &file->id)) < 0)
-		goto done;
-
-	input->mode = file->mode;
-	input->path = git__strdup(file->path);
-	input->mmfile.size = git_odb_object_size(input->odb_object);
-	input->mmfile.ptr = (char *)git_odb_object_data(input->odb_object);
-
-	if (input->label == NULL)
-		input->label = file->path;
-
-done:
-	git_odb_free(odb);
-
-	return error;
+	if (given_opts)
+		memcpy(out, given_opts, sizeof(git_merge_file_options));
+	else {
+		git_merge_file_options default_opts = GIT_MERGE_FILE_OPTIONS_INIT;
+		memcpy(out, &default_opts, sizeof(git_merge_file_options));
+	}
 }
 
-int git_merge_files(
+static int git_merge_file__from_inputs(
 	git_merge_file_result *out,
-	git_merge_file_input *ancestor,
-	git_merge_file_input *ours,
-	git_merge_file_input *theirs,
-	git_merge_file_options *opts)
+	const git_merge_file_input *ancestor,
+	const git_merge_file_input *ours,
+	const git_merge_file_input *theirs,
+	const git_merge_file_options *given_opts)
 {
 	xmparam_t xmparam;
+	mmfile_t ancestor_mmfile = {0}, our_mmfile = {0}, their_mmfile = {0};
 	mmbuffer_t mmbuffer;
+	git_merge_file_options options = GIT_MERGE_FILE_OPTIONS_INIT;
+	const char *path;
 	int xdl_result;
 	int error = 0;
 
-	assert(out && ancestor && ours && theirs);
-
 	memset(out, 0x0, sizeof(git_merge_file_result));
 
-	if (!GIT_MERGE_FILE_SIDE_EXISTS(ours) || !GIT_MERGE_FILE_SIDE_EXISTS(theirs))
-		return 0;
+	merge_file_normalize_opts(&options, given_opts);		
 
 	memset(&xmparam, 0x0, sizeof(xmparam_t));
-	xmparam.ancestor = ancestor->label;
-	xmparam.file1 = ours->label;
-	xmparam.file2 = theirs->label;
 
-	out->path = merge_file_best_path(ancestor, ours, theirs);
-	out->mode = merge_file_best_mode(ancestor, ours, theirs);
+	if (ancestor) {
+		xmparam.ancestor = (options.ancestor_label) ?
+			options.ancestor_label : ancestor->path;
+		ancestor_mmfile.ptr = (char *)ancestor->ptr;
+		ancestor_mmfile.size = ancestor->size;
+	}
 
-	if (opts && opts->favor == GIT_MERGE_FILE_FAVOR_OURS)
+	xmparam.file1 = (options.our_label) ?
+		options.our_label : ours->path;
+	our_mmfile.ptr = (char *)ours->ptr;
+	our_mmfile.size = ours->size;
+
+	xmparam.file2 = (options.their_label) ?
+		options.their_label : theirs->path;
+	their_mmfile.ptr = (char *)theirs->ptr;
+	their_mmfile.size = theirs->size;
+
+	if (options.favor == GIT_MERGE_FILE_FAVOR_OURS)
 		xmparam.favor = XDL_MERGE_FAVOR_OURS;
-	else if (opts && opts->favor == GIT_MERGE_FILE_FAVOR_THEIRS)
+	else if (options.favor == GIT_MERGE_FILE_FAVOR_THEIRS)
 		xmparam.favor = XDL_MERGE_FAVOR_THEIRS;
-	else if (opts && opts->favor == GIT_MERGE_FILE_FAVOR_UNION)
+	else if (options.favor == GIT_MERGE_FILE_FAVOR_UNION)
 		xmparam.favor = XDL_MERGE_FAVOR_UNION;
 
-	xmparam.level = 
-		(opts && (opts->flags & GIT_MERGE_FILE_SIMPLIFY_ALNUM)) ?
+	xmparam.level = (options.flags & GIT_MERGE_FILE_SIMPLIFY_ALNUM) ?
 		XDL_MERGE_ZEALOUS_ALNUM : XDL_MERGE_ZEALOUS;
 
-	if (opts && opts->style == GIT_MERGE_FILE_STYLE_DIFF3)
+	if (options.flags & GIT_MERGE_FILE_STYLE_DIFF3)
 		xmparam.style = XDL_MERGE_DIFF3;
 
-	if ((xdl_result = xdl_merge(&ancestor->mmfile, &ours->mmfile,
-		&theirs->mmfile, &xmparam, &mmbuffer)) < 0) {
+	if ((xdl_result = xdl_merge(&ancestor_mmfile, &our_mmfile,
+		&their_mmfile, &xmparam, &mmbuffer)) < 0) {
 		giterr_set(GITERR_MERGE, "Failed to merge files.");
 		error = -1;
 		goto done;
 	}
 
+	if ((path = merge_file_best_path(ancestor, ours, theirs)) != NULL &&
+		(out->path = strdup(path)) == NULL) {
+		error = -1;
+		goto done;
+	}
+
 	out->automergeable = (xdl_result == 0);
-	out->data = (unsigned char *)mmbuffer.ptr;
+	out->ptr = (unsigned char *)mmbuffer.ptr;
 	out->len = mmbuffer.size;
+	out->mode = merge_file_best_mode(ancestor, ours, theirs);
 
 done:
+	if (error < 0)
+		git_merge_file_result_free(out);
+
 	return error;
 }
+
+static git_merge_file_input *git_merge_file__normalize_inputs(
+	git_merge_file_input *out,
+	const git_merge_file_input *given)
+{
+	memcpy(out, given, sizeof(git_merge_file_input));
+
+	if (!out->path)
+		out->path = "file.txt";
+
+	if (!out->mode)
+		out->mode = 0100644;
+
+	return out;
+}
+
+int git_merge_file(
+	git_merge_file_result *out,
+	const git_merge_file_input *ancestor,
+	const git_merge_file_input *ours,
+	const git_merge_file_input *theirs,
+	const git_merge_file_options *options)
+{
+	git_merge_file_input inputs[3] = { {0} };
+
+	assert(out && ours && theirs);
+
+	memset(out, 0x0, sizeof(git_merge_file_result));
+
+	if (ancestor)
+		ancestor = git_merge_file__normalize_inputs(&inputs[0], ancestor);
+
+	ours = git_merge_file__normalize_inputs(&inputs[1], ours);
+	theirs = git_merge_file__normalize_inputs(&inputs[2], theirs);
+
+	return git_merge_file__from_inputs(out, ancestor, ours, theirs, options);
+}
+
+int git_merge_file_from_index(
+	git_merge_file_result *out,
+	git_repository *repo,
+	const git_index_entry *ancestor,
+	const git_index_entry *ours,
+	const git_index_entry *theirs,
+	const git_merge_file_options *options)
+{
+	git_merge_file_input inputs[3] = { {0} },
+		*ancestor_input = NULL, *our_input = NULL, *their_input = NULL;
+	git_odb *odb = NULL;
+	git_odb_object *odb_object[3] = { 0 };
+	int error = 0;
+
+	assert(out && repo && ours && theirs);
+
+	memset(out, 0x0, sizeof(git_merge_file_result));
+
+	if ((error = git_repository_odb(&odb, repo)) < 0)
+		goto done;
+
+	if (ancestor) {
+		if ((error = git_merge_file__input_from_index(
+			&inputs[0], &odb_object[0], odb, ancestor)) < 0)
+			goto done;
+
+		ancestor_input = &inputs[0];
+	}
+
+	if ((error = git_merge_file__input_from_index(
+		&inputs[1], &odb_object[1], odb, ours)) < 0)
+		goto done;
+
+	our_input = &inputs[1];
+
+	if ((error = git_merge_file__input_from_index(
+		&inputs[2], &odb_object[2], odb, theirs)) < 0)
+		goto done;
+
+	their_input = &inputs[2];
+
+	if ((error = git_merge_file__from_inputs(out,
+		ancestor_input, our_input, their_input, options)) < 0)
+		goto done;
+
+done:
+	git_odb_object_free(odb_object[0]);
+	git_odb_object_free(odb_object[1]);
+	git_odb_object_free(odb_object[2]);
+	git_odb_free(odb);
+
+	return error;
+}
+
+void git_merge_file_result_free(git_merge_file_result *result)
+{
+	if (result == NULL)
+		return;
+
+	git__free(result->path);
+
+	/* xdiff uses malloc() not git_malloc, so we use free(), not git_free() */
+	free(result->ptr);
+}
diff --git a/src/merge_file.h b/src/merge_file.h
index 332be49..263391e 100644
--- a/src/merge_file.h
+++ b/src/merge_file.h
@@ -11,82 +11,4 @@
 
 #include "git2/merge.h"
 
-typedef struct {
-	const char *label;
-	char *path;
-	unsigned int mode;
-	mmfile_t mmfile;
-
-	git_odb_object *odb_object;
-} git_merge_file_input;
-
-#define GIT_MERGE_FILE_INPUT_INIT	{0}
-
-typedef struct {
-	bool automergeable;
-
-	const char *path;
-	int mode;
-
-	unsigned char *data;
-	size_t len;
-} git_merge_file_result;
-
-#define GIT_MERGE_FILE_RESULT_INIT	{0}
-
-typedef enum {
-	/* Condense non-alphanumeric regions for simplified diff file */
-	GIT_MERGE_FILE_SIMPLIFY_ALNUM = (1 << 0),
-} git_merge_file_flags_t;
-
-typedef enum {
-	/* Create standard conflicted merge files */
-	GIT_MERGE_FILE_STYLE_MERGE = 0,
-
-	/* Create diff3-style files */
-	GIT_MERGE_FILE_STYLE_DIFF3 = 1,
-} git_merge_file_style_t;
-
-typedef struct {
-	git_merge_file_favor_t favor;
-	git_merge_file_flags_t flags;
-	git_merge_file_style_t style;
-} git_merge_file_options;
-
-#define GIT_MERGE_FILE_OPTIONS_INIT	{0}
-
-int git_merge_file_input_from_index_entry(
-	git_merge_file_input *input,
-	git_repository *repo,
-	const git_index_entry *entry);
-
-int git_merge_file_input_from_diff_file(
-	git_merge_file_input *input,
-	git_repository *repo,
-	const git_diff_file *file);
-
-int git_merge_files(
-	git_merge_file_result *out,
-	git_merge_file_input *ancestor,
-	git_merge_file_input *ours,
-	git_merge_file_input *theirs,
-	git_merge_file_options *opts);
-
-GIT_INLINE(void) git_merge_file_input_free(git_merge_file_input *input)
-{
-	assert(input);
-	git__free(input->path);
-	git_odb_object_free(input->odb_object);
-}
-
-GIT_INLINE(void) git_merge_file_result_free(git_merge_file_result *filediff)
-{
-	if (filediff == NULL)
-		return;
-
-	/* xdiff uses malloc() not git_malloc, so we use free(), not git_free() */
-	if (filediff->data != NULL)
-		free(filediff->data);
-}
-
 #endif
diff --git a/tests/merge/files.c b/tests/merge/files.c
new file mode 100644
index 0000000..c377471
--- /dev/null
+++ b/tests/merge/files.c
@@ -0,0 +1,175 @@
+#include "clar_libgit2.h"
+#include "git2/repository.h"
+#include "git2/merge.h"
+#include "buffer.h"
+#include "merge.h"
+#include "merge_helpers.h"
+#include "refs.h"
+#include "fileops.h"
+
+#define TEST_REPO_PATH "merge-resolve"
+#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
+
+static git_repository *repo;
+static git_index *repo_index;
+
+// Fixture setup and teardown
+void test_merge_files__initialize(void)
+{
+	git_config *cfg;
+
+	repo = cl_git_sandbox_init(TEST_REPO_PATH);
+	git_repository_index(&repo_index, repo);
+
+	/* 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"));
+	git_config_free(cfg);
+}
+
+void test_merge_files__cleanup(void)
+{
+	git_index_free(repo_index);
+	cl_git_sandbox_cleanup();
+}
+
+void test_merge_files__automerge_from_bufs(void)
+{
+	git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
+		ours = GIT_MERGE_FILE_INPUT_INIT,
+		theirs = GIT_MERGE_FILE_INPUT_INIT;
+	git_merge_file_result result = {0};
+	const char *expected = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n";
+
+	ancestor.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
+	ancestor.size = strlen(ancestor.ptr);
+	ancestor.path = "testfile.txt";
+	ancestor.mode = 0100755;
+
+	ours.ptr = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
+	ours.size = strlen(ours.ptr);
+	ours.path = "testfile.txt";
+	ours.mode = 0100755;
+
+	theirs.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n";
+	theirs.size = strlen(theirs.ptr);
+	theirs.path = "testfile.txt";
+	theirs.mode = 0100755;
+
+	cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, 0));
+
+	cl_assert_equal_i(1, result.automergeable);
+
+	cl_assert_equal_s("testfile.txt", result.path);
+	cl_assert_equal_i(0100755, result.mode);
+
+	cl_assert_equal_i(strlen(expected), result.len);
+	cl_assert_equal_strn(expected, result.ptr, result.len);
+
+	git_merge_file_result_free(&result);
+}
+
+void test_merge_files__automerge_use_best_path_and_mode(void)
+{
+	git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
+		ours = GIT_MERGE_FILE_INPUT_INIT,
+		theirs = GIT_MERGE_FILE_INPUT_INIT;
+	git_merge_file_result result = {0};
+	const char *expected = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n";
+
+	ancestor.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
+	ancestor.size = strlen(ancestor.ptr);
+	ancestor.path = "testfile.txt";
+	ancestor.mode = 0100755;
+
+	ours.ptr = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
+	ours.size = strlen(ours.ptr);
+	ours.path = "testfile.txt";
+	ours.mode = 0100644;
+
+	theirs.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n";
+	theirs.size = strlen(theirs.ptr);
+	theirs.path = "theirs.txt";
+	theirs.mode = 0100755;
+
+	cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, 0));
+
+	cl_assert_equal_i(1, result.automergeable);
+
+	cl_assert_equal_s("theirs.txt", result.path);
+	cl_assert_equal_i(0100644, result.mode);
+
+	cl_assert_equal_i(strlen(expected), result.len);
+	cl_assert_equal_strn(expected, result.ptr, result.len);
+
+	git_merge_file_result_free(&result);
+}
+
+void test_merge_files__conflict_from_bufs(void)
+{
+	git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
+		ours = GIT_MERGE_FILE_INPUT_INIT,
+		theirs = GIT_MERGE_FILE_INPUT_INIT;
+	git_merge_file_result result = {0};
+
+	const char *expected = "<<<<<<< testfile.txt\nAloha!\nOurs.\n=======\nHi!\nTheirs.\n>>>>>>> theirs.txt\n";
+	size_t expected_len = strlen(expected);
+
+	ancestor.ptr = "Hello!\nAncestor!\n";
+	ancestor.size = strlen(ancestor.ptr);
+	ancestor.path = "testfile.txt";
+	ancestor.mode = 0100755;
+
+	ours.ptr =  "Aloha!\nOurs.\n";
+	ours.size = strlen(ours.ptr);
+	ours.path = "testfile.txt";
+	ours.mode = 0100644;
+
+	theirs.ptr = "Hi!\nTheirs.\n";
+	theirs.size = strlen(theirs.ptr);
+	theirs.path = "theirs.txt";
+	theirs.mode = 0100755;
+
+	cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, NULL));
+
+	cl_assert_equal_i(0, result.automergeable);
+
+	cl_assert_equal_s("theirs.txt", result.path);
+	cl_assert_equal_i(0100644, result.mode);
+
+	cl_assert_equal_i(expected_len, result.len);
+	cl_assert_equal_strn(expected, result.ptr, expected_len);
+
+	git_merge_file_result_free(&result);
+}
+
+void test_merge_files__automerge_from_index(void)
+{
+	git_merge_file_result result = {0};
+	git_index_entry ancestor, ours, theirs;
+
+	git_oid_fromstr(&ancestor.id, "6212c31dab5e482247d7977e4f0dd3601decf13b");
+	ancestor.path = "automergeable.txt";
+	ancestor.mode = 0100644;
+
+	git_oid_fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf");
+	ours.path = "automergeable.txt";
+	ours.mode = 0100755;
+
+	git_oid_fromstr(&theirs.id, "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe");
+	theirs.path = "newname.txt";
+	theirs.mode = 0100644;
+
+	cl_git_pass(git_merge_file_from_index(&result, repo,
+		&ancestor, &ours, &theirs, 0));
+
+	cl_assert_equal_i(1, result.automergeable);
+
+	cl_assert_equal_s("newname.txt", result.path);
+	cl_assert_equal_i(0100755, result.mode);
+
+	cl_assert_equal_i(strlen(AUTOMERGEABLE_MERGED_FILE), result.len);
+	cl_assert_equal_strn(AUTOMERGEABLE_MERGED_FILE, result.ptr, result.len);
+
+	git_merge_file_result_free(&result);
+}
diff --git a/tests/merge/merge_helpers.h b/tests/merge/merge_helpers.h
index 3f53abc..63d1cb7 100644
--- a/tests/merge/merge_helpers.h
+++ b/tests/merge/merge_helpers.h
@@ -4,6 +4,49 @@
 #include "merge.h"
 #include "git2/merge.h"
 
+#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 AUTOMERGEABLE_MERGED_FILE_CRLF \
+	"this file is changed in master\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is automergeable\r\n" \
+	"this file is changed in branch\r\n"
+
+#define CONFLICTING_MERGE_FILE \
+	"<<<<<<< HEAD\n" \
+	"this file is changed in master and branch\n" \
+	"=======\n" \
+	"this file is changed in branch and master\n" \
+	">>>>>>> 7cb63eed597130ba4abb87b3e544b85021905520\n"
+
+#define CONFLICTING_DIFF3_FILE \
+	"<<<<<<< HEAD\n" \
+	"this file is changed in master and branch\n" \
+	"||||||| initial\n" \
+	"this file is a conflict\n" \
+	"=======\n" \
+	"this file is changed in branch and master\n" \
+	">>>>>>> 7cb63eed597130ba4abb87b3e544b85021905520\n"
+
+#define CONFLICTING_UNION_FILE \
+	"this file is changed in master and branch\n" \
+	"this file is changed in branch and master\n"
+
+
 struct merge_index_entry {
 	uint16_t mode;
 	char oid_str[41];
diff --git a/tests/merge/trees/automerge.c b/tests/merge/trees/automerge.c
index bd710e6..79069d8 100644
--- a/tests/merge/trees/automerge.c
+++ b/tests/merge/trees/automerge.c
@@ -54,28 +54,6 @@ static git_repository *repo;
 	  "", \
 	  "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5" }
 
-#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 AUTOMERGEABLE_MERGED_FILE_CRLF \
-	"this file is changed in master\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is changed in branch\r\n"
-
 // Fixture setup and teardown
 void test_merge_trees_automerge__initialize(void)
 {
diff --git a/tests/merge/trees/commits.c b/tests/merge/trees/commits.c
index eeb30da..08caf80 100644
--- a/tests/merge/trees/commits.c
+++ b/tests/merge/trees/commits.c
@@ -8,17 +8,6 @@ static git_repository *repo;
 
 #define TEST_REPO_PATH "merge-resolve"
 
-#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"
-
 void test_merge_trees_commits__initialize(void)
 {
 	repo = cl_git_sandbox_init(TEST_REPO_PATH);
diff --git a/tests/merge/workdir/simple.c b/tests/merge/workdir/simple.c
index a9a6365..07c60c5 100644
--- a/tests/merge/workdir/simple.c
+++ b/tests/merge/workdir/simple.c
@@ -71,47 +71,6 @@ static git_index *repo_index;
 	  "", \
 	  "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5" }
 
-#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 AUTOMERGEABLE_MERGED_FILE_CRLF \
-	"this file is changed in master\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is automergeable\r\n" \
-	"this file is changed in branch\r\n"
-
-#define CONFLICTING_MERGE_FILE \
-	"<<<<<<< HEAD\n" \
-	"this file is changed in master and branch\n" \
-	"=======\n" \
-	"this file is changed in branch and master\n" \
-	">>>>>>> 7cb63eed597130ba4abb87b3e544b85021905520\n"
-
-#define CONFLICTING_DIFF3_FILE \
-	"<<<<<<< HEAD\n" \
-	"this file is changed in master and branch\n" \
-	"||||||| initial\n" \
-	"this file is a conflict\n" \
-	"=======\n" \
-	"this file is changed in branch and master\n" \
-	">>>>>>> 7cb63eed597130ba4abb87b3e544b85021905520\n"
-
-#define CONFLICTING_UNION_FILE \
-	"this file is changed in master and branch\n" \
-	"this file is changed in branch and master\n"
 
 // Fixture setup and teardown
 void test_merge_workdir_simple__initialize(void)
diff --git a/tests/structinit/structinit.c b/tests/structinit/structinit.c
index 1df970d..61fe8c7 100644
--- a/tests/structinit/structinit.c
+++ b/tests/structinit/structinit.c
@@ -65,6 +65,16 @@ void test_structinit_structinit__compare(void)
 		git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
 		GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
 
+	/* merge_file_input */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
+		GIT_MERGE_FILE_INPUT_INIT, git_merge_file_init_input);
+
+	/* merge_file */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_merge_file_options, GIT_MERGE_FILE_OPTIONS_VERSION, \
+		GIT_MERGE_FILE_OPTIONS_INIT, git_merge_file_init_options);
+
 	/* merge_tree */
 	CHECK_MACRO_FUNC_INIT_EQUAL( \
 		git_merge_tree_opts, GIT_MERGE_TREE_OPTS_VERSION, \