2 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 fe621944 2020-11-10 stsp #include <sys/mman.h>
19 574ed2c3 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <sys/queue.h>
22 574ed2c3 2017-11-29 stsp #include <errno.h>
23 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
24 574ed2c3 2017-11-29 stsp #include <stdio.h>
25 574ed2c3 2017-11-29 stsp #include <stdlib.h>
26 574ed2c3 2017-11-29 stsp #include <string.h>
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 fe621944 2020-11-10 stsp #include "got_opentemp.h"
30 fe621944 2020-11-10 stsp #include "got_error.h"
31 4b752015 2022-06-30 stsp #include "got_diff.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
35 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience;
36 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide;
37 fe621944 2020-11-10 stsp const struct diff_algo_config patience;
38 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide;
40 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
41 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
42 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
43 fe621944 2020-11-10 stsp .fallback_algo = &patience,
46 fe621944 2020-11-10 stsp const struct diff_algo_config myers_then_myers_divide =
47 fe621944 2020-11-10 stsp (struct diff_algo_config){
48 fe621944 2020-11-10 stsp .impl = diff_algo_myers,
49 fe621944 2020-11-10 stsp .permitted_state_size = 1024 * 1024 * sizeof(int),
50 fe621944 2020-11-10 stsp .fallback_algo = &myers_divide,
53 fe621944 2020-11-10 stsp const struct diff_algo_config patience = (struct diff_algo_config){
54 fe621944 2020-11-10 stsp .impl = diff_algo_patience,
55 fe621944 2020-11-10 stsp /* After subdivision, do Patience again: */
56 fe621944 2020-11-10 stsp .inner_algo = &patience,
57 fe621944 2020-11-10 stsp /* If subdivision failed, do Myers Divide et Impera: */
58 fe621944 2020-11-10 stsp .fallback_algo = &myers_then_myers_divide,
61 fe621944 2020-11-10 stsp const struct diff_algo_config myers_divide = (struct diff_algo_config){
62 fe621944 2020-11-10 stsp .impl = diff_algo_myers_divide,
63 fe621944 2020-11-10 stsp /* When division succeeded, start from the top: */
64 fe621944 2020-11-10 stsp .inner_algo = &myers_then_myers_divide,
65 fe621944 2020-11-10 stsp /* (fallback_algo = NULL implies diff_algo_none). */
68 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
69 fe621944 2020-11-10 stsp * do a Myers-divide. */
70 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_myers_divide = {
71 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
72 fe621944 2020-11-10 stsp .algo = &myers_then_myers_divide,
75 fe621944 2020-11-10 stsp /* If the state for a forward-Myers is small enough, use Myers, otherwise first
76 fe621944 2020-11-10 stsp * do a Patience. */
77 fe621944 2020-11-10 stsp const struct diff_config diff_config_myers_then_patience = {
78 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
79 fe621944 2020-11-10 stsp .algo = &myers_then_patience,
82 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
83 fe621944 2020-11-10 stsp const struct diff_config diff_config_patience = {
84 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
85 fe621944 2020-11-10 stsp .algo = &patience,
88 fe621944 2020-11-10 stsp /* Directly force Patience as a first divider of the source file. */
89 fe621944 2020-11-10 stsp const struct diff_config diff_config_no_algo = {
90 fe621944 2020-11-10 stsp .atomize_func = diff_atomize_text_by_line,
93 fe621944 2020-11-10 stsp const struct got_error *
94 49d4a017 2022-06-30 stsp got_diffreg_close(char *p1, size_t size1, char *p2, size_t size2)
96 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
98 fe621944 2020-11-10 stsp if (p1 && munmap(p1, size1) == -1 && err == NULL)
99 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
100 fe621944 2020-11-10 stsp if (p2 && munmap(p2, size2) == -1 && err == NULL)
101 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
102 fe621944 2020-11-10 stsp return err;
105 cca5682e 2020-11-18 stsp const struct got_error *
106 cca5682e 2020-11-18 stsp got_diff_get_config(struct diff_config **cfg,
107 cca5682e 2020-11-18 stsp enum got_diff_algorithm algorithm,
108 cca5682e 2020-11-18 stsp diff_atomize_func_t atomize_func, void *atomize_func_data)
110 cca5682e 2020-11-18 stsp *cfg = calloc(1, sizeof(**cfg));
111 cca5682e 2020-11-18 stsp if (*cfg == NULL)
112 cca5682e 2020-11-18 stsp return got_error_from_errno("calloc");
114 fe621944 2020-11-10 stsp switch (algorithm) {
115 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_PATIENCE:
116 cca5682e 2020-11-18 stsp (*cfg)->algo = &patience;
118 fe621944 2020-11-10 stsp case GOT_DIFF_ALGORITHM_MYERS:
119 cca5682e 2020-11-18 stsp (*cfg)->algo = &myers_then_myers_divide;
122 cca5682e 2020-11-18 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad diff algorithm");
125 cca5682e 2020-11-18 stsp if (atomize_func) {
126 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = atomize_func;
127 cca5682e 2020-11-18 stsp (*cfg)->atomize_func_data = atomize_func_data;
129 cca5682e 2020-11-18 stsp (*cfg)->atomize_func = diff_atomize_text_by_line;
131 cca5682e 2020-11-18 stsp (*cfg)->max_recursion_depth = 0; /* use default recursion depth */
133 cca5682e 2020-11-18 stsp return NULL;
136 7d283eee 2017-11-29 stsp const struct got_error *
137 72254787 2020-11-18 stsp got_diff_prepare_file(FILE *f, char **p, size_t *size,
138 fe621944 2020-11-10 stsp struct diff_data *diff_data, const struct diff_config *cfg,
139 64453f7e 2020-11-21 stsp int ignore_whitespace, int force_text_diff)
141 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
142 fe621944 2020-11-10 stsp struct stat st;
143 fe621944 2020-11-10 stsp int diff_flags = 0, rc;
147 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_SHOW_PROTOTYPES;
148 fe621944 2020-11-10 stsp if (ignore_whitespace)
149 fe621944 2020-11-10 stsp diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
150 64453f7e 2020-11-21 stsp if (force_text_diff)
151 64453f7e 2020-11-21 stsp diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
153 72254787 2020-11-18 stsp if (fstat(fileno(f), &st) == -1) {
154 72254787 2020-11-18 stsp err = got_error_from_errno("fstat");
157 72254787 2020-11-18 stsp #ifndef GOT_DIFF_NO_MMAP
158 72254787 2020-11-18 stsp *p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
159 72254787 2020-11-18 stsp fileno(f), 0);
160 72254787 2020-11-18 stsp if (*p == MAP_FAILED)
162 72254787 2020-11-18 stsp *p = NULL; /* fall back on file I/O */
164 72254787 2020-11-18 stsp rc = diff_atomize_file(diff_data, cfg, f, *p, st.st_size, diff_flags);
166 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_atomize_file");
171 fe621944 2020-11-10 stsp diff_data_free(diff_data);
173 fe621944 2020-11-10 stsp *size = st.st_size;
174 fe621944 2020-11-10 stsp return err;
177 fe621944 2020-11-10 stsp const struct got_error *
178 fe621944 2020-11-10 stsp got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
179 64453f7e 2020-11-21 stsp enum got_diff_algorithm algorithm, int ignore_whitespace,
180 64453f7e 2020-11-21 stsp int force_text_diff)
182 fe621944 2020-11-10 stsp const struct got_error *err = NULL;
183 cca5682e 2020-11-18 stsp struct diff_config *cfg = NULL;
184 fe621944 2020-11-10 stsp char *p1 = NULL, *p2 = NULL;
185 fe621944 2020-11-10 stsp size_t size1, size2;
186 fe621944 2020-11-10 stsp struct diff_data d_left, d_right;
187 fe621944 2020-11-10 stsp struct diff_data *left, *right;
188 fe621944 2020-11-10 stsp struct diff_result *diff_result;
190 fe621944 2020-11-10 stsp if (diffreg_result) {
191 fe621944 2020-11-10 stsp *diffreg_result = calloc(1, sizeof(**diffreg_result));
192 fe621944 2020-11-10 stsp if (*diffreg_result == NULL)
193 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
194 fe621944 2020-11-10 stsp left = &(*diffreg_result)->left;
195 fe621944 2020-11-10 stsp right = &(*diffreg_result)->right;
197 fe621944 2020-11-10 stsp memset(&d_left, 0, sizeof(d_left));
198 fe621944 2020-11-10 stsp memset(&d_right, 0, sizeof(d_right));
199 fe621944 2020-11-10 stsp left = &d_left;
200 fe621944 2020-11-10 stsp right = &d_right;
203 cca5682e 2020-11-18 stsp err = got_diff_get_config(&cfg, algorithm, NULL, NULL);
207 72254787 2020-11-18 stsp err = got_diff_prepare_file(f1, &p1, &size1, left, cfg,
208 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
212 72254787 2020-11-18 stsp err = got_diff_prepare_file(f2, &p2, &size2, right, cfg,
213 64453f7e 2020-11-21 stsp ignore_whitespace, force_text_diff);
217 fe621944 2020-11-10 stsp diff_result = diff_main(cfg, left, right);
218 fe621944 2020-11-10 stsp if (diff_result == NULL) {
219 fe621944 2020-11-10 stsp err = got_error_set_errno(ENOMEM, "malloc");
222 fe621944 2020-11-10 stsp if (diff_result->rc != DIFF_RC_OK) {
223 fe621944 2020-11-10 stsp err = got_error_set_errno(diff_result->rc, "diff");
227 fe621944 2020-11-10 stsp if (diffreg_result) {
228 fe621944 2020-11-10 stsp (*diffreg_result)->result = diff_result;
229 fe621944 2020-11-10 stsp (*diffreg_result)->map1 = p1;
230 fe621944 2020-11-10 stsp (*diffreg_result)->size1 = size1;
231 fe621944 2020-11-10 stsp (*diffreg_result)->map2 = p2;
232 fe621944 2020-11-10 stsp (*diffreg_result)->size2 = size2;
236 fe621944 2020-11-10 stsp if (diffreg_result == NULL) {
237 fe621944 2020-11-10 stsp diff_data_free(left);
238 fe621944 2020-11-10 stsp diff_data_free(right);
241 49d4a017 2022-06-30 stsp got_diffreg_close(p1, size1, p2, size2);
242 fe621944 2020-11-10 stsp if (diffreg_result) {
243 fe621944 2020-11-10 stsp diff_data_free(left);
244 fe621944 2020-11-10 stsp diff_data_free(right);
245 fe621944 2020-11-10 stsp free(*diffreg_result);
246 fe621944 2020-11-10 stsp *diffreg_result = NULL;
250 fe621944 2020-11-10 stsp return err;
253 fe621944 2020-11-10 stsp const struct got_error *
254 fe621944 2020-11-10 stsp got_diffreg_output(off_t **line_offsets, size_t *nlines,
255 1cb46f00 2020-11-21 stsp struct got_diffreg_result *diff_result, int f1_exists, int f2_exists,
256 fe621944 2020-11-10 stsp const char *path1, const char *path2,
257 fe621944 2020-11-10 stsp enum got_diff_output_format output_format, int context_lines, FILE *outfile)
259 fe621944 2020-11-10 stsp struct diff_input_info info = {
260 fe621944 2020-11-10 stsp .left_path = path1,
261 fe621944 2020-11-10 stsp .right_path = path2,
262 1cb46f00 2020-11-21 stsp .flags = 0,
265 fe621944 2020-11-10 stsp struct diff_output_info *output_info;
267 1cb46f00 2020-11-21 stsp if (!f1_exists)
268 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_LEFT_NONEXISTENT;
269 1cb46f00 2020-11-21 stsp if (!f2_exists)
270 1cb46f00 2020-11-21 stsp info.flags |= DIFF_INPUT_RIGHT_NONEXISTENT;
272 fe621944 2020-11-10 stsp switch (output_format) {
273 fe621944 2020-11-10 stsp case GOT_DIFF_OUTPUT_UNIDIFF:
274 fe621944 2020-11-10 stsp rc = diff_output_unidiff(
275 fe621944 2020-11-10 stsp line_offsets ? &output_info : NULL, outfile, &info,
276 fe621944 2020-11-10 stsp diff_result->result, context_lines);
277 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
278 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_unidiff");
280 fe621944 2020-11-10 stsp case GOT_DIFF_OUTPUT_EDSCRIPT:
281 fe621944 2020-11-10 stsp rc = diff_output_edscript(line_offsets ? &output_info : NULL,
282 fe621944 2020-11-10 stsp outfile, &info, diff_result->result);
283 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
284 fe621944 2020-11-10 stsp return got_error_set_errno(rc, "diff_output_edscript");
289 fe621944 2020-11-10 stsp if (line_offsets && *line_offsets) {
290 fe621944 2020-11-10 stsp if (output_info->line_offsets.len > 0) {
291 fe621944 2020-11-10 stsp off_t prev_offset = 0, *p, *o;
292 fe621944 2020-11-10 stsp int i, len;
293 fe621944 2020-11-10 stsp if (*nlines > 0) {
294 fe621944 2020-11-10 stsp prev_offset = (*line_offsets)[*nlines - 1];
296 fe621944 2020-11-10 stsp * First line offset is always zero. Skip it
297 fe621944 2020-11-10 stsp * when appending to a pre-populated array.
299 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[1];
300 fe621944 2020-11-10 stsp len = output_info->line_offsets.len - 1;
302 fe621944 2020-11-10 stsp o = &output_info->line_offsets.head[0];
303 fe621944 2020-11-10 stsp len = output_info->line_offsets.len;
305 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + len,
306 fe621944 2020-11-10 stsp sizeof(off_t));
307 fe621944 2020-11-10 stsp if (p == NULL)
308 fe621944 2020-11-10 stsp return got_error_from_errno("calloc");
309 fe621944 2020-11-10 stsp for (i = 0; i < len; i++)
310 fe621944 2020-11-10 stsp p[*nlines + i] = o[i] + prev_offset;
311 fe621944 2020-11-10 stsp *line_offsets = p;
312 fe621944 2020-11-10 stsp *nlines += len;
314 fe621944 2020-11-10 stsp diff_output_info_free(output_info);
317 fe621944 2020-11-10 stsp return NULL;
320 fe621944 2020-11-10 stsp const struct got_error *
321 fe621944 2020-11-10 stsp got_diffreg_result_free(struct got_diffreg_result *diffreg_result)
323 fe621944 2020-11-10 stsp const struct got_error *err;
325 fe621944 2020-11-10 stsp diff_result_free(diffreg_result->result);
326 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
327 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
328 49d4a017 2022-06-30 stsp err = got_diffreg_close(diffreg_result->map1, diffreg_result->size1,
329 fe621944 2020-11-10 stsp diffreg_result->map2, diffreg_result->size2);
330 fe621944 2020-11-10 stsp free(diffreg_result);
331 fe621944 2020-11-10 stsp return err;
334 fe621944 2020-11-10 stsp const struct got_error *
335 fe621944 2020-11-10 stsp got_diffreg_result_free_left(struct got_diffreg_result *diffreg_result)
337 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->left);
338 fe621944 2020-11-10 stsp memset(&diffreg_result->left, 0, sizeof(diffreg_result->left));
339 49d4a017 2022-06-30 stsp return got_diffreg_close(diffreg_result->map1, diffreg_result->size1,
343 fe621944 2020-11-10 stsp const struct got_error *
344 fe621944 2020-11-10 stsp got_diffreg_result_free_right(struct got_diffreg_result *diffreg_result)
346 fe621944 2020-11-10 stsp diff_data_free(&diffreg_result->right);
347 fe621944 2020-11-10 stsp memset(&diffreg_result->right, 0, sizeof(diffreg_result->right));
348 49d4a017 2022-06-30 stsp return got_diffreg_close(NULL, 0, diffreg_result->map2,
349 49d4a017 2022-06-30 stsp diffreg_result->size2);