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