Blame


1 404c43c4 2018-06-21 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 c27a5e66 2020-11-18 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 404c43c4 2018-06-21 stsp *
5 404c43c4 2018-06-21 stsp * Permission to use, copy, modify, and distribute this software for any
6 404c43c4 2018-06-21 stsp * purpose with or without fee is hereby granted, provided that the above
7 404c43c4 2018-06-21 stsp * copyright notice and this permission notice appear in all copies.
8 404c43c4 2018-06-21 stsp *
9 404c43c4 2018-06-21 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 404c43c4 2018-06-21 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 404c43c4 2018-06-21 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 404c43c4 2018-06-21 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 404c43c4 2018-06-21 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 404c43c4 2018-06-21 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 404c43c4 2018-06-21 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 404c43c4 2018-06-21 stsp */
17 404c43c4 2018-06-21 stsp
18 fe621944 2020-11-10 stsp #include <sys/mman.h>
19 404c43c4 2018-06-21 stsp #include <sys/stat.h>
20 404c43c4 2018-06-21 stsp
21 8c35ff14 2020-11-19 stsp #include <errno.h>
22 404c43c4 2018-06-21 stsp #include <string.h>
23 404c43c4 2018-06-21 stsp #include <stdio.h>
24 404c43c4 2018-06-21 stsp #include <stdlib.h>
25 404c43c4 2018-06-21 stsp #include <time.h>
26 56e0773d 2019-11-28 stsp #include <limits.h>
27 404c43c4 2018-06-21 stsp #include <zlib.h>
28 dd038bc6 2021-09-21 thomas.ad
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 404c43c4 2018-06-21 stsp
31 404c43c4 2018-06-21 stsp #include "got_error.h"
32 404c43c4 2018-06-21 stsp #include "got_object.h"
33 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
34 404c43c4 2018-06-21 stsp #include "got_blame.h"
35 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
36 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
37 404c43c4 2018-06-21 stsp
38 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
39 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
40 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
41 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
42 404c43c4 2018-06-21 stsp
43 fe621944 2020-11-10 stsp #ifndef MAX
44 fe621944 2020-11-10 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
45 fe621944 2020-11-10 stsp #endif
46 fe621944 2020-11-10 stsp
47 404c43c4 2018-06-21 stsp struct got_blame_line {
48 404c43c4 2018-06-21 stsp int annotated;
49 9b94757a 2018-06-21 stsp struct got_object_id id;
50 404c43c4 2018-06-21 stsp };
51 404c43c4 2018-06-21 stsp
52 404c43c4 2018-06-21 stsp struct got_blame {
53 cca5682e 2020-11-18 stsp struct diff_config *cfg;
54 8c35ff14 2020-11-19 stsp int nlines; /* number of lines in file being blamed */
55 8c35ff14 2020-11-19 stsp int nannotated; /* number of lines already annotated */
56 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
57 c35a7943 2018-07-12 stsp int ncommits;
58 c27a5e66 2020-11-18 stsp
59 c27a5e66 2020-11-18 stsp /*
60 8c35ff14 2020-11-19 stsp * These change with every traversed commit. After diffing
61 8c35ff14 2020-11-19 stsp * commits N:N-1, in preparation for diffing commits N-1:N-2,
62 8c35ff14 2020-11-19 stsp * data for commit N is retained and flipped into data for N-1.
63 8c35ff14 2020-11-19 stsp *
64 8c35ff14 2020-11-19 stsp */
65 8c35ff14 2020-11-19 stsp FILE *f1; /* older version from commit N-1. */
66 8c35ff14 2020-11-19 stsp FILE *f2; /* newer version from commit N. */
67 9117a7b7 2022-07-01 thomas int fd;
68 8c35ff14 2020-11-19 stsp unsigned char *map1;
69 8c35ff14 2020-11-19 stsp unsigned char *map2;
70 8c35ff14 2020-11-19 stsp off_t size1;
71 8c35ff14 2020-11-19 stsp off_t size2;
72 8c35ff14 2020-11-19 stsp int nlines1;
73 8c35ff14 2020-11-19 stsp int nlines2;
74 8c35ff14 2020-11-19 stsp off_t *line_offsets1;
75 8c35ff14 2020-11-19 stsp off_t *line_offsets2;
76 8c35ff14 2020-11-19 stsp
77 8c35ff14 2020-11-19 stsp /*
78 c27a5e66 2020-11-18 stsp * Map line numbers of an older version of the file to valid line
79 8c35ff14 2020-11-19 stsp * numbers in the version of the file being blamed. This map is
80 8c35ff14 2020-11-19 stsp * updated with each commit we traverse throughout the file's history.
81 8c35ff14 2020-11-19 stsp * Lines mapped to -1 do not correspond to any line in the version
82 8c35ff14 2020-11-19 stsp * being blamed.
83 c27a5e66 2020-11-18 stsp */
84 8c35ff14 2020-11-19 stsp int *linemap1;
85 c27a5e66 2020-11-18 stsp int *linemap2;
86 8c35ff14 2020-11-19 stsp
87 8c35ff14 2020-11-19 stsp struct diff_data *data1;
88 8c35ff14 2020-11-19 stsp struct diff_data *data2;
89 404c43c4 2018-06-21 stsp };
90 404c43c4 2018-06-21 stsp
91 404c43c4 2018-06-21 stsp static const struct got_error *
92 10f173fe 2022-04-16 thomas annotate_line(struct got_blame *blame, int lineno,
93 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id,
94 10f173fe 2022-04-16 thomas got_blame_cb cb, void *arg)
95 404c43c4 2018-06-21 stsp {
96 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
97 404c43c4 2018-06-21 stsp struct got_blame_line *line;
98 404c43c4 2018-06-21 stsp
99 c27a5e66 2020-11-18 stsp if (lineno < 0 || lineno >= blame->nlines)
100 f2e233d8 2018-11-19 stsp return NULL;
101 3168e5da 2020-09-10 stsp
102 c27a5e66 2020-11-18 stsp line = &blame->lines[lineno];
103 404c43c4 2018-06-21 stsp if (line->annotated)
104 84451b3e 2018-07-10 stsp return NULL;
105 404c43c4 2018-06-21 stsp
106 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
107 404c43c4 2018-06-21 stsp line->annotated = 1;
108 06ca4d09 2018-11-19 stsp blame->nannotated++;
109 84451b3e 2018-07-10 stsp if (cb)
110 10f173fe 2022-04-16 thomas err = cb(arg, blame->nlines, lineno + 1, commit, id);
111 84451b3e 2018-07-10 stsp return err;
112 404c43c4 2018-06-21 stsp }
113 404c43c4 2018-06-21 stsp
114 404c43c4 2018-06-21 stsp static const struct got_error *
115 8c35ff14 2020-11-19 stsp blame_changes(struct got_blame *blame, struct diff_result *diff_result,
116 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *commit_id,
117 10f173fe 2022-04-16 thomas got_blame_cb cb, void *arg)
118 c35a7943 2018-07-12 stsp {
119 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
120 fe621944 2020-11-10 stsp int i;
121 c27a5e66 2020-11-18 stsp int idx1 = 0, idx2 = 0;
122 c35a7943 2018-07-12 stsp
123 c27a5e66 2020-11-18 stsp for (i = 0; i < diff_result->chunks.len &&
124 c27a5e66 2020-11-18 stsp blame->nannotated < blame->nlines; i++) {
125 fe621944 2020-11-10 stsp struct diff_chunk *c = diff_chunk_get(diff_result, i);
126 5d7e2cd0 2021-10-29 thomas unsigned int left_count, right_count;
127 c27a5e66 2020-11-18 stsp int j;
128 c35a7943 2018-07-12 stsp
129 c27a5e66 2020-11-18 stsp /*
130 c27a5e66 2020-11-18 stsp * We do not need to worry about idx1/idx2 growing out
131 c27a5e66 2020-11-18 stsp * of bounds because the diff implementation ensures
132 c27a5e66 2020-11-18 stsp * that chunk ranges never exceed the number of lines
133 c27a5e66 2020-11-18 stsp * in the left/right input files.
134 c27a5e66 2020-11-18 stsp */
135 c27a5e66 2020-11-18 stsp left_count = diff_chunk_get_left_count(c);
136 c27a5e66 2020-11-18 stsp right_count = diff_chunk_get_right_count(c);
137 c27a5e66 2020-11-18 stsp
138 c27a5e66 2020-11-18 stsp if (left_count == right_count) {
139 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
140 8c35ff14 2020-11-19 stsp blame->linemap1[idx1++] =
141 8c35ff14 2020-11-19 stsp blame->linemap2[idx2++];
142 c27a5e66 2020-11-18 stsp }
143 fe621944 2020-11-10 stsp continue;
144 c27a5e66 2020-11-18 stsp }
145 fe621944 2020-11-10 stsp
146 c27a5e66 2020-11-18 stsp if (right_count == 0) {
147 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
148 8c35ff14 2020-11-19 stsp blame->linemap1[idx1++] = -1;
149 c27a5e66 2020-11-18 stsp }
150 fe621944 2020-11-10 stsp continue;
151 c27a5e66 2020-11-18 stsp }
152 fe621944 2020-11-10 stsp
153 c27a5e66 2020-11-18 stsp for (j = 0; j < right_count; j++) {
154 c27a5e66 2020-11-18 stsp int ln = blame->linemap2[idx2++];
155 10f173fe 2022-04-16 thomas err = annotate_line(blame, ln, commit, commit_id,
156 10f173fe 2022-04-16 thomas cb, arg);
157 c35a7943 2018-07-12 stsp if (err)
158 c35a7943 2018-07-12 stsp return err;
159 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
160 4c9641fd 2019-08-21 stsp break;
161 c35a7943 2018-07-12 stsp }
162 c35a7943 2018-07-12 stsp }
163 c35a7943 2018-07-12 stsp
164 c35a7943 2018-07-12 stsp return NULL;
165 c35a7943 2018-07-12 stsp }
166 c35a7943 2018-07-12 stsp
167 c35a7943 2018-07-12 stsp static const struct got_error *
168 8c35ff14 2020-11-19 stsp blame_prepare_file(FILE *f, unsigned char **p, off_t *size,
169 8c35ff14 2020-11-19 stsp int *nlines, off_t **line_offsets, struct diff_data *diff_data,
170 8c35ff14 2020-11-19 stsp const struct diff_config *cfg, struct got_blob_object *blob)
171 8c35ff14 2020-11-19 stsp {
172 8c35ff14 2020-11-19 stsp const struct got_error *err = NULL;
173 b4737997 2020-11-21 stsp int diff_flags = 0, rc;
174 8c35ff14 2020-11-19 stsp
175 8c35ff14 2020-11-19 stsp err = got_object_blob_dump_to_file(size, nlines, line_offsets,
176 8c35ff14 2020-11-19 stsp f, blob);
177 8c35ff14 2020-11-19 stsp if (err)
178 8c35ff14 2020-11-19 stsp return err;
179 8c35ff14 2020-11-19 stsp
180 8c35ff14 2020-11-19 stsp #ifndef GOT_DIFF_NO_MMAP
181 8c35ff14 2020-11-19 stsp *p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
182 8c35ff14 2020-11-19 stsp if (*p == MAP_FAILED)
183 8c35ff14 2020-11-19 stsp #endif
184 8c35ff14 2020-11-19 stsp *p = NULL; /* fall back on file I/O */
185 8c35ff14 2020-11-19 stsp
186 b4737997 2020-11-21 stsp /* Allow blaming lines in binary files even though it's useless. */
187 b4737997 2020-11-21 stsp diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
188 b4737997 2020-11-21 stsp
189 b4737997 2020-11-21 stsp rc = diff_atomize_file(diff_data, cfg, f, *p, *size, diff_flags);
190 8c35ff14 2020-11-19 stsp if (rc)
191 8c35ff14 2020-11-19 stsp return got_error_set_errno(rc, "diff_atomize_file");
192 8c35ff14 2020-11-19 stsp
193 8c35ff14 2020-11-19 stsp return NULL;
194 8c35ff14 2020-11-19 stsp }
195 8c35ff14 2020-11-19 stsp
196 8c35ff14 2020-11-19 stsp static const struct got_error *
197 c27a5e66 2020-11-18 stsp blame_commit(struct got_blame *blame, struct got_object_id *id,
198 c27a5e66 2020-11-18 stsp const char *path, struct got_repository *repo,
199 10f173fe 2022-04-16 thomas got_blame_cb cb, void *arg)
200 404c43c4 2018-06-21 stsp {
201 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
202 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL, *pcommit = NULL;
203 c27a5e66 2020-11-18 stsp struct got_object_qid *pid = NULL;
204 8c35ff14 2020-11-19 stsp struct got_object_id *pblob_id = NULL;
205 8c35ff14 2020-11-19 stsp struct got_blob_object *pblob = NULL;
206 8c35ff14 2020-11-19 stsp struct diff_result *diff_result = NULL;
207 404c43c4 2018-06-21 stsp
208 c27a5e66 2020-11-18 stsp err = got_object_open_as_commit(&commit, repo, id);
209 8d725ae1 2019-08-18 stsp if (err)
210 8d725ae1 2019-08-18 stsp return err;
211 8d725ae1 2019-08-18 stsp
212 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
213 c27a5e66 2020-11-18 stsp if (pid == NULL) {
214 c27a5e66 2020-11-18 stsp got_object_commit_close(commit);
215 c27a5e66 2020-11-18 stsp return NULL;
216 c27a5e66 2020-11-18 stsp }
217 c27a5e66 2020-11-18 stsp
218 ec242592 2022-04-22 thomas err = got_object_open_as_commit(&pcommit, repo, &pid->id);
219 945f9229 2022-04-16 thomas if (err)
220 945f9229 2022-04-16 thomas goto done;
221 945f9229 2022-04-16 thomas
222 945f9229 2022-04-16 thomas err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
223 4c9641fd 2019-08-21 stsp if (err) {
224 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
225 4c9641fd 2019-08-21 stsp err = NULL;
226 404c43c4 2018-06-21 stsp goto done;
227 4c9641fd 2019-08-21 stsp }
228 27d434c2 2018-09-15 stsp
229 9117a7b7 2022-07-01 thomas err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, blame->fd);
230 27d434c2 2018-09-15 stsp if (err)
231 27d434c2 2018-09-15 stsp goto done;
232 27d434c2 2018-09-15 stsp
233 8c35ff14 2020-11-19 stsp err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
234 8c35ff14 2020-11-19 stsp &blame->nlines1, &blame->line_offsets1, blame->data1,
235 8c35ff14 2020-11-19 stsp blame->cfg, pblob);
236 404c43c4 2018-06-21 stsp if (err)
237 404c43c4 2018-06-21 stsp goto done;
238 404c43c4 2018-06-21 stsp
239 8c35ff14 2020-11-19 stsp diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
240 8c35ff14 2020-11-19 stsp if (diff_result == NULL) {
241 8c35ff14 2020-11-19 stsp err = got_error_set_errno(ENOMEM, "malloc");
242 4c9641fd 2019-08-21 stsp goto done;
243 4c9641fd 2019-08-21 stsp }
244 8c35ff14 2020-11-19 stsp if (diff_result->rc != DIFF_RC_OK) {
245 8c35ff14 2020-11-19 stsp err = got_error_set_errno(diff_result->rc, "diff");
246 404c43c4 2018-06-21 stsp goto done;
247 c27a5e66 2020-11-18 stsp }
248 8c35ff14 2020-11-19 stsp if (diff_result->chunks.len > 0) {
249 8c35ff14 2020-11-19 stsp if (blame->nlines1 > 0) {
250 8c35ff14 2020-11-19 stsp blame->linemap1 = calloc(blame->nlines1,
251 8c35ff14 2020-11-19 stsp sizeof(*blame->linemap1));
252 8c35ff14 2020-11-19 stsp if (blame->linemap1 == NULL) {
253 c27a5e66 2020-11-18 stsp err = got_error_from_errno("malloc");
254 c27a5e66 2020-11-18 stsp goto done;
255 c27a5e66 2020-11-18 stsp }
256 c27a5e66 2020-11-18 stsp }
257 10f173fe 2022-04-16 thomas err = blame_changes(blame, diff_result, commit, id, cb, arg);
258 8c35ff14 2020-11-19 stsp if (err)
259 c27a5e66 2020-11-18 stsp goto done;
260 d68a0a7d 2018-07-10 stsp } else if (cb)
261 10f173fe 2022-04-16 thomas err = cb(arg, blame->nlines, -1, commit, id);
262 404c43c4 2018-06-21 stsp done:
263 8c35ff14 2020-11-19 stsp if (diff_result)
264 8c35ff14 2020-11-19 stsp diff_result_free(diff_result);
265 8d725ae1 2019-08-18 stsp if (commit)
266 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
267 945f9229 2022-04-16 thomas if (pcommit)
268 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
269 c27a5e66 2020-11-18 stsp free(pblob_id);
270 c27a5e66 2020-11-18 stsp if (pblob)
271 c27a5e66 2020-11-18 stsp got_object_blob_close(pblob);
272 404c43c4 2018-06-21 stsp return err;
273 404c43c4 2018-06-21 stsp }
274 404c43c4 2018-06-21 stsp
275 fb43ecf1 2019-02-11 stsp static const struct got_error *
276 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
277 404c43c4 2018-06-21 stsp {
278 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
279 c35a7943 2018-07-12 stsp
280 8c35ff14 2020-11-19 stsp diff_data_free(blame->data1);
281 8c35ff14 2020-11-19 stsp free(blame->data1);
282 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2);
283 8c35ff14 2020-11-19 stsp free(blame->data2);
284 8c35ff14 2020-11-19 stsp if (blame->map1) {
285 8c35ff14 2020-11-19 stsp if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
286 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
287 8c35ff14 2020-11-19 stsp }
288 8c35ff14 2020-11-19 stsp if (blame->map2) {
289 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
290 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
291 8c35ff14 2020-11-19 stsp }
292 404c43c4 2018-06-21 stsp free(blame->lines);
293 8c35ff14 2020-11-19 stsp free(blame->line_offsets1);
294 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
295 8c35ff14 2020-11-19 stsp free(blame->linemap1);
296 c27a5e66 2020-11-18 stsp free(blame->linemap2);
297 cca5682e 2020-11-18 stsp free(blame->cfg);
298 404c43c4 2018-06-21 stsp free(blame);
299 fb43ecf1 2019-02-11 stsp return err;
300 8c35ff14 2020-11-19 stsp }
301 8c35ff14 2020-11-19 stsp
302 8c35ff14 2020-11-19 stsp static int
303 8c35ff14 2020-11-19 stsp atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
304 8c35ff14 2020-11-19 stsp off_t *line_offsets)
305 8c35ff14 2020-11-19 stsp {
306 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
307 b4737997 2020-11-21 stsp int embedded_nul = 0;
308 8c35ff14 2020-11-19 stsp
309 8c35ff14 2020-11-19 stsp ARRAYLIST_INIT(d->atoms, nlines);
310 8c35ff14 2020-11-19 stsp
311 8c35ff14 2020-11-19 stsp for (i = 0; i < nlines; i++) {
312 8c35ff14 2020-11-19 stsp struct diff_atom *atom;
313 8c35ff14 2020-11-19 stsp off_t len, pos = line_offsets[i];
314 8c35ff14 2020-11-19 stsp unsigned int hash = 0;
315 8c35ff14 2020-11-19 stsp int j;
316 8c35ff14 2020-11-19 stsp
317 8c35ff14 2020-11-19 stsp ARRAYLIST_ADD(atom, d->atoms);
318 8c35ff14 2020-11-19 stsp if (atom == NULL) {
319 8c35ff14 2020-11-19 stsp rc = errno;
320 8c35ff14 2020-11-19 stsp break;
321 8c35ff14 2020-11-19 stsp }
322 8c35ff14 2020-11-19 stsp
323 8c35ff14 2020-11-19 stsp if (i < nlines - 1)
324 8c35ff14 2020-11-19 stsp len = line_offsets[i + 1] - pos;
325 8c35ff14 2020-11-19 stsp else
326 8c35ff14 2020-11-19 stsp len = filesize - pos;
327 8c35ff14 2020-11-19 stsp
328 8c35ff14 2020-11-19 stsp if (fseeko(f, pos, SEEK_SET) == -1) {
329 8c35ff14 2020-11-19 stsp rc = errno;
330 8c35ff14 2020-11-19 stsp break;
331 8c35ff14 2020-11-19 stsp }
332 8c35ff14 2020-11-19 stsp for (j = 0; j < len; j++) {
333 8c35ff14 2020-11-19 stsp int c = fgetc(f);
334 8c35ff14 2020-11-19 stsp if (c == EOF) {
335 8c35ff14 2020-11-19 stsp if (feof(f))
336 8c35ff14 2020-11-19 stsp rc = EIO; /* unexpected EOF */
337 8c35ff14 2020-11-19 stsp else
338 8c35ff14 2020-11-19 stsp rc = errno;
339 8c35ff14 2020-11-19 stsp goto done;
340 8c35ff14 2020-11-19 stsp }
341 8c35ff14 2020-11-19 stsp
342 8c35ff14 2020-11-19 stsp hash = diff_atom_hash_update(hash, (unsigned char)c);
343 b4737997 2020-11-21 stsp
344 b4737997 2020-11-21 stsp if (c == '\0')
345 b4737997 2020-11-21 stsp embedded_nul = 1;
346 b4737997 2020-11-21 stsp
347 8c35ff14 2020-11-19 stsp }
348 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
349 8c35ff14 2020-11-19 stsp .root = d,
350 8c35ff14 2020-11-19 stsp .pos = pos,
351 8c35ff14 2020-11-19 stsp .at = NULL, /* atom data is not memory-mapped */
352 8c35ff14 2020-11-19 stsp .len = len,
353 8c35ff14 2020-11-19 stsp .hash = hash,
354 8c35ff14 2020-11-19 stsp };
355 8c35ff14 2020-11-19 stsp }
356 b4737997 2020-11-21 stsp
357 b4737997 2020-11-21 stsp /* File are considered binary if they contain embedded '\0' bytes. */
358 b4737997 2020-11-21 stsp if (embedded_nul)
359 b4737997 2020-11-21 stsp d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
360 8c35ff14 2020-11-19 stsp done:
361 8c35ff14 2020-11-19 stsp if (rc)
362 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
363 8c35ff14 2020-11-19 stsp
364 8c35ff14 2020-11-19 stsp return rc;
365 404c43c4 2018-06-21 stsp }
366 404c43c4 2018-06-21 stsp
367 8c35ff14 2020-11-19 stsp static int
368 8c35ff14 2020-11-19 stsp atomize_file_mmap(struct diff_data *d, unsigned char *p,
369 8c35ff14 2020-11-19 stsp off_t filesize, int nlines, off_t *line_offsets)
370 8c35ff14 2020-11-19 stsp {
371 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
372 b4737997 2020-11-21 stsp int embedded_nul = 0;
373 8c35ff14 2020-11-19 stsp
374 8c35ff14 2020-11-19 stsp ARRAYLIST_INIT(d->atoms, nlines);
375 8c35ff14 2020-11-19 stsp
376 8c35ff14 2020-11-19 stsp for (i = 0; i < nlines; i++) {
377 8c35ff14 2020-11-19 stsp struct diff_atom *atom;
378 8c35ff14 2020-11-19 stsp off_t len, pos = line_offsets[i];
379 8c35ff14 2020-11-19 stsp unsigned int hash = 0;
380 8c35ff14 2020-11-19 stsp int j;
381 8c35ff14 2020-11-19 stsp
382 8c35ff14 2020-11-19 stsp ARRAYLIST_ADD(atom, d->atoms);
383 8c35ff14 2020-11-19 stsp if (atom == NULL) {
384 8c35ff14 2020-11-19 stsp rc = errno;
385 8c35ff14 2020-11-19 stsp break;
386 8c35ff14 2020-11-19 stsp }
387 8c35ff14 2020-11-19 stsp
388 8c35ff14 2020-11-19 stsp if (i < nlines - 1)
389 8c35ff14 2020-11-19 stsp len = line_offsets[i + 1] - pos;
390 8c35ff14 2020-11-19 stsp else
391 8c35ff14 2020-11-19 stsp len = filesize - pos;
392 8c35ff14 2020-11-19 stsp
393 8c35ff14 2020-11-19 stsp for (j = 0; j < len; j++)
394 8c35ff14 2020-11-19 stsp hash = diff_atom_hash_update(hash, p[pos + j]);
395 b4737997 2020-11-21 stsp
396 b4737997 2020-11-21 stsp if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
397 b4737997 2020-11-21 stsp embedded_nul = 1;
398 8c35ff14 2020-11-19 stsp
399 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
400 8c35ff14 2020-11-19 stsp .root = d,
401 8c35ff14 2020-11-19 stsp .pos = pos,
402 8c35ff14 2020-11-19 stsp .at = &p[pos],
403 8c35ff14 2020-11-19 stsp .len = len,
404 8c35ff14 2020-11-19 stsp .hash = hash,
405 8c35ff14 2020-11-19 stsp };
406 8c35ff14 2020-11-19 stsp }
407 8c35ff14 2020-11-19 stsp
408 b4737997 2020-11-21 stsp /* File are considered binary if they contain embedded '\0' bytes. */
409 b4737997 2020-11-21 stsp if (embedded_nul)
410 b4737997 2020-11-21 stsp d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
411 b4737997 2020-11-21 stsp
412 8c35ff14 2020-11-19 stsp if (rc)
413 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
414 8c35ff14 2020-11-19 stsp
415 8c35ff14 2020-11-19 stsp return rc;
416 8c35ff14 2020-11-19 stsp }
417 8c35ff14 2020-11-19 stsp
418 8c35ff14 2020-11-19 stsp /* Implements diff_atomize_func_t */
419 8c35ff14 2020-11-19 stsp static int
420 8c35ff14 2020-11-19 stsp blame_atomize_file(void *arg, struct diff_data *d)
421 8c35ff14 2020-11-19 stsp {
422 8c35ff14 2020-11-19 stsp struct got_blame *blame = arg;
423 8c35ff14 2020-11-19 stsp
424 8c35ff14 2020-11-19 stsp if (d->f == blame->f1) {
425 8c35ff14 2020-11-19 stsp if (blame->map1)
426 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map1,
427 8c35ff14 2020-11-19 stsp blame->size1, blame->nlines1,
428 8c35ff14 2020-11-19 stsp blame->line_offsets1);
429 8c35ff14 2020-11-19 stsp else
430 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f1, blame->size1,
431 8c35ff14 2020-11-19 stsp blame->nlines1, blame->line_offsets1);
432 8c35ff14 2020-11-19 stsp } else if (d->f == blame->f2) {
433 8c35ff14 2020-11-19 stsp if (d->atoms.len > 0) {
434 8c35ff14 2020-11-19 stsp /* Re-use data from previous commit. */
435 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
436 8c35ff14 2020-11-19 stsp }
437 8c35ff14 2020-11-19 stsp if (blame->map2)
438 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map2,
439 8c35ff14 2020-11-19 stsp blame->size2, blame->nlines2,
440 8c35ff14 2020-11-19 stsp blame->line_offsets2);
441 8c35ff14 2020-11-19 stsp else
442 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f2, blame->size2,
443 8c35ff14 2020-11-19 stsp blame->nlines2, blame->line_offsets2);
444 8c35ff14 2020-11-19 stsp }
445 8c35ff14 2020-11-19 stsp
446 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
447 8c35ff14 2020-11-19 stsp }
448 8c35ff14 2020-11-19 stsp
449 404c43c4 2018-06-21 stsp static const struct got_error *
450 9117a7b7 2022-07-01 thomas flip_files(struct got_blame *blame)
451 8c35ff14 2020-11-19 stsp {
452 9117a7b7 2022-07-01 thomas const struct got_error *err = NULL;
453 8c35ff14 2020-11-19 stsp struct diff_data *d;
454 9117a7b7 2022-07-01 thomas FILE *tmp;
455 8c35ff14 2020-11-19 stsp
456 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
457 8c35ff14 2020-11-19 stsp blame->line_offsets2 = blame->line_offsets1;
458 8c35ff14 2020-11-19 stsp blame->line_offsets1 = NULL;
459 8c35ff14 2020-11-19 stsp
460 8c35ff14 2020-11-19 stsp free(blame->linemap2);
461 8c35ff14 2020-11-19 stsp blame->linemap2 = blame->linemap1;
462 8c35ff14 2020-11-19 stsp blame->linemap1 = NULL;
463 8c35ff14 2020-11-19 stsp
464 8c35ff14 2020-11-19 stsp if (blame->map2) {
465 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1)
466 8c35ff14 2020-11-19 stsp return got_error_from_errno("munmap");
467 8c35ff14 2020-11-19 stsp blame->map2 = blame->map1;
468 5e9266f9 2020-11-28 naddy blame->map1 = NULL;
469 8c35ff14 2020-11-19 stsp }
470 8c35ff14 2020-11-19 stsp blame->size2 = blame->size1;
471 8c35ff14 2020-11-19 stsp
472 9117a7b7 2022-07-01 thomas err = got_opentemp_truncate(blame->f2);
473 9117a7b7 2022-07-01 thomas if (err)
474 9117a7b7 2022-07-01 thomas return err;
475 9117a7b7 2022-07-01 thomas tmp = blame->f2;
476 8c35ff14 2020-11-19 stsp blame->f2 = blame->f1;
477 9117a7b7 2022-07-01 thomas blame->f1 = tmp;
478 9117a7b7 2022-07-01 thomas blame->size1 = 0;
479 8c35ff14 2020-11-19 stsp
480 8c35ff14 2020-11-19 stsp blame->nlines2 = blame->nlines1;
481 8c35ff14 2020-11-19 stsp blame->nlines1 = 0;
482 8c35ff14 2020-11-19 stsp
483 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2); /* does not free pointer itself */
484 8c35ff14 2020-11-19 stsp memset(blame->data2, 0, sizeof(*blame->data2));
485 8c35ff14 2020-11-19 stsp d = blame->data2;
486 8c35ff14 2020-11-19 stsp blame->data2 = blame->data1;
487 8c35ff14 2020-11-19 stsp blame->data1 = d;
488 8c35ff14 2020-11-19 stsp
489 8c35ff14 2020-11-19 stsp return NULL;
490 8c35ff14 2020-11-19 stsp }
491 8c35ff14 2020-11-19 stsp
492 8c35ff14 2020-11-19 stsp static const struct got_error *
493 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
494 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
495 00a8878e 2022-07-01 thomas got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg,
496 9117a7b7 2022-07-01 thomas int fd1, int fd2, FILE *f1, FILE *f2)
497 404c43c4 2018-06-21 stsp {
498 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
499 10f173fe 2022-04-16 thomas struct got_commit_object *start_commit = NULL, *last_commit = NULL;
500 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
501 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
502 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
503 c27a5e66 2020-11-18 stsp struct got_object_id *id = NULL;
504 00a8878e 2022-07-01 thomas int lineno;
505 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
506 404c43c4 2018-06-21 stsp
507 404c43c4 2018-06-21 stsp *blamep = NULL;
508 404c43c4 2018-06-21 stsp
509 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
510 404c43c4 2018-06-21 stsp if (err)
511 c27a5e66 2020-11-18 stsp goto done;
512 27d434c2 2018-09-15 stsp
513 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, repo, start_commit, path);
514 945f9229 2022-04-16 thomas if (err)
515 945f9229 2022-04-16 thomas goto done;
516 945f9229 2022-04-16 thomas
517 9117a7b7 2022-07-01 thomas err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
518 27d434c2 2018-09-15 stsp if (err)
519 27d434c2 2018-09-15 stsp goto done;
520 27d434c2 2018-09-15 stsp
521 8c35ff14 2020-11-19 stsp blame = calloc(1, sizeof(*blame));
522 8c35ff14 2020-11-19 stsp if (blame == NULL) {
523 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
524 404c43c4 2018-06-21 stsp goto done;
525 404c43c4 2018-06-21 stsp }
526 404c43c4 2018-06-21 stsp
527 8c35ff14 2020-11-19 stsp blame->data1 = calloc(1, sizeof(*blame->data1));
528 8c35ff14 2020-11-19 stsp if (blame->data1 == NULL) {
529 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
530 404c43c4 2018-06-21 stsp goto done;
531 8c35ff14 2020-11-19 stsp }
532 8c35ff14 2020-11-19 stsp blame->data2 = calloc(1, sizeof(*blame->data2));
533 8c35ff14 2020-11-19 stsp if (blame->data2 == NULL) {
534 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
535 c27a5e66 2020-11-18 stsp goto done;
536 c27a5e66 2020-11-18 stsp }
537 404c43c4 2018-06-21 stsp
538 9117a7b7 2022-07-01 thomas blame->f1 = f1;
539 9117a7b7 2022-07-01 thomas blame->f2 = f2;
540 9117a7b7 2022-07-01 thomas blame->fd = fd2;
541 9117a7b7 2022-07-01 thomas
542 cca5682e 2020-11-18 stsp err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
543 8c35ff14 2020-11-19 stsp blame_atomize_file, blame);
544 cca5682e 2020-11-18 stsp if (err)
545 cca5682e 2020-11-18 stsp goto done;
546 fe621944 2020-11-10 stsp
547 8c35ff14 2020-11-19 stsp err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
548 8c35ff14 2020-11-19 stsp &blame->nlines2, &blame->line_offsets2, blame->data2,
549 8c35ff14 2020-11-19 stsp blame->cfg, blob);
550 8c35ff14 2020-11-19 stsp blame->nlines = blame->nlines2;
551 8c35ff14 2020-11-19 stsp if (err || blame->nlines == 0)
552 8c35ff14 2020-11-19 stsp goto done;
553 8c35ff14 2020-11-19 stsp
554 8c35ff14 2020-11-19 stsp got_object_blob_close(blob);
555 8c35ff14 2020-11-19 stsp blob = NULL;
556 8c35ff14 2020-11-19 stsp
557 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
558 8c35ff14 2020-11-19 stsp if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
559 b02560ec 2019-08-19 stsp blame->nlines--;
560 404c43c4 2018-06-21 stsp
561 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
562 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
563 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
564 404c43c4 2018-06-21 stsp goto done;
565 404c43c4 2018-06-21 stsp }
566 404c43c4 2018-06-21 stsp
567 c27a5e66 2020-11-18 stsp blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
568 c27a5e66 2020-11-18 stsp if (blame->linemap2 == NULL) {
569 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
570 c27a5e66 2020-11-18 stsp goto done;
571 c27a5e66 2020-11-18 stsp }
572 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines2; lineno++)
573 c27a5e66 2020-11-18 stsp blame->linemap2[lineno] = lineno;
574 c27a5e66 2020-11-18 stsp
575 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
576 404c43c4 2018-06-21 stsp if (err)
577 c27a5e66 2020-11-18 stsp goto done;
578 c27a5e66 2020-11-18 stsp
579 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
580 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
581 293f6400 2018-09-20 stsp if (err)
582 404c43c4 2018-06-21 stsp goto done;
583 656b1f76 2019-05-11 jcs for (;;) {
584 c27a5e66 2020-11-18 stsp struct got_object_id *next_id;
585 c27a5e66 2020-11-18 stsp err = got_commit_graph_iter_next(&next_id, graph, repo,
586 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
587 404c43c4 2018-06-21 stsp if (err) {
588 c27a5e66 2020-11-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
589 4c9641fd 2019-08-21 stsp err = NULL;
590 c27a5e66 2020-11-18 stsp break;
591 c27a5e66 2020-11-18 stsp }
592 c27a5e66 2020-11-18 stsp goto done;
593 293f6400 2018-09-20 stsp }
594 c27a5e66 2020-11-18 stsp if (next_id) {
595 c27a5e66 2020-11-18 stsp id = next_id;
596 c27a5e66 2020-11-18 stsp err = blame_commit(blame, id, path, repo, cb, arg);
597 293f6400 2018-09-20 stsp if (err) {
598 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
599 293f6400 2018-09-20 stsp err = NULL;
600 c27a5e66 2020-11-18 stsp goto done;
601 293f6400 2018-09-20 stsp }
602 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
603 06ca4d09 2018-11-19 stsp break;
604 8c35ff14 2020-11-19 stsp
605 9117a7b7 2022-07-01 thomas err = flip_files(blame);
606 8c35ff14 2020-11-19 stsp if (err)
607 8c35ff14 2020-11-19 stsp goto done;
608 404c43c4 2018-06-21 stsp }
609 293f6400 2018-09-20 stsp }
610 ed77f2ae 2018-06-21 stsp
611 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
612 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
613 10f173fe 2022-04-16 thomas err = got_object_open_as_commit(&last_commit, repo, id);
614 10f173fe 2022-04-16 thomas if (err)
615 10f173fe 2022-04-16 thomas goto done;
616 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines; lineno++) {
617 10f173fe 2022-04-16 thomas err = annotate_line(blame, lineno, last_commit, id,
618 10f173fe 2022-04-16 thomas cb, arg);
619 293f6400 2018-09-20 stsp if (err)
620 293f6400 2018-09-20 stsp goto done;
621 404c43c4 2018-06-21 stsp }
622 404c43c4 2018-06-21 stsp }
623 404c43c4 2018-06-21 stsp
624 404c43c4 2018-06-21 stsp done:
625 293f6400 2018-09-20 stsp if (graph)
626 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
627 27d434c2 2018-09-15 stsp free(obj_id);
628 404c43c4 2018-06-21 stsp if (blob)
629 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
630 945f9229 2022-04-16 thomas if (start_commit)
631 945f9229 2022-04-16 thomas got_object_commit_close(start_commit);
632 10f173fe 2022-04-16 thomas if (last_commit)
633 10f173fe 2022-04-16 thomas got_object_commit_close(last_commit);
634 1828273a 2018-07-09 stsp if (err) {
635 1828273a 2018-07-09 stsp if (blame)
636 1828273a 2018-07-09 stsp blame_close(blame);
637 1828273a 2018-07-09 stsp } else
638 404c43c4 2018-06-21 stsp *blamep = blame;
639 404c43c4 2018-06-21 stsp
640 404c43c4 2018-06-21 stsp return err;
641 404c43c4 2018-06-21 stsp }
642 84451b3e 2018-07-10 stsp
643 84451b3e 2018-07-10 stsp const struct got_error *
644 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
645 10f173fe 2022-04-16 thomas struct got_repository *repo, got_blame_cb cb, void *arg,
646 9117a7b7 2022-07-01 thomas got_cancel_cb cancel_cb, void* cancel_arg, int fd1, int fd2, FILE *f1,
647 9117a7b7 2022-07-01 thomas FILE *f2)
648 84451b3e 2018-07-10 stsp {
649 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
650 84451b3e 2018-07-10 stsp struct got_blame *blame;
651 84451b3e 2018-07-10 stsp char *abspath;
652 84451b3e 2018-07-10 stsp
653 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
654 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
655 84451b3e 2018-07-10 stsp
656 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
657 9117a7b7 2022-07-01 thomas cancel_cb, cancel_arg, fd1, fd2, f1, f2);
658 84451b3e 2018-07-10 stsp free(abspath);
659 26206841 2018-07-12 stsp if (blame)
660 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
661 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
662 84451b3e 2018-07-10 stsp }