line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* This file is part of libgit2, distributed under the GNU GPL v2 with |
5
|
|
|
|
|
|
|
* a Linking Exception. For full terms see the included COPYING file. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "common.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "repository.h" |
11
|
|
|
|
|
|
|
#include "posix.h" |
12
|
|
|
|
|
|
|
#include "futils.h" |
13
|
|
|
|
|
|
|
#include "index.h" |
14
|
|
|
|
|
|
|
#include "diff_xdiff.h" |
15
|
|
|
|
|
|
|
#include "merge.h" |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#include "git2/repository.h" |
18
|
|
|
|
|
|
|
#include "git2/object.h" |
19
|
|
|
|
|
|
|
#include "git2/index.h" |
20
|
|
|
|
|
|
|
#include "git2/merge.h" |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#include "xdiff/xdiff.h" |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
/* only examine the first 8000 bytes for binaryness. |
25
|
|
|
|
|
|
|
* https://github.com/git/git/blob/77bd3ea9f54f1584147b594abc04c26ca516d987/xdiff-interface.c#L197 |
26
|
|
|
|
|
|
|
*/ |
27
|
|
|
|
|
|
|
#define GIT_MERGE_FILE_BINARY_SIZE 8000 |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#define GIT_MERGE_FILE_SIDE_EXISTS(X) ((X)->mode != 0) |
30
|
|
|
|
|
|
|
|
31
|
33
|
|
|
|
|
|
int git_merge_file__input_from_index( |
32
|
|
|
|
|
|
|
git_merge_file_input *input_out, |
33
|
|
|
|
|
|
|
git_odb_object **odb_object_out, |
34
|
|
|
|
|
|
|
git_odb *odb, |
35
|
|
|
|
|
|
|
const git_index_entry *entry) |
36
|
|
|
|
|
|
|
{ |
37
|
33
|
|
|
|
|
|
int error = 0; |
38
|
|
|
|
|
|
|
|
39
|
33
|
50
|
|
|
|
|
assert(input_out && odb_object_out && odb && entry); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
33
|
50
|
|
|
|
|
if ((error = git_odb_read(odb_object_out, odb, &entry->id)) < 0) |
42
|
0
|
|
|
|
|
|
goto done; |
43
|
|
|
|
|
|
|
|
44
|
33
|
|
|
|
|
|
input_out->path = entry->path; |
45
|
33
|
|
|
|
|
|
input_out->mode = entry->mode; |
46
|
33
|
|
|
|
|
|
input_out->ptr = (char *)git_odb_object_data(*odb_object_out); |
47
|
33
|
|
|
|
|
|
input_out->size = git_odb_object_size(*odb_object_out); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
done: |
50
|
33
|
|
|
|
|
|
return error; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
11
|
|
|
|
|
|
static void merge_file_normalize_opts( |
54
|
|
|
|
|
|
|
git_merge_file_options *out, |
55
|
|
|
|
|
|
|
const git_merge_file_options *given_opts) |
56
|
|
|
|
|
|
|
{ |
57
|
11
|
50
|
|
|
|
|
if (given_opts) |
58
|
11
|
|
|
|
|
|
memcpy(out, given_opts, sizeof(git_merge_file_options)); |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
|
git_merge_file_options default_opts = GIT_MERGE_FILE_OPTIONS_INIT; |
61
|
0
|
|
|
|
|
|
memcpy(out, &default_opts, sizeof(git_merge_file_options)); |
62
|
|
|
|
|
|
|
} |
63
|
11
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
11
|
|
|
|
|
|
static int merge_file__xdiff( |
66
|
|
|
|
|
|
|
git_merge_file_result *out, |
67
|
|
|
|
|
|
|
const git_merge_file_input *ancestor, |
68
|
|
|
|
|
|
|
const git_merge_file_input *ours, |
69
|
|
|
|
|
|
|
const git_merge_file_input *theirs, |
70
|
|
|
|
|
|
|
const git_merge_file_options *given_opts) |
71
|
|
|
|
|
|
|
{ |
72
|
|
|
|
|
|
|
xmparam_t xmparam; |
73
|
11
|
|
|
|
|
|
mmfile_t ancestor_mmfile = {0}, our_mmfile = {0}, their_mmfile = {0}; |
74
|
|
|
|
|
|
|
mmbuffer_t mmbuffer; |
75
|
11
|
|
|
|
|
|
git_merge_file_options options = GIT_MERGE_FILE_OPTIONS_INIT; |
76
|
|
|
|
|
|
|
const char *path; |
77
|
|
|
|
|
|
|
int xdl_result; |
78
|
11
|
|
|
|
|
|
int error = 0; |
79
|
|
|
|
|
|
|
|
80
|
11
|
|
|
|
|
|
memset(out, 0x0, sizeof(git_merge_file_result)); |
81
|
|
|
|
|
|
|
|
82
|
11
|
|
|
|
|
|
merge_file_normalize_opts(&options, given_opts); |
83
|
|
|
|
|
|
|
|
84
|
11
|
|
|
|
|
|
memset(&xmparam, 0x0, sizeof(xmparam_t)); |
85
|
|
|
|
|
|
|
|
86
|
11
|
50
|
|
|
|
|
if (ancestor) { |
87
|
22
|
|
|
|
|
|
xmparam.ancestor = (options.ancestor_label) ? |
88
|
11
|
100
|
|
|
|
|
options.ancestor_label : ancestor->path; |
89
|
11
|
|
|
|
|
|
ancestor_mmfile.ptr = (char *)ancestor->ptr; |
90
|
11
|
|
|
|
|
|
ancestor_mmfile.size = ancestor->size; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
22
|
|
|
|
|
|
xmparam.file1 = (options.our_label) ? |
94
|
11
|
100
|
|
|
|
|
options.our_label : ours->path; |
95
|
11
|
|
|
|
|
|
our_mmfile.ptr = (char *)ours->ptr; |
96
|
11
|
|
|
|
|
|
our_mmfile.size = ours->size; |
97
|
|
|
|
|
|
|
|
98
|
22
|
|
|
|
|
|
xmparam.file2 = (options.their_label) ? |
99
|
11
|
100
|
|
|
|
|
options.their_label : theirs->path; |
100
|
11
|
|
|
|
|
|
their_mmfile.ptr = (char *)theirs->ptr; |
101
|
11
|
|
|
|
|
|
their_mmfile.size = theirs->size; |
102
|
|
|
|
|
|
|
|
103
|
11
|
100
|
|
|
|
|
if (options.favor == GIT_MERGE_FILE_FAVOR_OURS) |
104
|
2
|
|
|
|
|
|
xmparam.favor = XDL_MERGE_FAVOR_OURS; |
105
|
9
|
100
|
|
|
|
|
else if (options.favor == GIT_MERGE_FILE_FAVOR_THEIRS) |
106
|
1
|
|
|
|
|
|
xmparam.favor = XDL_MERGE_FAVOR_THEIRS; |
107
|
8
|
50
|
|
|
|
|
else if (options.favor == GIT_MERGE_FILE_FAVOR_UNION) |
108
|
0
|
|
|
|
|
|
xmparam.favor = XDL_MERGE_FAVOR_UNION; |
109
|
|
|
|
|
|
|
|
110
|
22
|
|
|
|
|
|
xmparam.level = (options.flags & GIT_MERGE_FILE_SIMPLIFY_ALNUM) ? |
111
|
11
|
50
|
|
|
|
|
XDL_MERGE_ZEALOUS_ALNUM : XDL_MERGE_ZEALOUS; |
112
|
|
|
|
|
|
|
|
113
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_STYLE_DIFF3) |
114
|
0
|
|
|
|
|
|
xmparam.style = XDL_MERGE_DIFF3; |
115
|
|
|
|
|
|
|
|
116
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE) |
117
|
0
|
|
|
|
|
|
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE; |
118
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE) |
119
|
0
|
|
|
|
|
|
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_CHANGE; |
120
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL) |
121
|
0
|
|
|
|
|
|
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_AT_EOL; |
122
|
|
|
|
|
|
|
|
123
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_DIFF_PATIENCE) |
124
|
0
|
|
|
|
|
|
xmparam.xpp.flags |= XDF_PATIENCE_DIFF; |
125
|
|
|
|
|
|
|
|
126
|
11
|
50
|
|
|
|
|
if (options.flags & GIT_MERGE_FILE_DIFF_MINIMAL) |
127
|
0
|
|
|
|
|
|
xmparam.xpp.flags |= XDF_NEED_MINIMAL; |
128
|
|
|
|
|
|
|
|
129
|
11
|
|
|
|
|
|
xmparam.marker_size = options.marker_size; |
130
|
|
|
|
|
|
|
|
131
|
11
|
50
|
|
|
|
|
if ((xdl_result = xdl_merge(&ancestor_mmfile, &our_mmfile, |
132
|
|
|
|
|
|
|
&their_mmfile, &xmparam, &mmbuffer)) < 0) { |
133
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_MERGE, "failed to merge files"); |
134
|
0
|
|
|
|
|
|
error = -1; |
135
|
0
|
|
|
|
|
|
goto done; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
11
|
50
|
|
|
|
|
path = git_merge_file__best_path( |
139
|
|
|
|
|
|
|
ancestor ? ancestor->path : NULL, |
140
|
|
|
|
|
|
|
ours->path, |
141
|
|
|
|
|
|
|
theirs->path); |
142
|
|
|
|
|
|
|
|
143
|
11
|
50
|
|
|
|
|
if (path != NULL && (out->path = git__strdup(path)) == NULL) { |
|
|
50
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
error = -1; |
145
|
0
|
|
|
|
|
|
goto done; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
11
|
|
|
|
|
|
out->automergeable = (xdl_result == 0); |
149
|
11
|
|
|
|
|
|
out->ptr = (const char *)mmbuffer.ptr; |
150
|
11
|
|
|
|
|
|
out->len = mmbuffer.size; |
151
|
11
|
50
|
|
|
|
|
out->mode = git_merge_file__best_mode( |
152
|
|
|
|
|
|
|
ancestor ? ancestor->mode : 0, |
153
|
|
|
|
|
|
|
ours->mode, |
154
|
|
|
|
|
|
|
theirs->mode); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
done: |
157
|
11
|
50
|
|
|
|
|
if (error < 0) |
158
|
0
|
|
|
|
|
|
git_merge_file_result_free(out); |
159
|
|
|
|
|
|
|
|
160
|
11
|
|
|
|
|
|
return error; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
33
|
|
|
|
|
|
static bool merge_file__is_binary(const git_merge_file_input *file) |
164
|
|
|
|
|
|
|
{ |
165
|
33
|
50
|
|
|
|
|
size_t len = file ? file->size : 0; |
166
|
|
|
|
|
|
|
|
167
|
33
|
50
|
|
|
|
|
if (len > GIT_XDIFF_MAX_SIZE) |
168
|
0
|
|
|
|
|
|
return true; |
169
|
33
|
50
|
|
|
|
|
if (len > GIT_MERGE_FILE_BINARY_SIZE) |
170
|
0
|
|
|
|
|
|
len = GIT_MERGE_FILE_BINARY_SIZE; |
171
|
|
|
|
|
|
|
|
172
|
33
|
50
|
|
|
|
|
return len ? (memchr(file->ptr, 0, len) != NULL) : false; |
|
|
50
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
static int merge_file__binary( |
176
|
|
|
|
|
|
|
git_merge_file_result *out, |
177
|
|
|
|
|
|
|
const git_merge_file_input *ours, |
178
|
|
|
|
|
|
|
const git_merge_file_input *theirs, |
179
|
|
|
|
|
|
|
const git_merge_file_options *given_opts) |
180
|
|
|
|
|
|
|
{ |
181
|
0
|
|
|
|
|
|
const git_merge_file_input *favored = NULL; |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
memset(out, 0x0, sizeof(git_merge_file_result)); |
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_OURS) |
|
|
0
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
favored = ours; |
187
|
0
|
0
|
|
|
|
|
else if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_THEIRS) |
|
|
0
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
favored = theirs; |
189
|
|
|
|
|
|
|
else |
190
|
|
|
|
|
|
|
goto done; |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
if ((out->path = git__strdup(favored->path)) == NULL || |
193
|
0
|
|
|
|
|
|
(out->ptr = git__malloc(favored->size)) == NULL) |
194
|
|
|
|
|
|
|
goto done; |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
memcpy((char *)out->ptr, favored->ptr, favored->size); |
197
|
0
|
|
|
|
|
|
out->len = favored->size; |
198
|
0
|
|
|
|
|
|
out->mode = favored->mode; |
199
|
0
|
|
|
|
|
|
out->automergeable = 1; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
done: |
202
|
0
|
|
|
|
|
|
return 0; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
11
|
|
|
|
|
|
static int merge_file__from_inputs( |
206
|
|
|
|
|
|
|
git_merge_file_result *out, |
207
|
|
|
|
|
|
|
const git_merge_file_input *ancestor, |
208
|
|
|
|
|
|
|
const git_merge_file_input *ours, |
209
|
|
|
|
|
|
|
const git_merge_file_input *theirs, |
210
|
|
|
|
|
|
|
const git_merge_file_options *given_opts) |
211
|
|
|
|
|
|
|
{ |
212
|
22
|
|
|
|
|
|
if (merge_file__is_binary(ancestor) || |
213
|
22
|
50
|
|
|
|
|
merge_file__is_binary(ours) || |
214
|
11
|
|
|
|
|
|
merge_file__is_binary(theirs)) |
215
|
0
|
|
|
|
|
|
return merge_file__binary(out, ours, theirs, given_opts); |
216
|
|
|
|
|
|
|
|
217
|
11
|
|
|
|
|
|
return merge_file__xdiff(out, ancestor, ours, theirs, given_opts); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
static git_merge_file_input *git_merge_file__normalize_inputs( |
221
|
|
|
|
|
|
|
git_merge_file_input *out, |
222
|
|
|
|
|
|
|
const git_merge_file_input *given) |
223
|
|
|
|
|
|
|
{ |
224
|
0
|
|
|
|
|
|
memcpy(out, given, sizeof(git_merge_file_input)); |
225
|
|
|
|
|
|
|
|
226
|
0
|
0
|
|
|
|
|
if (!out->path) |
227
|
0
|
|
|
|
|
|
out->path = "file.txt"; |
228
|
|
|
|
|
|
|
|
229
|
0
|
0
|
|
|
|
|
if (!out->mode) |
230
|
0
|
|
|
|
|
|
out->mode = 0100644; |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
return out; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
int git_merge_file( |
236
|
|
|
|
|
|
|
git_merge_file_result *out, |
237
|
|
|
|
|
|
|
const git_merge_file_input *ancestor, |
238
|
|
|
|
|
|
|
const git_merge_file_input *ours, |
239
|
|
|
|
|
|
|
const git_merge_file_input *theirs, |
240
|
|
|
|
|
|
|
const git_merge_file_options *options) |
241
|
|
|
|
|
|
|
{ |
242
|
0
|
|
|
|
|
|
git_merge_file_input inputs[3] = { {0} }; |
243
|
|
|
|
|
|
|
|
244
|
0
|
0
|
|
|
|
|
assert(out && ours && theirs); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
memset(out, 0x0, sizeof(git_merge_file_result)); |
247
|
|
|
|
|
|
|
|
248
|
0
|
0
|
|
|
|
|
if (ancestor) |
249
|
0
|
|
|
|
|
|
ancestor = git_merge_file__normalize_inputs(&inputs[0], ancestor); |
250
|
|
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
ours = git_merge_file__normalize_inputs(&inputs[1], ours); |
252
|
0
|
|
|
|
|
|
theirs = git_merge_file__normalize_inputs(&inputs[2], theirs); |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
return merge_file__from_inputs(out, ancestor, ours, theirs, options); |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
11
|
|
|
|
|
|
int git_merge_file_from_index( |
258
|
|
|
|
|
|
|
git_merge_file_result *out, |
259
|
|
|
|
|
|
|
git_repository *repo, |
260
|
|
|
|
|
|
|
const git_index_entry *ancestor, |
261
|
|
|
|
|
|
|
const git_index_entry *ours, |
262
|
|
|
|
|
|
|
const git_index_entry *theirs, |
263
|
|
|
|
|
|
|
const git_merge_file_options *options) |
264
|
|
|
|
|
|
|
{ |
265
|
11
|
|
|
|
|
|
git_merge_file_input *ancestor_ptr = NULL, |
266
|
11
|
|
|
|
|
|
ancestor_input = {0}, our_input = {0}, their_input = {0}; |
267
|
11
|
|
|
|
|
|
git_odb *odb = NULL; |
268
|
11
|
|
|
|
|
|
git_odb_object *odb_object[3] = { 0 }; |
269
|
11
|
|
|
|
|
|
int error = 0; |
270
|
|
|
|
|
|
|
|
271
|
11
|
50
|
|
|
|
|
assert(out && repo && ours && theirs); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
11
|
|
|
|
|
|
memset(out, 0x0, sizeof(git_merge_file_result)); |
274
|
|
|
|
|
|
|
|
275
|
11
|
50
|
|
|
|
|
if ((error = git_repository_odb(&odb, repo)) < 0) |
276
|
0
|
|
|
|
|
|
goto done; |
277
|
|
|
|
|
|
|
|
278
|
11
|
50
|
|
|
|
|
if (ancestor) { |
279
|
11
|
50
|
|
|
|
|
if ((error = git_merge_file__input_from_index( |
280
|
|
|
|
|
|
|
&ancestor_input, &odb_object[0], odb, ancestor)) < 0) |
281
|
0
|
|
|
|
|
|
goto done; |
282
|
|
|
|
|
|
|
|
283
|
11
|
|
|
|
|
|
ancestor_ptr = &ancestor_input; |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
|
286
|
11
|
50
|
|
|
|
|
if ((error = git_merge_file__input_from_index( |
287
|
11
|
50
|
|
|
|
|
&our_input, &odb_object[1], odb, ours)) < 0 || |
288
|
11
|
|
|
|
|
|
(error = git_merge_file__input_from_index( |
289
|
|
|
|
|
|
|
&their_input, &odb_object[2], odb, theirs)) < 0) |
290
|
|
|
|
|
|
|
goto done; |
291
|
|
|
|
|
|
|
|
292
|
11
|
|
|
|
|
|
error = merge_file__from_inputs(out, |
293
|
|
|
|
|
|
|
ancestor_ptr, &our_input, &their_input, options); |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
done: |
296
|
11
|
|
|
|
|
|
git_odb_object_free(odb_object[0]); |
297
|
11
|
|
|
|
|
|
git_odb_object_free(odb_object[1]); |
298
|
11
|
|
|
|
|
|
git_odb_object_free(odb_object[2]); |
299
|
11
|
|
|
|
|
|
git_odb_free(odb); |
300
|
|
|
|
|
|
|
|
301
|
11
|
|
|
|
|
|
return error; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
17
|
|
|
|
|
|
void git_merge_file_result_free(git_merge_file_result *result) |
305
|
|
|
|
|
|
|
{ |
306
|
17
|
50
|
|
|
|
|
if (result == NULL) |
307
|
0
|
|
|
|
|
|
return; |
308
|
|
|
|
|
|
|
|
309
|
17
|
|
|
|
|
|
git__free((char *)result->path); |
310
|
17
|
|
|
|
|
|
git__free((char *)result->ptr); |
311
|
|
|
|
|
|
|
} |