2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
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"
44 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
47 struct got_blame_line {
49 struct got_object_id id;
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 */
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.
65 FILE *f1; /* older version from commit N-1. */
66 FILE *f2; /* newer version from commit N. */
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
86 struct diff_data *data1;
87 struct diff_data *data2;
90 static const struct got_error *
91 annotate_line(struct got_blame *blame, int lineno,
92 struct got_commit_object *commit, struct got_object_id *id,
93 got_blame_cb cb, void *arg)
95 const struct got_error *err = NULL;
96 struct got_blame_line *line;
98 if (lineno < 0 || lineno >= blame->nlines)
101 line = &blame->lines[lineno];
105 memcpy(&line->id, id, sizeof(line->id));
109 err = cb(arg, blame->nlines, lineno + 1, commit, id);
113 static const struct got_error *
114 blame_changes(struct got_blame *blame, struct diff_result *diff_result,
115 struct got_commit_object *commit, struct got_object_id *commit_id,
116 got_blame_cb cb, void *arg)
118 const struct got_error *err = NULL;
120 int idx1 = 0, idx2 = 0;
122 for (i = 0; i < diff_result->chunks.len &&
123 blame->nannotated < blame->nlines; i++) {
124 struct diff_chunk *c = diff_chunk_get(diff_result, i);
125 unsigned int left_count, right_count;
129 * We do not need to worry about idx1/idx2 growing out
130 * of bounds because the diff implementation ensures
131 * that chunk ranges never exceed the number of lines
132 * in the left/right input files.
134 left_count = diff_chunk_get_left_count(c);
135 right_count = diff_chunk_get_right_count(c);
137 if (left_count == right_count) {
138 for (j = 0; j < left_count; j++) {
139 blame->linemap1[idx1++] =
140 blame->linemap2[idx2++];
145 if (right_count == 0) {
146 for (j = 0; j < left_count; j++) {
147 blame->linemap1[idx1++] = -1;
152 for (j = 0; j < right_count; j++) {
153 int ln = blame->linemap2[idx2++];
154 err = annotate_line(blame, ln, commit, commit_id,
158 if (blame->nlines == blame->nannotated)
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,
179 #ifndef GOT_DIFF_NO_MMAP
180 *p = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
181 if (*p == MAP_FAILED)
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);
190 return got_error_set_errno(rc, "diff_atomize_file");
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 got_blame_cb cb, void *arg)
200 const struct got_error *err = NULL;
201 struct got_commit_object *commit = NULL, *pcommit = NULL;
202 struct got_object_qid *pid = NULL;
203 struct got_object_id *pblob_id = NULL;
204 struct got_blob_object *pblob = NULL;
205 struct diff_result *diff_result = NULL;
207 err = got_object_open_as_commit(&commit, repo, id);
211 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
213 got_object_commit_close(commit);
217 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
221 err = got_object_id_by_path(&pblob_id, repo, pcommit, path);
223 if (err->code == GOT_ERR_NO_TREE_ENTRY)
228 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
232 blame->f1 = got_opentemp();
233 if (blame->f1 == NULL) {
234 err = got_error_from_errno("got_opentemp");
238 err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
239 &blame->nlines1, &blame->line_offsets1, blame->data1,
244 diff_result = diff_main(blame->cfg, blame->data1, blame->data2);
245 if (diff_result == NULL) {
246 err = got_error_set_errno(ENOMEM, "malloc");
249 if (diff_result->rc != DIFF_RC_OK) {
250 err = got_error_set_errno(diff_result->rc, "diff");
253 if (diff_result->chunks.len > 0) {
254 if (blame->nlines1 > 0) {
255 blame->linemap1 = calloc(blame->nlines1,
256 sizeof(*blame->linemap1));
257 if (blame->linemap1 == NULL) {
258 err = got_error_from_errno("malloc");
262 err = blame_changes(blame, diff_result, commit, id, cb, arg);
266 err = cb(arg, blame->nlines, -1, commit, id);
269 diff_result_free(diff_result);
271 got_object_commit_close(commit);
273 got_object_commit_close(pcommit);
276 got_object_blob_close(pblob);
280 static const struct got_error *
281 blame_close(struct got_blame *blame)
283 const struct got_error *err = NULL;
285 diff_data_free(blame->data1);
287 diff_data_free(blame->data2);
290 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
291 err = got_error_from_errno("munmap");
294 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
295 err = got_error_from_errno("munmap");
297 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
298 err = got_error_from_errno("fclose");
299 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
300 err = got_error_from_errno("fclose");
302 free(blame->line_offsets1);
303 free(blame->line_offsets2);
304 free(blame->linemap1);
305 free(blame->linemap2);
312 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
315 int i, rc = DIFF_RC_OK;
316 int embedded_nul = 0;
318 ARRAYLIST_INIT(d->atoms, nlines);
320 for (i = 0; i < nlines; i++) {
321 struct diff_atom *atom;
322 off_t len, pos = line_offsets[i];
323 unsigned int hash = 0;
326 ARRAYLIST_ADD(atom, d->atoms);
333 len = line_offsets[i + 1] - pos;
335 len = filesize - pos;
337 if (fseeko(f, pos, SEEK_SET) == -1) {
341 for (j = 0; j < len; j++) {
345 rc = EIO; /* unexpected EOF */
351 hash = diff_atom_hash_update(hash, (unsigned char)c);
357 *atom = (struct diff_atom){
360 .at = NULL, /* atom data is not memory-mapped */
366 /* File are considered binary if they contain embedded '\0' bytes. */
368 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
371 ARRAYLIST_FREE(d->atoms);
377 atomize_file_mmap(struct diff_data *d, unsigned char *p,
378 off_t filesize, int nlines, off_t *line_offsets)
380 int i, rc = DIFF_RC_OK;
381 int embedded_nul = 0;
383 ARRAYLIST_INIT(d->atoms, nlines);
385 for (i = 0; i < nlines; i++) {
386 struct diff_atom *atom;
387 off_t len, pos = line_offsets[i];
388 unsigned int hash = 0;
391 ARRAYLIST_ADD(atom, d->atoms);
398 len = line_offsets[i + 1] - pos;
400 len = filesize - pos;
402 for (j = 0; j < len; j++)
403 hash = diff_atom_hash_update(hash, p[pos + j]);
405 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
408 *atom = (struct diff_atom){
417 /* File are considered binary if they contain embedded '\0' bytes. */
419 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
422 ARRAYLIST_FREE(d->atoms);
427 /* Implements diff_atomize_func_t */
429 blame_atomize_file(void *arg, struct diff_data *d)
431 struct got_blame *blame = arg;
433 if (d->f == blame->f1) {
435 return atomize_file_mmap(d, blame->map1,
436 blame->size1, blame->nlines1,
437 blame->line_offsets1);
439 return atomize_file(d, blame->f1, blame->size1,
440 blame->nlines1, blame->line_offsets1);
441 } else if (d->f == blame->f2) {
442 if (d->atoms.len > 0) {
443 /* Re-use data from previous commit. */
447 return atomize_file_mmap(d, blame->map2,
448 blame->size2, blame->nlines2,
449 blame->line_offsets2);
451 return atomize_file(d, blame->f2, blame->size2,
452 blame->nlines2, blame->line_offsets2);
458 static const struct got_error *
459 close_file2_and_reuse_file1(struct got_blame *blame)
463 free(blame->line_offsets2);
464 blame->line_offsets2 = blame->line_offsets1;
465 blame->line_offsets1 = NULL;
467 free(blame->linemap2);
468 blame->linemap2 = blame->linemap1;
469 blame->linemap1 = NULL;
472 if (munmap(blame->map2, blame->size2) == -1)
473 return got_error_from_errno("munmap");
474 blame->map2 = blame->map1;
478 blame->size2 = blame->size1;
481 if (fclose(blame->f2) == EOF)
482 return got_error_from_errno("fclose");
483 blame->f2 = blame->f1;
486 blame->nlines2 = blame->nlines1;
489 diff_data_free(blame->data2); /* does not free pointer itself */
490 memset(blame->data2, 0, sizeof(*blame->data2));
492 blame->data2 = blame->data1;
498 static const struct got_error *
499 blame_open(struct got_blame **blamep, const char *path,
500 struct got_object_id *start_commit_id, struct got_repository *repo,
501 got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
503 const struct got_error *err = NULL;
504 struct got_commit_object *start_commit = NULL, *last_commit = NULL;
505 struct got_object_id *obj_id = NULL;
506 struct got_blob_object *blob = NULL;
507 struct got_blame *blame = NULL;
508 struct got_object_id *id = NULL;
510 struct got_commit_graph *graph = NULL;
514 err = got_object_open_as_commit(&start_commit, repo, start_commit_id);
518 err = got_object_id_by_path(&obj_id, repo, start_commit, path);
522 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
526 blame = calloc(1, sizeof(*blame));
528 err = got_error_from_errno("calloc");
532 blame->data1 = calloc(1, sizeof(*blame->data1));
533 if (blame->data1 == NULL) {
534 err = got_error_from_errno("calloc");
537 blame->data2 = calloc(1, sizeof(*blame->data2));
538 if (blame->data2 == NULL) {
539 err = got_error_from_errno("calloc");
543 blame->f2 = got_opentemp();
544 if (blame->f2 == NULL) {
545 err = got_error_from_errno("got_opentemp");
548 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
549 blame_atomize_file, blame);
553 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
554 &blame->nlines2, &blame->line_offsets2, blame->data2,
556 blame->nlines = blame->nlines2;
557 if (err || blame->nlines == 0)
560 got_object_blob_close(blob);
563 /* Don't include \n at EOF in the blame line count. */
564 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
567 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
568 if (blame->lines == NULL) {
569 err = got_error_from_errno("calloc");
573 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
574 if (blame->linemap2 == NULL) {
575 err = got_error_from_errno("calloc");
578 for (lineno = 0; lineno < blame->nlines2; lineno++)
579 blame->linemap2[lineno] = lineno;
581 err = got_commit_graph_open(&graph, path, 1);
585 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
586 cancel_cb, cancel_arg);
590 struct got_object_id *next_id;
591 err = got_commit_graph_iter_next(&next_id, graph, repo,
592 cancel_cb, cancel_arg);
594 if (err->code == GOT_ERR_ITER_COMPLETED) {
602 err = blame_commit(blame, id, path, repo, cb, arg);
604 if (err->code == GOT_ERR_ITER_COMPLETED)
608 if (blame->nannotated == blame->nlines)
611 err = close_file2_and_reuse_file1(blame);
617 if (id && blame->nannotated < blame->nlines) {
618 /* Annotate remaining non-annotated lines with last commit. */
619 err = got_object_open_as_commit(&last_commit, repo, id);
622 for (lineno = 0; lineno < blame->nlines; lineno++) {
623 err = annotate_line(blame, lineno, last_commit, id,
632 got_commit_graph_close(graph);
635 got_object_blob_close(blob);
637 got_object_commit_close(start_commit);
639 got_object_commit_close(last_commit);
649 const struct got_error *
650 got_blame(const char *path, struct got_object_id *commit_id,
651 struct got_repository *repo, got_blame_cb cb, void *arg,
652 got_cancel_cb cancel_cb, void* cancel_arg)
654 const struct got_error *err = NULL, *close_err = NULL;
655 struct got_blame *blame;
658 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
659 return got_error_from_errno2("asprintf", path);
661 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
662 cancel_cb, cancel_arg);
665 close_err = blame_close(blame);
666 return err ? err : close_err;