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