Elijah Newren | 1c02840 | 2023-02-24 00:09:32 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 6723899 | 2023-05-16 06:34:04 +0000 | [diff] [blame] | 2 | #include "merge-ll.h" |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 3 | #include "blob.h" |
Junio C Hamano | fa2364e | 2012-12-06 15:08:01 -0800 | [diff] [blame] | 4 | #include "merge-blobs.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 5 | #include "object-store-ll.h" |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 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 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 13 | buf = repo_read_object_file(the_repository, &obj->object.oid, &type, |
| 14 | &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 | |
Nguyễn Thái Ngọc Duy | f4a55b2 | 2018-09-21 17:57:28 +0200 | [diff] [blame] | 31 | static void *three_way_filemerge(struct index_state *istate, |
| 32 | const char *path, |
| 33 | mmfile_t *base, |
| 34 | mmfile_t *our, |
| 35 | mmfile_t *their, |
| 36 | unsigned long *size) |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 37 | { |
Elijah Newren | 35f6967 | 2022-02-02 02:37:30 +0000 | [diff] [blame] | 38 | enum ll_merge_result merge_status; |
Junio C Hamano | 15b4f7a | 2010-01-16 21:45:40 -0800 | [diff] [blame] | 39 | mmbuffer_t res; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 40 | |
Jonathan Nieder | e44b385 | 2010-03-20 19:40:53 -0500 | [diff] [blame] | 41 | /* |
| 42 | * This function is only used by cmd_merge_tree, which |
| 43 | * does not respect the merge.conflictstyle option. |
| 44 | * There is no need to worry about a label for the |
| 45 | * common ancestor. |
| 46 | */ |
Jonathan Nieder | f01de62 | 2010-03-20 19:38:58 -0500 | [diff] [blame] | 47 | merge_status = ll_merge(&res, path, base, NULL, |
Nguyễn Thái Ngọc Duy | 32eaa46 | 2018-09-21 17:57:27 +0200 | [diff] [blame] | 48 | our, ".our", their, ".their", |
Nguyễn Thái Ngọc Duy | f4a55b2 | 2018-09-21 17:57:28 +0200 | [diff] [blame] | 49 | istate, NULL); |
Johannes Schindelin | e2b7008 | 2006-12-13 00:01:41 +0100 | [diff] [blame] | 50 | if (merge_status < 0) |
| 51 | return NULL; |
Elijah Newren | 35f6967 | 2022-02-02 02:37:30 +0000 | [diff] [blame] | 52 | if (merge_status == LL_MERGE_BINARY_CONFLICT) |
| 53 | warning("Cannot merge binary files: %s (%s vs. %s)", |
| 54 | path, ".our", ".their"); |
Johannes Schindelin | e2b7008 | 2006-12-13 00:01:41 +0100 | [diff] [blame] | 55 | |
| 56 | *size = res.size; |
| 57 | return res.ptr; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Nguyễn Thái Ngọc Duy | f4a55b2 | 2018-09-21 17:57:28 +0200 | [diff] [blame] | 60 | void *merge_blobs(struct index_state *istate, const char *path, |
| 61 | struct blob *base, struct blob *our, |
| 62 | struct blob *their, unsigned long *size) |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 63 | { |
| 64 | void *res = NULL; |
| 65 | mmfile_t f1, f2, common; |
| 66 | |
| 67 | /* |
| 68 | * Removed in either branch? |
| 69 | * |
| 70 | * NOTE! This depends on the caller having done the |
| 71 | * proper warning about removing a file that got |
| 72 | * modified in the other branch! |
| 73 | */ |
| 74 | if (!our || !their) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 75 | enum object_type type; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 76 | if (base) |
| 77 | return NULL; |
| 78 | if (!our) |
| 79 | our = their; |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 80 | return repo_read_object_file(the_repository, &our->object.oid, |
| 81 | &type, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (fill_mmfile_blob(&f1, our) < 0) |
| 85 | goto out_no_mmfile; |
| 86 | if (fill_mmfile_blob(&f2, their) < 0) |
| 87 | goto out_free_f1; |
| 88 | |
| 89 | if (base) { |
| 90 | if (fill_mmfile_blob(&common, base) < 0) |
| 91 | goto out_free_f2_f1; |
| 92 | } else { |
Jeff King | b779b3a | 2016-02-23 01:06:55 -0500 | [diff] [blame] | 93 | common.ptr = xstrdup(""); |
| 94 | common.size = 0; |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 95 | } |
Nguyễn Thái Ngọc Duy | f4a55b2 | 2018-09-21 17:57:28 +0200 | [diff] [blame] | 96 | res = three_way_filemerge(istate, path, &common, &f1, &f2, size); |
Linus Torvalds | 0c79938 | 2006-06-28 22:06:36 -0700 | [diff] [blame] | 97 | free_mmfile(&common); |
| 98 | out_free_f2_f1: |
| 99 | free_mmfile(&f2); |
| 100 | out_free_f1: |
| 101 | free_mmfile(&f1); |
| 102 | out_no_mmfile: |
| 103 | return res; |
| 104 | } |