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