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