Blame


1 404c43c4 2018-06-21 stsp /*
2 404c43c4 2018-06-21 stsp * Copyright (c) 2018 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 404c43c4 2018-06-21 stsp #include "got_blame.h"
31 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
32 404c43c4 2018-06-21 stsp
33 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
34 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
35 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
36 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
37 c35a7943 2018-07-12 stsp #include "got_lib_diffoffset.h"
38 293f6400 2018-09-20 stsp #include "got_commit_graph.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 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets {
46 c35a7943 2018-07-12 stsp struct got_diffoffset_chunks *chunks;
47 c35a7943 2018-07-12 stsp struct got_object_id *commit_id;
48 c35a7943 2018-07-12 stsp SLIST_ENTRY(got_blame_diff_offsets) entry;
49 c35a7943 2018-07-12 stsp };
50 c35a7943 2018-07-12 stsp
51 c35a7943 2018-07-12 stsp SLIST_HEAD(got_blame_diff_offsets_list, got_blame_diff_offsets);
52 c35a7943 2018-07-12 stsp
53 404c43c4 2018-06-21 stsp struct got_blame {
54 404c43c4 2018-06-21 stsp FILE *f;
55 6c4c42e0 2019-06-24 stsp size_t filesize;
56 6fcac457 2018-11-19 stsp int nlines;
57 06ca4d09 2018-11-19 stsp int nannotated;
58 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
59 6c4c42e0 2019-06-24 stsp off_t *line_offsets; /* one per line */
60 c35a7943 2018-07-12 stsp int ncommits;
61 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets_list diff_offsets_list;
62 404c43c4 2018-06-21 stsp };
63 404c43c4 2018-06-21 stsp
64 c35a7943 2018-07-12 stsp static void
65 c35a7943 2018-07-12 stsp free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
66 c35a7943 2018-07-12 stsp {
67 c35a7943 2018-07-12 stsp if (diff_offsets->chunks)
68 c35a7943 2018-07-12 stsp got_diffoffset_free(diff_offsets->chunks);
69 c35a7943 2018-07-12 stsp free(diff_offsets->commit_id);
70 c35a7943 2018-07-12 stsp free(diff_offsets);
71 c35a7943 2018-07-12 stsp }
72 c35a7943 2018-07-12 stsp
73 404c43c4 2018-06-21 stsp static const struct got_error *
74 c35a7943 2018-07-12 stsp alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
75 c35a7943 2018-07-12 stsp struct got_object_id *commit_id)
76 c35a7943 2018-07-12 stsp {
77 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
78 c35a7943 2018-07-12 stsp
79 c35a7943 2018-07-12 stsp *diff_offsets = calloc(1, sizeof(**diff_offsets));
80 c35a7943 2018-07-12 stsp if (*diff_offsets == NULL)
81 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
82 c35a7943 2018-07-12 stsp
83 c35a7943 2018-07-12 stsp (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
84 c35a7943 2018-07-12 stsp if ((*diff_offsets)->commit_id == NULL) {
85 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
86 c35a7943 2018-07-12 stsp free_diff_offsets(*diff_offsets);
87 c35a7943 2018-07-12 stsp *diff_offsets = NULL;
88 c35a7943 2018-07-12 stsp return err;
89 c35a7943 2018-07-12 stsp }
90 c35a7943 2018-07-12 stsp
91 c35a7943 2018-07-12 stsp err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
92 c35a7943 2018-07-12 stsp if (err) {
93 c35a7943 2018-07-12 stsp free_diff_offsets(*diff_offsets);
94 c35a7943 2018-07-12 stsp return err;
95 c35a7943 2018-07-12 stsp }
96 c35a7943 2018-07-12 stsp
97 c35a7943 2018-07-12 stsp return NULL;
98 c35a7943 2018-07-12 stsp }
99 c35a7943 2018-07-12 stsp
100 c35a7943 2018-07-12 stsp static const struct got_error *
101 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
102 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
103 84451b3e 2018-07-10 stsp void *arg)
104 404c43c4 2018-06-21 stsp {
105 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
106 404c43c4 2018-06-21 stsp struct got_blame_line *line;
107 404c43c4 2018-06-21 stsp
108 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
109 f2e233d8 2018-11-19 stsp return NULL;
110 404c43c4 2018-06-21 stsp
111 404c43c4 2018-06-21 stsp line = &blame->lines[lineno - 1];
112 404c43c4 2018-06-21 stsp if (line->annotated)
113 84451b3e 2018-07-10 stsp return NULL;
114 404c43c4 2018-06-21 stsp
115 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
116 404c43c4 2018-06-21 stsp line->annotated = 1;
117 06ca4d09 2018-11-19 stsp blame->nannotated++;
118 84451b3e 2018-07-10 stsp if (cb)
119 84451b3e 2018-07-10 stsp err = cb(arg, blame->nlines, lineno, id);
120 84451b3e 2018-07-10 stsp return err;
121 404c43c4 2018-06-21 stsp }
122 404c43c4 2018-06-21 stsp
123 c35a7943 2018-07-12 stsp static int
124 c35a7943 2018-07-12 stsp get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
125 c35a7943 2018-07-12 stsp int lineno)
126 c35a7943 2018-07-12 stsp {
127 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
128 548237bc 2019-08-19 stsp int offset = 0;
129 c35a7943 2018-07-12 stsp
130 c35a7943 2018-07-12 stsp SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
131 c35a7943 2018-07-12 stsp lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
132 c35a7943 2018-07-12 stsp
133 548237bc 2019-08-19 stsp return lineno + offset;
134 c35a7943 2018-07-12 stsp }
135 c35a7943 2018-07-12 stsp
136 404c43c4 2018-06-21 stsp static const struct got_error *
137 c35a7943 2018-07-12 stsp blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
138 c35a7943 2018-07-12 stsp struct got_object_id *commit_id,
139 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
140 c35a7943 2018-07-12 stsp void *arg)
141 c35a7943 2018-07-12 stsp {
142 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
143 c35a7943 2018-07-12 stsp struct got_diff_change *change;
144 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
145 c35a7943 2018-07-12 stsp
146 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
147 c35a7943 2018-07-12 stsp int c = change->cv.c;
148 c35a7943 2018-07-12 stsp int d = change->cv.d;
149 c35a7943 2018-07-12 stsp int new_lineno = c;
150 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
151 c35a7943 2018-07-12 stsp int ln;
152 c35a7943 2018-07-12 stsp
153 c35a7943 2018-07-12 stsp for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
154 c35a7943 2018-07-12 stsp err = annotate_line(blame,
155 c35a7943 2018-07-12 stsp get_blamed_line(&blame->diff_offsets_list, ln),
156 c35a7943 2018-07-12 stsp commit_id, cb, arg);
157 c35a7943 2018-07-12 stsp if (err)
158 c35a7943 2018-07-12 stsp return err;
159 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
160 06ca4d09 2018-11-19 stsp return NULL;
161 c35a7943 2018-07-12 stsp }
162 c35a7943 2018-07-12 stsp }
163 c35a7943 2018-07-12 stsp
164 c35a7943 2018-07-12 stsp err = alloc_diff_offsets(&diff_offsets, commit_id);
165 c35a7943 2018-07-12 stsp if (err)
166 c35a7943 2018-07-12 stsp return err;
167 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
168 c35a7943 2018-07-12 stsp int a = change->cv.a;
169 c35a7943 2018-07-12 stsp int b = change->cv.b;
170 c35a7943 2018-07-12 stsp int c = change->cv.c;
171 c35a7943 2018-07-12 stsp int d = change->cv.d;
172 c35a7943 2018-07-12 stsp int old_lineno = a;
173 c35a7943 2018-07-12 stsp int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
174 c35a7943 2018-07-12 stsp int new_lineno = c;
175 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
176 c35a7943 2018-07-12 stsp
177 c35a7943 2018-07-12 stsp err = got_diffoffset_add(diff_offsets->chunks,
178 c35a7943 2018-07-12 stsp old_lineno, old_length, new_lineno, new_length);
179 df9513f1 2018-07-13 stsp if (err) {
180 df9513f1 2018-07-13 stsp free_diff_offsets(diff_offsets);
181 c35a7943 2018-07-12 stsp return err;
182 df9513f1 2018-07-13 stsp }
183 c35a7943 2018-07-12 stsp }
184 c35a7943 2018-07-12 stsp SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
185 c35a7943 2018-07-12 stsp
186 c35a7943 2018-07-12 stsp return NULL;
187 c35a7943 2018-07-12 stsp }
188 c35a7943 2018-07-12 stsp
189 c35a7943 2018-07-12 stsp static const struct got_error *
190 404c43c4 2018-06-21 stsp blame_commit(struct got_blame *blame, struct got_object_id *id,
191 8d725ae1 2019-08-18 stsp const char *path, struct got_repository *repo,
192 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
193 84451b3e 2018-07-10 stsp void *arg)
194 404c43c4 2018-06-21 stsp {
195 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
196 404c43c4 2018-06-21 stsp struct got_object *obj = NULL, *pobj = NULL;
197 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL, *pobj_id = NULL;
198 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
199 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL, *pblob = NULL;
200 404c43c4 2018-06-21 stsp struct got_diff_changes *changes = NULL;
201 8d725ae1 2019-08-18 stsp struct got_object_qid *pid = NULL;
202 404c43c4 2018-06-21 stsp
203 8d725ae1 2019-08-18 stsp err = got_object_open_as_commit(&commit, repo, id);
204 8d725ae1 2019-08-18 stsp if (err)
205 8d725ae1 2019-08-18 stsp return err;
206 8d725ae1 2019-08-18 stsp
207 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, id, path);
208 404c43c4 2018-06-21 stsp if (err)
209 404c43c4 2018-06-21 stsp goto done;
210 27d434c2 2018-09-15 stsp
211 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
212 27d434c2 2018-09-15 stsp if (err)
213 27d434c2 2018-09-15 stsp goto done;
214 27d434c2 2018-09-15 stsp
215 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
216 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
217 404c43c4 2018-06-21 stsp goto done;
218 404c43c4 2018-06-21 stsp }
219 404c43c4 2018-06-21 stsp
220 8d725ae1 2019-08-18 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
221 78695fb7 2019-08-12 stsp if (pid) {
222 8d725ae1 2019-08-18 stsp err = got_object_id_by_path(&pobj_id, repo, pid->id, path);
223 78695fb7 2019-08-12 stsp if (err) {
224 78695fb7 2019-08-12 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
225 78695fb7 2019-08-12 stsp /* Blob's history began in previous commit. */
226 78695fb7 2019-08-12 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
227 78695fb7 2019-08-12 stsp }
228 78695fb7 2019-08-12 stsp goto done;
229 404c43c4 2018-06-21 stsp }
230 27d434c2 2018-09-15 stsp
231 78695fb7 2019-08-12 stsp /* If IDs match then don't bother with diffing. */
232 78695fb7 2019-08-12 stsp if (got_object_id_cmp(obj_id, pobj_id) == 0) {
233 78695fb7 2019-08-12 stsp if (cb)
234 78695fb7 2019-08-12 stsp err = cb(arg, blame->nlines, -1, id);
235 78695fb7 2019-08-12 stsp goto done;
236 78695fb7 2019-08-12 stsp }
237 27d434c2 2018-09-15 stsp
238 78695fb7 2019-08-12 stsp err = got_object_open(&pobj, repo, pobj_id);
239 78695fb7 2019-08-12 stsp if (err)
240 78695fb7 2019-08-12 stsp goto done;
241 27d434c2 2018-09-15 stsp
242 78695fb7 2019-08-12 stsp if (pobj->type != GOT_OBJ_TYPE_BLOB) {
243 78695fb7 2019-08-12 stsp /*
244 78695fb7 2019-08-12 stsp * Encountered a non-blob at the path (probably a tree).
245 78695fb7 2019-08-12 stsp * Blob's history began in previous commit.
246 78695fb7 2019-08-12 stsp */
247 78695fb7 2019-08-12 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
248 78695fb7 2019-08-12 stsp goto done;
249 78695fb7 2019-08-12 stsp }
250 78695fb7 2019-08-12 stsp
251 78695fb7 2019-08-12 stsp err = got_object_blob_open(&pblob, repo, pobj, 8192);
252 78695fb7 2019-08-12 stsp if (err)
253 78695fb7 2019-08-12 stsp goto done;
254 404c43c4 2018-06-21 stsp }
255 404c43c4 2018-06-21 stsp
256 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
257 404c43c4 2018-06-21 stsp if (err)
258 404c43c4 2018-06-21 stsp goto done;
259 404c43c4 2018-06-21 stsp
260 c35a7943 2018-07-12 stsp err = got_diff_blob_lines_changed(&changes, pblob, blob);
261 404c43c4 2018-06-21 stsp if (err)
262 404c43c4 2018-06-21 stsp goto done;
263 404c43c4 2018-06-21 stsp
264 404c43c4 2018-06-21 stsp if (changes) {
265 c35a7943 2018-07-12 stsp err = blame_changes(blame, changes, id, cb, arg);
266 ce7f1bfe 2018-07-13 stsp got_diff_free_changes(changes);
267 d68a0a7d 2018-07-10 stsp } else if (cb)
268 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
269 404c43c4 2018-06-21 stsp done:
270 8d725ae1 2019-08-18 stsp if (commit)
271 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
272 27d434c2 2018-09-15 stsp free(obj_id);
273 27d434c2 2018-09-15 stsp free(pobj_id);
274 404c43c4 2018-06-21 stsp if (obj)
275 404c43c4 2018-06-21 stsp got_object_close(obj);
276 404c43c4 2018-06-21 stsp if (pobj)
277 404c43c4 2018-06-21 stsp got_object_close(pobj);
278 404c43c4 2018-06-21 stsp if (blob)
279 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
280 404c43c4 2018-06-21 stsp if (pblob)
281 404c43c4 2018-06-21 stsp got_object_blob_close(pblob);
282 404c43c4 2018-06-21 stsp return err;
283 404c43c4 2018-06-21 stsp }
284 404c43c4 2018-06-21 stsp
285 fb43ecf1 2019-02-11 stsp static const struct got_error *
286 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
287 404c43c4 2018-06-21 stsp {
288 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
289 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
290 c35a7943 2018-07-12 stsp
291 fb43ecf1 2019-02-11 stsp if (blame->f && fclose(blame->f) != 0)
292 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
293 404c43c4 2018-06-21 stsp free(blame->lines);
294 c35a7943 2018-07-12 stsp while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
295 c35a7943 2018-07-12 stsp diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
296 c35a7943 2018-07-12 stsp SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
297 c35a7943 2018-07-12 stsp free_diff_offsets(diff_offsets);
298 c35a7943 2018-07-12 stsp }
299 404c43c4 2018-06-21 stsp free(blame);
300 fb43ecf1 2019-02-11 stsp return err;
301 404c43c4 2018-06-21 stsp }
302 404c43c4 2018-06-21 stsp
303 404c43c4 2018-06-21 stsp static const struct got_error *
304 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
305 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
306 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
307 84451b3e 2018-07-10 stsp void *arg)
308 404c43c4 2018-06-21 stsp {
309 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
310 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
311 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
312 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
313 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
314 8d725ae1 2019-08-18 stsp struct got_object_id *id = NULL, *next_id = NULL;
315 404c43c4 2018-06-21 stsp int lineno;
316 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
317 404c43c4 2018-06-21 stsp
318 404c43c4 2018-06-21 stsp *blamep = NULL;
319 404c43c4 2018-06-21 stsp
320 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
321 404c43c4 2018-06-21 stsp if (err)
322 404c43c4 2018-06-21 stsp return err;
323 27d434c2 2018-09-15 stsp
324 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
325 27d434c2 2018-09-15 stsp if (err)
326 27d434c2 2018-09-15 stsp goto done;
327 27d434c2 2018-09-15 stsp
328 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
329 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
330 404c43c4 2018-06-21 stsp goto done;
331 404c43c4 2018-06-21 stsp }
332 404c43c4 2018-06-21 stsp
333 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
334 404c43c4 2018-06-21 stsp if (err)
335 404c43c4 2018-06-21 stsp goto done;
336 404c43c4 2018-06-21 stsp
337 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
338 404c43c4 2018-06-21 stsp if (blame == NULL)
339 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
340 404c43c4 2018-06-21 stsp
341 404c43c4 2018-06-21 stsp blame->f = got_opentemp();
342 404c43c4 2018-06-21 stsp if (blame->f == NULL) {
343 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
344 404c43c4 2018-06-21 stsp goto done;
345 404c43c4 2018-06-21 stsp }
346 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
347 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
348 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
349 404c43c4 2018-06-21 stsp goto done;
350 b02560ec 2019-08-19 stsp
351 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
352 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
353 b02560ec 2019-08-19 stsp blame->nlines--;
354 404c43c4 2018-06-21 stsp
355 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
356 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
357 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
358 404c43c4 2018-06-21 stsp goto done;
359 404c43c4 2018-06-21 stsp }
360 404c43c4 2018-06-21 stsp
361 8d725ae1 2019-08-18 stsp err = got_commit_graph_open(&graph, start_commit_id, path, 1, repo);
362 404c43c4 2018-06-21 stsp if (err)
363 293f6400 2018-09-20 stsp return err;
364 293f6400 2018-09-20 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo);
365 293f6400 2018-09-20 stsp if (err)
366 404c43c4 2018-06-21 stsp goto done;
367 404c43c4 2018-06-21 stsp
368 293f6400 2018-09-20 stsp id = NULL;
369 656b1f76 2019-05-11 jcs for (;;) {
370 8d725ae1 2019-08-18 stsp err = got_commit_graph_iter_next(&next_id, graph);
371 404c43c4 2018-06-21 stsp if (err) {
372 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
373 78695fb7 2019-08-12 stsp if (id)
374 78695fb7 2019-08-12 stsp err = blame_commit(blame, id,
375 8d725ae1 2019-08-18 stsp path, repo, cb, arg);
376 78695fb7 2019-08-12 stsp else
377 78695fb7 2019-08-12 stsp err = NULL;
378 293f6400 2018-09-20 stsp break;
379 293f6400 2018-09-20 stsp }
380 293f6400 2018-09-20 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
381 293f6400 2018-09-20 stsp break;
382 293f6400 2018-09-20 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
383 293f6400 2018-09-20 stsp if (err)
384 293f6400 2018-09-20 stsp break;
385 78695fb7 2019-08-12 stsp continue;
386 293f6400 2018-09-20 stsp }
387 293f6400 2018-09-20 stsp if (id) {
388 8d725ae1 2019-08-18 stsp err = blame_commit(blame, id, path, repo,
389 293f6400 2018-09-20 stsp cb, arg);
390 293f6400 2018-09-20 stsp if (err) {
391 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
392 293f6400 2018-09-20 stsp err = NULL;
393 293f6400 2018-09-20 stsp break;
394 293f6400 2018-09-20 stsp }
395 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
396 06ca4d09 2018-11-19 stsp break;
397 404c43c4 2018-06-21 stsp }
398 8d725ae1 2019-08-18 stsp id = next_id;
399 293f6400 2018-09-20 stsp }
400 ed77f2ae 2018-06-21 stsp
401 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
402 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
403 293f6400 2018-09-20 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
404 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
405 293f6400 2018-09-20 stsp if (err)
406 293f6400 2018-09-20 stsp goto done;
407 404c43c4 2018-06-21 stsp }
408 404c43c4 2018-06-21 stsp }
409 404c43c4 2018-06-21 stsp
410 404c43c4 2018-06-21 stsp done:
411 293f6400 2018-09-20 stsp if (graph)
412 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
413 27d434c2 2018-09-15 stsp free(obj_id);
414 404c43c4 2018-06-21 stsp if (obj)
415 404c43c4 2018-06-21 stsp got_object_close(obj);
416 404c43c4 2018-06-21 stsp if (blob)
417 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
418 1828273a 2018-07-09 stsp if (err) {
419 1828273a 2018-07-09 stsp if (blame)
420 1828273a 2018-07-09 stsp blame_close(blame);
421 1828273a 2018-07-09 stsp } else
422 404c43c4 2018-06-21 stsp *blamep = blame;
423 404c43c4 2018-06-21 stsp
424 404c43c4 2018-06-21 stsp return err;
425 404c43c4 2018-06-21 stsp }
426 84451b3e 2018-07-10 stsp
427 84451b3e 2018-07-10 stsp const struct got_error *
428 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
429 84451b3e 2018-07-10 stsp struct got_repository *repo,
430 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
431 84451b3e 2018-07-10 stsp void *arg)
432 84451b3e 2018-07-10 stsp {
433 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
434 84451b3e 2018-07-10 stsp struct got_blame *blame;
435 84451b3e 2018-07-10 stsp char *abspath;
436 84451b3e 2018-07-10 stsp
437 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
438 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
439 84451b3e 2018-07-10 stsp
440 84451b3e 2018-07-10 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
441 84451b3e 2018-07-10 stsp free(abspath);
442 26206841 2018-07-12 stsp if (blame)
443 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
444 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
445 84451b3e 2018-07-10 stsp }