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_count, right_count;
128 int j;
130 /*
131 * We do not need to worry about idx1/idx2 growing out
132 * of bounds because the diff implementation ensures
133 * that chunk ranges never exceed the number of lines
134 * in the left/right input files.
135 */
136 left_count = diff_chunk_get_left_count(c);
137 right_count = diff_chunk_get_right_count(c);
139 if (left_count == right_count) {
140 for (j = 0; j < left_count; j++) {
141 blame->linemap1[idx1++] =
142 blame->linemap2[idx2++];
144 continue;
147 if (right_count == 0) {
148 for (j = 0; j < left_count; j++) {
149 blame->linemap1[idx1++] = -1;
151 continue;
154 for (j = 0; j < right_count; j++) {
155 int ln = blame->linemap2[idx2++];
156 err = annotate_line(blame, ln, commit_id, 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 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, *pcommit = 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 = STAILQ_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_open_as_commit(&pcommit, repo, pid->id);
220 if (err)
221 goto done;
223 err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
224 if (err) {
225 if (err->code == GOT_ERR_NO_TREE_ENTRY)
226 err = NULL;
227 goto done;
230 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
231 if (err)
232 goto done;
234 blame->f1 = got_opentemp();
235 if (blame->f1 == NULL) {
236 err = got_error_from_errno("got_opentemp");
237 goto done;
240 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
241 &blame->nlines1, &blame->line_offsets1, blame->data1,
242 blame->cfg, pblob);
243 if (err)
244 goto done;
246 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
247 if (diff_result == NULL) {
248 err = got_error_set_errno(ENOMEM, "malloc");
249 goto done;
251 if (diff_result->rc != DIFF_RC_OK) {
252 err = got_error_set_errno(diff_result->rc, "diff");
253 goto done;
255 if (diff_result->chunks.len > 0) {
256 if (blame->nlines1 > 0) {
257 blame->linemap1 = calloc(blame->nlines1,
258 sizeof(*blame->linemap1));
259 if (blame->linemap1 == NULL) {
260 err = got_error_from_errno("malloc");
261 goto done;
264 err = blame_changes(blame, diff_result, id, cb, arg);
265 if (err)
266 goto done;
267 } else if (cb)
268 err = cb(arg, blame->nlines, -1, id);
269 done:
270 if (diff_result)
271 diff_result_free(diff_result);
272 if (commit)
273 got_object_commit_close(commit);
274 if (pcommit)
275 got_object_commit_close(pcommit);
276 free(pblob_id);
277 if (pblob)
278 got_object_blob_close(pblob);
279 return err;
282 static const struct got_error *
283 blame_close(struct got_blame *blame)
285 const struct got_error *err = NULL;
287 diff_data_free(blame->data1);
288 free(blame->data1);
289 diff_data_free(blame->data2);
290 free(blame->data2);
291 if (blame->map1) {
292 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
293 err = got_error_from_errno("munmap");
295 if (blame->map2) {
296 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
297 err = got_error_from_errno("munmap");
299 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
300 err = got_error_from_errno("fclose");
301 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
302 err = got_error_from_errno("fclose");
303 free(blame->lines);
304 free(blame->line_offsets1);
305 free(blame->line_offsets2);
306 free(blame->linemap1);
307 free(blame->linemap2);
308 free(blame->cfg);
309 free(blame);
310 return err;
313 static int
314 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
315 off_t *line_offsets)
317 int i, rc = DIFF_RC_OK;
318 int embedded_nul = 0;
320 ARRAYLIST_INIT(d->atoms, nlines);
322 for (i = 0; i < nlines; i++) {
323 struct diff_atom *atom;
324 off_t len, pos = line_offsets[i];
325 unsigned int hash = 0;
326 int j;
328 ARRAYLIST_ADD(atom, d->atoms);
329 if (atom == NULL) {
330 rc = errno;
331 break;
334 if (i < nlines - 1)
335 len = line_offsets[i + 1] - pos;
336 else
337 len = filesize - pos;
339 if (fseeko(f, pos, SEEK_SET) == -1) {
340 rc = errno;
341 break;
343 for (j = 0; j < len; j++) {
344 int c = fgetc(f);
345 if (c == EOF) {
346 if (feof(f))
347 rc = EIO; /* unexpected EOF */
348 else
349 rc = errno;
350 goto done;
353 hash = diff_atom_hash_update(hash, (unsigned char)c);
355 if (c == '\0')
356 embedded_nul = 1;
359 *atom = (struct diff_atom){
360 .root = d,
361 .pos = pos,
362 .at = NULL, /* atom data is not memory-mapped */
363 .len = len,
364 .hash = hash,
365 };
368 /* File are considered binary if they contain embedded '\0' bytes. */
369 if (embedded_nul)
370 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
371 done:
372 if (rc)
373 ARRAYLIST_FREE(d->atoms);
375 return rc;
378 static int
379 atomize_file_mmap(struct diff_data *d, unsigned char *p,
380 off_t filesize, int nlines, off_t *line_offsets)
382 int i, rc = DIFF_RC_OK;
383 int embedded_nul = 0;
385 ARRAYLIST_INIT(d->atoms, nlines);
387 for (i = 0; i < nlines; i++) {
388 struct diff_atom *atom;
389 off_t len, pos = line_offsets[i];
390 unsigned int hash = 0;
391 int j;
393 ARRAYLIST_ADD(atom, d->atoms);
394 if (atom == NULL) {
395 rc = errno;
396 break;
399 if (i < nlines - 1)
400 len = line_offsets[i + 1] - pos;
401 else
402 len = filesize - pos;
404 for (j = 0; j < len; j++)
405 hash = diff_atom_hash_update(hash, p[pos + j]);
407 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
408 embedded_nul = 1;
410 *atom = (struct diff_atom){
411 .root = d,
412 .pos = pos,
413 .at = &p[pos],
414 .len = len,
415 .hash = hash,
416 };
419 /* File are considered binary if they contain embedded '\0' bytes. */
420 if (embedded_nul)
421 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
423 if (rc)
424 ARRAYLIST_FREE(d->atoms);
426 return rc;
429 /* Implements diff_atomize_func_t */
430 static int
431 blame_atomize_file(void *arg, struct diff_data *d)
433 struct got_blame *blame = arg;
435 if (d->f == blame->f1) {
436 if (blame->map1)
437 return atomize_file_mmap(d, blame->map1,
438 blame->size1, blame->nlines1,
439 blame->line_offsets1);
440 else
441 return atomize_file(d, blame->f1, blame->size1,
442 blame->nlines1, blame->line_offsets1);
443 } else if (d->f == blame->f2) {
444 if (d->atoms.len > 0) {
445 /* Re-use data from previous commit. */
446 return DIFF_RC_OK;
448 if (blame->map2)
449 return atomize_file_mmap(d, blame->map2,
450 blame->size2, blame->nlines2,
451 blame->line_offsets2);
452 else
453 return atomize_file(d, blame->f2, blame->size2,
454 blame->nlines2, blame->line_offsets2);
457 return DIFF_RC_OK;
460 static const struct got_error *
461 close_file2_and_reuse_file1(struct got_blame *blame)
463 struct diff_data *d;
465 free(blame->line_offsets2);
466 blame->line_offsets2 = blame->line_offsets1;
467 blame->line_offsets1 = NULL;
469 free(blame->linemap2);
470 blame->linemap2 = blame->linemap1;
471 blame->linemap1 = NULL;
473 if (blame->map2) {
474 if (munmap(blame->map2, blame->size2) == -1)
475 return got_error_from_errno("munmap");
476 blame->map2 = blame->map1;
477 blame->map1 = NULL;
480 blame->size2 = blame->size1;
481 blame->size1 = 0;
483 if (fclose(blame->f2) == EOF)
484 return got_error_from_errno("fclose");
485 blame->f2 = blame->f1;
486 blame->f1 = NULL;
488 blame->nlines2 = blame->nlines1;
489 blame->nlines1 = 0;
491 diff_data_free(blame->data2); /* does not free pointer itself */
492 memset(blame->data2, 0, sizeof(*blame->data2));
493 d = blame->data2;
494 blame->data2 = blame->data1;
495 blame->data1 = d;
497 return NULL;
500 static const struct got_error *
501 blame_open(struct got_blame **blamep, const char *path,
502 struct got_object_id *start_commit_id, struct got_repository *repo,
503 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
504 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
506 const struct got_error *err = NULL;
507 struct got_commit_object *start_commit = NULL;
508 struct got_object_id *obj_id = NULL;
509 struct got_blob_object *blob = NULL;
510 struct got_blame *blame = NULL;
511 struct got_object_id *id = NULL;
512 int lineno;
513 struct got_commit_graph *graph = NULL;
515 *blamep = NULL;
517 err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
518 if (err)
519 goto done;
521 err = got_object_id_by_path(&obj_id, repo, start_commit, path);
522 if (err)
523 goto done;
525 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
526 if (err)
527 goto done;
529 blame = calloc(1, sizeof(*blame));
530 if (blame == NULL) {
531 err = got_error_from_errno("calloc");
532 goto done;
535 blame->data1 = calloc(1, sizeof(*blame->data1));
536 if (blame->data1 == NULL) {
537 err = got_error_from_errno("calloc");
538 goto done;
540 blame->data2 = calloc(1, sizeof(*blame->data2));
541 if (blame->data2 == NULL) {
542 err = got_error_from_errno("calloc");
543 goto done;
546 blame->f2 = got_opentemp();
547 if (blame->f2 == NULL) {
548 err = got_error_from_errno("got_opentemp");
549 goto done;
551 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
552 blame_atomize_file, blame);
553 if (err)
554 goto done;
556 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
557 &blame->nlines2, &blame->line_offsets2, blame->data2,
558 blame->cfg, blob);
559 blame->nlines = blame->nlines2;
560 if (err || blame->nlines == 0)
561 goto done;
563 got_object_blob_close(blob);
564 blob = NULL;
566 /* Don't include \n at EOF in the blame line count. */
567 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
568 blame->nlines--;
570 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
571 if (blame->lines == NULL) {
572 err = got_error_from_errno("calloc");
573 goto done;
576 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
577 if (blame->linemap2 == NULL) {
578 err = got_error_from_errno("calloc");
579 goto done;
581 for (lineno = 0; lineno < blame->nlines2; lineno++)
582 blame->linemap2[lineno] = lineno;
584 err = got_commit_graph_open(&graph, path, 1);
585 if (err)
586 goto done;
588 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
589 cancel_cb, cancel_arg);
590 if (err)
591 goto done;
592 for (;;) {
593 struct got_object_id *next_id;
594 err = got_commit_graph_iter_next(&next_id, graph, repo,
595 cancel_cb, cancel_arg);
596 if (err) {
597 if (err->code == GOT_ERR_ITER_COMPLETED) {
598 err = NULL;
599 break;
601 goto done;
603 if (next_id) {
604 id = next_id;
605 err = blame_commit(blame, id, path, repo, cb, arg);
606 if (err) {
607 if (err->code == GOT_ERR_ITER_COMPLETED)
608 err = NULL;
609 goto done;
611 if (blame->nannotated == blame->nlines)
612 break;
614 err = close_file2_and_reuse_file1(blame);
615 if (err)
616 goto done;
620 if (id && blame->nannotated < blame->nlines) {
621 /* Annotate remaining non-annotated lines with last commit. */
622 for (lineno = 0; lineno < blame->nlines; lineno++) {
623 err = annotate_line(blame, lineno, id, cb, arg);
624 if (err)
625 goto done;
629 done:
630 if (graph)
631 got_commit_graph_close(graph);
632 free(obj_id);
633 if (blob)
634 got_object_blob_close(blob);
635 if (start_commit)
636 got_object_commit_close(start_commit);
637 if (err) {
638 if (blame)
639 blame_close(blame);
640 } else
641 *blamep = blame;
643 return err;
646 const struct got_error *
647 got_blame(const char *path, struct got_object_id *commit_id,
648 struct got_repository *repo,
649 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
650 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
652 const struct got_error *err = NULL, *close_err = NULL;
653 struct got_blame *blame;
654 char *abspath;
656 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
657 return got_error_from_errno2("asprintf", path);
659 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
660 cancel_cb, cancel_arg);
661 free(abspath);
662 if (blame)
663 close_err = blame_close(blame);
664 return err ? err : close_err;