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>
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"
45 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
48 struct got_blame_line {
50 struct got_object_id id;
56 struct diff_config *cfg;
60 struct got_blame_line *lines; /* one per line */
61 off_t *line_offsets; /* one per line */
65 * Map line numbers of an older version of the file to valid line
66 * numbers in blame->f. This map is updated with each commit we
67 * traverse throughout the file's history.
68 * Lines mapped to -1 do not correspond to any line in blame->f.
74 static const struct got_error *
75 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
76 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
79 const struct got_error *err = NULL;
80 struct got_blame_line *line;
82 if (lineno < 0 || lineno >= blame->nlines)
85 line = &blame->lines[lineno];
89 memcpy(&line->id, id, sizeof(line->id));
93 err = cb(arg, blame->nlines, lineno + 1, id);
97 static const struct got_error *
98 blame_changes(struct got_blame *blame, int *linemap1,
99 struct diff_result *diff_result, struct got_object_id *commit_id,
100 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
103 const struct got_error *err = NULL;
105 int idx1 = 0, idx2 = 0;
107 for (i = 0; i < diff_result->chunks.len &&
108 blame->nannotated < blame->nlines; i++) {
109 struct diff_chunk *c = diff_chunk_get(diff_result, i);
110 unsigned int left_start, left_count;
111 unsigned int right_start, right_count;
115 * We do not need to worry about idx1/idx2 growing out
116 * of bounds because the diff implementation ensures
117 * that chunk ranges never exceed the number of lines
118 * in the left/right input files.
120 left_start = diff_chunk_get_left_start(c, diff_result, 0);
121 left_count = diff_chunk_get_left_count(c);
122 right_start = diff_chunk_get_right_start(c, diff_result, 0);
123 right_count = diff_chunk_get_right_count(c);
125 if (left_count == right_count) {
126 for (j = 0; j < left_count; j++) {
127 linemap1[idx1++] = blame->linemap2[idx2++];
132 if (right_count == 0) {
133 for (j = 0; j < left_count; j++) {
134 linemap1[idx1++] = -1;
139 for (j = 0; j < right_count; j++) {
140 int ln = blame->linemap2[idx2++];
141 err = annotate_line(blame, ln, commit_id, cb, arg);
144 if (blame->nlines == blame->nannotated)
152 static const struct got_error *
153 blame_commit(struct got_blame *blame, struct got_object_id *id,
154 const char *path, struct got_repository *repo,
155 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
158 const struct got_error *err = NULL;
159 struct got_commit_object *commit = NULL;
160 struct got_object_qid *pid = NULL;
161 struct got_object_id *blob_id = NULL, *pblob_id = NULL;
162 struct got_blob_object *blob = NULL, *pblob = NULL;
163 struct got_diffreg_result *diffreg_result = NULL;
164 FILE *f1 = NULL, *f2 = NULL;
166 int nlines1, nlines2;
167 int *linemap1 = NULL;
169 err = got_object_open_as_commit(&commit, repo, id);
173 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
175 got_object_commit_close(commit);
179 err = got_object_id_by_path(&blob_id, repo, id, path);
181 if (err->code == GOT_ERR_NO_TREE_ENTRY)
186 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
192 err = got_error_from_errno("got_opentemp");
195 err = got_object_blob_dump_to_file(&size2, &nlines2, NULL,
200 err = got_object_id_by_path(&pblob_id, repo, pid->id, path);
202 if (err->code == GOT_ERR_NO_TREE_ENTRY)
207 err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
213 err = got_error_from_errno("got_opentemp");
216 err = got_object_blob_dump_to_file(&size1, &nlines1, NULL, f1, pblob);
220 err = got_diff_files(&diffreg_result, f1, "", f2, "",
224 if (diffreg_result->result->chunks.len > 0) {
226 linemap1 = calloc(nlines1, sizeof(*linemap1));
227 if (linemap1 == NULL) {
228 err = got_error_from_errno("malloc");
232 err = blame_changes(blame, linemap1,
233 diffreg_result->result, id, cb, arg);
239 free(blame->linemap2);
240 blame->linemap2 = linemap1;
241 blame->nlines2 = nlines1;
244 err = cb(arg, blame->nlines, -1, id);
246 if (diffreg_result) {
247 const struct got_error *free_err;
248 free_err = got_diffreg_result_free(diffreg_result);
249 if (free_err && err == NULL)
253 got_object_commit_close(commit);
257 got_object_blob_close(blob);
259 got_object_blob_close(pblob);
260 if (f1 && fclose(f1) != 0 && err == NULL)
261 err = got_error_from_errno("fclose");
262 if (f2 && fclose(f2) != 0 && err == NULL)
263 err = got_error_from_errno("fclose");
267 static const struct got_error *
268 blame_close(struct got_blame *blame)
270 const struct got_error *err = NULL;
272 if (blame->f && fclose(blame->f) != 0 && err == NULL)
273 err = got_error_from_errno("fclose");
275 free(blame->linemap2);
281 static const struct got_error *
282 blame_open(struct got_blame **blamep, const char *path,
283 struct got_object_id *start_commit_id, struct got_repository *repo,
284 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
285 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
287 const struct got_error *err = NULL;
288 struct got_object *obj = NULL;
289 struct got_object_id *obj_id = NULL;
290 struct got_blob_object *blob = NULL;
291 struct got_blame *blame = NULL;
292 struct got_object_id *id = NULL;
294 struct got_commit_graph *graph = NULL;
298 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
302 err = got_object_open(&obj, repo, obj_id);
306 if (obj->type != GOT_OBJ_TYPE_BLOB) {
307 err = got_error_path(path, GOT_ERR_OBJ_TYPE);
311 err = got_object_blob_open(&blob, repo, obj, 8192);
315 blame = calloc(1, sizeof(*blame));
317 err = got_error_from_errno("calloc");
321 blame->f = got_opentemp();
322 if (blame->f == NULL) {
323 err = got_error_from_errno("got_opentemp");
326 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
327 &blame->line_offsets, blame->f, blob);
328 if (err || blame->nlines == 0)
331 err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
336 /* Don't include \n at EOF in the blame line count. */
337 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
340 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
341 if (blame->lines == NULL) {
342 err = got_error_from_errno("calloc");
346 blame->nlines2 = blame->nlines;
347 blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
348 if (blame->linemap2 == NULL) {
349 err = got_error_from_errno("calloc");
352 for (lineno = 0; lineno < blame->nlines2; lineno++)
353 blame->linemap2[lineno] = lineno;
355 err = got_commit_graph_open(&graph, path, 1);
359 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
360 cancel_cb, cancel_arg);
364 struct got_object_id *next_id;
365 err = got_commit_graph_iter_next(&next_id, graph, repo,
366 cancel_cb, cancel_arg);
368 if (err->code == GOT_ERR_ITER_COMPLETED) {
376 err = blame_commit(blame, id, path, repo, cb, arg);
378 if (err->code == GOT_ERR_ITER_COMPLETED)
382 if (blame->nannotated == blame->nlines)
387 if (id && blame->nannotated < blame->nlines) {
388 /* Annotate remaining non-annotated lines with last commit. */
389 for (lineno = 0; lineno < blame->nlines; lineno++) {
390 err = annotate_line(blame, lineno, id, cb, arg);
398 got_commit_graph_close(graph);
401 got_object_close(obj);
403 got_object_blob_close(blob);
413 const struct got_error *
414 got_blame(const char *path, struct got_object_id *commit_id,
415 struct got_repository *repo,
416 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
417 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
419 const struct got_error *err = NULL, *close_err = NULL;
420 struct got_blame *blame;
423 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
424 return got_error_from_errno2("asprintf", path);
426 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
427 cancel_cb, cancel_arg);
430 close_err = blame_close(blame);
431 return err ? err : close_err;