Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "run-command.h" |
| 3 | #include "xdiff-interface.h" |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 4 | #include "ll-merge.h" |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 5 | #include "blob.h" |
| 6 | |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 7 | static int fill_mmfile_blob(mmfile_t *f, struct blob *obj) |
| 8 | { |
| 9 | void *buf; |
| 10 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 11 | enum object_type type; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 12 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 13 | buf = read_sha1_file(obj->object.sha1, &type, &size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 14 | if (!buf) |
| 15 | return -1; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 16 | if (type != OBJ_BLOB) |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 17 | return -1; |
| 18 | f->ptr = buf; |
| 19 | f->size = size; |
| 20 | return 0; |
| 21 | } |
| 22 | |
| 23 | static void free_mmfile(mmfile_t *f) |
| 24 | { |
| 25 | free(f->ptr); |
| 26 | } |
| 27 | |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 28 | static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size) |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 29 | { |
Johannes Schindelin | e2b7008 | 2006-12-13 00:01:41 +0100 | [diff] [blame] | 30 | int merge_status; |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 31 | mmbuffer_t res; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 32 | |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 33 | merge_status = ll_merge(&res, path, base, |
| 34 | our, ".our", their, ".their", 0); |
Johannes Schindelin | e2b7008 | 2006-12-13 00:01:41 +0100 | [diff] [blame] | 35 | if (merge_status < 0) |
| 36 | return NULL; |
| 37 | |
| 38 | *size = res.size; |
| 39 | return res.ptr; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf) |
| 43 | { |
| 44 | int i; |
| 45 | mmfile_t *dst = priv_; |
| 46 | |
| 47 | for (i = 0; i < nbuf; i++) { |
| 48 | memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size); |
| 49 | dst->size += mb[i].size; |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2) |
| 55 | { |
| 56 | unsigned long size = f1->size < f2->size ? f1->size : f2->size; |
| 57 | void *ptr = xmalloc(size); |
| 58 | xpparam_t xpp; |
| 59 | xdemitconf_t xecfg; |
| 60 | xdemitcb_t ecb; |
| 61 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 62 | memset(&xpp, 0, sizeof(xpp)); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 63 | xpp.flags = XDF_NEED_MINIMAL; |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 64 | memset(&xecfg, 0, sizeof(xecfg)); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 65 | xecfg.ctxlen = 3; |
| 66 | xecfg.flags = XDL_EMIT_COMMON; |
| 67 | ecb.outf = common_outf; |
| 68 | |
| 69 | res->ptr = ptr; |
| 70 | res->size = 0; |
| 71 | |
| 72 | ecb.priv = res; |
Junio C Hamano | c279d7e | 2007-12-13 13:25:07 -0800 | [diff] [blame] | 73 | return xdi_diff(f1, f2, &xpp, &xecfg, &ecb); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 76 | void *merge_file(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size) |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 77 | { |
| 78 | void *res = NULL; |
| 79 | mmfile_t f1, f2, common; |
| 80 | |
| 81 | /* |
| 82 | * Removed in either branch? |
| 83 | * |
| 84 | * NOTE! This depends on the caller having done the |
| 85 | * proper warning about removing a file that got |
| 86 | * modified in the other branch! |
| 87 | */ |
| 88 | if (!our || !their) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 89 | enum object_type type; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 90 | if (base) |
| 91 | return NULL; |
| 92 | if (!our) |
| 93 | our = their; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 94 | return read_sha1_file(our->object.sha1, &type, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (fill_mmfile_blob(&f1, our) < 0) |
| 98 | goto out_no_mmfile; |
| 99 | if (fill_mmfile_blob(&f2, their) < 0) |
| 100 | goto out_free_f1; |
| 101 | |
| 102 | if (base) { |
| 103 | if (fill_mmfile_blob(&common, base) < 0) |
| 104 | goto out_free_f2_f1; |
| 105 | } else { |
| 106 | if (generate_common_file(&common, &f1, &f2) < 0) |
| 107 | goto out_free_f2_f1; |
| 108 | } |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 109 | res = three_way_filemerge(path, &common, &f1, &f2, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 110 | free_mmfile(&common); |
| 111 | out_free_f2_f1: |
| 112 | free_mmfile(&f2); |
| 113 | out_free_f1: |
| 114 | free_mmfile(&f1); |
| 115 | out_no_mmfile: |
| 116 | return res; |
| 117 | } |