Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 125 lines (108 sloc) 2.674 kb
0c79938 Improved three-way blob merging code
Linus Torvalds authored
1 #include "cache.h"
2 #include "run-command.h"
3 #include "xdiff-interface.h"
15b4f7a @gitster merge-tree: use ll_merge() not xdl_merge()
gitster authored
4 #include "ll-merge.h"
0c79938 Improved three-way blob merging code
Linus Torvalds authored
5 #include "blob.h"
1e0f8c4 sparse: Fix an "symbol 'merge_file' not decared" warning
Ramsay Jones authored
6 #include "merge-file.h"
0c79938 Improved three-way blob merging code
Linus Torvalds authored
7
8 static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
9 {
10 void *buf;
11 unsigned long size;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
12 enum object_type type;
0c79938 Improved three-way blob merging code
Linus Torvalds authored
13
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
14 buf = read_sha1_file(obj->object.sha1, &type, &size);
0c79938 Improved three-way blob merging code
Linus Torvalds authored
15 if (!buf)
16 return -1;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
17 if (type != OBJ_BLOB)
0c79938 Improved three-way blob merging code
Linus Torvalds authored
18 return -1;
19 f->ptr = buf;
20 f->size = size;
21 return 0;
22 }
23
24 static void free_mmfile(mmfile_t *f)
25 {
26 free(f->ptr);
27 }
28
15b4f7a @gitster merge-tree: use ll_merge() not xdl_merge()
gitster authored
29 static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
0c79938 Improved three-way blob merging code
Linus Torvalds authored
30 {
e2b7008 @dscho Get rid of the dependency on RCS' merge program
dscho authored
31 int merge_status;
15b4f7a @gitster merge-tree: use ll_merge() not xdl_merge()
gitster authored
32 mmbuffer_t res;
0c79938 Improved three-way blob merging code
Linus Torvalds authored
33
e44b385 @jrn merge_file(): add comment explaining behavior wrt conflict style
jrn authored
34 /*
35 * This function is only used by cmd_merge_tree, which
36 * does not respect the merge.conflictstyle option.
37 * There is no need to worry about a label for the
38 * common ancestor.
39 */
f01de62 @jrn ll_merge(): add ancestor label parameter for diff3-style output
jrn authored
40 merge_status = ll_merge(&res, path, base, NULL,
712516b @jrn ll-merge: replace flag argument with options struct
jrn authored
41 our, ".our", their, ".their", NULL);
e2b7008 @dscho Get rid of the dependency on RCS' merge program
dscho authored
42 if (merge_status < 0)
43 return NULL;
44
45 *size = res.size;
46 return res.ptr;
0c79938 Improved three-way blob merging code
Linus Torvalds authored
47 }
48
49 static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf)
50 {
51 int i;
52 mmfile_t *dst = priv_;
53
54 for (i = 0; i < nbuf; i++) {
55 memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size);
56 dst->size += mb[i].size;
57 }
58 return 0;
59 }
60
61 static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2)
62 {
63 unsigned long size = f1->size < f2->size ? f1->size : f2->size;
64 void *ptr = xmalloc(size);
65 xpparam_t xpp;
66 xdemitconf_t xecfg;
67 xdemitcb_t ecb;
68
9ccd0a8 @bdowning Always initialize xpparam_t to 0
bdowning authored
69 memset(&xpp, 0, sizeof(xpp));
582aa00 git diff too slow for a file
René Scharfe authored
70 xpp.flags = 0;
30b2501 @dscho Future-proof source for changes in xdemitconf_t
dscho authored
71 memset(&xecfg, 0, sizeof(xecfg));
0c79938 Improved three-way blob merging code
Linus Torvalds authored
72 xecfg.ctxlen = 3;
73 xecfg.flags = XDL_EMIT_COMMON;
74 ecb.outf = common_outf;
75
76 res->ptr = ptr;
77 res->size = 0;
78
79 ecb.priv = res;
c279d7e @gitster xdl_diff: identify call sites.
gitster authored
80 return xdi_diff(f1, f2, &xpp, &xecfg, &ecb);
0c79938 Improved three-way blob merging code
Linus Torvalds authored
81 }
82
15b4f7a @gitster merge-tree: use ll_merge() not xdl_merge()
gitster authored
83 void *merge_file(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
0c79938 Improved three-way blob merging code
Linus Torvalds authored
84 {
85 void *res = NULL;
86 mmfile_t f1, f2, common;
87
88 /*
89 * Removed in either branch?
90 *
91 * NOTE! This depends on the caller having done the
92 * proper warning about removing a file that got
93 * modified in the other branch!
94 */
95 if (!our || !their) {
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
96 enum object_type type;
0c79938 Improved three-way blob merging code
Linus Torvalds authored
97 if (base)
98 return NULL;
99 if (!our)
100 our = their;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
101 return read_sha1_file(our->object.sha1, &type, size);
0c79938 Improved three-way blob merging code
Linus Torvalds authored
102 }
103
104 if (fill_mmfile_blob(&f1, our) < 0)
105 goto out_no_mmfile;
106 if (fill_mmfile_blob(&f2, their) < 0)
107 goto out_free_f1;
108
109 if (base) {
110 if (fill_mmfile_blob(&common, base) < 0)
111 goto out_free_f2_f1;
112 } else {
113 if (generate_common_file(&common, &f1, &f2) < 0)
114 goto out_free_f2_f1;
115 }
15b4f7a @gitster merge-tree: use ll_merge() not xdl_merge()
gitster authored
116 res = three_way_filemerge(path, &common, &f1, &f2, size);
0c79938 Improved three-way blob merging code
Linus Torvalds authored
117 free_mmfile(&common);
118 out_free_f2_f1:
119 free_mmfile(&f2);
120 out_free_f1:
121 free_mmfile(&f1);
122 out_no_mmfile:
123 return res;
124 }
Something went wrong with that request. Please try again.