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