Blame


1 404c43c4 2018-06-21 stsp /*
2 4c9641fd 2019-08-21 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 404c43c4 2018-06-21 stsp *
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.
7 404c43c4 2018-06-21 stsp *
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.
15 404c43c4 2018-06-21 stsp */
16 404c43c4 2018-06-21 stsp
17 404c43c4 2018-06-21 stsp #include <sys/queue.h>
18 404c43c4 2018-06-21 stsp #include <sys/stat.h>
19 404c43c4 2018-06-21 stsp
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 404c43c4 2018-06-21 stsp #include <util.h>
26 404c43c4 2018-06-21 stsp #include <zlib.h>
27 404c43c4 2018-06-21 stsp
28 404c43c4 2018-06-21 stsp #include "got_error.h"
29 404c43c4 2018-06-21 stsp #include "got_object.h"
30 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
31 404c43c4 2018-06-21 stsp #include "got_blame.h"
32 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
33 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
34 404c43c4 2018-06-21 stsp
35 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
36 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
37 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
38 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
39 404c43c4 2018-06-21 stsp
40 404c43c4 2018-06-21 stsp struct got_blame_line {
41 404c43c4 2018-06-21 stsp int annotated;
42 9b94757a 2018-06-21 stsp struct got_object_id id;
43 404c43c4 2018-06-21 stsp };
44 404c43c4 2018-06-21 stsp
45 404c43c4 2018-06-21 stsp struct got_blame {
46 404c43c4 2018-06-21 stsp FILE *f;
47 6c4c42e0 2019-06-24 stsp size_t filesize;
48 6fcac457 2018-11-19 stsp int nlines;
49 06ca4d09 2018-11-19 stsp int nannotated;
50 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
51 6c4c42e0 2019-06-24 stsp off_t *line_offsets; /* one per line */
52 c35a7943 2018-07-12 stsp int ncommits;
53 404c43c4 2018-06-21 stsp };
54 404c43c4 2018-06-21 stsp
55 404c43c4 2018-06-21 stsp static const struct got_error *
56 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
57 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
58 84451b3e 2018-07-10 stsp void *arg)
59 404c43c4 2018-06-21 stsp {
60 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
61 404c43c4 2018-06-21 stsp struct got_blame_line *line;
62 404c43c4 2018-06-21 stsp
63 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
64 f2e233d8 2018-11-19 stsp return NULL;
65 404c43c4 2018-06-21 stsp
66 404c43c4 2018-06-21 stsp line = &blame->lines[lineno - 1];
67 404c43c4 2018-06-21 stsp if (line->annotated)
68 84451b3e 2018-07-10 stsp return NULL;
69 404c43c4 2018-06-21 stsp
70 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
71 404c43c4 2018-06-21 stsp line->annotated = 1;
72 06ca4d09 2018-11-19 stsp blame->nannotated++;
73 84451b3e 2018-07-10 stsp if (cb)
74 84451b3e 2018-07-10 stsp err = cb(arg, blame->nlines, lineno, id);
75 84451b3e 2018-07-10 stsp return err;
76 404c43c4 2018-06-21 stsp }
77 404c43c4 2018-06-21 stsp
78 404c43c4 2018-06-21 stsp static const struct got_error *
79 c35a7943 2018-07-12 stsp blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
80 c35a7943 2018-07-12 stsp struct got_object_id *commit_id,
81 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
82 c35a7943 2018-07-12 stsp void *arg)
83 c35a7943 2018-07-12 stsp {
84 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
85 c35a7943 2018-07-12 stsp struct got_diff_change *change;
86 c35a7943 2018-07-12 stsp
87 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
88 c35a7943 2018-07-12 stsp int c = change->cv.c;
89 c35a7943 2018-07-12 stsp int d = change->cv.d;
90 4c9641fd 2019-08-21 stsp int new_lineno = (c < d ? c : d);
91 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
92 c35a7943 2018-07-12 stsp int ln;
93 c35a7943 2018-07-12 stsp
94 c35a7943 2018-07-12 stsp for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
95 4c9641fd 2019-08-21 stsp err = annotate_line(blame, ln, commit_id, cb, arg);
96 c35a7943 2018-07-12 stsp if (err)
97 c35a7943 2018-07-12 stsp return err;
98 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
99 4c9641fd 2019-08-21 stsp break;
100 c35a7943 2018-07-12 stsp }
101 c35a7943 2018-07-12 stsp }
102 c35a7943 2018-07-12 stsp
103 c35a7943 2018-07-12 stsp return NULL;
104 c35a7943 2018-07-12 stsp }
105 c35a7943 2018-07-12 stsp
106 c35a7943 2018-07-12 stsp static const struct got_error *
107 4c9641fd 2019-08-21 stsp blame_commit(struct got_blame *blame, struct got_object_id *parent_id,
108 4c9641fd 2019-08-21 stsp struct got_object_id *id, const char *path, struct got_repository *repo,
109 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
110 84451b3e 2018-07-10 stsp void *arg)
111 404c43c4 2018-06-21 stsp {
112 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
113 d0275cf7 2019-08-21 stsp struct got_object *obj = NULL;
114 4c9641fd 2019-08-21 stsp struct got_object_id *obj_id = NULL;
115 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
116 4c9641fd 2019-08-21 stsp struct got_blob_object *blob = NULL;
117 404c43c4 2018-06-21 stsp struct got_diff_changes *changes = NULL;
118 404c43c4 2018-06-21 stsp
119 4c9641fd 2019-08-21 stsp err = got_object_open_as_commit(&commit, repo, parent_id);
120 8d725ae1 2019-08-18 stsp if (err)
121 8d725ae1 2019-08-18 stsp return err;
122 8d725ae1 2019-08-18 stsp
123 4c9641fd 2019-08-21 stsp err = got_object_id_by_path(&obj_id, repo, parent_id, path);
124 4c9641fd 2019-08-21 stsp if (err) {
125 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
126 4c9641fd 2019-08-21 stsp err = NULL;
127 404c43c4 2018-06-21 stsp goto done;
128 4c9641fd 2019-08-21 stsp }
129 27d434c2 2018-09-15 stsp
130 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
131 27d434c2 2018-09-15 stsp if (err)
132 27d434c2 2018-09-15 stsp goto done;
133 27d434c2 2018-09-15 stsp
134 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
135 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
136 404c43c4 2018-06-21 stsp goto done;
137 404c43c4 2018-06-21 stsp }
138 404c43c4 2018-06-21 stsp
139 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
140 404c43c4 2018-06-21 stsp if (err)
141 404c43c4 2018-06-21 stsp goto done;
142 404c43c4 2018-06-21 stsp
143 4c9641fd 2019-08-21 stsp if (fseek(blame->f, 0L, SEEK_SET) == -1) {
144 4c9641fd 2019-08-21 stsp err = got_ferror(blame->f, GOT_ERR_IO);
145 4c9641fd 2019-08-21 stsp goto done;
146 4c9641fd 2019-08-21 stsp }
147 4c9641fd 2019-08-21 stsp
148 4c9641fd 2019-08-21 stsp err = got_diff_blob_file_lines_changed(&changes, blob, blame->f,
149 4c9641fd 2019-08-21 stsp blame->filesize);
150 404c43c4 2018-06-21 stsp if (err)
151 404c43c4 2018-06-21 stsp goto done;
152 404c43c4 2018-06-21 stsp
153 404c43c4 2018-06-21 stsp if (changes) {
154 c35a7943 2018-07-12 stsp err = blame_changes(blame, changes, id, cb, arg);
155 ce7f1bfe 2018-07-13 stsp got_diff_free_changes(changes);
156 d68a0a7d 2018-07-10 stsp } else if (cb)
157 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
158 404c43c4 2018-06-21 stsp done:
159 8d725ae1 2019-08-18 stsp if (commit)
160 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
161 27d434c2 2018-09-15 stsp free(obj_id);
162 404c43c4 2018-06-21 stsp if (obj)
163 404c43c4 2018-06-21 stsp got_object_close(obj);
164 404c43c4 2018-06-21 stsp if (blob)
165 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
166 404c43c4 2018-06-21 stsp return err;
167 404c43c4 2018-06-21 stsp }
168 404c43c4 2018-06-21 stsp
169 fb43ecf1 2019-02-11 stsp static const struct got_error *
170 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
171 404c43c4 2018-06-21 stsp {
172 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
173 c35a7943 2018-07-12 stsp
174 fb43ecf1 2019-02-11 stsp if (blame->f && fclose(blame->f) != 0)
175 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
176 404c43c4 2018-06-21 stsp free(blame->lines);
177 404c43c4 2018-06-21 stsp free(blame);
178 fb43ecf1 2019-02-11 stsp return err;
179 404c43c4 2018-06-21 stsp }
180 404c43c4 2018-06-21 stsp
181 404c43c4 2018-06-21 stsp static const struct got_error *
182 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
183 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
184 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
185 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
186 404c43c4 2018-06-21 stsp {
187 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
188 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
189 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
190 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
191 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
192 4c9641fd 2019-08-21 stsp struct got_object_id *id = NULL, *pid = NULL;
193 404c43c4 2018-06-21 stsp int lineno;
194 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
195 404c43c4 2018-06-21 stsp
196 404c43c4 2018-06-21 stsp *blamep = NULL;
197 404c43c4 2018-06-21 stsp
198 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
199 404c43c4 2018-06-21 stsp if (err)
200 404c43c4 2018-06-21 stsp return err;
201 27d434c2 2018-09-15 stsp
202 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
203 27d434c2 2018-09-15 stsp if (err)
204 27d434c2 2018-09-15 stsp goto done;
205 27d434c2 2018-09-15 stsp
206 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
207 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
208 404c43c4 2018-06-21 stsp goto done;
209 404c43c4 2018-06-21 stsp }
210 404c43c4 2018-06-21 stsp
211 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
212 404c43c4 2018-06-21 stsp if (err)
213 404c43c4 2018-06-21 stsp goto done;
214 404c43c4 2018-06-21 stsp
215 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
216 404c43c4 2018-06-21 stsp if (blame == NULL)
217 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
218 404c43c4 2018-06-21 stsp
219 404c43c4 2018-06-21 stsp blame->f = got_opentemp();
220 404c43c4 2018-06-21 stsp if (blame->f == NULL) {
221 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
222 404c43c4 2018-06-21 stsp goto done;
223 404c43c4 2018-06-21 stsp }
224 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
225 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
226 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
227 404c43c4 2018-06-21 stsp goto done;
228 b02560ec 2019-08-19 stsp
229 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
230 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
231 b02560ec 2019-08-19 stsp blame->nlines--;
232 404c43c4 2018-06-21 stsp
233 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
234 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
235 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
236 404c43c4 2018-06-21 stsp goto done;
237 404c43c4 2018-06-21 stsp }
238 404c43c4 2018-06-21 stsp
239 8d725ae1 2019-08-18 stsp err = got_commit_graph_open(&graph, start_commit_id, path, 1, repo);
240 404c43c4 2018-06-21 stsp if (err)
241 293f6400 2018-09-20 stsp return err;
242 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
243 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
244 293f6400 2018-09-20 stsp if (err)
245 404c43c4 2018-06-21 stsp goto done;
246 4c9641fd 2019-08-21 stsp id = start_commit_id;
247 656b1f76 2019-05-11 jcs for (;;) {
248 4c9641fd 2019-08-21 stsp err = got_commit_graph_iter_next(&pid, graph);
249 404c43c4 2018-06-21 stsp if (err) {
250 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
251 4c9641fd 2019-08-21 stsp err = NULL;
252 293f6400 2018-09-20 stsp break;
253 293f6400 2018-09-20 stsp }
254 293f6400 2018-09-20 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
255 293f6400 2018-09-20 stsp break;
256 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
257 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
258 293f6400 2018-09-20 stsp if (err)
259 293f6400 2018-09-20 stsp break;
260 78695fb7 2019-08-12 stsp continue;
261 293f6400 2018-09-20 stsp }
262 4c9641fd 2019-08-21 stsp if (pid) {
263 4c9641fd 2019-08-21 stsp err = blame_commit(blame, pid, id, path, repo, cb, arg);
264 293f6400 2018-09-20 stsp if (err) {
265 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
266 293f6400 2018-09-20 stsp err = NULL;
267 293f6400 2018-09-20 stsp break;
268 293f6400 2018-09-20 stsp }
269 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
270 06ca4d09 2018-11-19 stsp break;
271 404c43c4 2018-06-21 stsp }
272 4c9641fd 2019-08-21 stsp id = pid;
273 293f6400 2018-09-20 stsp }
274 ed77f2ae 2018-06-21 stsp
275 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
276 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
277 293f6400 2018-09-20 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
278 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
279 293f6400 2018-09-20 stsp if (err)
280 293f6400 2018-09-20 stsp goto done;
281 404c43c4 2018-06-21 stsp }
282 404c43c4 2018-06-21 stsp }
283 404c43c4 2018-06-21 stsp
284 404c43c4 2018-06-21 stsp done:
285 293f6400 2018-09-20 stsp if (graph)
286 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
287 27d434c2 2018-09-15 stsp free(obj_id);
288 404c43c4 2018-06-21 stsp if (obj)
289 404c43c4 2018-06-21 stsp got_object_close(obj);
290 404c43c4 2018-06-21 stsp if (blob)
291 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
292 1828273a 2018-07-09 stsp if (err) {
293 1828273a 2018-07-09 stsp if (blame)
294 1828273a 2018-07-09 stsp blame_close(blame);
295 1828273a 2018-07-09 stsp } else
296 404c43c4 2018-06-21 stsp *blamep = blame;
297 404c43c4 2018-06-21 stsp
298 404c43c4 2018-06-21 stsp return err;
299 404c43c4 2018-06-21 stsp }
300 84451b3e 2018-07-10 stsp
301 84451b3e 2018-07-10 stsp const struct got_error *
302 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
303 84451b3e 2018-07-10 stsp struct got_repository *repo,
304 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
305 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
306 84451b3e 2018-07-10 stsp {
307 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
308 84451b3e 2018-07-10 stsp struct got_blame *blame;
309 84451b3e 2018-07-10 stsp char *abspath;
310 84451b3e 2018-07-10 stsp
311 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
312 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
313 84451b3e 2018-07-10 stsp
314 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
315 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
316 84451b3e 2018-07-10 stsp free(abspath);
317 26206841 2018-07-12 stsp if (blame)
318 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
319 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
320 84451b3e 2018-07-10 stsp }