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;
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_id_by_path(&pblob_id, repo, pid->id, path);
220 if (err) {
221 if (err->code == GOT_ERR_NO_TREE_ENTRY)
222 err = NULL;
223 goto done;
226 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
227 if (err)
228 goto done;
230 blame->f1 = got_opentemp();
231 if (blame->f1 == NULL) {
232 err = got_error_from_errno("got_opentemp");
233 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, id, cb, arg);
261 if (err)
262 goto done;
263 } else if (cb)
264 err = cb(arg, blame->nlines, -1, id);
265 done:
266 if (diff_result)
267 diff_result_free(diff_result);
268 if (commit)
269 got_object_commit_close(commit);
270 free(pblob_id);
271 if (pblob)
272 got_object_blob_close(pblob);
273 return err;
276 static const struct got_error *
277 blame_close(struct got_blame *blame)
279 const struct got_error *err = NULL;
281 diff_data_free(blame->data1);
282 free(blame->data1);
283 diff_data_free(blame->data2);
284 free(blame->data2);
285 if (blame->map1) {
286 if (munmap(blame->map1, blame->size1) == -1 && err == NULL)
287 err = got_error_from_errno("munmap");
289 if (blame->map2) {
290 if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
291 err = got_error_from_errno("munmap");
293 if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
294 err = got_error_from_errno("fclose");
295 if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
296 err = got_error_from_errno("fclose");
297 free(blame->lines);
298 free(blame->line_offsets1);
299 free(blame->line_offsets2);
300 free(blame->linemap1);
301 free(blame->linemap2);
302 free(blame->cfg);
303 free(blame);
304 return err;
307 static int
308 atomize_file(struct diff_data *d, FILE *f, off_t filesize, int nlines,
309 off_t *line_offsets)
311 int i, rc = DIFF_RC_OK;
312 int embedded_nul = 0;
314 ARRAYLIST_INIT(d->atoms, nlines);
316 for (i = 0; i < nlines; i++) {
317 struct diff_atom *atom;
318 off_t len, pos = line_offsets[i];
319 unsigned int hash = 0;
320 int j;
322 ARRAYLIST_ADD(atom, d->atoms);
323 if (atom == NULL) {
324 rc = errno;
325 break;
328 if (i < nlines - 1)
329 len = line_offsets[i + 1] - pos;
330 else
331 len = filesize - pos;
333 if (fseeko(f, pos, SEEK_SET) == -1) {
334 rc = errno;
335 break;
337 for (j = 0; j < len; j++) {
338 int c = fgetc(f);
339 if (c == EOF) {
340 if (feof(f))
341 rc = EIO; /* unexpected EOF */
342 else
343 rc = errno;
344 goto done;
347 hash = diff_atom_hash_update(hash, (unsigned char)c);
349 if (c == '\0')
350 embedded_nul = 1;
353 *atom = (struct diff_atom){
354 .root = d,
355 .pos = pos,
356 .at = NULL, /* atom data is not memory-mapped */
357 .len = len,
358 .hash = hash,
359 };
362 /* File are considered binary if they contain embedded '\0' bytes. */
363 if (embedded_nul)
364 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
365 done:
366 if (rc)
367 ARRAYLIST_FREE(d->atoms);
369 return rc;
372 static int
373 atomize_file_mmap(struct diff_data *d, unsigned char *p,
374 off_t filesize, int nlines, off_t *line_offsets)
376 int i, rc = DIFF_RC_OK;
377 int embedded_nul = 0;
379 ARRAYLIST_INIT(d->atoms, nlines);
381 for (i = 0; i < nlines; i++) {
382 struct diff_atom *atom;
383 off_t len, pos = line_offsets[i];
384 unsigned int hash = 0;
385 int j;
387 ARRAYLIST_ADD(atom, d->atoms);
388 if (atom == NULL) {
389 rc = errno;
390 break;
393 if (i < nlines - 1)
394 len = line_offsets[i + 1] - pos;
395 else
396 len = filesize - pos;
398 for (j = 0; j < len; j++)
399 hash = diff_atom_hash_update(hash, p[pos + j]);
401 if (!embedded_nul && memchr(&p[pos], '\0', len) != NULL)
402 embedded_nul = 1;
404 *atom = (struct diff_atom){
405 .root = d,
406 .pos = pos,
407 .at = &p[pos],
408 .len = len,
409 .hash = hash,
410 };
413 /* File are considered binary if they contain embedded '\0' bytes. */
414 if (embedded_nul)
415 d->atomizer_flags |= DIFF_ATOMIZER_FOUND_BINARY_DATA;
417 if (rc)
418 ARRAYLIST_FREE(d->atoms);
420 return rc;
423 /* Implements diff_atomize_func_t */
424 static int
425 blame_atomize_file(void *arg, struct diff_data *d)
427 struct got_blame *blame = arg;
429 if (d->f == blame->f1) {
430 if (blame->map1)
431 return atomize_file_mmap(d, blame->map1,
432 blame->size1, blame->nlines1,
433 blame->line_offsets1);
434 else
435 return atomize_file(d, blame->f1, blame->size1,
436 blame->nlines1, blame->line_offsets1);
437 } else if (d->f == blame->f2) {
438 if (d->atoms.len > 0) {
439 /* Re-use data from previous commit. */
440 return DIFF_RC_OK;
442 if (blame->map2)
443 return atomize_file_mmap(d, blame->map2,
444 blame->size2, blame->nlines2,
445 blame->line_offsets2);
446 else
447 return atomize_file(d, blame->f2, blame->size2,
448 blame->nlines2, blame->line_offsets2);
451 return DIFF_RC_OK;
454 static const struct got_error *
455 close_file2_and_reuse_file1(struct got_blame *blame)
457 struct diff_data *d;
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;
474 blame->size2 = blame->size1;
475 blame->size1 = 0;
477 if (fclose(blame->f2) == EOF)
478 return got_error_from_errno("fclose");
479 blame->f2 = blame->f1;
480 blame->f1 = NULL;
482 blame->nlines2 = blame->nlines1;
483 blame->nlines1 = 0;
485 diff_data_free(blame->data2); /* does not free pointer itself */
486 memset(blame->data2, 0, sizeof(*blame->data2));
487 d = blame->data2;
488 blame->data2 = blame->data1;
489 blame->data1 = d;
491 return NULL;
494 static const struct got_error *
495 blame_open(struct got_blame **blamep, const char *path,
496 struct got_object_id *start_commit_id, struct got_repository *repo,
497 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
498 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
500 const struct got_error *err = NULL;
501 struct got_object_id *obj_id = NULL;
502 struct got_blob_object *blob = NULL;
503 struct got_blame *blame = NULL;
504 struct got_object_id *id = NULL;
505 int lineno;
506 struct got_commit_graph *graph = NULL;
508 *blamep = NULL;
510 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
511 if (err)
512 goto done;
514 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
515 if (err)
516 goto done;
518 blame = calloc(1, sizeof(*blame));
519 if (blame == NULL) {
520 err = got_error_from_errno("calloc");
521 goto done;
524 blame->data1 = calloc(1, sizeof(*blame->data1));
525 if (blame->data1 == NULL) {
526 err = got_error_from_errno("calloc");
527 goto done;
529 blame->data2 = calloc(1, sizeof(*blame->data2));
530 if (blame->data2 == NULL) {
531 err = got_error_from_errno("calloc");
532 goto done;
535 blame->f2 = got_opentemp();
536 if (blame->f2 == NULL) {
537 err = got_error_from_errno("got_opentemp");
538 goto done;
540 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
541 blame_atomize_file, blame);
542 if (err)
543 goto done;
545 err = blame_prepare_file(blame->f2, &blame->map2, &blame->size2,
546 &blame->nlines2, &blame->line_offsets2, blame->data2,
547 blame->cfg, blob);
548 blame->nlines = blame->nlines2;
549 if (err || blame->nlines == 0)
550 goto done;
552 got_object_blob_close(blob);
553 blob = NULL;
555 /* Don't include \n at EOF in the blame line count. */
556 if (blame->line_offsets2[blame->nlines - 1] == blame->size2)
557 blame->nlines--;
559 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
560 if (blame->lines == NULL) {
561 err = got_error_from_errno("calloc");
562 goto done;
565 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
566 if (blame->linemap2 == NULL) {
567 err = got_error_from_errno("calloc");
568 goto done;
570 for (lineno = 0; lineno < blame->nlines2; lineno++)
571 blame->linemap2[lineno] = lineno;
573 err = got_commit_graph_open(&graph, path, 1);
574 if (err)
575 goto done;
577 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
578 cancel_cb, cancel_arg);
579 if (err)
580 goto done;
581 for (;;) {
582 struct got_object_id *next_id;
583 err = got_commit_graph_iter_next(&next_id, graph, repo,
584 cancel_cb, cancel_arg);
585 if (err) {
586 if (err->code == GOT_ERR_ITER_COMPLETED) {
587 err = NULL;
588 break;
590 goto done;
592 if (next_id) {
593 id = next_id;
594 err = blame_commit(blame, id, path, repo, cb, arg);
595 if (err) {
596 if (err->code == GOT_ERR_ITER_COMPLETED)
597 err = NULL;
598 goto done;
600 if (blame->nannotated == blame->nlines)
601 break;
603 err = close_file2_and_reuse_file1(blame);
604 if (err)
605 goto done;
609 if (id && blame->nannotated < blame->nlines) {
610 /* Annotate remaining non-annotated lines with last commit. */
611 for (lineno = 0; lineno < blame->nlines; lineno++) {
612 err = annotate_line(blame, lineno, id, cb, arg);
613 if (err)
614 goto done;
618 done:
619 if (graph)
620 got_commit_graph_close(graph);
621 free(obj_id);
622 if (blob)
623 got_object_blob_close(blob);
624 if (err) {
625 if (blame)
626 blame_close(blame);
627 } else
628 *blamep = blame;
630 return err;
633 const struct got_error *
634 got_blame(const char *path, struct got_object_id *commit_id,
635 struct got_repository *repo,
636 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
637 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
639 const struct got_error *err = NULL, *close_err = NULL;
640 struct got_blame *blame;
641 char *abspath;
643 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
644 return got_error_from_errno2("asprintf", path);
646 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
647 cancel_cb, cancel_arg);
648 free(abspath);
649 if (blame)
650 close_err = blame_close(blame);
651 return err ? err : close_err;