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 404c43c4 2018-06-21 stsp size_t nlines;
56 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
57 c35a7943 2018-07-12 stsp int ncommits;
58 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets_list diff_offsets_list;
59 404c43c4 2018-06-21 stsp };
60 404c43c4 2018-06-21 stsp
61 c35a7943 2018-07-12 stsp static void
62 c35a7943 2018-07-12 stsp free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
63 c35a7943 2018-07-12 stsp {
64 c35a7943 2018-07-12 stsp if (diff_offsets->chunks)
65 c35a7943 2018-07-12 stsp got_diffoffset_free(diff_offsets->chunks);
66 c35a7943 2018-07-12 stsp free(diff_offsets->commit_id);
67 c35a7943 2018-07-12 stsp free(diff_offsets);
68 c35a7943 2018-07-12 stsp }
69 c35a7943 2018-07-12 stsp
70 404c43c4 2018-06-21 stsp static const struct got_error *
71 c35a7943 2018-07-12 stsp alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
72 c35a7943 2018-07-12 stsp struct got_object_id *commit_id)
73 c35a7943 2018-07-12 stsp {
74 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
75 c35a7943 2018-07-12 stsp
76 c35a7943 2018-07-12 stsp *diff_offsets = calloc(1, sizeof(**diff_offsets));
77 c35a7943 2018-07-12 stsp if (*diff_offsets == NULL)
78 c35a7943 2018-07-12 stsp return got_error_from_errno();
79 c35a7943 2018-07-12 stsp
80 c35a7943 2018-07-12 stsp (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
81 c35a7943 2018-07-12 stsp if ((*diff_offsets)->commit_id == NULL) {
82 c35a7943 2018-07-12 stsp err = got_error_from_errno();
83 c35a7943 2018-07-12 stsp free_diff_offsets(*diff_offsets);
84 c35a7943 2018-07-12 stsp *diff_offsets = NULL;
85 c35a7943 2018-07-12 stsp return err;
86 c35a7943 2018-07-12 stsp }
87 c35a7943 2018-07-12 stsp
88 c35a7943 2018-07-12 stsp err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
89 c35a7943 2018-07-12 stsp if (err) {
90 c35a7943 2018-07-12 stsp free_diff_offsets(*diff_offsets);
91 c35a7943 2018-07-12 stsp return err;
92 c35a7943 2018-07-12 stsp }
93 c35a7943 2018-07-12 stsp
94 c35a7943 2018-07-12 stsp return NULL;
95 c35a7943 2018-07-12 stsp }
96 c35a7943 2018-07-12 stsp
97 c35a7943 2018-07-12 stsp static const struct got_error *
98 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
99 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
100 84451b3e 2018-07-10 stsp void *arg)
101 404c43c4 2018-06-21 stsp {
102 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
103 404c43c4 2018-06-21 stsp struct got_blame_line *line;
104 404c43c4 2018-06-21 stsp
105 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
106 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
107 404c43c4 2018-06-21 stsp
108 404c43c4 2018-06-21 stsp line = &blame->lines[lineno - 1];
109 404c43c4 2018-06-21 stsp if (line->annotated)
110 84451b3e 2018-07-10 stsp return NULL;
111 404c43c4 2018-06-21 stsp
112 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
113 404c43c4 2018-06-21 stsp line->annotated = 1;
114 84451b3e 2018-07-10 stsp if (cb)
115 84451b3e 2018-07-10 stsp err = cb(arg, blame->nlines, lineno, id);
116 84451b3e 2018-07-10 stsp return err;
117 404c43c4 2018-06-21 stsp }
118 404c43c4 2018-06-21 stsp
119 c35a7943 2018-07-12 stsp static int
120 c35a7943 2018-07-12 stsp get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
121 c35a7943 2018-07-12 stsp int lineno)
122 c35a7943 2018-07-12 stsp {
123 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
124 c35a7943 2018-07-12 stsp
125 c35a7943 2018-07-12 stsp SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
126 c35a7943 2018-07-12 stsp lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
127 c35a7943 2018-07-12 stsp
128 c35a7943 2018-07-12 stsp return lineno;
129 c35a7943 2018-07-12 stsp }
130 c35a7943 2018-07-12 stsp
131 404c43c4 2018-06-21 stsp static const struct got_error *
132 c35a7943 2018-07-12 stsp blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
133 c35a7943 2018-07-12 stsp struct got_object_id *commit_id,
134 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
135 c35a7943 2018-07-12 stsp void *arg)
136 c35a7943 2018-07-12 stsp {
137 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
138 c35a7943 2018-07-12 stsp struct got_diff_change *change;
139 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
140 c35a7943 2018-07-12 stsp
141 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
142 c35a7943 2018-07-12 stsp int c = change->cv.c;
143 c35a7943 2018-07-12 stsp int d = change->cv.d;
144 c35a7943 2018-07-12 stsp int new_lineno = c;
145 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
146 c35a7943 2018-07-12 stsp int ln;
147 c35a7943 2018-07-12 stsp
148 c35a7943 2018-07-12 stsp for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
149 c35a7943 2018-07-12 stsp err = annotate_line(blame,
150 c35a7943 2018-07-12 stsp get_blamed_line(&blame->diff_offsets_list, ln),
151 c35a7943 2018-07-12 stsp commit_id, cb, arg);
152 c35a7943 2018-07-12 stsp if (err)
153 c35a7943 2018-07-12 stsp return err;
154 c35a7943 2018-07-12 stsp }
155 c35a7943 2018-07-12 stsp }
156 c35a7943 2018-07-12 stsp
157 c35a7943 2018-07-12 stsp err = alloc_diff_offsets(&diff_offsets, commit_id);
158 c35a7943 2018-07-12 stsp if (err)
159 c35a7943 2018-07-12 stsp return err;
160 c35a7943 2018-07-12 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
161 c35a7943 2018-07-12 stsp int a = change->cv.a;
162 c35a7943 2018-07-12 stsp int b = change->cv.b;
163 c35a7943 2018-07-12 stsp int c = change->cv.c;
164 c35a7943 2018-07-12 stsp int d = change->cv.d;
165 c35a7943 2018-07-12 stsp int old_lineno = a;
166 c35a7943 2018-07-12 stsp int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
167 c35a7943 2018-07-12 stsp int new_lineno = c;
168 c35a7943 2018-07-12 stsp int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
169 c35a7943 2018-07-12 stsp
170 c35a7943 2018-07-12 stsp err = got_diffoffset_add(diff_offsets->chunks,
171 c35a7943 2018-07-12 stsp old_lineno, old_length, new_lineno, new_length);
172 df9513f1 2018-07-13 stsp if (err) {
173 df9513f1 2018-07-13 stsp free_diff_offsets(diff_offsets);
174 c35a7943 2018-07-12 stsp return err;
175 df9513f1 2018-07-13 stsp }
176 c35a7943 2018-07-12 stsp }
177 c35a7943 2018-07-12 stsp SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
178 c35a7943 2018-07-12 stsp
179 c35a7943 2018-07-12 stsp return NULL;
180 c35a7943 2018-07-12 stsp }
181 c35a7943 2018-07-12 stsp
182 c35a7943 2018-07-12 stsp static const struct got_error *
183 404c43c4 2018-06-21 stsp blame_commit(struct got_blame *blame, struct got_object_id *id,
184 84451b3e 2018-07-10 stsp struct got_object_id *pid, const char *path, struct got_repository *repo,
185 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
186 84451b3e 2018-07-10 stsp void *arg)
187 404c43c4 2018-06-21 stsp {
188 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
189 404c43c4 2018-06-21 stsp struct got_object *obj = NULL, *pobj = NULL;
190 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL, *pobj_id = NULL;
191 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL, *pblob = NULL;
192 404c43c4 2018-06-21 stsp struct got_diff_changes *changes = NULL;
193 404c43c4 2018-06-21 stsp
194 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, id, path);
195 404c43c4 2018-06-21 stsp if (err)
196 404c43c4 2018-06-21 stsp goto done;
197 27d434c2 2018-09-15 stsp
198 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
199 27d434c2 2018-09-15 stsp if (err)
200 27d434c2 2018-09-15 stsp goto done;
201 27d434c2 2018-09-15 stsp
202 404c43c4 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
203 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
204 404c43c4 2018-06-21 stsp goto done;
205 404c43c4 2018-06-21 stsp }
206 404c43c4 2018-06-21 stsp
207 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&pobj_id, repo, pid, path);
208 404c43c4 2018-06-21 stsp if (err) {
209 404c43c4 2018-06-21 stsp if (err->code == GOT_ERR_NO_OBJ) {
210 404c43c4 2018-06-21 stsp /* Blob's history began in previous commit. */
211 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
212 404c43c4 2018-06-21 stsp }
213 404c43c4 2018-06-21 stsp goto done;
214 404c43c4 2018-06-21 stsp }
215 27d434c2 2018-09-15 stsp
216 27d434c2 2018-09-15 stsp /* If IDs match then don't bother with diffing. */
217 27d434c2 2018-09-15 stsp if (got_object_id_cmp(obj_id, pobj_id) == 0) {
218 27d434c2 2018-09-15 stsp if (cb)
219 27d434c2 2018-09-15 stsp err = cb(arg, blame->nlines, -1, id);
220 27d434c2 2018-09-15 stsp goto done;
221 27d434c2 2018-09-15 stsp }
222 27d434c2 2018-09-15 stsp
223 27d434c2 2018-09-15 stsp err = got_object_open(&pobj, repo, pobj_id);
224 27d434c2 2018-09-15 stsp if (err)
225 27d434c2 2018-09-15 stsp goto done;
226 27d434c2 2018-09-15 stsp
227 404c43c4 2018-06-21 stsp if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
228 404c43c4 2018-06-21 stsp /*
229 404c43c4 2018-06-21 stsp * Encountered a non-blob at the path (probably a tree).
230 404c43c4 2018-06-21 stsp * Blob's history began in previous commit.
231 404c43c4 2018-06-21 stsp */
232 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
233 404c43c4 2018-06-21 stsp goto done;
234 404c43c4 2018-06-21 stsp }
235 404c43c4 2018-06-21 stsp
236 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
237 404c43c4 2018-06-21 stsp if (err)
238 404c43c4 2018-06-21 stsp goto done;
239 404c43c4 2018-06-21 stsp
240 404c43c4 2018-06-21 stsp err = got_object_blob_open(&pblob, repo, pobj, 8192);
241 404c43c4 2018-06-21 stsp if (err)
242 404c43c4 2018-06-21 stsp goto done;
243 404c43c4 2018-06-21 stsp
244 c35a7943 2018-07-12 stsp err = got_diff_blob_lines_changed(&changes, pblob, blob);
245 404c43c4 2018-06-21 stsp if (err)
246 404c43c4 2018-06-21 stsp goto done;
247 404c43c4 2018-06-21 stsp
248 404c43c4 2018-06-21 stsp if (changes) {
249 c35a7943 2018-07-12 stsp err = blame_changes(blame, changes, id, cb, arg);
250 ce7f1bfe 2018-07-13 stsp got_diff_free_changes(changes);
251 d68a0a7d 2018-07-10 stsp } else if (cb)
252 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
253 404c43c4 2018-06-21 stsp done:
254 27d434c2 2018-09-15 stsp free(obj_id);
255 27d434c2 2018-09-15 stsp free(pobj_id);
256 404c43c4 2018-06-21 stsp if (obj)
257 404c43c4 2018-06-21 stsp got_object_close(obj);
258 404c43c4 2018-06-21 stsp if (pobj)
259 404c43c4 2018-06-21 stsp got_object_close(pobj);
260 404c43c4 2018-06-21 stsp if (blob)
261 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
262 404c43c4 2018-06-21 stsp if (pblob)
263 404c43c4 2018-06-21 stsp got_object_blob_close(pblob);
264 404c43c4 2018-06-21 stsp return err;
265 404c43c4 2018-06-21 stsp }
266 404c43c4 2018-06-21 stsp
267 404c43c4 2018-06-21 stsp static void
268 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
269 404c43c4 2018-06-21 stsp {
270 c35a7943 2018-07-12 stsp struct got_blame_diff_offsets *diff_offsets;
271 c35a7943 2018-07-12 stsp
272 404c43c4 2018-06-21 stsp if (blame->f)
273 404c43c4 2018-06-21 stsp fclose(blame->f);
274 404c43c4 2018-06-21 stsp free(blame->lines);
275 c35a7943 2018-07-12 stsp while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
276 c35a7943 2018-07-12 stsp diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
277 c35a7943 2018-07-12 stsp SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
278 c35a7943 2018-07-12 stsp free_diff_offsets(diff_offsets);
279 c35a7943 2018-07-12 stsp }
280 404c43c4 2018-06-21 stsp free(blame);
281 404c43c4 2018-06-21 stsp }
282 404c43c4 2018-06-21 stsp
283 404c43c4 2018-06-21 stsp static const struct got_error *
284 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
285 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
286 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
287 84451b3e 2018-07-10 stsp void *arg)
288 404c43c4 2018-06-21 stsp {
289 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
290 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
291 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
292 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
293 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
294 404c43c4 2018-06-21 stsp struct got_object_id *id = NULL;
295 404c43c4 2018-06-21 stsp int lineno;
296 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
297 404c43c4 2018-06-21 stsp
298 404c43c4 2018-06-21 stsp *blamep = NULL;
299 404c43c4 2018-06-21 stsp
300 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
301 404c43c4 2018-06-21 stsp if (err)
302 404c43c4 2018-06-21 stsp return err;
303 27d434c2 2018-09-15 stsp
304 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
305 27d434c2 2018-09-15 stsp if (err)
306 27d434c2 2018-09-15 stsp goto done;
307 27d434c2 2018-09-15 stsp
308 404c43c4 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
309 404c43c4 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
310 404c43c4 2018-06-21 stsp goto done;
311 404c43c4 2018-06-21 stsp }
312 404c43c4 2018-06-21 stsp
313 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
314 404c43c4 2018-06-21 stsp if (err)
315 404c43c4 2018-06-21 stsp goto done;
316 404c43c4 2018-06-21 stsp
317 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
318 404c43c4 2018-06-21 stsp if (blame == NULL)
319 404c43c4 2018-06-21 stsp return got_error_from_errno();
320 404c43c4 2018-06-21 stsp
321 404c43c4 2018-06-21 stsp blame->f = got_opentemp();
322 404c43c4 2018-06-21 stsp if (blame->f == NULL) {
323 404c43c4 2018-06-21 stsp err = got_error_from_errno();
324 404c43c4 2018-06-21 stsp goto done;
325 404c43c4 2018-06-21 stsp }
326 84451b3e 2018-07-10 stsp err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
327 84451b3e 2018-07-10 stsp blob);
328 404c43c4 2018-06-21 stsp if (err)
329 404c43c4 2018-06-21 stsp goto done;
330 404c43c4 2018-06-21 stsp
331 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
332 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
333 404c43c4 2018-06-21 stsp err = got_error_from_errno();
334 404c43c4 2018-06-21 stsp goto done;
335 404c43c4 2018-06-21 stsp }
336 404c43c4 2018-06-21 stsp
337 293f6400 2018-09-20 stsp err = got_commit_graph_open(&graph, start_commit_id, path, 0, repo);
338 404c43c4 2018-06-21 stsp if (err)
339 293f6400 2018-09-20 stsp return err;
340 293f6400 2018-09-20 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo);
341 293f6400 2018-09-20 stsp if (err)
342 404c43c4 2018-06-21 stsp goto done;
343 404c43c4 2018-06-21 stsp
344 293f6400 2018-09-20 stsp id = NULL;
345 293f6400 2018-09-20 stsp while (1) {
346 293f6400 2018-09-20 stsp struct got_object_id *next_id;
347 293f6400 2018-09-20 stsp
348 293f6400 2018-09-20 stsp err = got_commit_graph_iter_next(&next_id, graph);
349 404c43c4 2018-06-21 stsp if (err) {
350 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
351 404c43c4 2018-06-21 stsp err = NULL;
352 293f6400 2018-09-20 stsp break;
353 293f6400 2018-09-20 stsp }
354 293f6400 2018-09-20 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
355 293f6400 2018-09-20 stsp break;
356 293f6400 2018-09-20 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
357 293f6400 2018-09-20 stsp if (err)
358 293f6400 2018-09-20 stsp break;
359 293f6400 2018-09-20 stsp else
360 293f6400 2018-09-20 stsp continue;
361 293f6400 2018-09-20 stsp }
362 293f6400 2018-09-20 stsp if (next_id == NULL)
363 404c43c4 2018-06-21 stsp break;
364 293f6400 2018-09-20 stsp if (id) {
365 293f6400 2018-09-20 stsp err = blame_commit(blame, id, next_id, path, repo,
366 293f6400 2018-09-20 stsp cb, arg);
367 293f6400 2018-09-20 stsp if (err) {
368 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
369 293f6400 2018-09-20 stsp err = NULL;
370 293f6400 2018-09-20 stsp break;
371 293f6400 2018-09-20 stsp }
372 404c43c4 2018-06-21 stsp }
373 293f6400 2018-09-20 stsp id = next_id;
374 293f6400 2018-09-20 stsp }
375 ed77f2ae 2018-06-21 stsp
376 293f6400 2018-09-20 stsp if (id) {
377 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
378 293f6400 2018-09-20 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
379 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
380 293f6400 2018-09-20 stsp if (err)
381 293f6400 2018-09-20 stsp goto done;
382 404c43c4 2018-06-21 stsp }
383 404c43c4 2018-06-21 stsp }
384 404c43c4 2018-06-21 stsp
385 404c43c4 2018-06-21 stsp done:
386 293f6400 2018-09-20 stsp if (graph)
387 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
388 27d434c2 2018-09-15 stsp free(obj_id);
389 404c43c4 2018-06-21 stsp if (obj)
390 404c43c4 2018-06-21 stsp got_object_close(obj);
391 404c43c4 2018-06-21 stsp if (blob)
392 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
393 1828273a 2018-07-09 stsp if (err) {
394 1828273a 2018-07-09 stsp if (blame)
395 1828273a 2018-07-09 stsp blame_close(blame);
396 1828273a 2018-07-09 stsp } else
397 404c43c4 2018-06-21 stsp *blamep = blame;
398 404c43c4 2018-06-21 stsp
399 404c43c4 2018-06-21 stsp return err;
400 404c43c4 2018-06-21 stsp }
401 404c43c4 2018-06-21 stsp
402 404c43c4 2018-06-21 stsp static const struct got_error *
403 404c43c4 2018-06-21 stsp blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
404 404c43c4 2018-06-21 stsp {
405 404c43c4 2018-06-21 stsp if (lineno < 1 || lineno > blame->nlines)
406 404c43c4 2018-06-21 stsp return got_error(GOT_ERR_RANGE);
407 404c43c4 2018-06-21 stsp *id = &blame->lines[lineno - 1].id;
408 404c43c4 2018-06-21 stsp return NULL;
409 404c43c4 2018-06-21 stsp }
410 404c43c4 2018-06-21 stsp
411 404c43c4 2018-06-21 stsp static char *
412 404c43c4 2018-06-21 stsp parse_next_line(FILE *f, size_t *len)
413 404c43c4 2018-06-21 stsp {
414 404c43c4 2018-06-21 stsp char *line;
415 404c43c4 2018-06-21 stsp size_t linelen;
416 404c43c4 2018-06-21 stsp size_t lineno;
417 404c43c4 2018-06-21 stsp const char delim[3] = { '\0', '\0', '\0'};
418 404c43c4 2018-06-21 stsp
419 404c43c4 2018-06-21 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
420 404c43c4 2018-06-21 stsp if (len)
421 404c43c4 2018-06-21 stsp *len = linelen;
422 404c43c4 2018-06-21 stsp return line;
423 404c43c4 2018-06-21 stsp }
424 404c43c4 2018-06-21 stsp
425 404c43c4 2018-06-21 stsp const struct got_error *
426 404c43c4 2018-06-21 stsp got_blame(const char *path, struct got_object_id *start_commit_id,
427 404c43c4 2018-06-21 stsp struct got_repository *repo, FILE *outfile)
428 404c43c4 2018-06-21 stsp {
429 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
430 404c43c4 2018-06-21 stsp struct got_blame *blame;
431 404c43c4 2018-06-21 stsp int lineno;
432 404c43c4 2018-06-21 stsp char *abspath;
433 404c43c4 2018-06-21 stsp
434 404c43c4 2018-06-21 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
435 404c43c4 2018-06-21 stsp return got_error_from_errno();
436 404c43c4 2018-06-21 stsp
437 84451b3e 2018-07-10 stsp err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
438 404c43c4 2018-06-21 stsp if (err) {
439 404c43c4 2018-06-21 stsp free(abspath);
440 404c43c4 2018-06-21 stsp return err;
441 404c43c4 2018-06-21 stsp }
442 404c43c4 2018-06-21 stsp
443 dd031dc0 2018-07-04 stsp for (lineno = 1; lineno <= blame->nlines; lineno++) {
444 404c43c4 2018-06-21 stsp struct got_object_id *id;
445 404c43c4 2018-06-21 stsp char *line, *id_str;
446 404c43c4 2018-06-21 stsp
447 404c43c4 2018-06-21 stsp line = parse_next_line(blame->f, NULL);
448 404c43c4 2018-06-21 stsp if (line == NULL)
449 404c43c4 2018-06-21 stsp break;
450 404c43c4 2018-06-21 stsp
451 404c43c4 2018-06-21 stsp err = blame_line(&id, blame, lineno);
452 d4af3990 2018-07-13 stsp if (err) {
453 d4af3990 2018-07-13 stsp free(line);
454 404c43c4 2018-06-21 stsp break;
455 d4af3990 2018-07-13 stsp }
456 404c43c4 2018-06-21 stsp
457 404c43c4 2018-06-21 stsp err = got_object_id_str(&id_str, id);
458 58c811c5 2018-07-13 stsp /* Do not free id; It points into blame->lines. */
459 404c43c4 2018-06-21 stsp if (err) {
460 404c43c4 2018-06-21 stsp free(line);
461 404c43c4 2018-06-21 stsp break;
462 404c43c4 2018-06-21 stsp }
463 404c43c4 2018-06-21 stsp
464 404c43c4 2018-06-21 stsp fprintf(outfile, "%.8s %s\n", id_str, line);
465 404c43c4 2018-06-21 stsp free(line);
466 404c43c4 2018-06-21 stsp free(id_str);
467 404c43c4 2018-06-21 stsp }
468 404c43c4 2018-06-21 stsp
469 404c43c4 2018-06-21 stsp blame_close(blame);
470 404c43c4 2018-06-21 stsp free(abspath);
471 404c43c4 2018-06-21 stsp return err;
472 404c43c4 2018-06-21 stsp }
473 84451b3e 2018-07-10 stsp
474 84451b3e 2018-07-10 stsp const struct got_error *
475 84451b3e 2018-07-10 stsp got_blame_incremental(const char *path, struct got_object_id *commit_id,
476 84451b3e 2018-07-10 stsp struct got_repository *repo,
477 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
478 84451b3e 2018-07-10 stsp void *arg)
479 84451b3e 2018-07-10 stsp {
480 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
481 84451b3e 2018-07-10 stsp struct got_blame *blame;
482 84451b3e 2018-07-10 stsp char *abspath;
483 84451b3e 2018-07-10 stsp
484 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
485 84451b3e 2018-07-10 stsp return got_error_from_errno();
486 84451b3e 2018-07-10 stsp
487 84451b3e 2018-07-10 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
488 84451b3e 2018-07-10 stsp free(abspath);
489 26206841 2018-07-12 stsp if (blame)
490 75b7a700 2018-07-10 stsp blame_close(blame);
491 84451b3e 2018-07-10 stsp return err;
492 84451b3e 2018-07-10 stsp }