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,
93 struct got_commit_object *commit, struct got_object_id *id,
94 got_blame_cb cb, 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, commit, 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_commit_object *commit, struct got_object_id *commit_id,
117 got_blame_cb cb, void *arg)
119 const struct got_error *err = NULL;
120 int i;
121 int idx1 = 0, idx2 = 0;
123 for (i = 0; i < diff_result->chunks.len &&
124 blame->nannotated < blame->nlines; i++) {
125 struct diff_chunk *c = diff_chunk_get(diff_result, i);
126 unsigned int left_count, right_count;
127 int j;
129 /*
130 * We do not need to worry about idx1/idx2 growing out
131 * of bounds because the diff implementation ensures
132 * that chunk ranges never exceed the number of lines
133 * in the left/right input files.
134 */
135 left_count = diff_chunk_get_left_count(c);
136 right_count = diff_chunk_get_right_count(c);
138 if (left_count == right_count) {
139 for (j = 0; j < left_count; j++) {
140 blame->linemap1[idx1++] =
141 blame->linemap2[idx2++];
143 continue;
146 if (right_count == 0) {
147 for (j = 0; j < left_count; j++) {
148 blame->linemap1[idx1++] = -1;
150 continue;
153 for (j = 0; j < right_count; j++) {
154 int ln = blame->linemap2[idx2++];
155 err = annotate_line(blame, ln, commit, commit_id,
156 cb, arg);
157 if (err)
158 return err;
159 if (blame->nlines == blame->nannotated)
160 break;
164 return NULL;
167 static const struct got_error *
168 blame_prepare_file(FILE *f, unsigned char **p, off_t *size,
169 int *nlines, off_t **line_offsets, struct diff_data *diff_data,
170 const struct diff_config *cfg, struct got_blob_object *blob)
172 const struct got_error *err = NULL;
173 int diff_flags = 0, rc;
175 err = got_object_blob_dump_to_file(size, nlines, line_offsets,
176 f, blob);
177 if (err)
178 return err;
180 #ifndef GOT_DIFF_NO_MMAP
181 *p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
182 if (*p == MAP_FAILED)
183 #endif
184 *p = NULL; /* fall back on file I/O */
186 /* Allow blaming lines in binary files even though it's useless. */
187 diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
189 rc = diff_atomize_file(diff_data, cfg, f, *p, *size, diff_flags);
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 got_blame_cb cb, void *arg)
201 const struct got_error *err = NULL;
202 struct got_commit_object *commit = NULL, *pcommit = NULL;
203 struct got_object_qid *pid = NULL;
204 struct got_object_id *pblob_id = NULL;
205 struct got_blob_object *pblob = NULL;
206 struct diff_result *diff_result = NULL;
207 int fd = -1;
209 err = got_object_open_as_commit(&commit, repo, id);
210 if (err)
211 return err;
213 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
214 if (pid == NULL) {
215 got_object_commit_close(commit);
216 return NULL;
219 fd = got_opentempfd();
220 if (fd == -1) {
221 err = got_error_from_errno("got_opentempfd");
222 goto done;
225 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
226 if (err)
227 goto done;
229 err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
230 if (err) {
231 if (err->code == GOT_ERR_NO_TREE_ENTRY)
232 err = NULL;
233 goto done;
236 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, fd);
237 if (err)
238 goto done;
240 blame->f1 = got_opentemp();
241 if (blame->f1 == NULL) {
242 err = got_error_from_errno("got_opentemp");
243 goto done;
246 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
247 &blame->nlines1, &blame->line_offsets1, blame->data1,
248 blame->cfg, pblob);
249 if (err)
250 goto done;
252 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
253 if (diff_result == NULL) {
254 err = got_error_set_errno(ENOMEM, "malloc");
255 goto done;
257 if (diff_result->rc != DIFF_RC_OK) {
258 err = got_error_set_errno(diff_result->rc, "diff");
259 goto done;
261 if (diff_result->chunks.len > 0) {
262 if (blame->nlines1 > 0) {
263 blame->linemap1 = calloc(blame->nlines1,
264 sizeof(*blame->linemap1));
265 if (blame->linemap1 == NULL) {
266 err = got_error_from_errno("malloc");
267 goto done;
270 err = blame_changes(blame, diff_result, commit, id, cb, arg);
271 if (err)
272 goto done;
273 } else if (cb)
274 err = cb(arg, blame->nlines, -1, commit, id);
275 done:
276 if (diff_result)
277 diff_result_free(diff_result);
278 if (commit)
279 got_object_commit_close(commit);
280 if (pcommit)
281 got_object_commit_close(pcommit);
282 free(pblob_id);
283 if (fd != -1 && close(fd) == -1 && err == NULL)
284 err = got_error_from_errno("close");
285 if (pblob)
286 got_object_blob_close(pblob);
287 return err;
290 static const struct got_error *
291 blame_close(struct got_blame *blame)
293 const struct got_error *err = NULL;
295 diff_data_free(blame->data1);
296 free(blame->data1);
297 diff_data_free(blame->data2);
298 free(blame->data2);
299 if (blame->map1) {
300 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
301 err = got_error_from_errno("munmap");
303 if (blame->map2) {
304 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
305 err = got_error_from_errno("munmap");
307 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
308 err = got_error_from_errno("fclose");
309 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
310 err = got_error_from_errno("fclose");
311 free(blame->lines);
312 free(blame->line_offsets1);
313 free(blame->line_offsets2);
314 free(blame->linemap1);
315 free(blame->linemap2);
316 free(blame->cfg);
317 free(blame);
318 return err;
321 static int
322 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
323 off_t *line_offsets)
325 int i, rc = DIFF_RC_OK;
326 int embedded_nul = 0;
328 ARRAYLIST_INIT(d->atoms, nlines);
330 for (i = 0; i < nlines; i++) {
331 struct diff_atom *atom;
332 off_t len, pos = line_offsets[i];
333 unsigned int hash = 0;
334 int j;
336 ARRAYLIST_ADD(atom, d->atoms);
337 if (atom == NULL) {
338 rc = errno;
339 break;
342 if (i < nlines - 1)
343 len = line_offsets[i + 1] - pos;
344 else
345 len = filesize - pos;
347 if (fseeko(f, pos, SEEK_SET) == -1) {
348 rc = errno;
349 break;
351 for (j = 0; j < len; j++) {
352 int c = fgetc(f);
353 if (c == EOF) {
354 if (feof(f))
355 rc = EIO; /* unexpected EOF */
356 else
357 rc = errno;
358 goto done;
361 hash = diff_atom_hash_update(hash, (unsigned char)c);
363 if (c == '\0')
364 embedded_nul = 1;
367 *atom = (struct diff_atom){
368 .root = d,
369 .pos = pos,
370 .at = NULL, /* atom data is not memory-mapped */
371 .len = len,
372 .hash = hash,
373 };
376 /* File are considered binary if they contain embedded '\0' bytes. */
377 if (embedded_nul)
378 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
379 done:
380 if (rc)
381 ARRAYLIST_FREE(d->atoms);
383 return rc;
386 static int
387 atomize_file_mmap(struct diff_data *d, unsigned char *p,
388 off_t filesize, int nlines, off_t *line_offsets)
390 int i, rc = DIFF_RC_OK;
391 int embedded_nul = 0;
393 ARRAYLIST_INIT(d->atoms, nlines);
395 for (i = 0; i < nlines; i++) {
396 struct diff_atom *atom;
397 off_t len, pos = line_offsets[i];
398 unsigned int hash = 0;
399 int j;
401 ARRAYLIST_ADD(atom, d->atoms);
402 if (atom == NULL) {
403 rc = errno;
404 break;
407 if (i < nlines - 1)
408 len = line_offsets[i + 1] - pos;
409 else
410 len = filesize - pos;
412 for (j = 0; j < len; j++)
413 hash = diff_atom_hash_update(hash, p[pos + j]);
415 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
416 embedded_nul = 1;
418 *atom = (struct diff_atom){
419 .root = d,
420 .pos = pos,
421 .at = &p[pos],
422 .len = len,
423 .hash = hash,
424 };
427 /* File are considered binary if they contain embedded '\0' bytes. */
428 if (embedded_nul)
429 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
431 if (rc)
432 ARRAYLIST_FREE(d->atoms);
434 return rc;
437 /* Implements diff_atomize_func_t */
438 static int
439 blame_atomize_file(void *arg, struct diff_data *d)
441 struct got_blame *blame = arg;
443 if (d->f == blame->f1) {
444 if (blame->map1)
445 return atomize_file_mmap(d, blame->map1,
446 blame->size1, blame->nlines1,
447 blame->line_offsets1);
448 else
449 return atomize_file(d, blame->f1, blame->size1,
450 blame->nlines1, blame->line_offsets1);
451 } else if (d->f == blame->f2) {
452 if (d->atoms.len > 0) {
453 /* Re-use data from previous commit. */
454 return DIFF_RC_OK;
456 if (blame->map2)
457 return atomize_file_mmap(d, blame->map2,
458 blame->size2, blame->nlines2,
459 blame->line_offsets2);
460 else
461 return atomize_file(d, blame->f2, blame->size2,
462 blame->nlines2, blame->line_offsets2);
465 return DIFF_RC_OK;
468 static const struct got_error *
469 close_file2_and_reuse_file1(struct got_blame *blame)
471 struct diff_data *d;
473 free(blame->line_offsets2);
474 blame->line_offsets2 = blame->line_offsets1;
475 blame->line_offsets1 = NULL;
477 free(blame->linemap2);
478 blame->linemap2 = blame->linemap1;
479 blame->linemap1 = NULL;
481 if (blame->map2) {
482 if (munmap(blame->map2, blame->size2) == -1)
483 return got_error_from_errno("munmap");
484 blame->map2 = blame->map1;
485 blame->map1 = NULL;
488 blame->size2 = blame->size1;
489 blame->size1 = 0;
491 if (fclose(blame->f2) == EOF)
492 return got_error_from_errno("fclose");
493 blame->f2 = blame->f1;
494 blame->f1 = NULL;
496 blame->nlines2 = blame->nlines1;
497 blame->nlines1 = 0;
499 diff_data_free(blame->data2); /* does not free pointer itself */
500 memset(blame->data2, 0, sizeof(*blame->data2));
501 d = blame->data2;
502 blame->data2 = blame->data1;
503 blame->data1 = d;
505 return NULL;
508 static const struct got_error *
509 blame_open(struct got_blame **blamep, const char *path,
510 struct got_object_id *start_commit_id, struct got_repository *repo,
511 got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg,
512 int fd)
514 const struct got_error *err = NULL;
515 struct got_commit_object *start_commit = NULL, *last_commit = NULL;
516 struct got_object_id *obj_id = NULL;
517 struct got_blob_object *blob = NULL;
518 struct got_blame *blame = NULL;
519 struct got_object_id *id = NULL;
520 int lineno;
521 struct got_commit_graph *graph = NULL;
523 *blamep = NULL;
525 err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
526 if (err)
527 goto done;
529 err = got_object_id_by_path(&obj_id, repo, start_commit, path);
530 if (err)
531 goto done;
533 err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
534 if (err)
535 goto done;
537 blame = calloc(1, sizeof(*blame));
538 if (blame == NULL) {
539 err = got_error_from_errno("calloc");
540 goto done;
543 blame->data1 = calloc(1, sizeof(*blame->data1));
544 if (blame->data1 == NULL) {
545 err = got_error_from_errno("calloc");
546 goto done;
548 blame->data2 = calloc(1, sizeof(*blame->data2));
549 if (blame->data2 == NULL) {
550 err = got_error_from_errno("calloc");
551 goto done;
554 blame->f2 = got_opentemp();
555 if (blame->f2 == NULL) {
556 err = got_error_from_errno("got_opentemp");
557 goto done;
559 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
560 blame_atomize_file, blame);
561 if (err)
562 goto done;
564 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
565 &blame->nlines2, &blame->line_offsets2, blame->data2,
566 blame->cfg, blob);
567 blame->nlines = blame->nlines2;
568 if (err || blame->nlines == 0)
569 goto done;
571 got_object_blob_close(blob);
572 blob = NULL;
574 /* Don't include \n at EOF in the blame line count. */
575 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
576 blame->nlines--;
578 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
579 if (blame->lines == NULL) {
580 err = got_error_from_errno("calloc");
581 goto done;
584 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
585 if (blame->linemap2 == NULL) {
586 err = got_error_from_errno("calloc");
587 goto done;
589 for (lineno = 0; lineno < blame->nlines2; lineno++)
590 blame->linemap2[lineno] = lineno;
592 err = got_commit_graph_open(&graph, path, 1);
593 if (err)
594 goto done;
596 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
597 cancel_cb, cancel_arg);
598 if (err)
599 goto done;
600 for (;;) {
601 struct got_object_id *next_id;
602 err = got_commit_graph_iter_next(&next_id, graph, repo,
603 cancel_cb, cancel_arg);
604 if (err) {
605 if (err->code == GOT_ERR_ITER_COMPLETED) {
606 err = NULL;
607 break;
609 goto done;
611 if (next_id) {
612 id = next_id;
613 err = blame_commit(blame, id, path, repo, cb, arg);
614 if (err) {
615 if (err->code == GOT_ERR_ITER_COMPLETED)
616 err = NULL;
617 goto done;
619 if (blame->nannotated == blame->nlines)
620 break;
622 err = close_file2_and_reuse_file1(blame);
623 if (err)
624 goto done;
628 if (id && blame->nannotated < blame->nlines) {
629 /* Annotate remaining non-annotated lines with last commit. */
630 err = got_object_open_as_commit(&last_commit, repo, id);
631 if (err)
632 goto done;
633 for (lineno = 0; lineno < blame->nlines; lineno++) {
634 err = annotate_line(blame, lineno, last_commit, id,
635 cb, arg);
636 if (err)
637 goto done;
641 done:
642 if (graph)
643 got_commit_graph_close(graph);
644 free(obj_id);
645 if (blob)
646 got_object_blob_close(blob);
647 if (start_commit)
648 got_object_commit_close(start_commit);
649 if (last_commit)
650 got_object_commit_close(last_commit);
651 if (err) {
652 if (blame)
653 blame_close(blame);
654 } else
655 *blamep = blame;
657 return err;
660 const struct got_error *
661 got_blame(const char *path, struct got_object_id *commit_id,
662 struct got_repository *repo, got_blame_cb cb, void *arg,
663 got_cancel_cb cancel_cb, void* cancel_arg, int fd)
665 const struct got_error *err = NULL, *close_err = NULL;
666 struct got_blame *blame;
667 char *abspath;
669 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
670 return got_error_from_errno2("asprintf", path);
672 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
673 cancel_cb, cancel_arg, fd);
674 free(abspath);
675 if (blame)
676 close_err = blame_close(blame);
677 return err ? err : close_err;