Blame


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