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