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;
208 err = got_object_open_as_commit(&commit, repo, id);
209 if (err)
210 return err;
212 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
213 if (pid == NULL) {
214 got_object_commit_close(commit);
215 return NULL;
218 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
219 if (err)
220 goto done;
222 err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
223 if (err) {
224 if (err->code == GOT_ERR_NO_TREE_ENTRY)
225 err = NULL;
226 goto done;
229 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
230 if (err)
231 goto done;
233 blame->f1 = got_opentemp();
234 if (blame->f1 == NULL) {
235 err = got_error_from_errno("got_opentemp");
236 goto done;
239 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
240 &blame->nlines1, &blame->line_offsets1, blame->data1,
241 blame->cfg, pblob);
242 if (err)
243 goto done;
245 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
246 if (diff_result == NULL) {
247 err = got_error_set_errno(ENOMEM, "malloc");
248 goto done;
250 if (diff_result->rc != DIFF_RC_OK) {
251 err = got_error_set_errno(diff_result->rc, "diff");
252 goto done;
254 if (diff_result->chunks.len > 0) {
255 if (blame->nlines1 > 0) {
256 blame->linemap1 = calloc(blame->nlines1,
257 sizeof(*blame->linemap1));
258 if (blame->linemap1 == NULL) {
259 err = got_error_from_errno("malloc");
260 goto done;
263 err = blame_changes(blame, diff_result, commit, id, cb, arg);
264 if (err)
265 goto done;
266 } else if (cb)
267 err = cb(arg, blame->nlines, -1, commit, id);
268 done:
269 if (diff_result)
270 diff_result_free(diff_result);
271 if (commit)
272 got_object_commit_close(commit);
273 if (pcommit)
274 got_object_commit_close(pcommit);
275 free(pblob_id);
276 if (pblob)
277 got_object_blob_close(pblob);
278 return err;
281 static const struct got_error *
282 blame_close(struct got_blame *blame)
284 const struct got_error *err = NULL;
286 diff_data_free(blame->data1);
287 free(blame->data1);
288 diff_data_free(blame->data2);
289 free(blame->data2);
290 if (blame->map1) {
291 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
292 err = got_error_from_errno("munmap");
294 if (blame->map2) {
295 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
296 err = got_error_from_errno("munmap");
298 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
299 err = got_error_from_errno("fclose");
300 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
301 err = got_error_from_errno("fclose");
302 free(blame->lines);
303 free(blame->line_offsets1);
304 free(blame->line_offsets2);
305 free(blame->linemap1);
306 free(blame->linemap2);
307 free(blame->cfg);
308 free(blame);
309 return err;
312 static int
313 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
314 off_t *line_offsets)
316 int i, rc = DIFF_RC_OK;
317 int embedded_nul = 0;
319 ARRAYLIST_INIT(d->atoms, nlines);
321 for (i = 0; i < nlines; i++) {
322 struct diff_atom *atom;
323 off_t len, pos = line_offsets[i];
324 unsigned int hash = 0;
325 int j;
327 ARRAYLIST_ADD(atom, d->atoms);
328 if (atom == NULL) {
329 rc = errno;
330 break;
333 if (i < nlines - 1)
334 len = line_offsets[i + 1] - pos;
335 else
336 len = filesize - pos;
338 if (fseeko(f, pos, SEEK_SET) == -1) {
339 rc = errno;
340 break;
342 for (j = 0; j < len; j++) {
343 int c = fgetc(f);
344 if (c == EOF) {
345 if (feof(f))
346 rc = EIO; /* unexpected EOF */
347 else
348 rc = errno;
349 goto done;
352 hash = diff_atom_hash_update(hash, (unsigned char)c);
354 if (c == '\0')
355 embedded_nul = 1;
358 *atom = (struct diff_atom){
359 .root = d,
360 .pos = pos,
361 .at = NULL, /* atom data is not memory-mapped */
362 .len = len,
363 .hash = hash,
364 };
367 /* File are considered binary if they contain embedded '\0' bytes. */
368 if (embedded_nul)
369 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
370 done:
371 if (rc)
372 ARRAYLIST_FREE(d->atoms);
374 return rc;
377 static int
378 atomize_file_mmap(struct diff_data *d, unsigned char *p,
379 off_t filesize, int nlines, off_t *line_offsets)
381 int i, rc = DIFF_RC_OK;
382 int embedded_nul = 0;
384 ARRAYLIST_INIT(d->atoms, nlines);
386 for (i = 0; i < nlines; i++) {
387 struct diff_atom *atom;
388 off_t len, pos = line_offsets[i];
389 unsigned int hash = 0;
390 int j;
392 ARRAYLIST_ADD(atom, d->atoms);
393 if (atom == NULL) {
394 rc = errno;
395 break;
398 if (i < nlines - 1)
399 len = line_offsets[i + 1] - pos;
400 else
401 len = filesize - pos;
403 for (j = 0; j < len; j++)
404 hash = diff_atom_hash_update(hash, p[pos + j]);
406 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
407 embedded_nul = 1;
409 *atom = (struct diff_atom){
410 .root = d,
411 .pos = pos,
412 .at = &p[pos],
413 .len = len,
414 .hash = hash,
415 };
418 /* File are considered binary if they contain embedded '\0' bytes. */
419 if (embedded_nul)
420 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
422 if (rc)
423 ARRAYLIST_FREE(d->atoms);
425 return rc;
428 /* Implements diff_atomize_func_t */
429 static int
430 blame_atomize_file(void *arg, struct diff_data *d)
432 struct got_blame *blame = arg;
434 if (d->f == blame->f1) {
435 if (blame->map1)
436 return atomize_file_mmap(d, blame->map1,
437 blame->size1, blame->nlines1,
438 blame->line_offsets1);
439 else
440 return atomize_file(d, blame->f1, blame->size1,
441 blame->nlines1, blame->line_offsets1);
442 } else if (d->f == blame->f2) {
443 if (d->atoms.len > 0) {
444 /* Re-use data from previous commit. */
445 return DIFF_RC_OK;
447 if (blame->map2)
448 return atomize_file_mmap(d, blame->map2,
449 blame->size2, blame->nlines2,
450 blame->line_offsets2);
451 else
452 return atomize_file(d, blame->f2, blame->size2,
453 blame->nlines2, blame->line_offsets2);
456 return DIFF_RC_OK;
459 static const struct got_error *
460 close_file2_and_reuse_file1(struct got_blame *blame)
462 struct diff_data *d;
464 free(blame->line_offsets2);
465 blame->line_offsets2 = blame->line_offsets1;
466 blame->line_offsets1 = NULL;
468 free(blame->linemap2);
469 blame->linemap2 = blame->linemap1;
470 blame->linemap1 = NULL;
472 if (blame->map2) {
473 if (munmap(blame->map2, blame->size2) == -1)
474 return got_error_from_errno("munmap");
475 blame->map2 = blame->map1;
476 blame->map1 = NULL;
479 blame->size2 = blame->size1;
480 blame->size1 = 0;
482 if (fclose(blame->f2) == EOF)
483 return got_error_from_errno("fclose");
484 blame->f2 = blame->f1;
485 blame->f1 = NULL;
487 blame->nlines2 = blame->nlines1;
488 blame->nlines1 = 0;
490 diff_data_free(blame->data2); /* does not free pointer itself */
491 memset(blame->data2, 0, sizeof(*blame->data2));
492 d = blame->data2;
493 blame->data2 = blame->data1;
494 blame->data1 = d;
496 return NULL;
499 static const struct got_error *
500 blame_open(struct got_blame **blamep, const char *path,
501 struct got_object_id *start_commit_id, struct got_repository *repo,
502 got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
504 const struct got_error *err = NULL;
505 struct got_commit_object *start_commit = NULL, *last_commit = NULL;
506 struct got_object_id *obj_id = NULL;
507 struct got_blob_object *blob = NULL;
508 struct got_blame *blame = NULL;
509 struct got_object_id *id = NULL;
510 int lineno;
511 struct got_commit_graph *graph = NULL;
513 *blamep = NULL;
515 err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
516 if (err)
517 goto done;
519 err = got_object_id_by_path(&obj_id, repo, start_commit, path);
520 if (err)
521 goto done;
523 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
524 if (err)
525 goto done;
527 blame = calloc(1, sizeof(*blame));
528 if (blame == NULL) {
529 err = got_error_from_errno("calloc");
530 goto done;
533 blame->data1 = calloc(1, sizeof(*blame->data1));
534 if (blame->data1 == NULL) {
535 err = got_error_from_errno("calloc");
536 goto done;
538 blame->data2 = calloc(1, sizeof(*blame->data2));
539 if (blame->data2 == NULL) {
540 err = got_error_from_errno("calloc");
541 goto done;
544 blame->f2 = got_opentemp();
545 if (blame->f2 == NULL) {
546 err = got_error_from_errno("got_opentemp");
547 goto done;
549 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
550 blame_atomize_file, blame);
551 if (err)
552 goto done;
554 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
555 &blame->nlines2, &blame->line_offsets2, blame->data2,
556 blame->cfg, blob);
557 blame->nlines = blame->nlines2;
558 if (err || blame->nlines == 0)
559 goto done;
561 got_object_blob_close(blob);
562 blob = NULL;
564 /* Don't include \n at EOF in the blame line count. */
565 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
566 blame->nlines--;
568 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
569 if (blame->lines == NULL) {
570 err = got_error_from_errno("calloc");
571 goto done;
574 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
575 if (blame->linemap2 == NULL) {
576 err = got_error_from_errno("calloc");
577 goto done;
579 for (lineno = 0; lineno < blame->nlines2; lineno++)
580 blame->linemap2[lineno] = lineno;
582 err = got_commit_graph_open(&graph, path, 1);
583 if (err)
584 goto done;
586 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
587 cancel_cb, cancel_arg);
588 if (err)
589 goto done;
590 for (;;) {
591 struct got_object_id *next_id;
592 err = got_commit_graph_iter_next(&next_id, graph, repo,
593 cancel_cb, cancel_arg);
594 if (err) {
595 if (err->code == GOT_ERR_ITER_COMPLETED) {
596 err = NULL;
597 break;
599 goto done;
601 if (next_id) {
602 id = next_id;
603 err = blame_commit(blame, id, path, repo, cb, arg);
604 if (err) {
605 if (err->code == GOT_ERR_ITER_COMPLETED)
606 err = NULL;
607 goto done;
609 if (blame->nannotated == blame->nlines)
610 break;
612 err = close_file2_and_reuse_file1(blame);
613 if (err)
614 goto done;
618 if (id && blame->nannotated < blame->nlines) {
619 /* Annotate remaining non-annotated lines with last commit. */
620 err = got_object_open_as_commit(&last_commit, repo, id);
621 if (err)
622 goto done;
623 for (lineno = 0; lineno < blame->nlines; lineno++) {
624 err = annotate_line(blame, lineno, last_commit, id,
625 cb, arg);
626 if (err)
627 goto done;
631 done:
632 if (graph)
633 got_commit_graph_close(graph);
634 free(obj_id);
635 if (blob)
636 got_object_blob_close(blob);
637 if (start_commit)
638 got_object_commit_close(start_commit);
639 if (last_commit)
640 got_object_commit_close(last_commit);
641 if (err) {
642 if (blame)
643 blame_close(blame);
644 } else
645 *blamep = blame;
647 return err;
650 const struct got_error *
651 got_blame(const char *path, struct got_object_id *commit_id,
652 struct got_repository *repo, got_blame_cb cb, void *arg,
653 got_cancel_cb cancel_cb, void* cancel_arg)
655 const struct got_error *err = NULL, *close_err = NULL;
656 struct got_blame *blame;
657 char *abspath;
659 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
660 return got_error_from_errno2("asprintf", path);
662 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
663 cancel_cb, cancel_arg);
664 free(abspath);
665 if (blame)
666 close_err = blame_close(blame);
667 return err ? err : close_err;