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.
18 #include <sys/queue.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_cancel.h"
35 #include "got_commit_graph.h"
36 #include "got_opentemp.h"
38 #include "got_blame.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_diff.h"
46 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
49 struct got_blame_line
{
51 struct got_object_id id
;
55 struct diff_config
*cfg
;
56 int nlines
; /* number of lines in file being blamed */
57 int nannotated
; /* number of lines already annotated */
58 struct got_blame_line
*lines
; /* one per line */
62 * These change with every traversed commit. After diffing
63 * commits N:N-1, in preparation for diffing commits N-1:N-2,
64 * data for commit N is retained and flipped into data for N-1.
67 FILE *f1
; /* older version from commit N-1. */
68 FILE *f2
; /* newer version from commit N. */
80 * Map line numbers of an older version of the file to valid line
81 * numbers in the version of the file being blamed. This map is
82 * updated with each commit we traverse throughout the file's history.
83 * Lines mapped to -1 do not correspond to any line in the version
89 struct diff_data
*data1
;
90 struct diff_data
*data2
;
93 static const struct got_error
*
94 annotate_line(struct got_blame
*blame
, int lineno
,
95 struct got_commit_object
*commit
, struct got_object_id
*id
,
96 got_blame_cb cb
, void *arg
)
98 const struct got_error
*err
= NULL
;
99 struct got_blame_line
*line
;
101 if (lineno
< 0 || lineno
>= blame
->nlines
)
104 line
= &blame
->lines
[lineno
];
108 memcpy(&line
->id
, id
, sizeof(line
->id
));
112 err
= cb(arg
, blame
->nlines
, lineno
+ 1, commit
, id
);
116 static const struct got_error
*
117 blame_changes(struct got_blame
*blame
, struct diff_result
*diff_result
,
118 struct got_commit_object
*commit
, struct got_object_id
*commit_id
,
119 got_blame_cb cb
, void *arg
)
121 const struct got_error
*err
= NULL
;
123 int idx1
= 0, idx2
= 0;
125 for (i
= 0; i
< diff_result
->chunks
.len
&&
126 blame
->nannotated
< blame
->nlines
; i
++) {
127 struct diff_chunk
*c
= diff_chunk_get(diff_result
, i
);
128 unsigned int left_count
, right_count
;
132 * We do not need to worry about idx1/idx2 growing out
133 * of bounds because the diff implementation ensures
134 * that chunk ranges never exceed the number of lines
135 * in the left/right input files.
137 left_count
= diff_chunk_get_left_count(c
);
138 right_count
= diff_chunk_get_right_count(c
);
140 if (left_count
== right_count
) {
141 for (j
= 0; j
< left_count
; j
++) {
142 blame
->linemap1
[idx1
++] =
143 blame
->linemap2
[idx2
++];
148 if (right_count
== 0) {
149 for (j
= 0; j
< left_count
; j
++) {
150 blame
->linemap1
[idx1
++] = -1;
155 for (j
= 0; j
< right_count
; j
++) {
156 int ln
= blame
->linemap2
[idx2
++];
157 err
= annotate_line(blame
, ln
, commit
, commit_id
,
161 if (blame
->nlines
== blame
->nannotated
)
169 static const struct got_error
*
170 blame_prepare_file(FILE *f
, unsigned char **p
, off_t
*size
,
171 int *nlines
, off_t
**line_offsets
, struct diff_data
*diff_data
,
172 const struct diff_config
*cfg
, struct got_blob_object
*blob
)
174 const struct got_error
*err
= NULL
;
175 int diff_flags
= 0, rc
;
177 err
= got_object_blob_dump_to_file(size
, nlines
, line_offsets
,
182 #ifndef GOT_DIFF_NO_MMAP
183 *p
= mmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fileno(f
), 0);
184 if (*p
== MAP_FAILED
)
186 *p
= NULL
; /* fall back on file I/O */
188 /* Allow blaming lines in binary files even though it's useless. */
189 diff_flags
|= DIFF_FLAG_FORCE_TEXT_DATA
;
191 rc
= diff_atomize_file(diff_data
, cfg
, f
, *p
, *size
, diff_flags
);
193 return got_error_set_errno(rc
, "diff_atomize_file");
198 static const struct got_error
*
199 blame_commit(struct got_blame
*blame
, struct got_object_id
*id
,
200 const char *path
, struct got_repository
*repo
,
201 got_blame_cb cb
, void *arg
)
203 const struct got_error
*err
= NULL
;
204 struct got_commit_object
*commit
= NULL
, *pcommit
= NULL
;
205 struct got_object_qid
*pid
= NULL
;
206 struct got_object_id
*pblob_id
= NULL
;
207 struct got_blob_object
*pblob
= NULL
;
208 struct diff_result
*diff_result
= NULL
;
210 err
= got_object_open_as_commit(&commit
, repo
, id
);
214 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
216 got_object_commit_close(commit
);
220 err
= got_object_open_as_commit(&pcommit
, repo
, &pid
->id
);
224 err
= got_object_id_by_path(&pblob_id
, repo
, pcommit
, path
);
226 if (err
->code
== GOT_ERR_NO_TREE_ENTRY
)
231 err
= got_object_open_as_blob(&pblob
, repo
, pblob_id
, 8192, blame
->fd
);
235 err
= blame_prepare_file(blame
->f1
, &blame
->map1
, &blame
->size1
,
236 &blame
->nlines1
, &blame
->line_offsets1
, blame
->data1
,
241 diff_result
= diff_main(blame
->cfg
, blame
->data1
, blame
->data2
);
242 if (diff_result
== NULL
) {
243 err
= got_error_set_errno(ENOMEM
, "malloc");
246 if (diff_result
->rc
!= DIFF_RC_OK
) {
247 err
= got_error_set_errno(diff_result
->rc
, "diff");
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");
259 err
= blame_changes(blame
, diff_result
, commit
, id
, cb
, arg
);
263 err
= cb(arg
, blame
->nlines
, -1, commit
, id
);
266 diff_result_free(diff_result
);
268 got_object_commit_close(commit
);
270 got_object_commit_close(pcommit
);
273 got_object_blob_close(pblob
);
277 static const struct got_error
*
278 blame_close(struct got_blame
*blame
)
280 const struct got_error
*err
= NULL
;
282 diff_data_free(blame
->data1
);
284 diff_data_free(blame
->data2
);
287 if (munmap(blame
->map1
, blame
->size1
) == -1 && err
== NULL
)
288 err
= got_error_from_errno("munmap");
291 if (munmap(blame
->map2
, blame
->size2
) == -1 && err
== NULL
)
292 err
= got_error_from_errno("munmap");
295 free(blame
->line_offsets1
);
296 free(blame
->line_offsets2
);
297 free(blame
->linemap1
);
298 free(blame
->linemap2
);
305 atomize_file(struct diff_data
*d
, FILE *f
, off_t filesize
, int nlines
,
308 int i
, rc
= DIFF_RC_OK
;
309 int embedded_nul
= 0;
311 ARRAYLIST_INIT(d
->atoms
, nlines
);
313 for (i
= 0; i
< nlines
; i
++) {
314 struct diff_atom
*atom
;
315 off_t len
, pos
= line_offsets
[i
];
316 unsigned int hash
= 0;
319 ARRAYLIST_ADD(atom
, d
->atoms
);
326 len
= line_offsets
[i
+ 1] - pos
;
328 len
= filesize
- pos
;
330 if (fseeko(f
, pos
, SEEK_SET
) == -1) {
334 for (j
= 0; j
< len
; j
++) {
338 rc
= EIO
; /* unexpected EOF */
344 hash
= diff_atom_hash_update(hash
, (unsigned char)c
);
350 *atom
= (struct diff_atom
){
353 .at
= NULL
, /* atom data is not memory-mapped */
359 /* File are considered binary if they contain embedded '\0' bytes. */
361 d
->atomizer_flags
|= DIFF_ATOMIZER_FOUND_BINARY_DATA
;
364 ARRAYLIST_FREE(d
->atoms
);
370 atomize_file_mmap(struct diff_data
*d
, unsigned char *p
,
371 off_t filesize
, int nlines
, off_t
*line_offsets
)
373 int i
, rc
= DIFF_RC_OK
;
374 int embedded_nul
= 0;
376 ARRAYLIST_INIT(d
->atoms
, nlines
);
378 for (i
= 0; i
< nlines
; i
++) {
379 struct diff_atom
*atom
;
380 off_t len
, pos
= line_offsets
[i
];
381 unsigned int hash
= 0;
384 ARRAYLIST_ADD(atom
, d
->atoms
);
391 len
= line_offsets
[i
+ 1] - pos
;
393 len
= filesize
- pos
;
395 for (j
= 0; j
< len
; j
++)
396 hash
= diff_atom_hash_update(hash
, p
[pos
+ j
]);
398 if (!embedded_nul
&& memchr(&p
[pos
], '\0', len
) != NULL
)
401 *atom
= (struct diff_atom
){
410 /* File are considered binary if they contain embedded '\0' bytes. */
412 d
->atomizer_flags
|= DIFF_ATOMIZER_FOUND_BINARY_DATA
;
415 ARRAYLIST_FREE(d
->atoms
);
420 /* Implements diff_atomize_func_t */
422 blame_atomize_file(void *arg
, struct diff_data
*d
)
424 struct got_blame
*blame
= arg
;
426 if (d
->f
== blame
->f1
) {
428 return atomize_file_mmap(d
, blame
->map1
,
429 blame
->size1
, blame
->nlines1
,
430 blame
->line_offsets1
);
432 return atomize_file(d
, blame
->f1
, blame
->size1
,
433 blame
->nlines1
, blame
->line_offsets1
);
434 } else if (d
->f
== blame
->f2
) {
435 if (d
->atoms
.len
> 0) {
436 /* Re-use data from previous commit. */
440 return atomize_file_mmap(d
, blame
->map2
,
441 blame
->size2
, blame
->nlines2
,
442 blame
->line_offsets2
);
444 return atomize_file(d
, blame
->f2
, blame
->size2
,
445 blame
->nlines2
, blame
->line_offsets2
);
451 static const struct got_error
*
452 flip_files(struct got_blame
*blame
)
454 const struct got_error
*err
= NULL
;
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
;
467 if (munmap(blame
->map2
, blame
->size2
) == -1)
468 return got_error_from_errno("munmap");
469 blame
->map2
= blame
->map1
;
472 blame
->size2
= blame
->size1
;
474 err
= got_opentemp_truncate(blame
->f2
);
478 blame
->f2
= blame
->f1
;
482 blame
->nlines2
= blame
->nlines1
;
485 diff_data_free(blame
->data2
); /* does not free pointer itself */
486 memset(blame
->data2
, 0, sizeof(*blame
->data2
));
488 blame
->data2
= blame
->data1
;
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 enum got_diff_algorithm diff_algo
, got_blame_cb cb
, void *arg
,
498 got_cancel_cb cancel_cb
, void *cancel_arg
,
499 int fd1
, int fd2
, FILE *f1
, FILE *f2
)
501 const struct got_error
*err
= NULL
;
502 struct got_commit_object
*start_commit
= NULL
, *last_commit
= NULL
;
503 struct got_object_id
*obj_id
= NULL
;
504 struct got_blob_object
*blob
= NULL
;
505 struct got_blame
*blame
= NULL
;
506 struct got_object_id id
;
507 int lineno
, have_id
= 0;
508 struct got_commit_graph
*graph
= NULL
;
512 err
= got_object_open_as_commit(&start_commit
, repo
, start_commit_id
);
516 err
= got_object_id_by_path(&obj_id
, repo
, start_commit
, path
);
520 err
= got_object_open_as_blob(&blob
, repo
, obj_id
, 8192, fd1
);
524 blame
= calloc(1, sizeof(*blame
));
526 err
= got_error_from_errno("calloc");
530 blame
->data1
= calloc(1, sizeof(*blame
->data1
));
531 if (blame
->data1
== NULL
) {
532 err
= got_error_from_errno("calloc");
535 blame
->data2
= calloc(1, sizeof(*blame
->data2
));
536 if (blame
->data2
== NULL
) {
537 err
= got_error_from_errno("calloc");
545 err
= got_diff_get_config(&blame
->cfg
, diff_algo
, blame_atomize_file
,
550 err
= blame_prepare_file(blame
->f2
, &blame
->map2
, &blame
->size2
,
551 &blame
->nlines2
, &blame
->line_offsets2
, blame
->data2
,
553 blame
->nlines
= blame
->nlines2
;
554 if (err
|| blame
->nlines
== 0)
557 got_object_blob_close(blob
);
560 /* Don't include \n at EOF in the blame line count. */
561 if (blame
->line_offsets2
[blame
->nlines
- 1] == blame
->size2
)
564 blame
->lines
= calloc(blame
->nlines
, sizeof(*blame
->lines
));
565 if (blame
->lines
== NULL
) {
566 err
= got_error_from_errno("calloc");
570 blame
->linemap2
= calloc(blame
->nlines2
, sizeof(*blame
->linemap2
));
571 if (blame
->linemap2
== NULL
) {
572 err
= got_error_from_errno("calloc");
575 for (lineno
= 0; lineno
< blame
->nlines2
; lineno
++)
576 blame
->linemap2
[lineno
] = lineno
;
578 err
= got_commit_graph_open(&graph
, path
, 1);
582 err
= got_commit_graph_iter_start(graph
, start_commit_id
, repo
,
583 cancel_cb
, cancel_arg
);
587 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
588 cancel_cb
, cancel_arg
);
590 if (err
->code
== GOT_ERR_ITER_COMPLETED
) {
598 err
= blame_commit(blame
, &id
, path
, repo
, cb
, arg
);
600 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
604 if (blame
->nannotated
== blame
->nlines
)
607 err
= flip_files(blame
);
612 if (have_id
&& blame
->nannotated
< blame
->nlines
) {
613 /* Annotate remaining non-annotated lines with last commit. */
614 err
= got_object_open_as_commit(&last_commit
, repo
, &id
);
617 for (lineno
= 0; lineno
< blame
->nlines
; lineno
++) {
618 err
= annotate_line(blame
, lineno
, last_commit
, &id
,
627 got_commit_graph_close(graph
);
630 got_object_blob_close(blob
);
632 got_object_commit_close(start_commit
);
634 got_object_commit_close(last_commit
);
644 const struct got_error
*
645 got_blame(const char *path
, struct got_object_id
*commit_id
,
646 struct got_repository
*repo
, enum got_diff_algorithm diff_algo
,
647 got_blame_cb cb
, void *arg
, got_cancel_cb cancel_cb
, void* cancel_arg
,
648 int fd1
, int fd2
, FILE *f1
, FILE *f2
)
650 const struct got_error
*err
= NULL
, *close_err
= NULL
;
651 struct got_blame
*blame
;
654 if (asprintf(&abspath
, "%s%s", path
[0] == '/' ? "" : "/", path
) == -1)
655 return got_error_from_errno2("asprintf", path
);
657 err
= blame_open(&blame
, abspath
, commit_id
, repo
, diff_algo
,
658 cb
, arg
, cancel_cb
, cancel_arg
, fd1
, fd2
, f1
, f2
);
661 close_err
= blame_close(blame
);
662 return err
? err
: close_err
;