2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.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;
55 struct diff_data left_data;
56 struct diff_data right_data;
57 const struct diff_config *cfg;
61 struct got_blame_line *lines; /* one per line */
62 off_t *line_offsets; /* one per line */
66 static const struct got_error *
67 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
68 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
71 const struct got_error *err = NULL;
72 struct got_blame_line *line;
74 if (lineno < 1 || lineno > blame->nlines)
77 line = &blame->lines[lineno - 1];
81 memcpy(&line->id, id, sizeof(line->id));
85 err = cb(arg, blame->nlines, lineno, id);
89 static const struct got_error *
90 blame_changes(struct got_blame *blame, struct diff_result *diff_result,
91 struct got_object_id *commit_id,
92 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
95 const struct got_error *err = NULL;
98 for (i = 0; i < diff_result->chunks.len; i++) {
99 struct diff_chunk *c = diff_chunk_get(diff_result, i);
100 unsigned int right_start, right_count;
104 if (diff_chunk_get_left_count(c) != 0)
107 len = diff_chunk_get_right_count(c);
111 right_start = diff_chunk_get_right_start(c, diff_result, 0);
112 right_count = diff_chunk_get_right_count(c);
114 lineno = right_start + 1;
116 for (ln = lineno; ln < lineno + len; ln++) {
117 err = annotate_line(blame, ln, commit_id, cb, arg);
120 if (blame->nlines == blame->nannotated)
128 static const struct got_error *
129 blame_commit(struct got_blame *blame, struct got_object_id *parent_id,
130 struct got_object_id *id, const char *path, struct got_repository *repo,
131 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
134 const struct got_error *err = NULL;
135 struct got_object *obj = NULL;
136 struct got_object_id *obj_id = NULL;
137 struct got_commit_object *commit = NULL;
138 struct got_blob_object *blob = NULL;
139 struct got_diffreg_result *diffreg_result = NULL;
141 err = got_object_open_as_commit(&commit, repo, parent_id);
145 err = got_object_id_by_path(&obj_id, repo, parent_id, path);
147 if (err->code == GOT_ERR_NO_TREE_ENTRY)
152 err = got_object_open(&obj, repo, obj_id);
156 if (obj->type != GOT_OBJ_TYPE_BLOB) {
157 err = got_error_path(path, GOT_ERR_OBJ_TYPE);
161 err = got_object_blob_open(&blob, repo, obj, 8192);
165 if (fseek(blame->f, 0L, SEEK_SET) == -1) {
166 err = got_ferror(blame->f, GOT_ERR_IO);
170 diff_data_free(&blame->left_data);
171 memset(&blame->left_data, 0, sizeof(blame->left_data));
172 err = got_diff_blob_prepared_file(&diffreg_result, &blame->left_data,
173 blob, &blame->right_data, blame->f, blame->map, blame->filesize,
178 if (diffreg_result->result->chunks.len > 0) {
179 err = blame_changes(blame, diffreg_result->result, id, cb, arg);
181 err = cb(arg, blame->nlines, -1, id);
183 if (diffreg_result) {
184 const struct got_error *free_err;
185 free_err = got_diffreg_result_free_left(diffreg_result);
186 if (free_err && err == NULL)
190 got_object_commit_close(commit);
193 got_object_close(obj);
195 got_object_blob_close(blob);
199 static const struct got_error *
200 blame_close(struct got_blame *blame)
202 const struct got_error *err = NULL;
204 diff_data_free(&blame->left_data);
205 diff_data_free(&blame->right_data);
206 if (blame->map && munmap(blame->map, blame->filesize) == -1)
207 err = got_error_from_errno("munmap");
208 if (blame->f && fclose(blame->f) != 0 && err == NULL)
209 err = got_error_from_errno("fclose");
215 static const struct got_error *
216 blame_open(struct got_blame **blamep, const char *path,
217 struct got_object_id *start_commit_id, struct got_repository *repo,
218 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
219 void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
221 const struct got_error *err = NULL;
222 struct got_object *obj = NULL;
223 struct got_object_id *obj_id = NULL;
224 struct got_blob_object *blob = NULL;
225 struct got_blame *blame = NULL;
226 struct got_object_id *id = NULL, *pid = NULL;
229 struct got_commit_graph *graph = NULL;
233 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
237 err = got_object_open(&obj, repo, obj_id);
241 if (obj->type != GOT_OBJ_TYPE_BLOB) {
242 err = got_error_path(path, GOT_ERR_OBJ_TYPE);
246 err = got_object_blob_open(&blob, repo, obj, 8192);
250 blame = calloc(1, sizeof(*blame));
252 return got_error_from_errno("calloc");
254 blame->f = got_opentemp();
255 if (blame->f == NULL) {
256 err = got_error_from_errno("got_opentemp");
259 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
260 &blame->line_offsets, blame->f, blob);
261 if (err || blame->nlines == 0)
264 blame->cfg = got_diff_get_config(GOT_DIFF_ALGORITHM_PATIENCE),
265 err = got_diff_prepare_file(&blame->f, &blame->map, &created, &size,
266 &blame->right_data, blame->cfg, 0);
270 /* Don't include \n at EOF in the blame line count. */
271 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
274 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
275 if (blame->lines == NULL) {
276 err = got_error_from_errno("calloc");
280 err = got_commit_graph_open(&graph, path, 1);
283 err = got_commit_graph_iter_start(graph, start_commit_id, repo,
284 cancel_cb, cancel_arg);
287 id = start_commit_id;
289 err = got_commit_graph_iter_next(&pid, graph, repo,
290 cancel_cb, cancel_arg);
292 if (err->code == GOT_ERR_ITER_COMPLETED)
297 err = blame_commit(blame, pid, id, path, repo, cb, arg);
299 if (err->code == GOT_ERR_ITER_COMPLETED)
303 if (blame->nannotated == blame->nlines)
309 if (id && blame->nannotated < blame->nlines) {
310 /* Annotate remaining non-annotated lines with last commit. */
311 for (lineno = 1; lineno <= blame->nlines; lineno++) {
312 err = annotate_line(blame, lineno, id, cb, arg);
320 got_commit_graph_close(graph);
323 got_object_close(obj);
325 got_object_blob_close(blob);
335 const struct got_error *
336 got_blame(const char *path, struct got_object_id *commit_id,
337 struct got_repository *repo,
338 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
339 void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
341 const struct got_error *err = NULL, *close_err = NULL;
342 struct got_blame *blame;
345 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
346 return got_error_from_errno2("asprintf", path);
348 err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
349 cancel_cb, cancel_arg);
352 close_err = blame_close(blame);
353 return err ? err : close_err;