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
, struct got_object_id
*id
,
92 const struct got_error
*(*cb
)(void *, int, int, struct got_object_id
*),
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, id
);
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
*),
119 const struct got_error
*err
= NULL
;
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_start
, left_count
;
127 unsigned int right_start
, right_count
;
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.
136 left_start
= diff_chunk_get_left_start(c
, diff_result
, 0);
137 left_count
= diff_chunk_get_left_count(c
);
138 right_start
= diff_chunk_get_right_start(c
, diff_result
, 0);
139 right_count
= diff_chunk_get_right_count(c
);
141 if (left_count
== right_count
) {
142 for (j
= 0; j
< left_count
; j
++) {
143 blame
->linemap1
[idx1
++] =
144 blame
->linemap2
[idx2
++];
149 if (right_count
== 0) {
150 for (j
= 0; j
< left_count
; j
++) {
151 blame
->linemap1
[idx1
++] = -1;
156 for (j
= 0; j
< right_count
; j
++) {
157 int ln
= blame
->linemap2
[idx2
++];
158 err
= annotate_line(blame
, ln
, commit_id
, cb
, arg
);
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 const struct got_error
*(*cb
)(void *, int, int, struct got_object_id
*),
204 const struct got_error
*err
= NULL
;
205 struct got_commit_object
*commit
= NULL
;
206 struct got_object_qid
*pid
= NULL
;
207 struct got_object_id
*pblob_id
= NULL
;
208 struct got_blob_object
*pblob
= NULL
;
209 struct diff_result
*diff_result
= NULL
;
211 err
= got_object_open_as_commit(&commit
, repo
, id
);
215 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
217 got_object_commit_close(commit
);
221 err
= got_object_id_by_path(&pblob_id
, repo
, pid
->id
, 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
, id
, cb
, arg
);
266 err
= cb(arg
, blame
->nlines
, -1, id
);
269 diff_result_free(diff_result
);
271 got_object_commit_close(commit
);
274 got_object_blob_close(pblob
);
278 static const struct got_error
*
279 blame_close(struct got_blame
*blame
)
281 const struct got_error
*err
= NULL
;
283 diff_data_free(blame
->data1
);
285 diff_data_free(blame
->data2
);
288 if (munmap(blame
->map1
, blame
->size1
) == -1 && err
== NULL
)
289 err
= got_error_from_errno("munmap");
292 if (munmap(blame
->map2
, blame
->size2
) == -1 && err
== NULL
)
293 err
= got_error_from_errno("munmap");
295 if (blame
->f1
&& fclose(blame
->f1
) == EOF
&& err
== NULL
)
296 err
= got_error_from_errno("fclose");
297 if (blame
->f2
&& fclose(blame
->f2
) == EOF
&& err
== NULL
)
298 err
= got_error_from_errno("fclose");
300 free(blame
->line_offsets1
);
301 free(blame
->line_offsets2
);
302 free(blame
->linemap1
);
303 free(blame
->linemap2
);
310 atomize_file(struct diff_data
*d
, FILE *f
, off_t filesize
, int nlines
,
313 int i
, rc
= DIFF_RC_OK
;
314 int embedded_nul
= 0;
316 ARRAYLIST_INIT(d
->atoms
, nlines
);
318 for (i
= 0; i
< nlines
; i
++) {
319 struct diff_atom
*atom
;
320 off_t len
, pos
= line_offsets
[i
];
321 unsigned int hash
= 0;
324 ARRAYLIST_ADD(atom
, d
->atoms
);
331 len
= line_offsets
[i
+ 1] - pos
;
333 len
= filesize
- pos
;
335 if (fseeko(f
, pos
, SEEK_SET
) == -1) {
339 for (j
= 0; j
< len
; j
++) {
343 rc
= EIO
; /* unexpected EOF */
349 hash
= diff_atom_hash_update(hash
, (unsigned char)c
);
355 *atom
= (struct diff_atom
){
358 .at
= NULL
, /* atom data is not memory-mapped */
364 /* File are considered binary if they contain embedded '\0' bytes. */
366 d
->atomizer_flags
|= DIFF_ATOMIZER_FOUND_BINARY_DATA
;
369 ARRAYLIST_FREE(d
->atoms
);
375 atomize_file_mmap(struct diff_data
*d
, unsigned char *p
,
376 off_t filesize
, int nlines
, off_t
*line_offsets
)
378 int i
, rc
= DIFF_RC_OK
;
379 int embedded_nul
= 0;
381 ARRAYLIST_INIT(d
->atoms
, nlines
);
383 for (i
= 0; i
< nlines
; i
++) {
384 struct diff_atom
*atom
;
385 off_t len
, pos
= line_offsets
[i
];
386 unsigned int hash
= 0;
389 ARRAYLIST_ADD(atom
, d
->atoms
);
396 len
= line_offsets
[i
+ 1] - pos
;
398 len
= filesize
- pos
;
400 for (j
= 0; j
< len
; j
++)
401 hash
= diff_atom_hash_update(hash
, p
[pos
+ j
]);
403 if (!embedded_nul
&& memchr(&p
[pos
], '\0', len
) != NULL
)
406 *atom
= (struct diff_atom
){
415 /* File are considered binary if they contain embedded '\0' bytes. */
417 d
->atomizer_flags
|= DIFF_ATOMIZER_FOUND_BINARY_DATA
;
420 ARRAYLIST_FREE(d
->atoms
);
425 /* Implements diff_atomize_func_t */
427 blame_atomize_file(void *arg
, struct diff_data
*d
)
429 struct got_blame
*blame
= arg
;
431 if (d
->f
== blame
->f1
) {
433 return atomize_file_mmap(d
, blame
->map1
,
434 blame
->size1
, blame
->nlines1
,
435 blame
->line_offsets1
);
437 return atomize_file(d
, blame
->f1
, blame
->size1
,
438 blame
->nlines1
, blame
->line_offsets1
);
439 } else if (d
->f
== blame
->f2
) {
440 if (d
->atoms
.len
> 0) {
441 /* Re-use data from previous commit. */
445 return atomize_file_mmap(d
, blame
->map2
,
446 blame
->size2
, blame
->nlines2
,
447 blame
->line_offsets2
);
449 return atomize_file(d
, blame
->f2
, blame
->size2
,
450 blame
->nlines2
, blame
->line_offsets2
);
456 static const struct got_error
*
457 close_file2_and_reuse_file1(struct got_blame
*blame
)
461 free(blame
->line_offsets2
);
462 blame
->line_offsets2
= blame
->line_offsets1
;
463 blame
->line_offsets1
= NULL
;
465 free(blame
->linemap2
);
466 blame
->linemap2
= blame
->linemap1
;
467 blame
->linemap1
= NULL
;
470 if (munmap(blame
->map2
, blame
->size2
) == -1)
471 return got_error_from_errno("munmap");
472 blame
->map2
= blame
->map1
;
476 blame
->size2
= blame
->size1
;
479 if (fclose(blame
->f2
) == EOF
)
480 return got_error_from_errno("fclose");
481 blame
->f2
= blame
->f1
;
484 blame
->nlines2
= blame
->nlines1
;
487 diff_data_free(blame
->data2
); /* does not free pointer itself */
488 memset(blame
->data2
, 0, sizeof(*blame
->data2
));
490 blame
->data2
= blame
->data1
;
496 static const struct got_error
*
497 blame_open(struct got_blame
**blamep
, const char *path
,
498 struct got_object_id
*start_commit_id
, struct got_repository
*repo
,
499 const struct got_error
*(*cb
)(void *, int, int, struct got_object_id
*),
500 void *arg
, got_cancel_cb cancel_cb
, void *cancel_arg
)
502 const struct got_error
*err
= 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
= NULL
;
508 struct got_commit_graph
*graph
= NULL
;
512 err
= got_object_id_by_path(&obj_id
, repo
, start_commit_id
, path
);
516 err
= got_object_open_as_blob(&blob
, repo
, obj_id
, 8192);
520 blame
= calloc(1, sizeof(*blame
));
522 err
= got_error_from_errno("calloc");
526 blame
->data1
= calloc(1, sizeof(*blame
->data1
));
527 if (blame
->data1
== NULL
) {
528 err
= got_error_from_errno("calloc");
531 blame
->data2
= calloc(1, sizeof(*blame
->data2
));
532 if (blame
->data2
== NULL
) {
533 err
= got_error_from_errno("calloc");
537 blame
->f2
= got_opentemp();
538 if (blame
->f2
== NULL
) {
539 err
= got_error_from_errno("got_opentemp");
542 err
= got_diff_get_config(&blame
->cfg
, GOT_DIFF_ALGORITHM_PATIENCE
,
543 blame_atomize_file
, blame
);
547 err
= blame_prepare_file(blame
->f2
, &blame
->map2
, &blame
->size2
,
548 &blame
->nlines2
, &blame
->line_offsets2
, blame
->data2
,
550 blame
->nlines
= blame
->nlines2
;
551 if (err
|| blame
->nlines
== 0)
554 got_object_blob_close(blob
);
557 /* Don't include \n at EOF in the blame line count. */
558 if (blame
->line_offsets2
[blame
->nlines
- 1] == blame
->size2
)
561 blame
->lines
= calloc(blame
->nlines
, sizeof(*blame
->lines
));
562 if (blame
->lines
== NULL
) {
563 err
= got_error_from_errno("calloc");
567 blame
->linemap2
= calloc(blame
->nlines2
, sizeof(*blame
->linemap2
));
568 if (blame
->linemap2
== NULL
) {
569 err
= got_error_from_errno("calloc");
572 for (lineno
= 0; lineno
< blame
->nlines2
; lineno
++)
573 blame
->linemap2
[lineno
] = lineno
;
575 err
= got_commit_graph_open(&graph
, path
, 1);
579 err
= got_commit_graph_iter_start(graph
, start_commit_id
, repo
,
580 cancel_cb
, cancel_arg
);
584 struct got_object_id
*next_id
;
585 err
= got_commit_graph_iter_next(&next_id
, graph
, repo
,
586 cancel_cb
, cancel_arg
);
588 if (err
->code
== GOT_ERR_ITER_COMPLETED
) {
596 err
= blame_commit(blame
, id
, path
, repo
, cb
, arg
);
598 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
602 if (blame
->nannotated
== blame
->nlines
)
605 err
= close_file2_and_reuse_file1(blame
);
611 if (id
&& blame
->nannotated
< blame
->nlines
) {
612 /* Annotate remaining non-annotated lines with last commit. */
613 for (lineno
= 0; lineno
< blame
->nlines
; lineno
++) {
614 err
= annotate_line(blame
, lineno
, id
, cb
, arg
);
622 got_commit_graph_close(graph
);
625 got_object_blob_close(blob
);
635 const struct got_error
*
636 got_blame(const char *path
, struct got_object_id
*commit_id
,
637 struct got_repository
*repo
,
638 const struct got_error
*(*cb
)(void *, int, int, struct got_object_id
*),
639 void *arg
, got_cancel_cb cancel_cb
, void* cancel_arg
)
641 const struct got_error
*err
= NULL
, *close_err
= NULL
;
642 struct got_blame
*blame
;
645 if (asprintf(&abspath
, "%s%s", path
[0] == '/' ? "" : "/", path
) == -1)
646 return got_error_from_errno2("asprintf", path
);
648 err
= blame_open(&blame
, abspath
, commit_id
, repo
, cb
, arg
,
649 cancel_cb
, cancel_arg
);
652 close_err
= blame_close(blame
);
653 return err
? err
: close_err
;