15
#include "unpack-trees.h"
16
#include "path-list.h"
17
#include "xdiff-interface.h"
18
+#include "attr.h"
19
20
static int subtree_merge;
21
660
mm->size = size;
661
}
662
663
+/* Low-level merge functions */
664
+typedef int (*ll_merge_fn)(mmfile_t *orig,
665
+ mmfile_t *src1, const char *name1,
666
+ mmfile_t *src2, const char *name2,
667
+ mmbuffer_t *result);
668
+
669
+static int ll_xdl_merge(mmfile_t *orig,
670
+ mmfile_t *src1, const char *name1,
671
+ mmfile_t *src2, const char *name2,
672
+ mmbuffer_t *result)
673
+{
674
+ xpparam_t xpp;
675
+
676
+ memset(&xpp, 0, sizeof(xpp));
677
+ return xdl_merge(orig,
678
+ src1, name1,
679
+ src2, name2,
680
+ &xpp, XDL_MERGE_ZEALOUS,
681
+ result);
682
+}
683
+
684
+static int ll_union_merge(mmfile_t *orig,
685
+ mmfile_t *src1, const char *name1,
686
+ mmfile_t *src2, const char *name2,
687
+ mmbuffer_t *result)
688
+{
689
+ char *src, *dst;
690
+ long size;
691
+ const int marker_size = 7;
692
+
693
+ int status = ll_xdl_merge(orig, src1, NULL, src2, NULL, result);
694
+ if (status <= 0)
695
+ return status;
696
+ size = result->size;
697
+ src = dst = result->ptr;
698
+ while (size) {
699
+ char ch;
700
+ if ((marker_size < size) &&
701
+ (*src == '<' || *src == '=' || *src == '>')) {
702
+ int i;
703
+ ch = *src;
704
+ for (i = 0; i < marker_size; i++)
705
+ if (src[i] != ch)
706
+ goto not_a_marker;
707
+ if (src[marker_size] != '\n')
708
+ goto not_a_marker;
709
+ src += marker_size + 1;
710
+ size -= marker_size + 1;
711
+ continue;
712
+ }
713
+ not_a_marker:
714
+ do {
715
+ ch = *src++;
716
+ *dst++ = ch;
717
+ size--;
718
+ } while (ch != '\n' && size);
719
+ }
720
+ result->size = dst - result->ptr;
721
+ return 0;
722
+}
723
+
724
+static int ll_binary_merge(mmfile_t *orig,
725
+ mmfile_t *src1, const char *name1,
726
+ mmfile_t *src2, const char *name2,
727
+ mmbuffer_t *result)
728
+{
729
+ /*
730
+ * The tentative merge result is "ours" for the final round,
731
+ * or common ancestor for an internal merge. Still return
732
+ * "conflicted merge" status.
733
+ */
734
+ mmfile_t *stolen = index_only ? orig : src1;
735
+
736
+ result->ptr = stolen->ptr;
737
+ result->size = stolen->size;
738
+ stolen->ptr = NULL;
739
+ return 1;
740
+}
741
+
742
+static struct {
743
+ const char *name;
744
+ ll_merge_fn fn;
745
+} ll_merge_fns[] = {
746
+ { "text", ll_xdl_merge },
747
+ { "binary", ll_binary_merge },
748
+ { "union", ll_union_merge },
749
+ { NULL, NULL },
750
+};
751
+
752
+static ll_merge_fn find_ll_merge_fn(void *merge_attr)
753
+{
754
+ const char *name;
755
+ int i;
756
+
757
+ if (ATTR_TRUE(merge_attr) || ATTR_UNSET(merge_attr))
758
+ return ll_xdl_merge;
759
+ else if (ATTR_FALSE(merge_attr))
760
+ return ll_binary_merge;
761
+
762
+ /* Otherwise merge_attr may name the merge function */
763
+ name = merge_attr;
764
+ for (i = 0; ll_merge_fns[i].name; i++)
765
+ if (!strcmp(ll_merge_fns[i].name, name))
766
+ return ll_merge_fns[i].fn;
767
+
768
+ /* default to the 3-way */
769
+ return ll_xdl_merge;
770
+}
771
+
772
+static void *git_path_check_merge(const char *path)
773
+{
774
+ static struct git_attr_check attr_merge_check;
775
+
776
+ if (!attr_merge_check.attr)
777
+ attr_merge_check.attr = git_attr("merge", 5);
778
+
779
+ if (git_checkattr(path, 1, &attr_merge_check))
780
+ return ATTR__UNSET;
781
+ return attr_merge_check.value;
782
+}
783
+
784
static int ll_merge(mmbuffer_t *result_buf,
785
struct diff_filespec *o,
786
struct diff_filespec *a,
789
const char *branch2)
790
{
791
mmfile_t orig, src1, src2;
670
- xpparam_t xpp;
792
char *name1, *name2;
793
int merge_status;
794
+ void *merge_attr;
795
+ ll_merge_fn fn;
796
797
name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
798
name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
801
fill_mm(a->sha1, &src1);
802
fill_mm(b->sha1, &src2);
803
681
- memset(&xpp, 0, sizeof(xpp));
682
- merge_status = xdl_merge(&orig,
683
- &src1, name1,
684
- &src2, name2,
685
- &xpp, XDL_MERGE_ZEALOUS,
686
- result_buf);
804
+ merge_attr = git_path_check_merge(a->path);
805
+ fn = find_ll_merge_fn(merge_attr);
806
+
807
+ merge_status = fn(&orig, &src1, name1, &src2, name2, result_buf);
808
+
809
free(name1);
810
free(name2);
811
free(orig.ptr);