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 404c43c4 2018-06-21 stsp #include <sys/stat.h>
20 404c43c4 2018-06-21 stsp #include <sha1.h>
21 404c43c4 2018-06-21 stsp #include <string.h>
22 404c43c4 2018-06-21 stsp #include <stdio.h>
23 404c43c4 2018-06-21 stsp #include <stdlib.h>
24 404c43c4 2018-06-21 stsp #include <time.h>
25 56e0773d 2019-11-28 stsp #include <limits.h>
26 404c43c4 2018-06-21 stsp #include <util.h>
27 404c43c4 2018-06-21 stsp #include <zlib.h>
29 404c43c4 2018-06-21 stsp #include "got_error.h"
30 404c43c4 2018-06-21 stsp #include "got_object.h"
31 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
32 404c43c4 2018-06-21 stsp #include "got_blame.h"
33 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
34 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
36 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
37 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
38 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
39 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
41 404c43c4 2018-06-21 stsp struct got_blame_line {
42 404c43c4 2018-06-21 stsp int annotated;
43 9b94757a 2018-06-21 stsp struct got_object_id id;
46 404c43c4 2018-06-21 stsp struct got_blame {
48 6c4c42e0 2019-06-24 stsp size_t filesize;
50 06ca4d09 2018-11-19 stsp int nannotated;
51 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
52 6c4c42e0 2019-06-24 stsp off_t *line_offsets; /* one per line */
53 c35a7943 2018-07-12 stsp int ncommits;
56 404c43c4 2018-06-21 stsp static const struct got_error *
57 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
58 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
61 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
62 404c43c4 2018-06-21 stsp struct got_blame_line *line;
64 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
65 f2e233d8 2018-11-19 stsp return NULL;
67 404c43c4 2018-06-21 stsp line = &blame->lines[lineno - 1];
68 404c43c4 2018-06-21 stsp if (line->annotated)
69 84451b3e 2018-07-10 stsp return NULL;
71 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
72 404c43c4 2018-06-21 stsp line->annotated = 1;
73 06ca4d09 2018-11-19 stsp blame->nannotated++;
75 84451b3e 2018-07-10 stsp err = cb(arg, blame->nlines, lineno, id);
79 404c43c4 2018-06-21 stsp static const struct got_error *
80 c35a7943 2018-07-12 stsp blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
81 c35a7943 2018-07-12 stsp struct got_object_id *commit_id,
82 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
85 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
86 c35a7943 2018-07-12 stsp struct got_diff_change *change;
88 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
89 c35a7943 2018-07-12 stsp int c = change->cv.c;
90 c35a7943 2018-07-12 stsp int d = change->cv.d;
91 4c9641fd 2019-08-21 stsp int new_lineno = (c < d ? c : d);
92 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
95 c35a7943 2018-07-12 stsp for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
96 4c9641fd 2019-08-21 stsp err = annotate_line(blame, ln, commit_id, cb, arg);
99 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
104 c35a7943 2018-07-12 stsp return NULL;
107 c35a7943 2018-07-12 stsp static const struct got_error *
108 4c9641fd 2019-08-21 stsp blame_commit(struct got_blame *blame, struct got_object_id *parent_id,
109 4c9641fd 2019-08-21 stsp struct got_object_id *id, const char *path, struct got_repository *repo,
110 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
113 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
114 d0275cf7 2019-08-21 stsp struct got_object *obj = NULL;
115 4c9641fd 2019-08-21 stsp struct got_object_id *obj_id = NULL;
116 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
117 4c9641fd 2019-08-21 stsp struct got_blob_object *blob = NULL;
118 404c43c4 2018-06-21 stsp struct got_diff_changes *changes = NULL;
120 4c9641fd 2019-08-21 stsp err = got_object_open_as_commit(&commit, repo, parent_id);
122 8d725ae1 2019-08-18 stsp return err;
124 4c9641fd 2019-08-21 stsp err = got_object_id_by_path(&obj_id, repo, parent_id, path);
126 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
127 4c9641fd 2019-08-21 stsp err = NULL;
131 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
135 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
136 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
140 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
144 4c9641fd 2019-08-21 stsp if (fseek(blame->f, 0L, SEEK_SET) == -1) {
145 4c9641fd 2019-08-21 stsp err = got_ferror(blame->f, GOT_ERR_IO);
149 4c9641fd 2019-08-21 stsp err = got_diff_blob_file_lines_changed(&changes, blob, blame->f,
150 4c9641fd 2019-08-21 stsp blame->filesize);
154 404c43c4 2018-06-21 stsp if (changes) {
155 c35a7943 2018-07-12 stsp err = blame_changes(blame, changes, id, cb, arg);
156 ce7f1bfe 2018-07-13 stsp got_diff_free_changes(changes);
157 d68a0a7d 2018-07-10 stsp } else if (cb)
158 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
160 8d725ae1 2019-08-18 stsp if (commit)
161 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
162 27d434c2 2018-09-15 stsp free(obj_id);
164 404c43c4 2018-06-21 stsp got_object_close(obj);
166 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
167 404c43c4 2018-06-21 stsp return err;
170 fb43ecf1 2019-02-11 stsp static const struct got_error *
171 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
173 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
175 fb43ecf1 2019-02-11 stsp if (blame->f && fclose(blame->f) != 0)
176 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
177 404c43c4 2018-06-21 stsp free(blame->lines);
178 404c43c4 2018-06-21 stsp free(blame);
179 fb43ecf1 2019-02-11 stsp return err;
182 404c43c4 2018-06-21 stsp static const struct got_error *
183 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
184 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
185 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
186 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
188 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
189 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
190 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
191 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
192 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
193 4c9641fd 2019-08-21 stsp struct got_object_id *id = NULL, *pid = NULL;
194 404c43c4 2018-06-21 stsp int lineno;
195 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
197 404c43c4 2018-06-21 stsp *blamep = NULL;
199 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
201 404c43c4 2018-06-21 stsp return err;
203 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
207 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
208 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
212 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
216 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
217 404c43c4 2018-06-21 stsp if (blame == NULL)
218 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
220 404c43c4 2018-06-21 stsp blame->f = got_opentemp();
221 404c43c4 2018-06-21 stsp if (blame->f == NULL) {
222 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
225 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
226 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
227 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
230 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
231 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
232 b02560ec 2019-08-19 stsp blame->nlines--;
234 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
235 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
236 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
240 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
242 293f6400 2018-09-20 stsp return err;
243 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
244 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
247 4c9641fd 2019-08-21 stsp id = start_commit_id;
249 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&pid, graph, repo,
250 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
252 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
253 4c9641fd 2019-08-21 stsp err = NULL;
257 4c9641fd 2019-08-21 stsp err = blame_commit(blame, pid, id, path, repo, cb, arg);
259 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
260 293f6400 2018-09-20 stsp err = NULL;
263 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
269 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
270 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
271 293f6400 2018-09-20 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
272 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
280 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
281 27d434c2 2018-09-15 stsp free(obj_id);
283 404c43c4 2018-06-21 stsp got_object_close(obj);
285 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
288 1828273a 2018-07-09 stsp blame_close(blame);
290 404c43c4 2018-06-21 stsp *blamep = blame;
292 404c43c4 2018-06-21 stsp return err;
295 84451b3e 2018-07-10 stsp const struct got_error *
296 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
297 84451b3e 2018-07-10 stsp struct got_repository *repo,
298 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
299 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
301 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
302 84451b3e 2018-07-10 stsp struct got_blame *blame;
303 84451b3e 2018-07-10 stsp char *abspath;
305 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
306 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
308 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
309 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
310 84451b3e 2018-07-10 stsp free(abspath);
312 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
313 fb43ecf1 2019-02-11 stsp return err ? err : close_err;