Blame


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