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/mman.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <limits.h>
27 #include <zlib.h>
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_cancel.h"
34 #include "got_blame.h"
35 #include "got_commit_graph.h"
36 #include "got_opentemp.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_diff.h"
43 #ifndef MAX
44 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
45 #endif
47 struct got_blame_line {
48 int annotated;
49 struct got_object_id id;
50 };
52 struct got_blame {
53 struct diff_config *cfg;
54 int nlines; /* number of lines in file being blamed */
55 int nannotated; /* number of lines already annotated */
56 struct got_blame_line *lines; /* one per line */
57 int ncommits;
59 /*
60 * These change with every traversed commit. After diffing
61 * commits N:N-1, in preparation for diffing commits N-1:N-2,
62 * data for commit N is retained and flipped into data for N-1.
63 *
64 */
65 FILE *f1; /* older version from commit N-1. */
66 FILE *f2; /* newer version from commit N. */
67 unsigned char *map1;
68 unsigned char *map2;
69 off_t size1;
70 off_t size2;
71 int nlines1;
72 int nlines2;
73 off_t *line_offsets1;
74 off_t *line_offsets2;
76 /*
77 * Map line numbers of an older version of the file to valid line
78 * numbers in the version of the file being blamed. This map is
79 * updated with each commit we traverse throughout the file's history.
80 * Lines mapped to -1 do not correspond to any line in the version
81 * being blamed.
82 */
83 int *linemap1;
84 int *linemap2;
86 struct diff_data *data1;
87 struct diff_data *data2;
88 };
90 static const struct got_error *
91 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
92 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
93 void *arg)
94 {
95 const struct got_error *err = NULL;
96 struct got_blame_line *line;
98 if (lineno < 0 || lineno >= blame->nlines)
99 return NULL;
101 line = &blame->lines[lineno];
102 if (line->annotated)
103 return NULL;
105 memcpy(&line->id, id, sizeof(line->id));
106 line->annotated = 1;
107 blame->nannotated++;
108 if (cb)
109 err = cb(arg, blame->nlines, lineno + 1, id);
110 return err;
113 static const struct got_error *
114 blame_changes(struct got_blame *blame, struct diff_result *diff_result,
115 struct got_object_id *commit_id,
116 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
117 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_id, cb, arg);
156 if (err)
157 return err;
158 if (blame->nlines == blame->nannotated)
159 break;
163 return NULL;
166 static const struct got_error *
167 blame_prepare_file(FILE *f, unsigned char **p, off_t *size,
168 int *nlines, off_t **line_offsets, struct diff_data *diff_data,
169 const struct diff_config *cfg, struct got_blob_object *blob)
171 const struct got_error *err = NULL;
172 int diff_flags = 0, rc;
174 err = got_object_blob_dump_to_file(size, nlines, line_offsets,
175 f, blob);
176 if (err)
177 return err;
179 #ifndef GOT_DIFF_NO_MMAP
180 *p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
181 if (*p == MAP_FAILED)
182 #endif
183 *p = NULL; /* fall back on file I/O */
185 /* Allow blaming lines in binary files even though it's useless. */
186 diff_flags |= DIFF_FLAG_FORCE_TEXT_DATA;
188 rc = diff_atomize_file(diff_data, cfg, f, *p, *size, diff_flags);
189 if (rc)
190 return got_error_set_errno(rc, "diff_atomize_file");
192 return NULL;
195 static const struct got_error *
196 blame_commit(struct got_blame *blame, struct got_object_id *id,
197 const char *path, struct got_repository *repo,
198 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
199 void *arg)
201 const struct got_error *err = NULL;
202 struct got_commit_object *commit = 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_id_by_path(&pblob_id, repo, pid->id, path);
219 if (err) {
220 if (err->code == GOT_ERR_NO_TREE_ENTRY)
221 err = NULL;
222 goto done;
225 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
226 if (err)
227 goto done;
229 blame->f1 = got_opentemp();
230 if (blame->f1 == NULL) {
231 err = got_error_from_errno("got_opentemp");
232 goto done;
235 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
236 &blame->nlines1, &blame->line_offsets1, blame->data1,
237 blame->cfg, pblob);
238 if (err)
239 goto done;
241 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
242 if (diff_result == NULL) {
243 err = got_error_set_errno(ENOMEM, "malloc");
244 goto done;
246 if (diff_result->rc != DIFF_RC_OK) {
247 err = got_error_set_errno(diff_result->rc, "diff");
248 goto done;
250 if (diff_result->chunks.len > 0) {
251 if (blame->nlines1 > 0) {
252 blame->linemap1 = calloc(blame->nlines1,
253 sizeof(*blame->linemap1));
254 if (blame->linemap1 == NULL) {
255 err = got_error_from_errno("malloc");
256 goto done;
259 err = blame_changes(blame, diff_result, id, cb, arg);
260 if (err)
261 goto done;
262 } else if (cb)
263 err = cb(arg, blame->nlines, -1, id);
264 done:
265 if (diff_result)
266 diff_result_free(diff_result);
267 if (commit)
268 got_object_commit_close(commit);
269 free(pblob_id);
270 if (pblob)
271 got_object_blob_close(pblob);
272 return err;
275 static const struct got_error *
276 blame_close(struct got_blame *blame)
278 const struct got_error *err = NULL;
280 diff_data_free(blame->data1);
281 free(blame->data1);
282 diff_data_free(blame->data2);
283 free(blame->data2);
284 if (blame->map1) {
285 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
286 err = got_error_from_errno("munmap");
288 if (blame->map2) {
289 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
290 err = got_error_from_errno("munmap");
292 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
293 err = got_error_from_errno("fclose");
294 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
295 err = got_error_from_errno("fclose");
296 free(blame->lines);
297 free(blame->line_offsets1);
298 free(blame->line_offsets2);
299 free(blame->linemap1);
300 free(blame->linemap2);
301 free(blame->cfg);
302 free(blame);
303 return err;
306 static int
307 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
308 off_t *line_offsets)
310 int i, rc = DIFF_RC_OK;
311 int embedded_nul = 0;
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 if (c == '\0')
349 embedded_nul = 1;
352 *atom = (struct diff_atom){
353 .root = d,
354 .pos = pos,
355 .at = NULL, /* atom data is not memory-mapped */
356 .len = len,
357 .hash = hash,
358 };
361 /* File are considered binary if they contain embedded '\0' bytes. */
362 if (embedded_nul)
363 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
364 done:
365 if (rc)
366 ARRAYLIST_FREE(d->atoms);
368 return rc;
371 static int
372 atomize_file_mmap(struct diff_data *d, unsigned char *p,
373 off_t filesize, int nlines, off_t *line_offsets)
375 int i, rc = DIFF_RC_OK;
376 int embedded_nul = 0;
378 ARRAYLIST_INIT(d->atoms, nlines);
380 for (i = 0; i < nlines; i++) {
381 struct diff_atom *atom;
382 off_t len, pos = line_offsets[i];
383 unsigned int hash = 0;
384 int j;
386 ARRAYLIST_ADD(atom, d->atoms);
387 if (atom == NULL) {
388 rc = errno;
389 break;
392 if (i < nlines - 1)
393 len = line_offsets[i + 1] - pos;
394 else
395 len = filesize - pos;
397 for (j = 0; j < len; j++)
398 hash = diff_atom_hash_update(hash, p[pos + j]);
400 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
401 embedded_nul = 1;
403 *atom = (struct diff_atom){
404 .root = d,
405 .pos = pos,
406 .at = &p[pos],
407 .len = len,
408 .hash = hash,
409 };
412 /* File are considered binary if they contain embedded '\0' bytes. */
413 if (embedded_nul)
414 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
416 if (rc)
417 ARRAYLIST_FREE(d->atoms);
419 return rc;
422 /* Implements diff_atomize_func_t */
423 static int
424 blame_atomize_file(void *arg, struct diff_data *d)
426 struct got_blame *blame = arg;
428 if (d->f == blame->f1) {
429 if (blame->map1)
430 return atomize_file_mmap(d, blame->map1,
431 blame->size1, blame->nlines1,
432 blame->line_offsets1);
433 else
434 return atomize_file(d, blame->f1, blame->size1,
435 blame->nlines1, blame->line_offsets1);
436 } else if (d->f == blame->f2) {
437 if (d->atoms.len > 0) {
438 /* Re-use data from previous commit. */
439 return DIFF_RC_OK;
441 if (blame->map2)
442 return atomize_file_mmap(d, blame->map2,
443 blame->size2, blame->nlines2,
444 blame->line_offsets2);
445 else
446 return atomize_file(d, blame->f2, blame->size2,
447 blame->nlines2, blame->line_offsets2);
450 return DIFF_RC_OK;
453 static const struct got_error *
454 close_file2_and_reuse_file1(struct got_blame *blame)
456 struct diff_data *d;
458 free(blame->line_offsets2);
459 blame->line_offsets2 = blame->line_offsets1;
460 blame->line_offsets1 = NULL;
462 free(blame->linemap2);
463 blame->linemap2 = blame->linemap1;
464 blame->linemap1 = NULL;
466 if (blame->map2) {
467 if (munmap(blame->map2, blame->size2) == -1)
468 return got_error_from_errno("munmap");
469 blame->map2 = blame->map1;
470 blame->map1 = NULL;
473 blame->size2 = blame->size1;
474 blame->size1 = 0;
476 if (fclose(blame->f2) == EOF)
477 return got_error_from_errno("fclose");
478 blame->f2 = blame->f1;
479 blame->f1 = NULL;
481 blame->nlines2 = blame->nlines1;
482 blame->nlines1 = 0;
484 diff_data_free(blame->data2); /* does not free pointer itself */
485 memset(blame->data2, 0, sizeof(*blame->data2));
486 d = blame->data2;
487 blame->data2 = blame->data1;
488 blame->data1 = d;
490 return NULL;
493 static const struct got_error *
494 blame_open(struct got_blame **blamep, const char *path,
495 struct got_object_id *start_commit_id, struct got_repository *repo,
496 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
497 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
499 const struct got_error *err = NULL;
500 struct got_object_id *obj_id = NULL;
501 struct got_blob_object *blob = NULL;
502 struct got_blame *blame = NULL;
503 struct got_object_id *id = NULL;
504 int lineno;
505 struct got_commit_graph *graph = NULL;
507 *blamep = NULL;
509 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
510 if (err)
511 goto done;
513 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
514 if (err)
515 goto done;
517 blame = calloc(1, sizeof(*blame));
518 if (blame == NULL) {
519 err = got_error_from_errno("calloc");
520 goto done;
523 blame->data1 = calloc(1, sizeof(*blame->data1));
524 if (blame->data1 == NULL) {
525 err = got_error_from_errno("calloc");
526 goto done;
528 blame->data2 = calloc(1, sizeof(*blame->data2));
529 if (blame->data2 == NULL) {
530 err = got_error_from_errno("calloc");
531 goto done;
534 blame->f2 = got_opentemp();
535 if (blame->f2 == NULL) {
536 err = got_error_from_errno("got_opentemp");
537 goto done;
539 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
540 blame_atomize_file, blame);
541 if (err)
542 goto done;
544 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
545 &blame->nlines2, &blame->line_offsets2, blame->data2,
546 blame->cfg, blob);
547 blame->nlines = blame->nlines2;
548 if (err || blame->nlines == 0)
549 goto done;
551 got_object_blob_close(blob);
552 blob = NULL;
554 /* Don't include \n at EOF in the blame line count. */
555 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
556 blame->nlines--;
558 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
559 if (blame->lines == NULL) {
560 err = got_error_from_errno("calloc");
561 goto done;
564 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
565 if (blame->linemap2 == NULL) {
566 err = got_error_from_errno("calloc");
567 goto done;
569 for (lineno = 0; lineno < blame->nlines2; lineno++)
570 blame->linemap2[lineno] = lineno;
572 err = got_commit_graph_open(&graph, path, 1);
573 if (err)
574 goto done;
576 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
577 cancel_cb, cancel_arg);
578 if (err)
579 goto done;
580 for (;;) {
581 struct got_object_id *next_id;
582 err = got_commit_graph_iter_next(&next_id, graph, repo,
583 cancel_cb, cancel_arg);
584 if (err) {
585 if (err->code == GOT_ERR_ITER_COMPLETED) {
586 err = NULL;
587 break;
589 goto done;
591 if (next_id) {
592 id = next_id;
593 err = blame_commit(blame, id, path, repo, cb, arg);
594 if (err) {
595 if (err->code == GOT_ERR_ITER_COMPLETED)
596 err = NULL;
597 goto done;
599 if (blame->nannotated == blame->nlines)
600 break;
602 err = close_file2_and_reuse_file1(blame);
603 if (err)
604 goto done;
608 if (id && blame->nannotated < blame->nlines) {
609 /* Annotate remaining non-annotated lines with last commit. */
610 for (lineno = 0; lineno < blame->nlines; lineno++) {
611 err = annotate_line(blame, lineno, id, cb, arg);
612 if (err)
613 goto done;
617 done:
618 if (graph)
619 got_commit_graph_close(graph);
620 free(obj_id);
621 if (blob)
622 got_object_blob_close(blob);
623 if (err) {
624 if (blame)
625 blame_close(blame);
626 } else
627 *blamep = blame;
629 return err;
632 const struct got_error *
633 got_blame(const char *path, struct got_object_id *commit_id,
634 struct got_repository *repo,
635 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
636 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
638 const struct got_error *err = NULL, *close_err = NULL;
639 struct got_blame *blame;
640 char *abspath;
642 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
643 return got_error_from_errno2("asprintf", path);
645 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
646 cancel_cb, cancel_arg);
647 free(abspath);
648 if (blame)
649 close_err = blame_close(blame);
650 return err ? err : close_err;