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 404c43c4 2018-06-21 stsp #include <string.h>
25 404c43c4 2018-06-21 stsp #include <stdio.h>
26 404c43c4 2018-06-21 stsp #include <stdlib.h>
27 404c43c4 2018-06-21 stsp #include <time.h>
28 56e0773d 2019-11-28 stsp #include <limits.h>
29 404c43c4 2018-06-21 stsp #include <util.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 404c43c4 2018-06-21 stsp #include "got_blame.h"
36 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
37 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
38 404c43c4 2018-06-21 stsp
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
41 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
42 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
43 404c43c4 2018-06-21 stsp
44 fe621944 2020-11-10 stsp #ifndef MAX
45 fe621944 2020-11-10 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 fe621944 2020-11-10 stsp #endif
47 fe621944 2020-11-10 stsp
48 404c43c4 2018-06-21 stsp struct got_blame_line {
49 404c43c4 2018-06-21 stsp int annotated;
50 9b94757a 2018-06-21 stsp struct got_object_id id;
51 404c43c4 2018-06-21 stsp };
52 404c43c4 2018-06-21 stsp
53 404c43c4 2018-06-21 stsp struct got_blame {
54 cca5682e 2020-11-18 stsp struct diff_config *cfg;
55 8c35ff14 2020-11-19 stsp int nlines; /* number of lines in file being blamed */
56 8c35ff14 2020-11-19 stsp int nannotated; /* number of lines already annotated */
57 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
58 c35a7943 2018-07-12 stsp int ncommits;
59 c27a5e66 2020-11-18 stsp
60 c27a5e66 2020-11-18 stsp /*
61 8c35ff14 2020-11-19 stsp * These change with every traversed commit. After diffing
62 8c35ff14 2020-11-19 stsp * commits N:N-1, in preparation for diffing commits N-1:N-2,
63 8c35ff14 2020-11-19 stsp * data for commit N is retained and flipped into data for N-1.
64 8c35ff14 2020-11-19 stsp *
65 8c35ff14 2020-11-19 stsp */
66 8c35ff14 2020-11-19 stsp FILE *f1; /* older version from commit N-1. */
67 8c35ff14 2020-11-19 stsp FILE *f2; /* newer version from commit N. */
68 8c35ff14 2020-11-19 stsp unsigned char *map1;
69 8c35ff14 2020-11-19 stsp unsigned char *map2;
70 8c35ff14 2020-11-19 stsp off_t size1;
71 8c35ff14 2020-11-19 stsp off_t size2;
72 8c35ff14 2020-11-19 stsp int nlines1;
73 8c35ff14 2020-11-19 stsp int nlines2;
74 8c35ff14 2020-11-19 stsp off_t *line_offsets1;
75 8c35ff14 2020-11-19 stsp off_t *line_offsets2;
76 8c35ff14 2020-11-19 stsp
77 8c35ff14 2020-11-19 stsp /*
78 c27a5e66 2020-11-18 stsp * Map line numbers of an older version of the file to valid line
79 8c35ff14 2020-11-19 stsp * numbers in the version of the file being blamed. This map is
80 8c35ff14 2020-11-19 stsp * updated with each commit we traverse throughout the file's history.
81 8c35ff14 2020-11-19 stsp * Lines mapped to -1 do not correspond to any line in the version
82 8c35ff14 2020-11-19 stsp * being blamed.
83 c27a5e66 2020-11-18 stsp */
84 8c35ff14 2020-11-19 stsp int *linemap1;
85 c27a5e66 2020-11-18 stsp int *linemap2;
86 8c35ff14 2020-11-19 stsp
87 8c35ff14 2020-11-19 stsp struct diff_data *data1;
88 8c35ff14 2020-11-19 stsp struct diff_data *data2;
89 404c43c4 2018-06-21 stsp };
90 404c43c4 2018-06-21 stsp
91 404c43c4 2018-06-21 stsp static const struct got_error *
92 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
93 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
94 84451b3e 2018-07-10 stsp void *arg)
95 404c43c4 2018-06-21 stsp {
96 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
97 404c43c4 2018-06-21 stsp struct got_blame_line *line;
98 404c43c4 2018-06-21 stsp
99 c27a5e66 2020-11-18 stsp if (lineno < 0 || lineno >= blame->nlines)
100 f2e233d8 2018-11-19 stsp return NULL;
101 3168e5da 2020-09-10 stsp
102 c27a5e66 2020-11-18 stsp line = &blame->lines[lineno];
103 404c43c4 2018-06-21 stsp if (line->annotated)
104 84451b3e 2018-07-10 stsp return NULL;
105 404c43c4 2018-06-21 stsp
106 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
107 404c43c4 2018-06-21 stsp line->annotated = 1;
108 06ca4d09 2018-11-19 stsp blame->nannotated++;
109 84451b3e 2018-07-10 stsp if (cb)
110 c27a5e66 2020-11-18 stsp err = cb(arg, blame->nlines, lineno + 1, id);
111 84451b3e 2018-07-10 stsp return err;
112 404c43c4 2018-06-21 stsp }
113 404c43c4 2018-06-21 stsp
114 404c43c4 2018-06-21 stsp static const struct got_error *
115 8c35ff14 2020-11-19 stsp blame_changes(struct got_blame *blame, struct diff_result *diff_result,
116 8c35ff14 2020-11-19 stsp struct got_object_id *commit_id,
117 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
118 c35a7943 2018-07-12 stsp void *arg)
119 c35a7943 2018-07-12 stsp {
120 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
121 fe621944 2020-11-10 stsp int i;
122 c27a5e66 2020-11-18 stsp int idx1 = 0, idx2 = 0;
123 c35a7943 2018-07-12 stsp
124 c27a5e66 2020-11-18 stsp for (i = 0; i < diff_result->chunks.len &&
125 c27a5e66 2020-11-18 stsp blame->nannotated < blame->nlines; i++) {
126 fe621944 2020-11-10 stsp struct diff_chunk *c = diff_chunk_get(diff_result, i);
127 c27a5e66 2020-11-18 stsp unsigned int left_start, left_count;
128 fe621944 2020-11-10 stsp unsigned int right_start, 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_start = diff_chunk_get_left_start(c, diff_result, 0);
138 c27a5e66 2020-11-18 stsp left_count = diff_chunk_get_left_count(c);
139 c27a5e66 2020-11-18 stsp right_start = diff_chunk_get_right_start(c, diff_result, 0);
140 c27a5e66 2020-11-18 stsp right_count = diff_chunk_get_right_count(c);
141 c27a5e66 2020-11-18 stsp
142 c27a5e66 2020-11-18 stsp if (left_count == right_count) {
143 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
144 8c35ff14 2020-11-19 stsp blame->linemap1[idx1++] =
145 8c35ff14 2020-11-19 stsp blame->linemap2[idx2++];
146 c27a5e66 2020-11-18 stsp }
147 fe621944 2020-11-10 stsp continue;
148 c27a5e66 2020-11-18 stsp }
149 fe621944 2020-11-10 stsp
150 c27a5e66 2020-11-18 stsp if (right_count == 0) {
151 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
152 8c35ff14 2020-11-19 stsp blame->linemap1[idx1++] = -1;
153 c27a5e66 2020-11-18 stsp }
154 fe621944 2020-11-10 stsp continue;
155 c27a5e66 2020-11-18 stsp }
156 fe621944 2020-11-10 stsp
157 c27a5e66 2020-11-18 stsp for (j = 0; j < right_count; j++) {
158 c27a5e66 2020-11-18 stsp int ln = blame->linemap2[idx2++];
159 4c9641fd 2019-08-21 stsp err = annotate_line(blame, ln, commit_id, 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 8c35ff14 2020-11-19 stsp int 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 8c35ff14 2020-11-19 stsp rc = diff_atomize_file(diff_data, cfg, f, *p, *size, 0);
190 8c35ff14 2020-11-19 stsp if (rc)
191 8c35ff14 2020-11-19 stsp return got_error_set_errno(rc, "diff_atomize_file");
192 8c35ff14 2020-11-19 stsp
193 8c35ff14 2020-11-19 stsp return NULL;
194 8c35ff14 2020-11-19 stsp }
195 8c35ff14 2020-11-19 stsp
196 8c35ff14 2020-11-19 stsp static const struct got_error *
197 c27a5e66 2020-11-18 stsp blame_commit(struct got_blame *blame, struct got_object_id *id,
198 c27a5e66 2020-11-18 stsp const char *path, struct got_repository *repo,
199 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
200 84451b3e 2018-07-10 stsp void *arg)
201 404c43c4 2018-06-21 stsp {
202 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
203 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
204 c27a5e66 2020-11-18 stsp struct got_object_qid *pid = NULL;
205 8c35ff14 2020-11-19 stsp struct got_object_id *pblob_id = NULL;
206 8c35ff14 2020-11-19 stsp struct got_blob_object *pblob = NULL;
207 8c35ff14 2020-11-19 stsp struct diff_result *diff_result = NULL;
208 404c43c4 2018-06-21 stsp
209 c27a5e66 2020-11-18 stsp err = got_object_open_as_commit(&commit, repo, id);
210 8d725ae1 2019-08-18 stsp if (err)
211 8d725ae1 2019-08-18 stsp return err;
212 8d725ae1 2019-08-18 stsp
213 c27a5e66 2020-11-18 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
214 c27a5e66 2020-11-18 stsp if (pid == NULL) {
215 c27a5e66 2020-11-18 stsp got_object_commit_close(commit);
216 c27a5e66 2020-11-18 stsp return NULL;
217 c27a5e66 2020-11-18 stsp }
218 c27a5e66 2020-11-18 stsp
219 8c35ff14 2020-11-19 stsp err = got_object_id_by_path(&pblob_id, repo, pid->id, path);
220 4c9641fd 2019-08-21 stsp if (err) {
221 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
222 4c9641fd 2019-08-21 stsp err = NULL;
223 404c43c4 2018-06-21 stsp goto done;
224 4c9641fd 2019-08-21 stsp }
225 27d434c2 2018-09-15 stsp
226 8c35ff14 2020-11-19 stsp err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
227 27d434c2 2018-09-15 stsp if (err)
228 27d434c2 2018-09-15 stsp goto done;
229 27d434c2 2018-09-15 stsp
230 8c35ff14 2020-11-19 stsp blame->f1 = got_opentemp();
231 8c35ff14 2020-11-19 stsp if (blame->f1 == NULL) {
232 c27a5e66 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
233 404c43c4 2018-06-21 stsp goto done;
234 404c43c4 2018-06-21 stsp }
235 8c35ff14 2020-11-19 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 8c35ff14 2020-11-19 stsp err = blame_changes(blame, diff_result, 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 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, 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 c27a5e66 2020-11-18 stsp free(pblob_id);
271 c27a5e66 2020-11-18 stsp if (pblob)
272 c27a5e66 2020-11-18 stsp got_object_blob_close(pblob);
273 404c43c4 2018-06-21 stsp return err;
274 404c43c4 2018-06-21 stsp }
275 404c43c4 2018-06-21 stsp
276 fb43ecf1 2019-02-11 stsp static const struct got_error *
277 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
278 404c43c4 2018-06-21 stsp {
279 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
280 c35a7943 2018-07-12 stsp
281 8c35ff14 2020-11-19 stsp diff_data_free(blame->data1);
282 8c35ff14 2020-11-19 stsp free(blame->data1);
283 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2);
284 8c35ff14 2020-11-19 stsp free(blame->data2);
285 8c35ff14 2020-11-19 stsp if (blame->map1) {
286 8c35ff14 2020-11-19 stsp if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
287 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
288 8c35ff14 2020-11-19 stsp }
289 8c35ff14 2020-11-19 stsp if (blame->map2) {
290 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
291 8c35ff14 2020-11-19 stsp err = got_error_from_errno("munmap");
292 8c35ff14 2020-11-19 stsp }
293 8c35ff14 2020-11-19 stsp if (blame->f1 && fclose(blame->f1) != 0 && err == NULL)
294 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
295 8c35ff14 2020-11-19 stsp if (blame->f2 && fclose(blame->f2) != 0 && err == NULL)
296 8c35ff14 2020-11-19 stsp err = got_error_from_errno("fclose");
297 404c43c4 2018-06-21 stsp free(blame->lines);
298 8c35ff14 2020-11-19 stsp free(blame->line_offsets1);
299 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
300 8c35ff14 2020-11-19 stsp free(blame->linemap1);
301 c27a5e66 2020-11-18 stsp free(blame->linemap2);
302 cca5682e 2020-11-18 stsp free(blame->cfg);
303 404c43c4 2018-06-21 stsp free(blame);
304 fb43ecf1 2019-02-11 stsp return err;
305 8c35ff14 2020-11-19 stsp }
306 8c35ff14 2020-11-19 stsp
307 8c35ff14 2020-11-19 stsp static int
308 8c35ff14 2020-11-19 stsp atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
309 8c35ff14 2020-11-19 stsp off_t *line_offsets)
310 8c35ff14 2020-11-19 stsp {
311 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
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 8c35ff14 2020-11-19 stsp }
348 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
349 8c35ff14 2020-11-19 stsp .root = d,
350 8c35ff14 2020-11-19 stsp .pos = pos,
351 8c35ff14 2020-11-19 stsp .at = NULL, /* atom data is not memory-mapped */
352 8c35ff14 2020-11-19 stsp .len = len,
353 8c35ff14 2020-11-19 stsp .hash = hash,
354 8c35ff14 2020-11-19 stsp };
355 8c35ff14 2020-11-19 stsp }
356 8c35ff14 2020-11-19 stsp done:
357 8c35ff14 2020-11-19 stsp if (rc)
358 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
359 8c35ff14 2020-11-19 stsp
360 8c35ff14 2020-11-19 stsp return rc;
361 404c43c4 2018-06-21 stsp }
362 404c43c4 2018-06-21 stsp
363 8c35ff14 2020-11-19 stsp static int
364 8c35ff14 2020-11-19 stsp atomize_file_mmap(struct diff_data *d, unsigned char *p,
365 8c35ff14 2020-11-19 stsp off_t filesize, int nlines, off_t *line_offsets)
366 8c35ff14 2020-11-19 stsp {
367 8c35ff14 2020-11-19 stsp int i, rc = DIFF_RC_OK;
368 8c35ff14 2020-11-19 stsp
369 8c35ff14 2020-11-19 stsp ARRAYLIST_INIT(d->atoms, nlines);
370 8c35ff14 2020-11-19 stsp
371 8c35ff14 2020-11-19 stsp for (i = 0; i < nlines; i++) {
372 8c35ff14 2020-11-19 stsp struct diff_atom *atom;
373 8c35ff14 2020-11-19 stsp off_t len, pos = line_offsets[i];
374 8c35ff14 2020-11-19 stsp unsigned int hash = 0;
375 8c35ff14 2020-11-19 stsp int j;
376 8c35ff14 2020-11-19 stsp
377 8c35ff14 2020-11-19 stsp ARRAYLIST_ADD(atom, d->atoms);
378 8c35ff14 2020-11-19 stsp if (atom == NULL) {
379 8c35ff14 2020-11-19 stsp rc = errno;
380 8c35ff14 2020-11-19 stsp break;
381 8c35ff14 2020-11-19 stsp }
382 8c35ff14 2020-11-19 stsp
383 8c35ff14 2020-11-19 stsp if (i < nlines - 1)
384 8c35ff14 2020-11-19 stsp len = line_offsets[i + 1] - pos;
385 8c35ff14 2020-11-19 stsp else
386 8c35ff14 2020-11-19 stsp len = filesize - pos;
387 8c35ff14 2020-11-19 stsp
388 8c35ff14 2020-11-19 stsp for (j = 0; j < len; j++)
389 8c35ff14 2020-11-19 stsp hash = diff_atom_hash_update(hash, p[pos + j]);
390 8c35ff14 2020-11-19 stsp
391 8c35ff14 2020-11-19 stsp *atom = (struct diff_atom){
392 8c35ff14 2020-11-19 stsp .root = d,
393 8c35ff14 2020-11-19 stsp .pos = pos,
394 8c35ff14 2020-11-19 stsp .at = &p[pos],
395 8c35ff14 2020-11-19 stsp .len = len,
396 8c35ff14 2020-11-19 stsp .hash = hash,
397 8c35ff14 2020-11-19 stsp };
398 8c35ff14 2020-11-19 stsp }
399 8c35ff14 2020-11-19 stsp
400 8c35ff14 2020-11-19 stsp if (rc)
401 8c35ff14 2020-11-19 stsp ARRAYLIST_FREE(d->atoms);
402 8c35ff14 2020-11-19 stsp
403 8c35ff14 2020-11-19 stsp return rc;
404 8c35ff14 2020-11-19 stsp }
405 8c35ff14 2020-11-19 stsp
406 8c35ff14 2020-11-19 stsp /* Implements diff_atomize_func_t */
407 8c35ff14 2020-11-19 stsp static int
408 8c35ff14 2020-11-19 stsp blame_atomize_file(void *arg, struct diff_data *d)
409 8c35ff14 2020-11-19 stsp {
410 8c35ff14 2020-11-19 stsp struct got_blame *blame = arg;
411 8c35ff14 2020-11-19 stsp
412 8c35ff14 2020-11-19 stsp if (d->f == blame->f1) {
413 8c35ff14 2020-11-19 stsp if (blame->map1)
414 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map1,
415 8c35ff14 2020-11-19 stsp blame->size1, blame->nlines1,
416 8c35ff14 2020-11-19 stsp blame->line_offsets1);
417 8c35ff14 2020-11-19 stsp else
418 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f1, blame->size1,
419 8c35ff14 2020-11-19 stsp blame->nlines1, blame->line_offsets1);
420 8c35ff14 2020-11-19 stsp } else if (d->f == blame->f2) {
421 8c35ff14 2020-11-19 stsp if (d->atoms.len > 0) {
422 8c35ff14 2020-11-19 stsp /* Re-use data from previous commit. */
423 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
424 8c35ff14 2020-11-19 stsp }
425 8c35ff14 2020-11-19 stsp if (blame->map2)
426 8c35ff14 2020-11-19 stsp return atomize_file_mmap(d, blame->map2,
427 8c35ff14 2020-11-19 stsp blame->size2, blame->nlines2,
428 8c35ff14 2020-11-19 stsp blame->line_offsets2);
429 8c35ff14 2020-11-19 stsp else
430 8c35ff14 2020-11-19 stsp return atomize_file(d, blame->f2, blame->size2,
431 8c35ff14 2020-11-19 stsp blame->nlines2, blame->line_offsets2);
432 8c35ff14 2020-11-19 stsp }
433 8c35ff14 2020-11-19 stsp
434 8c35ff14 2020-11-19 stsp return DIFF_RC_OK;
435 8c35ff14 2020-11-19 stsp }
436 8c35ff14 2020-11-19 stsp
437 404c43c4 2018-06-21 stsp static const struct got_error *
438 8c35ff14 2020-11-19 stsp close_file2_and_reuse_file1(struct got_blame *blame)
439 8c35ff14 2020-11-19 stsp {
440 8c35ff14 2020-11-19 stsp struct diff_data *d;
441 8c35ff14 2020-11-19 stsp
442 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
443 8c35ff14 2020-11-19 stsp blame->line_offsets2 = blame->line_offsets1;
444 8c35ff14 2020-11-19 stsp blame->line_offsets1 = NULL;
445 8c35ff14 2020-11-19 stsp
446 8c35ff14 2020-11-19 stsp free(blame->linemap2);
447 8c35ff14 2020-11-19 stsp blame->linemap2 = blame->linemap1;
448 8c35ff14 2020-11-19 stsp blame->linemap1 = NULL;
449 8c35ff14 2020-11-19 stsp
450 8c35ff14 2020-11-19 stsp if (blame->map2) {
451 8c35ff14 2020-11-19 stsp if (munmap(blame->map2, blame->size2) == -1)
452 8c35ff14 2020-11-19 stsp return got_error_from_errno("munmap");
453 8c35ff14 2020-11-19 stsp blame->map2 = blame->map1;
454 8c35ff14 2020-11-19 stsp blame->map2 = NULL;
455 8c35ff14 2020-11-19 stsp
456 8c35ff14 2020-11-19 stsp }
457 8c35ff14 2020-11-19 stsp blame->size2 = blame->size1;
458 8c35ff14 2020-11-19 stsp blame->size1 = 0;
459 8c35ff14 2020-11-19 stsp
460 8c35ff14 2020-11-19 stsp if (fclose(blame->f2) == EOF)
461 8c35ff14 2020-11-19 stsp return got_error_from_errno("fclose");
462 8c35ff14 2020-11-19 stsp blame->f2 = blame->f1;
463 8c35ff14 2020-11-19 stsp blame->f1 = NULL;
464 8c35ff14 2020-11-19 stsp
465 8c35ff14 2020-11-19 stsp blame->nlines2 = blame->nlines1;
466 8c35ff14 2020-11-19 stsp blame->nlines1 = 0;
467 8c35ff14 2020-11-19 stsp
468 8c35ff14 2020-11-19 stsp free(blame->line_offsets2);
469 8c35ff14 2020-11-19 stsp blame->line_offsets2 = blame->line_offsets1;
470 8c35ff14 2020-11-19 stsp blame->line_offsets2 = NULL;
471 8c35ff14 2020-11-19 stsp
472 8c35ff14 2020-11-19 stsp diff_data_free(blame->data2); /* does not free pointer itself */
473 8c35ff14 2020-11-19 stsp memset(blame->data2, 0, sizeof(*blame->data2));
474 8c35ff14 2020-11-19 stsp d = blame->data2;
475 8c35ff14 2020-11-19 stsp blame->data2 = blame->data1;
476 8c35ff14 2020-11-19 stsp blame->data1 = d;
477 8c35ff14 2020-11-19 stsp
478 8c35ff14 2020-11-19 stsp return NULL;
479 8c35ff14 2020-11-19 stsp }
480 8c35ff14 2020-11-19 stsp
481 8c35ff14 2020-11-19 stsp static const struct got_error *
482 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
483 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
484 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
485 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
486 404c43c4 2018-06-21 stsp {
487 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
488 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
489 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
490 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
491 c27a5e66 2020-11-18 stsp struct got_object_id *id = NULL;
492 c27a5e66 2020-11-18 stsp int lineno;
493 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
494 404c43c4 2018-06-21 stsp
495 404c43c4 2018-06-21 stsp *blamep = NULL;
496 404c43c4 2018-06-21 stsp
497 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
498 404c43c4 2018-06-21 stsp if (err)
499 c27a5e66 2020-11-18 stsp goto done;
500 27d434c2 2018-09-15 stsp
501 8c35ff14 2020-11-19 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
502 27d434c2 2018-09-15 stsp if (err)
503 27d434c2 2018-09-15 stsp goto done;
504 27d434c2 2018-09-15 stsp
505 8c35ff14 2020-11-19 stsp blame = calloc(1, sizeof(*blame));
506 8c35ff14 2020-11-19 stsp if (blame == NULL) {
507 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
508 404c43c4 2018-06-21 stsp goto done;
509 404c43c4 2018-06-21 stsp }
510 404c43c4 2018-06-21 stsp
511 8c35ff14 2020-11-19 stsp blame->data1 = calloc(1, sizeof(*blame->data1));
512 8c35ff14 2020-11-19 stsp if (blame->data1 == NULL) {
513 8c35ff14 2020-11-19 stsp err = got_error_from_errno("calloc");
514 404c43c4 2018-06-21 stsp goto done;
515 8c35ff14 2020-11-19 stsp }
516 8c35ff14 2020-11-19 stsp blame->data2 = calloc(1, sizeof(*blame->data2));
517 8c35ff14 2020-11-19 stsp if (blame->data2 == NULL) {
518 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
519 c27a5e66 2020-11-18 stsp goto done;
520 c27a5e66 2020-11-18 stsp }
521 404c43c4 2018-06-21 stsp
522 8c35ff14 2020-11-19 stsp blame->f2 = got_opentemp();
523 8c35ff14 2020-11-19 stsp if (blame->f2 == NULL) {
524 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
525 404c43c4 2018-06-21 stsp goto done;
526 404c43c4 2018-06-21 stsp }
527 cca5682e 2020-11-18 stsp err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
528 8c35ff14 2020-11-19 stsp blame_atomize_file, blame);
529 cca5682e 2020-11-18 stsp if (err)
530 cca5682e 2020-11-18 stsp goto done;
531 fe621944 2020-11-10 stsp
532 8c35ff14 2020-11-19 stsp err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
533 8c35ff14 2020-11-19 stsp &blame->nlines2, &blame->line_offsets2, blame->data2,
534 8c35ff14 2020-11-19 stsp blame->cfg, blob);
535 8c35ff14 2020-11-19 stsp blame->nlines = blame->nlines2;
536 8c35ff14 2020-11-19 stsp if (err || blame->nlines == 0)
537 8c35ff14 2020-11-19 stsp goto done;
538 8c35ff14 2020-11-19 stsp
539 8c35ff14 2020-11-19 stsp got_object_blob_close(blob);
540 8c35ff14 2020-11-19 stsp blob = NULL;
541 8c35ff14 2020-11-19 stsp
542 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
543 8c35ff14 2020-11-19 stsp if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
544 b02560ec 2019-08-19 stsp blame->nlines--;
545 404c43c4 2018-06-21 stsp
546 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
547 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
548 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
549 404c43c4 2018-06-21 stsp goto done;
550 404c43c4 2018-06-21 stsp }
551 404c43c4 2018-06-21 stsp
552 c27a5e66 2020-11-18 stsp blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
553 c27a5e66 2020-11-18 stsp if (blame->linemap2 == NULL) {
554 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
555 c27a5e66 2020-11-18 stsp goto done;
556 c27a5e66 2020-11-18 stsp }
557 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines2; lineno++)
558 c27a5e66 2020-11-18 stsp blame->linemap2[lineno] = lineno;
559 c27a5e66 2020-11-18 stsp
560 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
561 404c43c4 2018-06-21 stsp if (err)
562 c27a5e66 2020-11-18 stsp goto done;
563 c27a5e66 2020-11-18 stsp
564 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
565 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
566 293f6400 2018-09-20 stsp if (err)
567 404c43c4 2018-06-21 stsp goto done;
568 656b1f76 2019-05-11 jcs for (;;) {
569 c27a5e66 2020-11-18 stsp struct got_object_id *next_id;
570 c27a5e66 2020-11-18 stsp err = got_commit_graph_iter_next(&next_id, graph, repo,
571 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
572 404c43c4 2018-06-21 stsp if (err) {
573 c27a5e66 2020-11-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
574 4c9641fd 2019-08-21 stsp err = NULL;
575 c27a5e66 2020-11-18 stsp break;
576 c27a5e66 2020-11-18 stsp }
577 c27a5e66 2020-11-18 stsp goto done;
578 293f6400 2018-09-20 stsp }
579 c27a5e66 2020-11-18 stsp if (next_id) {
580 c27a5e66 2020-11-18 stsp id = next_id;
581 c27a5e66 2020-11-18 stsp err = blame_commit(blame, id, path, repo, cb, arg);
582 293f6400 2018-09-20 stsp if (err) {
583 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
584 293f6400 2018-09-20 stsp err = NULL;
585 c27a5e66 2020-11-18 stsp goto done;
586 293f6400 2018-09-20 stsp }
587 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
588 06ca4d09 2018-11-19 stsp break;
589 8c35ff14 2020-11-19 stsp
590 8c35ff14 2020-11-19 stsp err = close_file2_and_reuse_file1(blame);
591 8c35ff14 2020-11-19 stsp if (err)
592 8c35ff14 2020-11-19 stsp goto done;
593 404c43c4 2018-06-21 stsp }
594 293f6400 2018-09-20 stsp }
595 ed77f2ae 2018-06-21 stsp
596 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
597 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
598 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines; lineno++) {
599 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
600 293f6400 2018-09-20 stsp if (err)
601 293f6400 2018-09-20 stsp goto done;
602 404c43c4 2018-06-21 stsp }
603 404c43c4 2018-06-21 stsp }
604 404c43c4 2018-06-21 stsp
605 404c43c4 2018-06-21 stsp done:
606 293f6400 2018-09-20 stsp if (graph)
607 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
608 27d434c2 2018-09-15 stsp free(obj_id);
609 404c43c4 2018-06-21 stsp if (blob)
610 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
611 1828273a 2018-07-09 stsp if (err) {
612 1828273a 2018-07-09 stsp if (blame)
613 1828273a 2018-07-09 stsp blame_close(blame);
614 1828273a 2018-07-09 stsp } else
615 404c43c4 2018-06-21 stsp *blamep = blame;
616 404c43c4 2018-06-21 stsp
617 404c43c4 2018-06-21 stsp return err;
618 404c43c4 2018-06-21 stsp }
619 84451b3e 2018-07-10 stsp
620 84451b3e 2018-07-10 stsp const struct got_error *
621 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
622 84451b3e 2018-07-10 stsp struct got_repository *repo,
623 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
624 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
625 84451b3e 2018-07-10 stsp {
626 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
627 84451b3e 2018-07-10 stsp struct got_blame *blame;
628 84451b3e 2018-07-10 stsp char *abspath;
629 84451b3e 2018-07-10 stsp
630 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
631 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
632 84451b3e 2018-07-10 stsp
633 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
634 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
635 84451b3e 2018-07-10 stsp free(abspath);
636 26206841 2018-07-12 stsp if (blame)
637 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
638 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
639 84451b3e 2018-07-10 stsp }