2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 404c43c4 2018-06-21 stsp * Permission to use, copy, modify, and distribute this software for any
5 404c43c4 2018-06-21 stsp * purpose with or without fee is hereby granted, provided that the above
6 404c43c4 2018-06-21 stsp * copyright notice and this permission notice appear in all copies.
8 404c43c4 2018-06-21 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 404c43c4 2018-06-21 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 404c43c4 2018-06-21 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 404c43c4 2018-06-21 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 404c43c4 2018-06-21 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 404c43c4 2018-06-21 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 404c43c4 2018-06-21 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 404c43c4 2018-06-21 stsp #include <sys/queue.h>
18 fe621944 2020-11-10 stsp #include <sys/mman.h>
19 404c43c4 2018-06-21 stsp #include <sys/stat.h>
21 404c43c4 2018-06-21 stsp #include <sha1.h>
22 404c43c4 2018-06-21 stsp #include <string.h>
23 fe621944 2020-11-10 stsp #include <stdbool.h>
24 404c43c4 2018-06-21 stsp #include <stdio.h>
25 404c43c4 2018-06-21 stsp #include <stdlib.h>
26 404c43c4 2018-06-21 stsp #include <time.h>
27 56e0773d 2019-11-28 stsp #include <limits.h>
28 404c43c4 2018-06-21 stsp #include <util.h>
29 404c43c4 2018-06-21 stsp #include <zlib.h>
31 404c43c4 2018-06-21 stsp #include "got_error.h"
32 404c43c4 2018-06-21 stsp #include "got_object.h"
33 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
34 404c43c4 2018-06-21 stsp #include "got_blame.h"
35 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
36 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
38 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
39 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
40 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
41 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
44 fe621944 2020-11-10 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
47 404c43c4 2018-06-21 stsp struct got_blame_line {
48 404c43c4 2018-06-21 stsp int annotated;
49 9b94757a 2018-06-21 stsp struct got_object_id id;
52 404c43c4 2018-06-21 stsp struct got_blame {
55 fe621944 2020-11-10 stsp struct diff_data left_data;
56 fe621944 2020-11-10 stsp struct diff_data right_data;
57 fe621944 2020-11-10 stsp const struct diff_config *cfg;
58 6c4c42e0 2019-06-24 stsp size_t filesize;
60 06ca4d09 2018-11-19 stsp int nannotated;
61 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
62 6c4c42e0 2019-06-24 stsp off_t *line_offsets; /* one per line */
63 c35a7943 2018-07-12 stsp int ncommits;
66 404c43c4 2018-06-21 stsp static const struct got_error *
67 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
68 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
71 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
72 404c43c4 2018-06-21 stsp struct got_blame_line *line;
74 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
75 f2e233d8 2018-11-19 stsp return NULL;
77 404c43c4 2018-06-21 stsp line = &blame->lines[lineno - 1];
78 404c43c4 2018-06-21 stsp if (line->annotated)
79 84451b3e 2018-07-10 stsp return NULL;
81 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
82 404c43c4 2018-06-21 stsp line->annotated = 1;
83 06ca4d09 2018-11-19 stsp blame->nannotated++;
85 84451b3e 2018-07-10 stsp err = cb(arg, blame->nlines, lineno, id);
89 404c43c4 2018-06-21 stsp static const struct got_error *
90 fe621944 2020-11-10 stsp blame_changes(struct got_blame *blame, struct diff_result *diff_result,
91 c35a7943 2018-07-12 stsp struct got_object_id *commit_id,
92 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
95 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
98 fe621944 2020-11-10 stsp for (i = 0; i < diff_result->chunks.len; i++) {
99 fe621944 2020-11-10 stsp struct diff_chunk *c = diff_chunk_get(diff_result, i);
100 fe621944 2020-11-10 stsp unsigned int right_start, right_count;
101 fe621944 2020-11-10 stsp int lineno, len;
104 fe621944 2020-11-10 stsp if (diff_chunk_get_left_count(c) != 0)
107 fe621944 2020-11-10 stsp len = diff_chunk_get_right_count(c);
108 fe621944 2020-11-10 stsp if (len == 0)
111 fe621944 2020-11-10 stsp right_start = diff_chunk_get_right_start(c, diff_result, 0);
112 fe621944 2020-11-10 stsp right_count = diff_chunk_get_right_count(c);
114 fe621944 2020-11-10 stsp lineno = right_start + 1;
115 fe621944 2020-11-10 stsp len = right_count;
116 fe621944 2020-11-10 stsp for (ln = lineno; ln < lineno + len; ln++) {
117 4c9641fd 2019-08-21 stsp err = annotate_line(blame, ln, commit_id, cb, arg);
119 c35a7943 2018-07-12 stsp return err;
120 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
125 c35a7943 2018-07-12 stsp return NULL;
128 c35a7943 2018-07-12 stsp static const struct got_error *
129 4c9641fd 2019-08-21 stsp blame_commit(struct got_blame *blame, struct got_object_id *parent_id,
130 4c9641fd 2019-08-21 stsp struct got_object_id *id, const char *path, struct got_repository *repo,
131 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
134 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
135 d0275cf7 2019-08-21 stsp struct got_object *obj = NULL;
136 4c9641fd 2019-08-21 stsp struct got_object_id *obj_id = NULL;
137 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
138 4c9641fd 2019-08-21 stsp struct got_blob_object *blob = NULL;
139 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
141 4c9641fd 2019-08-21 stsp err = got_object_open_as_commit(&commit, repo, parent_id);
143 8d725ae1 2019-08-18 stsp return err;
145 4c9641fd 2019-08-21 stsp err = got_object_id_by_path(&obj_id, repo, parent_id, path);
147 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
148 4c9641fd 2019-08-21 stsp err = NULL;
152 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
156 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
157 2c2d5c5f 2020-06-07 stsp err = got_error_path(path, GOT_ERR_OBJ_TYPE);
161 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
165 4c9641fd 2019-08-21 stsp if (fseek(blame->f, 0L, SEEK_SET) == -1) {
166 4c9641fd 2019-08-21 stsp err = got_ferror(blame->f, GOT_ERR_IO);
170 fe621944 2020-11-10 stsp diff_data_free(&blame->left_data);
171 fe621944 2020-11-10 stsp memset(&blame->left_data, 0, sizeof(blame->left_data));
172 fe621944 2020-11-10 stsp err = got_diff_blob_prepared_file(&diffreg_result, &blame->left_data,
173 fe621944 2020-11-10 stsp blob, &blame->right_data, blame->f, blame->map, blame->filesize,
174 fe621944 2020-11-10 stsp blame->cfg, 0);
178 fe621944 2020-11-10 stsp if (diffreg_result->result->chunks.len > 0) {
179 fe621944 2020-11-10 stsp err = blame_changes(blame, diffreg_result->result, id, cb, arg);
180 d68a0a7d 2018-07-10 stsp } else if (cb)
181 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
183 fe621944 2020-11-10 stsp if (diffreg_result) {
184 fe621944 2020-11-10 stsp const struct got_error *free_err;
185 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free_left(diffreg_result);
186 fe621944 2020-11-10 stsp if (free_err && err == NULL)
187 fe621944 2020-11-10 stsp err = free_err;
189 8d725ae1 2019-08-18 stsp if (commit)
190 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
191 27d434c2 2018-09-15 stsp free(obj_id);
193 404c43c4 2018-06-21 stsp got_object_close(obj);
195 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
196 404c43c4 2018-06-21 stsp return err;
199 fb43ecf1 2019-02-11 stsp static const struct got_error *
200 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
202 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
204 fe621944 2020-11-10 stsp diff_data_free(&blame->left_data);
205 fe621944 2020-11-10 stsp diff_data_free(&blame->right_data);
206 fe621944 2020-11-10 stsp if (blame->map && munmap(blame->map, blame->filesize) == -1)
207 fe621944 2020-11-10 stsp err = got_error_from_errno("munmap");
208 fe621944 2020-11-10 stsp if (blame->f && fclose(blame->f) != 0 && err == NULL)
209 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
210 404c43c4 2018-06-21 stsp free(blame->lines);
211 404c43c4 2018-06-21 stsp free(blame);
212 fb43ecf1 2019-02-11 stsp return err;
215 404c43c4 2018-06-21 stsp static const struct got_error *
216 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
217 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
218 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
219 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
221 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
222 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
223 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
224 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
225 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
226 4c9641fd 2019-08-21 stsp struct got_object_id *id = NULL, *pid = NULL;
227 fe621944 2020-11-10 stsp int lineno, created;
228 fe621944 2020-11-10 stsp size_t size;
229 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
231 404c43c4 2018-06-21 stsp *blamep = NULL;
233 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
235 404c43c4 2018-06-21 stsp return err;
237 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
241 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
242 2c2d5c5f 2020-06-07 stsp err = got_error_path(path, GOT_ERR_OBJ_TYPE);
246 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
250 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
251 404c43c4 2018-06-21 stsp if (blame == NULL)
252 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
254 404c43c4 2018-06-21 stsp blame->f = got_opentemp();
255 404c43c4 2018-06-21 stsp if (blame->f == NULL) {
256 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
259 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
260 62d463ca 2020-10-20 naddy &blame->line_offsets, blame->f, blob);
261 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
264 fe621944 2020-11-10 stsp blame->cfg = got_diff_get_config(GOT_DIFF_ALGORITHM_PATIENCE),
265 fe621944 2020-11-10 stsp err = got_diff_prepare_file(&blame->f, &blame->map, &created, &size,
266 fe621944 2020-11-10 stsp &blame->right_data, blame->cfg, 0);
270 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
271 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
272 b02560ec 2019-08-19 stsp blame->nlines--;
274 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
275 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
276 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
280 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
282 293f6400 2018-09-20 stsp return err;
283 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
284 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
287 4c9641fd 2019-08-21 stsp id = start_commit_id;
289 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&pid, graph, repo,
290 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
292 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
293 4c9641fd 2019-08-21 stsp err = NULL;
297 4c9641fd 2019-08-21 stsp err = blame_commit(blame, pid, id, path, repo, cb, arg);
299 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
300 293f6400 2018-09-20 stsp err = NULL;
303 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
309 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
310 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
311 293f6400 2018-09-20 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
312 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
320 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
321 27d434c2 2018-09-15 stsp free(obj_id);
323 404c43c4 2018-06-21 stsp got_object_close(obj);
325 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
328 1828273a 2018-07-09 stsp blame_close(blame);
330 404c43c4 2018-06-21 stsp *blamep = blame;
332 404c43c4 2018-06-21 stsp return err;
335 84451b3e 2018-07-10 stsp const struct got_error *
336 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
337 84451b3e 2018-07-10 stsp struct got_repository *repo,
338 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
339 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
341 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
342 84451b3e 2018-07-10 stsp struct got_blame *blame;
343 84451b3e 2018-07-10 stsp char *abspath;
345 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
346 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
348 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
349 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
350 84451b3e 2018-07-10 stsp free(abspath);
352 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
353 fb43ecf1 2019-02-11 stsp return err ? err : close_err;