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 404c43c4 2018-06-21 stsp #include <sys/queue.h>
19 fe621944 2020-11-10 stsp #include <sys/mman.h>
20 404c43c4 2018-06-21 stsp #include <sys/stat.h>
21 404c43c4 2018-06-21 stsp
22 8c35ff14 2020-11-19 stsp #include <errno.h>
23 404c43c4 2018-06-21 stsp #include <sha1.h>
24 404c43c4 2018-06-21 stsp #include <string.h>
25 404c43c4 2018-06-21 stsp #include <stdio.h>
26 404c43c4 2018-06-21 stsp #include <stdlib.h>
27 404c43c4 2018-06-21 stsp #include <time.h>
28 56e0773d 2019-11-28 stsp #include <limits.h>
29 404c43c4 2018-06-21 stsp #include <util.h>
30 404c43c4 2018-06-21 stsp #include <zlib.h>
31 404c43c4 2018-06-21 stsp
32 404c43c4 2018-06-21 stsp #include "got_error.h"
33 404c43c4 2018-06-21 stsp #include "got_object.h"
34 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
35 404c43c4 2018-06-21 stsp #include "got_blame.h"
36 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
37 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
38 404c43c4 2018-06-21 stsp
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
41 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
42 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
43 404c43c4 2018-06-21 stsp
44 fe621944 2020-11-10 stsp #ifndef MAX
45 fe621944 2020-11-10 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 fe621944 2020-11-10 stsp #endif
47 fe621944 2020-11-10 stsp
48 404c43c4 2018-06-21 stsp struct got_blame_line {
49 404c43c4 2018-06-21 stsp int annotated;
50 9b94757a 2018-06-21 stsp struct got_object_id id;
51 404c43c4 2018-06-21 stsp };
52 404c43c4 2018-06-21 stsp
53 404c43c4 2018-06-21 stsp struct got_blame {
54 cca5682e 2020-11-18 stsp struct diff_config *cfg;
55 8c35ff14 2020-11-19 stsp int nlines; /* number of lines in file being blamed */
56 8c35ff14 2020-11-19 stsp int nannotated; /* number of lines already annotated */
57 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
58 c35a7943 2018-07-12 stsp int ncommits;
59 c27a5e66 2020-11-18 stsp
60 c27a5e66 2020-11-18 stsp /*
61 8c35ff14 2020-11-19 stsp * These change with every traversed commit. After diffing
62 8c35ff14 2020-11-19 stsp * commits N:N-1, in preparation for diffing commits N-1:N-2,
63 8c35ff14 2020-11-19 stsp * data for commit N is retained and flipped into data for N-1.
64 8c35ff14 2020-11-19 stsp *
65 8c35ff14 2020-11-19 stsp */
66 8c35ff14 2020-11-19 stsp FILE *f1; /* older version from commit N-1. */
67 8c35ff14 2020-11-19 stsp FILE *f2; /* newer version from commit N. */
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 392891ce 2022-04-07 stsp annotate_line(struct got_blame *blame, int lineno,
93 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id,
94 392891ce 2022-04-07 stsp 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 392891ce 2022-04-07 stsp 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 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
117 392891ce 2022-04-07 stsp 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 47a90748 2021-10-27 naddy 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 392891ce 2022-04-07 stsp err = annotate_line(blame, ln, commit, commit_id,
156 392891ce 2022-04-07 stsp 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 392891ce 2022-04-07 stsp 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 a44927cc 2022-04-07 stsp 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 eb81bc23 2022-06-28 tracey int fd = -1;
208 404c43c4 2018-06-21 stsp
209 c27a5e66 2020-11-18 stsp err = got_object_open_as_commit(&commit, repo, id);
210 8d725ae1 2019-08-18 stsp if (err)
211 8d725ae1 2019-08-18 stsp return err;
212 8d725ae1 2019-08-18 stsp
213 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
214 c27a5e66 2020-11-18 stsp if (pid == NULL) {
215 c27a5e66 2020-11-18 stsp got_object_commit_close(commit);
216 c27a5e66 2020-11-18 stsp return NULL;
217 c27a5e66 2020-11-18 stsp }
218 c27a5e66 2020-11-18 stsp
219 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
220 eb81bc23 2022-06-28 tracey if (fd == -1) {
221 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
222 eb81bc23 2022-06-28 tracey goto done;
223 eb81bc23 2022-06-28 tracey }
224 eb81bc23 2022-06-28 tracey
225 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&pcommit, repo, &pid->id);
226 a44927cc 2022-04-07 stsp if (err)
227 a44927cc 2022-04-07 stsp goto done;
228 a44927cc 2022-04-07 stsp
229 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
230 4c9641fd 2019-08-21 stsp if (err) {
231 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
232 4c9641fd 2019-08-21 stsp err = NULL;
233 404c43c4 2018-06-21 stsp goto done;
234 4c9641fd 2019-08-21 stsp }
235 27d434c2 2018-09-15 stsp
236 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, fd);
237 27d434c2 2018-09-15 stsp if (err)
238 27d434c2 2018-09-15 stsp goto done;
239 27d434c2 2018-09-15 stsp
240 8c35ff14 2020-11-19 stsp blame->f1 = got_opentemp();
241 8c35ff14 2020-11-19 stsp if (blame->f1 == NULL) {
242 c27a5e66 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
243 404c43c4 2018-06-21 stsp goto done;
244 404c43c4 2018-06-21 stsp }
245 8c35ff14 2020-11-19 stsp
246 8c35ff14 2020-11-19 stsp err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
247 8c35ff14 2020-11-19 stsp &blame->nlines1, &blame->line_offsets1, blame->data1,
248 8c35ff14 2020-11-19 stsp blame->cfg, pblob);
249 404c43c4 2018-06-21 stsp if (err)
250 404c43c4 2018-06-21 stsp goto done;
251 404c43c4 2018-06-21 stsp
252 8c35ff14 2020-11-19 stsp diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
253 8c35ff14 2020-11-19 stsp if (diff_result == NULL) {
254 8c35ff14 2020-11-19 stsp err = got_error_set_errno(ENOMEM, "malloc");
255 4c9641fd 2019-08-21 stsp goto done;
256 4c9641fd 2019-08-21 stsp }
257 8c35ff14 2020-11-19 stsp if (diff_result->rc != DIFF_RC_OK) {
258 8c35ff14 2020-11-19 stsp err = got_error_set_errno(diff_result->rc, "diff");
259 404c43c4 2018-06-21 stsp goto done;
260 c27a5e66 2020-11-18 stsp }
261 8c35ff14 2020-11-19 stsp if (diff_result->chunks.len > 0) {
262 8c35ff14 2020-11-19 stsp if (blame->nlines1 > 0) {
263 8c35ff14 2020-11-19 stsp blame->linemap1 = calloc(blame->nlines1,
264 8c35ff14 2020-11-19 stsp sizeof(*blame->linemap1));
265 8c35ff14 2020-11-19 stsp if (blame->linemap1 == NULL) {
266 c27a5e66 2020-11-18 stsp err = got_error_from_errno("malloc");
267 c27a5e66 2020-11-18 stsp goto done;
268 c27a5e66 2020-11-18 stsp }
269 c27a5e66 2020-11-18 stsp }
270 392891ce 2022-04-07 stsp err = blame_changes(blame, diff_result, commit, id, cb, arg);
271 8c35ff14 2020-11-19 stsp if (err)
272 c27a5e66 2020-11-18 stsp goto done;
273 d68a0a7d 2018-07-10 stsp } else if (cb)
274 392891ce 2022-04-07 stsp err = cb(arg, blame->nlines, -1, commit, id);
275 404c43c4 2018-06-21 stsp done:
276 8c35ff14 2020-11-19 stsp if (diff_result)
277 8c35ff14 2020-11-19 stsp diff_result_free(diff_result);
278 8d725ae1 2019-08-18 stsp if (commit)
279 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
280 a44927cc 2022-04-07 stsp if (pcommit)
281 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
282 c27a5e66 2020-11-18 stsp free(pblob_id);
283 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
284 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
285 c27a5e66 2020-11-18 stsp if (pblob)
286 c27a5e66 2020-11-18 stsp got_object_blob_close(pblob);
287 404c43c4 2018-06-21 stsp return err;
288 404c43c4 2018-06-21 stsp }
289 404c43c4 2018-06-21 stsp
290 fb43ecf1 2019-02-11 stsp static const struct got_error *
291 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
292 404c43c4 2018-06-21 stsp {
293 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
294 c35a7943 2018-07-12 stsp
295 8c35ff14 2020-11-19 stsp diff_data_free(blame->data1);
296 8c35ff14 2020-11-19 stsp free(blame->data1);
297 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2);
298 8c35ff14 2020-11-19 stsp free(blame->data2);
299 8c35ff14 2020-11-19 stsp if (blame->map1) {
300 8c35ff14 2020-11-19 stsp if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
301 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
302 8c35ff14 2020-11-19 stsp }
303 8c35ff14 2020-11-19 stsp if (blame->map2) {
304 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
305 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
306 8c35ff14 2020-11-19 stsp }
307 56b63ca4 2021-01-22 stsp if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
308 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
309 56b63ca4 2021-01-22 stsp if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
310 8c35ff14 2020-11-19 stsp err = got_error_from_errno("fclose");
311 404c43c4 2018-06-21 stsp free(blame->lines);
312 8c35ff14 2020-11-19 stsp free(blame->line_offsets1);
313 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
314 8c35ff14 2020-11-19 stsp free(blame->linemap1);
315 c27a5e66 2020-11-18 stsp free(blame->linemap2);
316 cca5682e 2020-11-18 stsp free(blame->cfg);
317 404c43c4 2018-06-21 stsp free(blame);
318 fb43ecf1 2019-02-11 stsp return err;
319 8c35ff14 2020-11-19 stsp }
320 8c35ff14 2020-11-19 stsp
321 8c35ff14 2020-11-19 stsp static int
322 8c35ff14 2020-11-19 stsp atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
323 8c35ff14 2020-11-19 stsp off_t *line_offsets)
324 8c35ff14 2020-11-19 stsp {
325 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
326 b4737997 2020-11-21 stsp int embedded_nul = 0;
327 8c35ff14 2020-11-19 stsp
328 8c35ff14 2020-11-19 stsp ARRAYLIST_INIT(d->atoms, nlines);
329 8c35ff14 2020-11-19 stsp
330 8c35ff14 2020-11-19 stsp for (i = 0; i < nlines; i++) {
331 8c35ff14 2020-11-19 stsp struct diff_atom *atom;
332 8c35ff14 2020-11-19 stsp off_t len, pos = line_offsets[i];
333 8c35ff14 2020-11-19 stsp unsigned int hash = 0;
334 8c35ff14 2020-11-19 stsp int j;
335 8c35ff14 2020-11-19 stsp
336 8c35ff14 2020-11-19 stsp ARRAYLIST_ADD(atom, d->atoms);
337 8c35ff14 2020-11-19 stsp if (atom == NULL) {
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
342 8c35ff14 2020-11-19 stsp if (i < nlines - 1)
343 8c35ff14 2020-11-19 stsp len = line_offsets[i + 1] - pos;
344 8c35ff14 2020-11-19 stsp else
345 8c35ff14 2020-11-19 stsp len = filesize - pos;
346 8c35ff14 2020-11-19 stsp
347 8c35ff14 2020-11-19 stsp if (fseeko(f, pos, SEEK_SET) == -1) {
348 8c35ff14 2020-11-19 stsp rc = errno;
349 8c35ff14 2020-11-19 stsp break;
350 8c35ff14 2020-11-19 stsp }
351 8c35ff14 2020-11-19 stsp for (j = 0; j < len; j++) {
352 8c35ff14 2020-11-19 stsp int c = fgetc(f);
353 8c35ff14 2020-11-19 stsp if (c == EOF) {
354 8c35ff14 2020-11-19 stsp if (feof(f))
355 8c35ff14 2020-11-19 stsp rc = EIO; /* unexpected EOF */
356 8c35ff14 2020-11-19 stsp else
357 8c35ff14 2020-11-19 stsp rc = errno;
358 8c35ff14 2020-11-19 stsp goto done;
359 8c35ff14 2020-11-19 stsp }
360 8c35ff14 2020-11-19 stsp
361 8c35ff14 2020-11-19 stsp hash = diff_atom_hash_update(hash, (unsigned char)c);
362 b4737997 2020-11-21 stsp
363 b4737997 2020-11-21 stsp if (c == '\0')
364 b4737997 2020-11-21 stsp embedded_nul = 1;
365 b4737997 2020-11-21 stsp
366 8c35ff14 2020-11-19 stsp }
367 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
368 8c35ff14 2020-11-19 stsp .root = d,
369 8c35ff14 2020-11-19 stsp .pos = pos,
370 8c35ff14 2020-11-19 stsp .at = NULL, /* atom data is not memory-mapped */
371 8c35ff14 2020-11-19 stsp .len = len,
372 8c35ff14 2020-11-19 stsp .hash = hash,
373 8c35ff14 2020-11-19 stsp };
374 8c35ff14 2020-11-19 stsp }
375 b4737997 2020-11-21 stsp
376 b4737997 2020-11-21 stsp /* File are considered binary if they contain embedded '\0' bytes. */
377 b4737997 2020-11-21 stsp if (embedded_nul)
378 b4737997 2020-11-21 stsp d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
379 8c35ff14 2020-11-19 stsp done:
380 8c35ff14 2020-11-19 stsp if (rc)
381 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
382 8c35ff14 2020-11-19 stsp
383 8c35ff14 2020-11-19 stsp return rc;
384 404c43c4 2018-06-21 stsp }
385 404c43c4 2018-06-21 stsp
386 8c35ff14 2020-11-19 stsp static int
387 8c35ff14 2020-11-19 stsp atomize_file_mmap(struct diff_data *d, unsigned char *p,
388 8c35ff14 2020-11-19 stsp off_t filesize, int nlines, off_t *line_offsets)
389 8c35ff14 2020-11-19 stsp {
390 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
391 b4737997 2020-11-21 stsp int embedded_nul = 0;
392 8c35ff14 2020-11-19 stsp
393 8c35ff14 2020-11-19 stsp ARRAYLIST_INIT(d->atoms, nlines);
394 8c35ff14 2020-11-19 stsp
395 8c35ff14 2020-11-19 stsp for (i = 0; i < nlines; i++) {
396 8c35ff14 2020-11-19 stsp struct diff_atom *atom;
397 8c35ff14 2020-11-19 stsp off_t len, pos = line_offsets[i];
398 8c35ff14 2020-11-19 stsp unsigned int hash = 0;
399 8c35ff14 2020-11-19 stsp int j;
400 8c35ff14 2020-11-19 stsp
401 8c35ff14 2020-11-19 stsp ARRAYLIST_ADD(atom, d->atoms);
402 8c35ff14 2020-11-19 stsp if (atom == NULL) {
403 8c35ff14 2020-11-19 stsp rc = errno;
404 8c35ff14 2020-11-19 stsp break;
405 8c35ff14 2020-11-19 stsp }
406 8c35ff14 2020-11-19 stsp
407 8c35ff14 2020-11-19 stsp if (i < nlines - 1)
408 8c35ff14 2020-11-19 stsp len = line_offsets[i + 1] - pos;
409 8c35ff14 2020-11-19 stsp else
410 8c35ff14 2020-11-19 stsp len = filesize - pos;
411 8c35ff14 2020-11-19 stsp
412 8c35ff14 2020-11-19 stsp for (j = 0; j < len; j++)
413 8c35ff14 2020-11-19 stsp hash = diff_atom_hash_update(hash, p[pos + j]);
414 b4737997 2020-11-21 stsp
415 b4737997 2020-11-21 stsp if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
416 b4737997 2020-11-21 stsp embedded_nul = 1;
417 8c35ff14 2020-11-19 stsp
418 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
419 8c35ff14 2020-11-19 stsp .root = d,
420 8c35ff14 2020-11-19 stsp .pos = pos,
421 8c35ff14 2020-11-19 stsp .at = &p[pos],
422 8c35ff14 2020-11-19 stsp .len = len,
423 8c35ff14 2020-11-19 stsp .hash = hash,
424 8c35ff14 2020-11-19 stsp };
425 8c35ff14 2020-11-19 stsp }
426 8c35ff14 2020-11-19 stsp
427 b4737997 2020-11-21 stsp /* File are considered binary if they contain embedded '\0' bytes. */
428 b4737997 2020-11-21 stsp if (embedded_nul)
429 b4737997 2020-11-21 stsp d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
430 b4737997 2020-11-21 stsp
431 8c35ff14 2020-11-19 stsp if (rc)
432 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
433 8c35ff14 2020-11-19 stsp
434 8c35ff14 2020-11-19 stsp return rc;
435 8c35ff14 2020-11-19 stsp }
436 8c35ff14 2020-11-19 stsp
437 8c35ff14 2020-11-19 stsp /* Implements diff_atomize_func_t */
438 8c35ff14 2020-11-19 stsp static int
439 8c35ff14 2020-11-19 stsp blame_atomize_file(void *arg, struct diff_data *d)
440 8c35ff14 2020-11-19 stsp {
441 8c35ff14 2020-11-19 stsp struct got_blame *blame = arg;
442 8c35ff14 2020-11-19 stsp
443 8c35ff14 2020-11-19 stsp if (d->f == blame->f1) {
444 8c35ff14 2020-11-19 stsp if (blame->map1)
445 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map1,
446 8c35ff14 2020-11-19 stsp blame->size1, blame->nlines1,
447 8c35ff14 2020-11-19 stsp blame->line_offsets1);
448 8c35ff14 2020-11-19 stsp else
449 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f1, blame->size1,
450 8c35ff14 2020-11-19 stsp blame->nlines1, blame->line_offsets1);
451 8c35ff14 2020-11-19 stsp } else if (d->f == blame->f2) {
452 8c35ff14 2020-11-19 stsp if (d->atoms.len > 0) {
453 8c35ff14 2020-11-19 stsp /* Re-use data from previous commit. */
454 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
455 8c35ff14 2020-11-19 stsp }
456 8c35ff14 2020-11-19 stsp if (blame->map2)
457 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map2,
458 8c35ff14 2020-11-19 stsp blame->size2, blame->nlines2,
459 8c35ff14 2020-11-19 stsp blame->line_offsets2);
460 8c35ff14 2020-11-19 stsp else
461 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f2, blame->size2,
462 8c35ff14 2020-11-19 stsp blame->nlines2, blame->line_offsets2);
463 8c35ff14 2020-11-19 stsp }
464 8c35ff14 2020-11-19 stsp
465 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
466 8c35ff14 2020-11-19 stsp }
467 8c35ff14 2020-11-19 stsp
468 404c43c4 2018-06-21 stsp static const struct got_error *
469 8c35ff14 2020-11-19 stsp close_file2_and_reuse_file1(struct got_blame *blame)
470 8c35ff14 2020-11-19 stsp {
471 8c35ff14 2020-11-19 stsp struct diff_data *d;
472 8c35ff14 2020-11-19 stsp
473 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
474 8c35ff14 2020-11-19 stsp blame->line_offsets2 = blame->line_offsets1;
475 8c35ff14 2020-11-19 stsp blame->line_offsets1 = NULL;
476 8c35ff14 2020-11-19 stsp
477 8c35ff14 2020-11-19 stsp free(blame->linemap2);
478 8c35ff14 2020-11-19 stsp blame->linemap2 = blame->linemap1;
479 8c35ff14 2020-11-19 stsp blame->linemap1 = NULL;
480 8c35ff14 2020-11-19 stsp
481 8c35ff14 2020-11-19 stsp if (blame->map2) {
482 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1)
483 8c35ff14 2020-11-19 stsp return got_error_from_errno("munmap");
484 8c35ff14 2020-11-19 stsp blame->map2 = blame->map1;
485 5e9266f9 2020-11-28 naddy blame->map1 = NULL;
486 8c35ff14 2020-11-19 stsp
487 8c35ff14 2020-11-19 stsp }
488 8c35ff14 2020-11-19 stsp blame->size2 = blame->size1;
489 8c35ff14 2020-11-19 stsp blame->size1 = 0;
490 8c35ff14 2020-11-19 stsp
491 8c35ff14 2020-11-19 stsp if (fclose(blame->f2) == EOF)
492 8c35ff14 2020-11-19 stsp return got_error_from_errno("fclose");
493 8c35ff14 2020-11-19 stsp blame->f2 = blame->f1;
494 8c35ff14 2020-11-19 stsp blame->f1 = NULL;
495 8c35ff14 2020-11-19 stsp
496 8c35ff14 2020-11-19 stsp blame->nlines2 = blame->nlines1;
497 8c35ff14 2020-11-19 stsp blame->nlines1 = 0;
498 8c35ff14 2020-11-19 stsp
499 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2); /* does not free pointer itself */
500 8c35ff14 2020-11-19 stsp memset(blame->data2, 0, sizeof(*blame->data2));
501 8c35ff14 2020-11-19 stsp d = blame->data2;
502 8c35ff14 2020-11-19 stsp blame->data2 = blame->data1;
503 8c35ff14 2020-11-19 stsp blame->data1 = d;
504 8c35ff14 2020-11-19 stsp
505 8c35ff14 2020-11-19 stsp return NULL;
506 8c35ff14 2020-11-19 stsp }
507 8c35ff14 2020-11-19 stsp
508 8c35ff14 2020-11-19 stsp static const struct got_error *
509 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
510 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
511 1b484788 2022-06-28 tracey got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg,
512 1b484788 2022-06-28 tracey int fd)
513 404c43c4 2018-06-21 stsp {
514 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
515 392891ce 2022-04-07 stsp struct got_commit_object *start_commit = NULL, *last_commit = NULL;
516 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
517 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
518 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
519 c27a5e66 2020-11-18 stsp struct got_object_id *id = NULL;
520 1b484788 2022-06-28 tracey int lineno;
521 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
522 404c43c4 2018-06-21 stsp
523 404c43c4 2018-06-21 stsp *blamep = NULL;
524 404c43c4 2018-06-21 stsp
525 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
526 404c43c4 2018-06-21 stsp if (err)
527 c27a5e66 2020-11-18 stsp goto done;
528 27d434c2 2018-09-15 stsp
529 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, repo, start_commit, path);
530 a44927cc 2022-04-07 stsp if (err)
531 a44927cc 2022-04-07 stsp goto done;
532 a44927cc 2022-04-07 stsp
533 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
534 27d434c2 2018-09-15 stsp if (err)
535 27d434c2 2018-09-15 stsp goto done;
536 27d434c2 2018-09-15 stsp
537 8c35ff14 2020-11-19 stsp blame = calloc(1, sizeof(*blame));
538 8c35ff14 2020-11-19 stsp if (blame == NULL) {
539 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
540 404c43c4 2018-06-21 stsp goto done;
541 404c43c4 2018-06-21 stsp }
542 404c43c4 2018-06-21 stsp
543 8c35ff14 2020-11-19 stsp blame->data1 = calloc(1, sizeof(*blame->data1));
544 8c35ff14 2020-11-19 stsp if (blame->data1 == NULL) {
545 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
546 404c43c4 2018-06-21 stsp goto done;
547 8c35ff14 2020-11-19 stsp }
548 8c35ff14 2020-11-19 stsp blame->data2 = calloc(1, sizeof(*blame->data2));
549 8c35ff14 2020-11-19 stsp if (blame->data2 == NULL) {
550 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
551 c27a5e66 2020-11-18 stsp goto done;
552 c27a5e66 2020-11-18 stsp }
553 404c43c4 2018-06-21 stsp
554 8c35ff14 2020-11-19 stsp blame->f2 = got_opentemp();
555 8c35ff14 2020-11-19 stsp if (blame->f2 == NULL) {
556 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
557 404c43c4 2018-06-21 stsp goto done;
558 404c43c4 2018-06-21 stsp }
559 cca5682e 2020-11-18 stsp err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
560 8c35ff14 2020-11-19 stsp blame_atomize_file, blame);
561 cca5682e 2020-11-18 stsp if (err)
562 cca5682e 2020-11-18 stsp goto done;
563 fe621944 2020-11-10 stsp
564 8c35ff14 2020-11-19 stsp err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
565 8c35ff14 2020-11-19 stsp &blame->nlines2, &blame->line_offsets2, blame->data2,
566 8c35ff14 2020-11-19 stsp blame->cfg, blob);
567 8c35ff14 2020-11-19 stsp blame->nlines = blame->nlines2;
568 8c35ff14 2020-11-19 stsp if (err || blame->nlines == 0)
569 8c35ff14 2020-11-19 stsp goto done;
570 8c35ff14 2020-11-19 stsp
571 8c35ff14 2020-11-19 stsp got_object_blob_close(blob);
572 8c35ff14 2020-11-19 stsp blob = NULL;
573 8c35ff14 2020-11-19 stsp
574 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
575 8c35ff14 2020-11-19 stsp if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
576 b02560ec 2019-08-19 stsp blame->nlines--;
577 404c43c4 2018-06-21 stsp
578 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
579 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
580 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
581 404c43c4 2018-06-21 stsp goto done;
582 404c43c4 2018-06-21 stsp }
583 404c43c4 2018-06-21 stsp
584 c27a5e66 2020-11-18 stsp blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
585 c27a5e66 2020-11-18 stsp if (blame->linemap2 == NULL) {
586 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
587 c27a5e66 2020-11-18 stsp goto done;
588 c27a5e66 2020-11-18 stsp }
589 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines2; lineno++)
590 c27a5e66 2020-11-18 stsp blame->linemap2[lineno] = lineno;
591 c27a5e66 2020-11-18 stsp
592 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
593 404c43c4 2018-06-21 stsp if (err)
594 c27a5e66 2020-11-18 stsp goto done;
595 c27a5e66 2020-11-18 stsp
596 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
597 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
598 293f6400 2018-09-20 stsp if (err)
599 404c43c4 2018-06-21 stsp goto done;
600 656b1f76 2019-05-11 jcs for (;;) {
601 c27a5e66 2020-11-18 stsp struct got_object_id *next_id;
602 c27a5e66 2020-11-18 stsp err = got_commit_graph_iter_next(&next_id, graph, repo,
603 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
604 404c43c4 2018-06-21 stsp if (err) {
605 c27a5e66 2020-11-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
606 4c9641fd 2019-08-21 stsp err = NULL;
607 c27a5e66 2020-11-18 stsp break;
608 c27a5e66 2020-11-18 stsp }
609 c27a5e66 2020-11-18 stsp goto done;
610 293f6400 2018-09-20 stsp }
611 c27a5e66 2020-11-18 stsp if (next_id) {
612 c27a5e66 2020-11-18 stsp id = next_id;
613 c27a5e66 2020-11-18 stsp err = blame_commit(blame, id, path, repo, cb, arg);
614 293f6400 2018-09-20 stsp if (err) {
615 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
616 293f6400 2018-09-20 stsp err = NULL;
617 c27a5e66 2020-11-18 stsp goto done;
618 293f6400 2018-09-20 stsp }
619 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
620 06ca4d09 2018-11-19 stsp break;
621 8c35ff14 2020-11-19 stsp
622 8c35ff14 2020-11-19 stsp err = close_file2_and_reuse_file1(blame);
623 8c35ff14 2020-11-19 stsp if (err)
624 8c35ff14 2020-11-19 stsp goto done;
625 404c43c4 2018-06-21 stsp }
626 293f6400 2018-09-20 stsp }
627 ed77f2ae 2018-06-21 stsp
628 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
629 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
630 392891ce 2022-04-07 stsp err = got_object_open_as_commit(&last_commit, repo, id);
631 392891ce 2022-04-07 stsp if (err)
632 392891ce 2022-04-07 stsp goto done;
633 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines; lineno++) {
634 392891ce 2022-04-07 stsp err = annotate_line(blame, lineno, last_commit, id,
635 392891ce 2022-04-07 stsp cb, arg);
636 293f6400 2018-09-20 stsp if (err)
637 293f6400 2018-09-20 stsp goto done;
638 404c43c4 2018-06-21 stsp }
639 404c43c4 2018-06-21 stsp }
640 404c43c4 2018-06-21 stsp
641 404c43c4 2018-06-21 stsp done:
642 293f6400 2018-09-20 stsp if (graph)
643 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
644 27d434c2 2018-09-15 stsp free(obj_id);
645 404c43c4 2018-06-21 stsp if (blob)
646 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
647 a44927cc 2022-04-07 stsp if (start_commit)
648 a44927cc 2022-04-07 stsp got_object_commit_close(start_commit);
649 392891ce 2022-04-07 stsp if (last_commit)
650 392891ce 2022-04-07 stsp got_object_commit_close(last_commit);
651 1828273a 2018-07-09 stsp if (err) {
652 1828273a 2018-07-09 stsp if (blame)
653 1828273a 2018-07-09 stsp blame_close(blame);
654 1828273a 2018-07-09 stsp } else
655 404c43c4 2018-06-21 stsp *blamep = blame;
656 404c43c4 2018-06-21 stsp
657 404c43c4 2018-06-21 stsp return err;
658 404c43c4 2018-06-21 stsp }
659 84451b3e 2018-07-10 stsp
660 84451b3e 2018-07-10 stsp const struct got_error *
661 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
662 392891ce 2022-04-07 stsp struct got_repository *repo, got_blame_cb cb, void *arg,
663 1b484788 2022-06-28 tracey got_cancel_cb cancel_cb, void* cancel_arg, int fd)
664 84451b3e 2018-07-10 stsp {
665 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
666 84451b3e 2018-07-10 stsp struct got_blame *blame;
667 84451b3e 2018-07-10 stsp char *abspath;
668 84451b3e 2018-07-10 stsp
669 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
671 84451b3e 2018-07-10 stsp
672 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
673 1b484788 2022-06-28 tracey cancel_cb, cancel_arg, fd);
674 84451b3e 2018-07-10 stsp free(abspath);
675 26206841 2018-07-12 stsp if (blame)
676 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
677 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
678 84451b3e 2018-07-10 stsp }