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