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 <sha2.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <limits.h>
30 #include <util.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_cancel.h"
36 #include "got_commit_graph.h"
37 #include "got_opentemp.h"
38 #include "got_diff.h"
39 #include "got_blame.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_diff.h"
46 #ifndef MAX
47 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
48 #endif
50 struct got_blame_line {
51 int annotated;
52 struct got_object_id id;
53 };
55 struct got_blame {
56 struct diff_config *cfg;
57 int nlines; /* number of lines in file being blamed */
58 int nannotated; /* number of lines already annotated */
59 struct got_blame_line *lines; /* one per line */
60 int ncommits;
62 /*
63 * These change with every traversed commit. After diffing
64 * commits N:N-1, in preparation for diffing commits N-1:N-2,
65 * data for commit N is retained and flipped into data for N-1.
66 *
67 */
68 FILE *f1; /* older version from commit N-1. */
69 FILE *f2; /* newer version from commit N. */
70 int fd;
71 unsigned char *map1;
72 unsigned char *map2;
73 off_t size1;
74 off_t size2;
75 int nlines1;
76 int nlines2;
77 off_t *line_offsets1;
78 off_t *line_offsets2;
80 /*
81 * Map line numbers of an older version of the file to valid line
82 * numbers in the version of the file being blamed. This map is
83 * updated with each commit we traverse throughout the file's history.
84 * Lines mapped to -1 do not correspond to any line in the version
85 * being blamed.
86 */
87 int *linemap1;
88 int *linemap2;
90 struct diff_data *data1;
91 struct diff_data *data2;
92 };
94 static const struct got_error *
95 annotate_line(struct got_blame *blame, int lineno,
96 struct got_commit_object *commit, struct got_object_id *id,
97 got_blame_cb cb, void *arg)
98 {
99 const struct got_error *err = NULL;
100 struct got_blame_line *line;
102 if (lineno < 0 || lineno >= blame->nlines)
103 return NULL;
105 line = &blame->lines[lineno];
106 if (line->annotated)
107 return NULL;
109 memcpy(&line->id, id, sizeof(line->id));
110 line->annotated = 1;
111 blame->nannotated++;
112 if (cb)
113 err = cb(arg, blame->nlines, lineno + 1, commit, id);
114 return err;
117 static const struct got_error *
118 blame_changes(struct got_blame *blame, struct diff_result *diff_result,
119 struct got_commit_object *commit, struct got_object_id *commit_id,
120 got_blame_cb cb, void *arg)
122 const struct got_error *err = NULL;
123 int i;
124 int idx1 = 0, idx2 = 0;
126 for (i = 0; i < diff_result->chunks.len &&
127 blame->nannotated < blame->nlines; i++) {
128 struct diff_chunk *c = diff_chunk_get(diff_result, i);
129 unsigned int left_count, right_count;
130 int j;
132 /*
133 * We do not need to worry about idx1/idx2 growing out
134 * of bounds because the diff implementation ensures
135 * that chunk ranges never exceed the number of lines
136 * in the left/right input files.
137 */
138 left_count = diff_chunk_get_left_count(c);
139 right_count = diff_chunk_get_right_count(c);
141 if (left_count == right_count) {
142 for (j = 0; j < left_count; j++) {
143 blame->linemap1[idx1++] =
144 blame->linemap2[idx2++];
146 continue;
149 if (right_count == 0) {
150 for (j = 0; j < left_count; j++) {
151 blame->linemap1[idx1++] = -1;
153 continue;
156 for (j = 0; j < right_count; j++) {
157 int ln = blame->linemap2[idx2++];
158 err = annotate_line(blame, ln, commit, commit_id,
159 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 diff_flags = 0, 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 /* Allow blaming lines in binary files even though it's useless. */
190 diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
192 rc = diff_atomize_file(diff_data, cfg, f, *p, *size, diff_flags);
193 if (rc)
194 return got_error_set_errno(rc, "diff_atomize_file");
196 return NULL;
199 static const struct got_error *
200 blame_commit(struct got_blame *blame, struct got_object_id *id,
201 const char *path, struct got_repository *repo,
202 got_blame_cb cb, void *arg)
204 const struct got_error *err = NULL;
205 struct got_commit_object *commit = NULL, *pcommit = NULL;
206 struct got_object_qid *pid = NULL;
207 struct got_object_id *pblob_id = NULL;
208 struct got_blob_object *pblob = NULL;
209 struct diff_result *diff_result = NULL;
211 err = got_object_open_as_commit(&commit, repo, id);
212 if (err)
213 return err;
215 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
216 if (pid == NULL) {
217 got_object_commit_close(commit);
218 return NULL;
221 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
222 if (err)
223 goto done;
225 err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
226 if (err) {
227 if (err->code == GOT_ERR_NO_TREE_ENTRY)
228 err = NULL;
229 goto done;
232 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, blame->fd);
233 if (err)
234 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, commit, id, cb, arg);
261 if (err)
262 goto done;
263 } else if (cb)
264 err = cb(arg, blame->nlines, -1, commit, id);
265 done:
266 if (diff_result)
267 diff_result_free(diff_result);
268 if (commit)
269 got_object_commit_close(commit);
270 if (pcommit)
271 got_object_commit_close(pcommit);
272 free(pblob_id);
273 if (pblob)
274 got_object_blob_close(pblob);
275 return err;
278 static const struct got_error *
279 blame_close(struct got_blame *blame)
281 const struct got_error *err = NULL;
283 diff_data_free(blame->data1);
284 free(blame->data1);
285 diff_data_free(blame->data2);
286 free(blame->data2);
287 if (blame->map1) {
288 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
289 err = got_error_from_errno("munmap");
291 if (blame->map2) {
292 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
293 err = got_error_from_errno("munmap");
295 free(blame->lines);
296 free(blame->line_offsets1);
297 free(blame->line_offsets2);
298 free(blame->linemap1);
299 free(blame->linemap2);
300 free(blame->cfg);
301 free(blame);
302 return err;
305 static int
306 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
307 off_t *line_offsets)
309 int i, rc = DIFF_RC_OK;
310 int embedded_nul = 0;
312 ARRAYLIST_INIT(d->atoms, nlines);
314 for (i = 0; i < nlines; i++) {
315 struct diff_atom *atom;
316 off_t len, pos = line_offsets[i];
317 unsigned int hash = 0;
318 int j;
320 ARRAYLIST_ADD(atom, d->atoms);
321 if (atom == NULL) {
322 rc = errno;
323 break;
326 if (i < nlines - 1)
327 len = line_offsets[i + 1] - pos;
328 else
329 len = filesize - pos;
331 if (fseeko(f, pos, SEEK_SET) == -1) {
332 rc = errno;
333 break;
335 for (j = 0; j < len; j++) {
336 int c = fgetc(f);
337 if (c == EOF) {
338 if (feof(f))
339 rc = EIO; /* unexpected EOF */
340 else
341 rc = errno;
342 goto done;
345 hash = diff_atom_hash_update(hash, (unsigned char)c);
347 if (c == '\0')
348 embedded_nul = 1;
351 *atom = (struct diff_atom){
352 .root = d,
353 .pos = pos,
354 .at = NULL, /* atom data is not memory-mapped */
355 .len = len,
356 .hash = hash,
357 };
360 /* File are considered binary if they contain embedded '\0' bytes. */
361 if (embedded_nul)
362 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
363 done:
364 if (rc)
365 ARRAYLIST_FREE(d->atoms);
367 return rc;
370 static int
371 atomize_file_mmap(struct diff_data *d, unsigned char *p,
372 off_t filesize, int nlines, off_t *line_offsets)
374 int i, rc = DIFF_RC_OK;
375 int embedded_nul = 0;
377 ARRAYLIST_INIT(d->atoms, nlines);
379 for (i = 0; i < nlines; i++) {
380 struct diff_atom *atom;
381 off_t len, pos = line_offsets[i];
382 unsigned int hash = 0;
383 int j;
385 ARRAYLIST_ADD(atom, d->atoms);
386 if (atom == NULL) {
387 rc = errno;
388 break;
391 if (i < nlines - 1)
392 len = line_offsets[i + 1] - pos;
393 else
394 len = filesize - pos;
396 for (j = 0; j < len; j++)
397 hash = diff_atom_hash_update(hash, p[pos + j]);
399 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
400 embedded_nul = 1;
402 *atom = (struct diff_atom){
403 .root = d,
404 .pos = pos,
405 .at = &p[pos],
406 .len = len,
407 .hash = hash,
408 };
411 /* File are considered binary if they contain embedded '\0' bytes. */
412 if (embedded_nul)
413 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
415 if (rc)
416 ARRAYLIST_FREE(d->atoms);
418 return rc;
421 /* Implements diff_atomize_func_t */
422 static int
423 blame_atomize_file(void *arg, struct diff_data *d)
425 struct got_blame *blame = arg;
427 if (d->f == blame->f1) {
428 if (blame->map1)
429 return atomize_file_mmap(d, blame->map1,
430 blame->size1, blame->nlines1,
431 blame->line_offsets1);
432 else
433 return atomize_file(d, blame->f1, blame->size1,
434 blame->nlines1, blame->line_offsets1);
435 } else if (d->f == blame->f2) {
436 if (d->atoms.len > 0) {
437 /* Re-use data from previous commit. */
438 return DIFF_RC_OK;
440 if (blame->map2)
441 return atomize_file_mmap(d, blame->map2,
442 blame->size2, blame->nlines2,
443 blame->line_offsets2);
444 else
445 return atomize_file(d, blame->f2, blame->size2,
446 blame->nlines2, blame->line_offsets2);
449 return DIFF_RC_OK;
452 static const struct got_error *
453 flip_files(struct got_blame *blame)
455 const struct got_error *err = NULL;
456 struct diff_data *d;
457 FILE *tmp;
459 free(blame->line_offsets2);
460 blame->line_offsets2 = blame->line_offsets1;
461 blame->line_offsets1 = NULL;
463 free(blame->linemap2);
464 blame->linemap2 = blame->linemap1;
465 blame->linemap1 = NULL;
467 if (blame->map2) {
468 if (munmap(blame->map2, blame->size2) == -1)
469 return got_error_from_errno("munmap");
470 blame->map2 = blame->map1;
471 blame->map1 = NULL;
473 blame->size2 = blame->size1;
475 err = got_opentemp_truncate(blame->f2);
476 if (err)
477 return err;
478 tmp = blame->f2;
479 blame->f2 = blame->f1;
480 blame->f1 = tmp;
481 blame->size1 = 0;
483 blame->nlines2 = blame->nlines1;
484 blame->nlines1 = 0;
486 diff_data_free(blame->data2); /* does not free pointer itself */
487 memset(blame->data2, 0, sizeof(*blame->data2));
488 d = blame->data2;
489 blame->data2 = blame->data1;
490 blame->data1 = d;
492 return NULL;
495 static const struct got_error *
496 blame_open(struct got_blame **blamep, const char *path,
497 struct got_object_id *start_commit_id, struct got_repository *repo,
498 enum got_diff_algorithm diff_algo, got_blame_cb cb, void *arg,
499 got_cancel_cb cancel_cb, void *cancel_arg,
500 int fd1, int fd2, FILE *f1, FILE *f2)
502 const struct got_error *err = NULL;
503 struct got_commit_object *start_commit = NULL, *last_commit = NULL;
504 struct got_object_id *obj_id = NULL;
505 struct got_blob_object *blob = NULL;
506 struct got_blame *blame = NULL;
507 struct got_object_id id;
508 int lineno, have_id = 0;
509 struct got_commit_graph *graph = NULL;
511 *blamep = NULL;
513 err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
514 if (err)
515 goto done;
517 err = got_object_id_by_path(&obj_id, repo, start_commit, path);
518 if (err)
519 goto done;
521 err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
522 if (err)
523 goto done;
525 blame = calloc(1, sizeof(*blame));
526 if (blame == NULL) {
527 err = got_error_from_errno("calloc");
528 goto done;
531 blame->data1 = calloc(1, sizeof(*blame->data1));
532 if (blame->data1 == NULL) {
533 err = got_error_from_errno("calloc");
534 goto done;
536 blame->data2 = calloc(1, sizeof(*blame->data2));
537 if (blame->data2 == NULL) {
538 err = got_error_from_errno("calloc");
539 goto done;
542 blame->f1 = f1;
543 blame->f2 = f2;
544 blame->fd = fd2;
546 err = got_diff_get_config(&blame->cfg, diff_algo, blame_atomize_file,
547 blame);
548 if (err)
549 goto done;
551 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
552 &blame->nlines2, &blame->line_offsets2, blame->data2,
553 blame->cfg, blob);
554 blame->nlines = blame->nlines2;
555 if (err || blame->nlines == 0)
556 goto done;
558 got_object_blob_close(blob);
559 blob = NULL;
561 /* Don't include \n at EOF in the blame line count. */
562 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
563 blame->nlines--;
565 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
566 if (blame->lines == NULL) {
567 err = got_error_from_errno("calloc");
568 goto done;
571 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
572 if (blame->linemap2 == NULL) {
573 err = got_error_from_errno("calloc");
574 goto done;
576 for (lineno = 0; lineno < blame->nlines2; lineno++)
577 blame->linemap2[lineno] = lineno;
579 err = got_commit_graph_open(&graph, path, 1);
580 if (err)
581 goto done;
583 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
584 cancel_cb, cancel_arg);
585 if (err)
586 goto done;
587 for (;;) {
588 err = got_commit_graph_iter_next(&id, graph, repo,
589 cancel_cb, cancel_arg);
590 if (err) {
591 if (err->code == GOT_ERR_ITER_COMPLETED) {
592 err = NULL;
593 break;
595 goto done;
597 have_id = 1;
599 err = blame_commit(blame, &id, path, repo, cb, arg);
600 if (err) {
601 if (err->code == GOT_ERR_ITER_COMPLETED)
602 err = NULL;
603 goto done;
605 if (blame->nannotated == blame->nlines)
606 break;
608 err = flip_files(blame);
609 if (err)
610 goto done;
613 if (have_id && blame->nannotated < blame->nlines) {
614 /* Annotate remaining non-annotated lines with last commit. */
615 err = got_object_open_as_commit(&last_commit, repo, &id);
616 if (err)
617 goto done;
618 for (lineno = 0; lineno < blame->nlines; lineno++) {
619 err = annotate_line(blame, lineno, last_commit, &id,
620 cb, arg);
621 if (err)
622 goto done;
626 done:
627 if (graph)
628 got_commit_graph_close(graph);
629 free(obj_id);
630 if (blob)
631 got_object_blob_close(blob);
632 if (start_commit)
633 got_object_commit_close(start_commit);
634 if (last_commit)
635 got_object_commit_close(last_commit);
636 if (err) {
637 if (blame)
638 blame_close(blame);
639 } else
640 *blamep = blame;
642 return err;
645 const struct got_error *
646 got_blame(const char *path, struct got_object_id *commit_id,
647 struct got_repository *repo, enum got_diff_algorithm diff_algo,
648 got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void* cancel_arg,
649 int fd1, int fd2, FILE *f1, FILE *f2)
651 const struct got_error *err = NULL, *close_err = NULL;
652 struct got_blame *blame;
653 char *abspath;
655 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
656 return got_error_from_errno2("asprintf", path);
658 err = blame_open(&blame, abspath, commit_id, repo, diff_algo,
659 cb, arg, cancel_cb, cancel_arg, fd1, fd2, f1, f2);
660 free(abspath);
661 if (blame)
662 close_err = blame_close(blame);
663 return err ? err : close_err;