Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <sha1.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <limits.h>
29 #include <util.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_cancel.h"
35 #include "got_blame.h"
36 #include "got_commit_graph.h"
37 #include "got_opentemp.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_diff.h"
44 #ifndef MAX
45 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 #endif
48 struct got_blame_line {
49 int annotated;
50 struct got_object_id id;
51 };
53 struct got_blame {
54 struct diff_config *cfg;
55 int nlines; /* number of lines in file being blamed */
56 int nannotated; /* number of lines already annotated */
57 struct got_blame_line *lines; /* one per line */
58 int ncommits;
60 /*
61 * These change with every traversed commit. After diffing
62 * commits N:N-1, in preparation for diffing commits N-1:N-2,
63 * data for commit N is retained and flipped into data for N-1.
64 *
65 */
66 FILE *f1; /* older version from commit N-1. */
67 FILE *f2; /* newer version from commit N. */
68 unsigned char *map1;
69 unsigned char *map2;
70 off_t size1;
71 off_t size2;
72 int nlines1;
73 int nlines2;
74 off_t *line_offsets1;
75 off_t *line_offsets2;
77 /*
78 * Map line numbers of an older version of the file to valid line
79 * numbers in the version of the file being blamed. This map is
80 * updated with each commit we traverse throughout the file's history.
81 * Lines mapped to -1 do not correspond to any line in the version
82 * being blamed.
83 */
84 int *linemap1;
85 int *linemap2;
87 struct diff_data *data1;
88 struct diff_data *data2;
89 };
91 static const struct got_error *
92 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
93 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
94 void *arg)
95 {
96 const struct got_error *err = NULL;
97 struct got_blame_line *line;
99 if (lineno < 0 || lineno >= blame->nlines)
100 return NULL;
102 line = &blame->lines[lineno];
103 if (line->annotated)
104 return NULL;
106 memcpy(&line->id, id, sizeof(line->id));
107 line->annotated = 1;
108 blame->nannotated++;
109 if (cb)
110 err = cb(arg, blame->nlines, lineno + 1, id);
111 return err;
114 static const struct got_error *
115 blame_changes(struct got_blame *blame, struct diff_result *diff_result,
116 struct got_object_id *commit_id,
117 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
118 void *arg)
120 const struct got_error *err = NULL;
121 int i;
122 int idx1 = 0, idx2 = 0;
124 for (i = 0; i < diff_result->chunks.len &&
125 blame->nannotated < blame->nlines; i++) {
126 struct diff_chunk *c = diff_chunk_get(diff_result, i);
127 unsigned int left_start, left_count;
128 unsigned int right_start, right_count;
129 int j;
131 /*
132 * We do not need to worry about idx1/idx2 growing out
133 * of bounds because the diff implementation ensures
134 * that chunk ranges never exceed the number of lines
135 * in the left/right input files.
136 */
137 left_start = diff_chunk_get_left_start(c, diff_result, 0);
138 left_count = diff_chunk_get_left_count(c);
139 right_start = diff_chunk_get_right_start(c, diff_result, 0);
140 right_count = diff_chunk_get_right_count(c);
142 if (left_count == right_count) {
143 for (j = 0; j < left_count; j++) {
144 blame->linemap1[idx1++] =
145 blame->linemap2[idx2++];
147 continue;
150 if (right_count == 0) {
151 for (j = 0; j < left_count; j++) {
152 blame->linemap1[idx1++] = -1;
154 continue;
157 for (j = 0; j < right_count; j++) {
158 int ln = blame->linemap2[idx2++];
159 err = annotate_line(blame, ln, commit_id, cb, arg);
160 if (err)
161 return err;
162 if (blame->nlines == blame->nannotated)
163 break;
167 return NULL;
170 static const struct got_error *
171 blame_prepare_file(FILE *f, unsigned char **p, off_t *size,
172 int *nlines, off_t **line_offsets, struct diff_data *diff_data,
173 const struct diff_config *cfg, struct got_blob_object *blob)
175 const struct got_error *err = NULL;
176 int rc;
178 err = got_object_blob_dump_to_file(size, nlines, line_offsets,
179 f, blob);
180 if (err)
181 return err;
183 #ifndef GOT_DIFF_NO_MMAP
184 *p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
185 if (*p == MAP_FAILED)
186 #endif
187 *p = NULL; /* fall back on file I/O */
189 rc = diff_atomize_file(diff_data, cfg, f, *p, *size, 0);
190 if (rc)
191 return got_error_set_errno(rc, "diff_atomize_file");
193 return NULL;
196 static const struct got_error *
197 blame_commit(struct got_blame *blame, struct got_object_id *id,
198 const char *path, struct got_repository *repo,
199 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
200 void *arg)
202 const struct got_error *err = NULL;
203 struct got_commit_object *commit = NULL;
204 struct got_object_qid *pid = NULL;
205 struct got_object_id *pblob_id = NULL;
206 struct got_blob_object *pblob = NULL;
207 struct diff_result *diff_result = NULL;
209 err = got_object_open_as_commit(&commit, repo, id);
210 if (err)
211 return err;
213 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
214 if (pid == NULL) {
215 got_object_commit_close(commit);
216 return NULL;
219 err = got_object_id_by_path(&pblob_id, repo, pid->id, path);
220 if (err) {
221 if (err->code == GOT_ERR_NO_TREE_ENTRY)
222 err = NULL;
223 goto done;
226 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
227 if (err)
228 goto done;
230 blame->f1 = got_opentemp();
231 if (blame->f1 == NULL) {
232 err = got_error_from_errno("got_opentemp");
233 goto done;
236 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
237 &blame->nlines1, &blame->line_offsets1, blame->data1,
238 blame->cfg, pblob);
239 if (err)
240 goto done;
242 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
243 if (diff_result == NULL) {
244 err = got_error_set_errno(ENOMEM, "malloc");
245 goto done;
247 if (diff_result->rc != DIFF_RC_OK) {
248 err = got_error_set_errno(diff_result->rc, "diff");
249 goto done;
251 if (diff_result->chunks.len > 0) {
252 if (blame->nlines1 > 0) {
253 blame->linemap1 = calloc(blame->nlines1,
254 sizeof(*blame->linemap1));
255 if (blame->linemap1 == NULL) {
256 err = got_error_from_errno("malloc");
257 goto done;
260 err = blame_changes(blame, diff_result, id, cb, arg);
261 if (err)
262 goto done;
263 } else if (cb)
264 err = cb(arg, blame->nlines, -1, id);
265 done:
266 if (diff_result)
267 diff_result_free(diff_result);
268 if (commit)
269 got_object_commit_close(commit);
270 free(pblob_id);
271 if (pblob)
272 got_object_blob_close(pblob);
273 return err;
276 static const struct got_error *
277 blame_close(struct got_blame *blame)
279 const struct got_error *err = NULL;
281 diff_data_free(blame->data1);
282 free(blame->data1);
283 diff_data_free(blame->data2);
284 free(blame->data2);
285 if (blame->map1) {
286 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
287 err = got_error_from_errno("munmap");
289 if (blame->map2) {
290 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
291 err = got_error_from_errno("munmap");
293 if (blame->f1 && fclose(blame->f1) != 0 && err == NULL)
294 err = got_error_from_errno("fclose");
295 if (blame->f2 && fclose(blame->f2) != 0 && err == NULL)
296 err = got_error_from_errno("fclose");
297 free(blame->lines);
298 free(blame->line_offsets1);
299 free(blame->line_offsets2);
300 free(blame->linemap1);
301 free(blame->linemap2);
302 free(blame->cfg);
303 free(blame);
304 return err;
307 static int
308 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
309 off_t *line_offsets)
311 int i, rc = DIFF_RC_OK;
313 ARRAYLIST_INIT(d->atoms, nlines);
315 for (i = 0; i < nlines; i++) {
316 struct diff_atom *atom;
317 off_t len, pos = line_offsets[i];
318 unsigned int hash = 0;
319 int j;
321 ARRAYLIST_ADD(atom, d->atoms);
322 if (atom == NULL) {
323 rc = errno;
324 break;
327 if (i < nlines - 1)
328 len = line_offsets[i + 1] - pos;
329 else
330 len = filesize - pos;
332 if (fseeko(f, pos, SEEK_SET) == -1) {
333 rc = errno;
334 break;
336 for (j = 0; j < len; j++) {
337 int c = fgetc(f);
338 if (c == EOF) {
339 if (feof(f))
340 rc = EIO; /* unexpected EOF */
341 else
342 rc = errno;
343 goto done;
346 hash = diff_atom_hash_update(hash, (unsigned char)c);
348 *atom = (struct diff_atom){
349 .root = d,
350 .pos = pos,
351 .at = NULL, /* atom data is not memory-mapped */
352 .len = len,
353 .hash = hash,
354 };
356 done:
357 if (rc)
358 ARRAYLIST_FREE(d->atoms);
360 return rc;
363 static int
364 atomize_file_mmap(struct diff_data *d, unsigned char *p,
365 off_t filesize, int nlines, off_t *line_offsets)
367 int i, rc = DIFF_RC_OK;
369 ARRAYLIST_INIT(d->atoms, nlines);
371 for (i = 0; i < nlines; i++) {
372 struct diff_atom *atom;
373 off_t len, pos = line_offsets[i];
374 unsigned int hash = 0;
375 int j;
377 ARRAYLIST_ADD(atom, d->atoms);
378 if (atom == NULL) {
379 rc = errno;
380 break;
383 if (i < nlines - 1)
384 len = line_offsets[i + 1] - pos;
385 else
386 len = filesize - pos;
388 for (j = 0; j < len; j++)
389 hash = diff_atom_hash_update(hash, p[pos + j]);
391 *atom = (struct diff_atom){
392 .root = d,
393 .pos = pos,
394 .at = &p[pos],
395 .len = len,
396 .hash = hash,
397 };
400 if (rc)
401 ARRAYLIST_FREE(d->atoms);
403 return rc;
406 /* Implements diff_atomize_func_t */
407 static int
408 blame_atomize_file(void *arg, struct diff_data *d)
410 struct got_blame *blame = arg;
412 if (d->f == blame->f1) {
413 if (blame->map1)
414 return atomize_file_mmap(d, blame->map1,
415 blame->size1, blame->nlines1,
416 blame->line_offsets1);
417 else
418 return atomize_file(d, blame->f1, blame->size1,
419 blame->nlines1, blame->line_offsets1);
420 } else if (d->f == blame->f2) {
421 if (d->atoms.len > 0) {
422 /* Re-use data from previous commit. */
423 return DIFF_RC_OK;
425 if (blame->map2)
426 return atomize_file_mmap(d, blame->map2,
427 blame->size2, blame->nlines2,
428 blame->line_offsets2);
429 else
430 return atomize_file(d, blame->f2, blame->size2,
431 blame->nlines2, blame->line_offsets2);
434 return DIFF_RC_OK;
437 static const struct got_error *
438 close_file2_and_reuse_file1(struct got_blame *blame)
440 struct diff_data *d;
442 free(blame->line_offsets2);
443 blame->line_offsets2 = blame->line_offsets1;
444 blame->line_offsets1 = NULL;
446 free(blame->linemap2);
447 blame->linemap2 = blame->linemap1;
448 blame->linemap1 = NULL;
450 if (blame->map2) {
451 if (munmap(blame->map2, blame->size2) == -1)
452 return got_error_from_errno("munmap");
453 blame->map2 = blame->map1;
454 blame->map2 = NULL;
457 blame->size2 = blame->size1;
458 blame->size1 = 0;
460 if (fclose(blame->f2) == EOF)
461 return got_error_from_errno("fclose");
462 blame->f2 = blame->f1;
463 blame->f1 = NULL;
465 blame->nlines2 = blame->nlines1;
466 blame->nlines1 = 0;
468 free(blame->line_offsets2);
469 blame->line_offsets2 = blame->line_offsets1;
470 blame->line_offsets2 = NULL;
472 diff_data_free(blame->data2); /* does not free pointer itself */
473 memset(blame->data2, 0, sizeof(*blame->data2));
474 d = blame->data2;
475 blame->data2 = blame->data1;
476 blame->data1 = d;
478 return NULL;
481 static const struct got_error *
482 blame_open(struct got_blame **blamep, const char *path,
483 struct got_object_id *start_commit_id, struct got_repository *repo,
484 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
485 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
487 const struct got_error *err = NULL;
488 struct got_object_id *obj_id = NULL;
489 struct got_blob_object *blob = NULL;
490 struct got_blame *blame = NULL;
491 struct got_object_id *id = NULL;
492 int lineno;
493 struct got_commit_graph *graph = NULL;
495 *blamep = NULL;
497 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
498 if (err)
499 goto done;
501 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
502 if (err)
503 goto done;
505 blame = calloc(1, sizeof(*blame));
506 if (blame == NULL) {
507 err = got_error_from_errno("calloc");
508 goto done;
511 blame->data1 = calloc(1, sizeof(*blame->data1));
512 if (blame->data1 == NULL) {
513 err = got_error_from_errno("calloc");
514 goto done;
516 blame->data2 = calloc(1, sizeof(*blame->data2));
517 if (blame->data2 == NULL) {
518 err = got_error_from_errno("calloc");
519 goto done;
522 blame->f2 = got_opentemp();
523 if (blame->f2 == NULL) {
524 err = got_error_from_errno("got_opentemp");
525 goto done;
527 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
528 blame_atomize_file, blame);
529 if (err)
530 goto done;
532 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
533 &blame->nlines2, &blame->line_offsets2, blame->data2,
534 blame->cfg, blob);
535 blame->nlines = blame->nlines2;
536 if (err || blame->nlines == 0)
537 goto done;
539 got_object_blob_close(blob);
540 blob = NULL;
542 /* Don't include \n at EOF in the blame line count. */
543 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
544 blame->nlines--;
546 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
547 if (blame->lines == NULL) {
548 err = got_error_from_errno("calloc");
549 goto done;
552 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
553 if (blame->linemap2 == NULL) {
554 err = got_error_from_errno("calloc");
555 goto done;
557 for (lineno = 0; lineno < blame->nlines2; lineno++)
558 blame->linemap2[lineno] = lineno;
560 err = got_commit_graph_open(&graph, path, 1);
561 if (err)
562 goto done;
564 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
565 cancel_cb, cancel_arg);
566 if (err)
567 goto done;
568 for (;;) {
569 struct got_object_id *next_id;
570 err = got_commit_graph_iter_next(&next_id, graph, repo,
571 cancel_cb, cancel_arg);
572 if (err) {
573 if (err->code == GOT_ERR_ITER_COMPLETED) {
574 err = NULL;
575 break;
577 goto done;
579 if (next_id) {
580 id = next_id;
581 err = blame_commit(blame, id, path, repo, cb, arg);
582 if (err) {
583 if (err->code == GOT_ERR_ITER_COMPLETED)
584 err = NULL;
585 goto done;
587 if (blame->nannotated == blame->nlines)
588 break;
590 err = close_file2_and_reuse_file1(blame);
591 if (err)
592 goto done;
596 if (id && blame->nannotated < blame->nlines) {
597 /* Annotate remaining non-annotated lines with last commit. */
598 for (lineno = 0; lineno < blame->nlines; lineno++) {
599 err = annotate_line(blame, lineno, id, cb, arg);
600 if (err)
601 goto done;
605 done:
606 if (graph)
607 got_commit_graph_close(graph);
608 free(obj_id);
609 if (blob)
610 got_object_blob_close(blob);
611 if (err) {
612 if (blame)
613 blame_close(blame);
614 } else
615 *blamep = blame;
617 return err;
620 const struct got_error *
621 got_blame(const char *path, struct got_object_id *commit_id,
622 struct got_repository *repo,
623 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
624 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
626 const struct got_error *err = NULL, *close_err = NULL;
627 struct got_blame *blame;
628 char *abspath;
630 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
631 return got_error_from_errno2("asprintf", path);
633 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
634 cancel_cb, cancel_arg);
635 free(abspath);
636 if (blame)
637 close_err = blame_close(blame);
638 return err ? err : close_err;