668fb2e05dd39ecb45f7f78a86949b2d98c80ebf
   1#include "cache.h"
   2#include "run-command.h"
   3#include "xdiff-interface.h"
   4#include "ll-merge.h"
   5#include "blob.h"
   6#include "merge-blobs.h"
   7#include "object-store.h"
   8
   9static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
  10{
  11        void *buf;
  12        unsigned long size;
  13        enum object_type type;
  14
  15        buf = read_object_file(&obj->object.oid, &type, &size);
  16        if (!buf)
  17                return -1;
  18        if (type != OBJ_BLOB) {
  19                free(buf);
  20                return -1;
  21        }
  22        f->ptr = buf;
  23        f->size = size;
  24        return 0;
  25}
  26
  27static void free_mmfile(mmfile_t *f)
  28{
  29        free(f->ptr);
  30}
  31
  32static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
  33{
  34        int merge_status;
  35        mmbuffer_t res;
  36
  37        /*
  38         * This function is only used by cmd_merge_tree, which
  39         * does not respect the merge.conflictstyle option.
  40         * There is no need to worry about a label for the
  41         * common ancestor.
  42         */
  43        merge_status = ll_merge(&res, path, base, NULL,
  44                                our, ".our", their, ".their",
  45                                &the_index, NULL);
  46        if (merge_status < 0)
  47                return NULL;
  48
  49        *size = res.size;
  50        return res.ptr;
  51}
  52
  53void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
  54{
  55        void *res = NULL;
  56        mmfile_t f1, f2, common;
  57
  58        /*
  59         * Removed in either branch?
  60         *
  61         * NOTE! This depends on the caller having done the
  62         * proper warning about removing a file that got
  63         * modified in the other branch!
  64         */
  65        if (!our || !their) {
  66                enum object_type type;
  67                if (base)
  68                        return NULL;
  69                if (!our)
  70                        our = their;
  71                return read_object_file(&our->object.oid, &type, size);
  72        }
  73
  74        if (fill_mmfile_blob(&f1, our) < 0)
  75                goto out_no_mmfile;
  76        if (fill_mmfile_blob(&f2, their) < 0)
  77                goto out_free_f1;
  78
  79        if (base) {
  80                if (fill_mmfile_blob(&common, base) < 0)
  81                        goto out_free_f2_f1;
  82        } else {
  83                common.ptr = xstrdup("");
  84                common.size = 0;
  85        }
  86        res = three_way_filemerge(path, &common, &f1, &f2, size);
  87        free_mmfile(&common);
  88out_free_f2_f1:
  89        free_mmfile(&f2);
  90out_free_f1:
  91        free_mmfile(&f1);
  92out_no_mmfile:
  93        return res;
  94}