Blame


1 404c43c4 2018-06-21 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 c27a5e66 2020-11-18 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 404c43c4 2018-06-21 stsp *
5 404c43c4 2018-06-21 stsp * Permission to use, copy, modify, and distribute this software for any
6 404c43c4 2018-06-21 stsp * purpose with or without fee is hereby granted, provided that the above
7 404c43c4 2018-06-21 stsp * copyright notice and this permission notice appear in all copies.
8 404c43c4 2018-06-21 stsp *
9 404c43c4 2018-06-21 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 404c43c4 2018-06-21 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 404c43c4 2018-06-21 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 404c43c4 2018-06-21 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 404c43c4 2018-06-21 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 404c43c4 2018-06-21 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 404c43c4 2018-06-21 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 404c43c4 2018-06-21 stsp */
17 404c43c4 2018-06-21 stsp
18 404c43c4 2018-06-21 stsp #include <sys/queue.h>
19 fe621944 2020-11-10 stsp #include <sys/mman.h>
20 404c43c4 2018-06-21 stsp #include <sys/stat.h>
21 404c43c4 2018-06-21 stsp
22 404c43c4 2018-06-21 stsp #include <sha1.h>
23 404c43c4 2018-06-21 stsp #include <string.h>
24 fe621944 2020-11-10 stsp #include <stdbool.h>
25 404c43c4 2018-06-21 stsp #include <stdio.h>
26 404c43c4 2018-06-21 stsp #include <stdlib.h>
27 404c43c4 2018-06-21 stsp #include <time.h>
28 56e0773d 2019-11-28 stsp #include <limits.h>
29 404c43c4 2018-06-21 stsp #include <util.h>
30 404c43c4 2018-06-21 stsp #include <zlib.h>
31 404c43c4 2018-06-21 stsp
32 404c43c4 2018-06-21 stsp #include "got_error.h"
33 404c43c4 2018-06-21 stsp #include "got_object.h"
34 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
35 404c43c4 2018-06-21 stsp #include "got_blame.h"
36 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
37 404c43c4 2018-06-21 stsp #include "got_opentemp.h"
38 404c43c4 2018-06-21 stsp
39 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
40 404c43c4 2018-06-21 stsp #include "got_lib_delta.h"
41 404c43c4 2018-06-21 stsp #include "got_lib_object.h"
42 404c43c4 2018-06-21 stsp #include "got_lib_diff.h"
43 404c43c4 2018-06-21 stsp
44 fe621944 2020-11-10 stsp #ifndef MAX
45 fe621944 2020-11-10 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
46 fe621944 2020-11-10 stsp #endif
47 fe621944 2020-11-10 stsp
48 404c43c4 2018-06-21 stsp struct got_blame_line {
49 404c43c4 2018-06-21 stsp int annotated;
50 9b94757a 2018-06-21 stsp struct got_object_id id;
51 404c43c4 2018-06-21 stsp };
52 404c43c4 2018-06-21 stsp
53 404c43c4 2018-06-21 stsp struct got_blame {
54 404c43c4 2018-06-21 stsp FILE *f;
55 c27a5e66 2020-11-18 stsp off_t size;
56 fe621944 2020-11-10 stsp const struct diff_config *cfg;
57 6c4c42e0 2019-06-24 stsp size_t filesize;
58 6fcac457 2018-11-19 stsp int nlines;
59 06ca4d09 2018-11-19 stsp int nannotated;
60 404c43c4 2018-06-21 stsp struct got_blame_line *lines; /* one per line */
61 6c4c42e0 2019-06-24 stsp off_t *line_offsets; /* one per line */
62 c35a7943 2018-07-12 stsp int ncommits;
63 c27a5e66 2020-11-18 stsp
64 c27a5e66 2020-11-18 stsp /*
65 c27a5e66 2020-11-18 stsp * Map line numbers of an older version of the file to valid line
66 c27a5e66 2020-11-18 stsp * numbers in blame->f. This map is updated with each commit we
67 c27a5e66 2020-11-18 stsp * traverse throughout the file's history.
68 c27a5e66 2020-11-18 stsp * Lines mapped to -1 do not correspond to any line in blame->f.
69 c27a5e66 2020-11-18 stsp */
70 c27a5e66 2020-11-18 stsp int *linemap2;
71 c27a5e66 2020-11-18 stsp int nlines2;
72 404c43c4 2018-06-21 stsp };
73 404c43c4 2018-06-21 stsp
74 404c43c4 2018-06-21 stsp static const struct got_error *
75 84451b3e 2018-07-10 stsp annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
76 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
77 84451b3e 2018-07-10 stsp void *arg)
78 404c43c4 2018-06-21 stsp {
79 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
80 404c43c4 2018-06-21 stsp struct got_blame_line *line;
81 404c43c4 2018-06-21 stsp
82 c27a5e66 2020-11-18 stsp if (lineno < 0 || lineno >= blame->nlines)
83 f2e233d8 2018-11-19 stsp return NULL;
84 3168e5da 2020-09-10 stsp
85 c27a5e66 2020-11-18 stsp line = &blame->lines[lineno];
86 404c43c4 2018-06-21 stsp if (line->annotated)
87 84451b3e 2018-07-10 stsp return NULL;
88 404c43c4 2018-06-21 stsp
89 404c43c4 2018-06-21 stsp memcpy(&line->id, id, sizeof(line->id));
90 404c43c4 2018-06-21 stsp line->annotated = 1;
91 06ca4d09 2018-11-19 stsp blame->nannotated++;
92 84451b3e 2018-07-10 stsp if (cb)
93 c27a5e66 2020-11-18 stsp err = cb(arg, blame->nlines, lineno + 1, id);
94 84451b3e 2018-07-10 stsp return err;
95 404c43c4 2018-06-21 stsp }
96 404c43c4 2018-06-21 stsp
97 404c43c4 2018-06-21 stsp static const struct got_error *
98 c27a5e66 2020-11-18 stsp blame_changes(struct got_blame *blame, int *linemap1,
99 c27a5e66 2020-11-18 stsp struct diff_result *diff_result, struct got_object_id *commit_id,
100 c35a7943 2018-07-12 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
101 c35a7943 2018-07-12 stsp void *arg)
102 c35a7943 2018-07-12 stsp {
103 c35a7943 2018-07-12 stsp const struct got_error *err = NULL;
104 fe621944 2020-11-10 stsp int i;
105 c27a5e66 2020-11-18 stsp int idx1 = 0, idx2 = 0;
106 c35a7943 2018-07-12 stsp
107 c27a5e66 2020-11-18 stsp for (i = 0; i < diff_result->chunks.len &&
108 c27a5e66 2020-11-18 stsp blame->nannotated < blame->nlines; i++) {
109 fe621944 2020-11-10 stsp struct diff_chunk *c = diff_chunk_get(diff_result, i);
110 c27a5e66 2020-11-18 stsp unsigned int left_start, left_count;
111 fe621944 2020-11-10 stsp unsigned int right_start, right_count;
112 c27a5e66 2020-11-18 stsp int j;
113 c35a7943 2018-07-12 stsp
114 c27a5e66 2020-11-18 stsp /*
115 c27a5e66 2020-11-18 stsp * We do not need to worry about idx1/idx2 growing out
116 c27a5e66 2020-11-18 stsp * of bounds because the diff implementation ensures
117 c27a5e66 2020-11-18 stsp * that chunk ranges never exceed the number of lines
118 c27a5e66 2020-11-18 stsp * in the left/right input files.
119 c27a5e66 2020-11-18 stsp */
120 c27a5e66 2020-11-18 stsp left_start = diff_chunk_get_left_start(c, diff_result, 0);
121 c27a5e66 2020-11-18 stsp left_count = diff_chunk_get_left_count(c);
122 c27a5e66 2020-11-18 stsp right_start = diff_chunk_get_right_start(c, diff_result, 0);
123 c27a5e66 2020-11-18 stsp right_count = diff_chunk_get_right_count(c);
124 c27a5e66 2020-11-18 stsp
125 c27a5e66 2020-11-18 stsp if (left_count == right_count) {
126 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
127 c27a5e66 2020-11-18 stsp linemap1[idx1++] = blame->linemap2[idx2++];
128 c27a5e66 2020-11-18 stsp }
129 fe621944 2020-11-10 stsp continue;
130 c27a5e66 2020-11-18 stsp }
131 fe621944 2020-11-10 stsp
132 c27a5e66 2020-11-18 stsp if (right_count == 0) {
133 c27a5e66 2020-11-18 stsp for (j = 0; j < left_count; j++) {
134 c27a5e66 2020-11-18 stsp linemap1[idx1++] = -1;
135 c27a5e66 2020-11-18 stsp }
136 fe621944 2020-11-10 stsp continue;
137 c27a5e66 2020-11-18 stsp }
138 fe621944 2020-11-10 stsp
139 c27a5e66 2020-11-18 stsp for (j = 0; j < right_count; j++) {
140 c27a5e66 2020-11-18 stsp int ln = blame->linemap2[idx2++];
141 4c9641fd 2019-08-21 stsp err = annotate_line(blame, ln, commit_id, cb, arg);
142 c35a7943 2018-07-12 stsp if (err)
143 c35a7943 2018-07-12 stsp return err;
144 06ca4d09 2018-11-19 stsp if (blame->nlines == blame->nannotated)
145 4c9641fd 2019-08-21 stsp break;
146 c35a7943 2018-07-12 stsp }
147 c35a7943 2018-07-12 stsp }
148 c35a7943 2018-07-12 stsp
149 c35a7943 2018-07-12 stsp return NULL;
150 c35a7943 2018-07-12 stsp }
151 c35a7943 2018-07-12 stsp
152 c35a7943 2018-07-12 stsp static const struct got_error *
153 c27a5e66 2020-11-18 stsp blame_commit(struct got_blame *blame, struct got_object_id *id,
154 c27a5e66 2020-11-18 stsp const char *path, struct got_repository *repo,
155 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
156 84451b3e 2018-07-10 stsp void *arg)
157 404c43c4 2018-06-21 stsp {
158 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
159 8d725ae1 2019-08-18 stsp struct got_commit_object *commit = NULL;
160 c27a5e66 2020-11-18 stsp struct got_object_qid *pid = NULL;
161 c27a5e66 2020-11-18 stsp struct got_object_id *blob_id = NULL, *pblob_id = NULL;
162 c27a5e66 2020-11-18 stsp struct got_blob_object *blob = NULL, *pblob = NULL;
163 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
164 c27a5e66 2020-11-18 stsp FILE *f1 = NULL, *f2 = NULL;
165 c27a5e66 2020-11-18 stsp size_t size1, size2;
166 c27a5e66 2020-11-18 stsp int nlines1, nlines2;
167 c27a5e66 2020-11-18 stsp int *linemap1 = NULL;
168 404c43c4 2018-06-21 stsp
169 c27a5e66 2020-11-18 stsp err = got_object_open_as_commit(&commit, repo, id);
170 8d725ae1 2019-08-18 stsp if (err)
171 8d725ae1 2019-08-18 stsp return err;
172 8d725ae1 2019-08-18 stsp
173 c27a5e66 2020-11-18 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
174 c27a5e66 2020-11-18 stsp if (pid == NULL) {
175 c27a5e66 2020-11-18 stsp got_object_commit_close(commit);
176 c27a5e66 2020-11-18 stsp return NULL;
177 c27a5e66 2020-11-18 stsp }
178 c27a5e66 2020-11-18 stsp
179 c27a5e66 2020-11-18 stsp err = got_object_id_by_path(&blob_id, repo, id, path);
180 4c9641fd 2019-08-21 stsp if (err) {
181 4c9641fd 2019-08-21 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
182 4c9641fd 2019-08-21 stsp err = NULL;
183 404c43c4 2018-06-21 stsp goto done;
184 4c9641fd 2019-08-21 stsp }
185 27d434c2 2018-09-15 stsp
186 c27a5e66 2020-11-18 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
187 27d434c2 2018-09-15 stsp if (err)
188 27d434c2 2018-09-15 stsp goto done;
189 27d434c2 2018-09-15 stsp
190 c27a5e66 2020-11-18 stsp f2 = got_opentemp();
191 c27a5e66 2020-11-18 stsp if (f2 == NULL) {
192 c27a5e66 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
193 404c43c4 2018-06-21 stsp goto done;
194 404c43c4 2018-06-21 stsp }
195 c27a5e66 2020-11-18 stsp err = got_object_blob_dump_to_file(&size2, &nlines2, NULL,
196 c27a5e66 2020-11-18 stsp f2, blob);
197 404c43c4 2018-06-21 stsp if (err)
198 404c43c4 2018-06-21 stsp goto done;
199 404c43c4 2018-06-21 stsp
200 c27a5e66 2020-11-18 stsp err = got_object_id_by_path(&pblob_id, repo, pid->id, path);
201 c27a5e66 2020-11-18 stsp if (err) {
202 c27a5e66 2020-11-18 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
203 c27a5e66 2020-11-18 stsp err = NULL;
204 4c9641fd 2019-08-21 stsp goto done;
205 4c9641fd 2019-08-21 stsp }
206 4c9641fd 2019-08-21 stsp
207 c27a5e66 2020-11-18 stsp err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192);
208 404c43c4 2018-06-21 stsp if (err)
209 404c43c4 2018-06-21 stsp goto done;
210 404c43c4 2018-06-21 stsp
211 c27a5e66 2020-11-18 stsp f1 = got_opentemp();
212 c27a5e66 2020-11-18 stsp if (f1 == NULL) {
213 c27a5e66 2020-11-18 stsp err = got_error_from_errno("got_opentemp");
214 c27a5e66 2020-11-18 stsp goto done;
215 c27a5e66 2020-11-18 stsp }
216 c27a5e66 2020-11-18 stsp err = got_object_blob_dump_to_file(&size1, &nlines1, NULL, f1, pblob);
217 c27a5e66 2020-11-18 stsp if (err)
218 c27a5e66 2020-11-18 stsp goto done;
219 c27a5e66 2020-11-18 stsp
220 c27a5e66 2020-11-18 stsp err = got_diff_files(&diffreg_result, f1, "", f2, "",
221 c27a5e66 2020-11-18 stsp 0, 0, NULL);
222 c27a5e66 2020-11-18 stsp if (err)
223 c27a5e66 2020-11-18 stsp goto done;
224 fe621944 2020-11-10 stsp if (diffreg_result->result->chunks.len > 0) {
225 c27a5e66 2020-11-18 stsp if (nlines1 > 0) {
226 c27a5e66 2020-11-18 stsp linemap1 = calloc(nlines1, sizeof(*linemap1));
227 c27a5e66 2020-11-18 stsp if (linemap1 == NULL) {
228 c27a5e66 2020-11-18 stsp err = got_error_from_errno("malloc");
229 c27a5e66 2020-11-18 stsp goto done;
230 c27a5e66 2020-11-18 stsp }
231 c27a5e66 2020-11-18 stsp }
232 c27a5e66 2020-11-18 stsp err = blame_changes(blame, linemap1,
233 c27a5e66 2020-11-18 stsp diffreg_result->result, id, cb, arg);
234 c27a5e66 2020-11-18 stsp if (err) {
235 c27a5e66 2020-11-18 stsp free(linemap1);
236 c27a5e66 2020-11-18 stsp goto done;
237 c27a5e66 2020-11-18 stsp }
238 c27a5e66 2020-11-18 stsp if (linemap1) {
239 c27a5e66 2020-11-18 stsp free(blame->linemap2);
240 c27a5e66 2020-11-18 stsp blame->linemap2 = linemap1;
241 c27a5e66 2020-11-18 stsp blame->nlines2 = nlines1;
242 c27a5e66 2020-11-18 stsp }
243 d68a0a7d 2018-07-10 stsp } else if (cb)
244 3bf198ba 2018-07-10 stsp err = cb(arg, blame->nlines, -1, id);
245 404c43c4 2018-06-21 stsp done:
246 fe621944 2020-11-10 stsp if (diffreg_result) {
247 fe621944 2020-11-10 stsp const struct got_error *free_err;
248 c27a5e66 2020-11-18 stsp free_err = got_diffreg_result_free(diffreg_result);
249 fe621944 2020-11-10 stsp if (free_err && err == NULL)
250 fe621944 2020-11-10 stsp err = free_err;
251 fe621944 2020-11-10 stsp }
252 8d725ae1 2019-08-18 stsp if (commit)
253 8d725ae1 2019-08-18 stsp got_object_commit_close(commit);
254 c27a5e66 2020-11-18 stsp free(blob_id);
255 c27a5e66 2020-11-18 stsp free(pblob_id);
256 404c43c4 2018-06-21 stsp if (blob)
257 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
258 c27a5e66 2020-11-18 stsp if (pblob)
259 c27a5e66 2020-11-18 stsp got_object_blob_close(pblob);
260 c27a5e66 2020-11-18 stsp if (f1 && fclose(f1) != 0 && err == NULL)
261 c27a5e66 2020-11-18 stsp err = got_error_from_errno("fclose");
262 c27a5e66 2020-11-18 stsp if (f2 && fclose(f2) != 0 && err == NULL)
263 c27a5e66 2020-11-18 stsp err = got_error_from_errno("fclose");
264 404c43c4 2018-06-21 stsp return err;
265 404c43c4 2018-06-21 stsp }
266 404c43c4 2018-06-21 stsp
267 fb43ecf1 2019-02-11 stsp static const struct got_error *
268 404c43c4 2018-06-21 stsp blame_close(struct got_blame *blame)
269 404c43c4 2018-06-21 stsp {
270 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
271 c35a7943 2018-07-12 stsp
272 fe621944 2020-11-10 stsp if (blame->f && fclose(blame->f) != 0 && err == NULL)
273 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
274 404c43c4 2018-06-21 stsp free(blame->lines);
275 c27a5e66 2020-11-18 stsp free(blame->linemap2);
276 404c43c4 2018-06-21 stsp free(blame);
277 fb43ecf1 2019-02-11 stsp return err;
278 404c43c4 2018-06-21 stsp }
279 404c43c4 2018-06-21 stsp
280 404c43c4 2018-06-21 stsp static const struct got_error *
281 404c43c4 2018-06-21 stsp blame_open(struct got_blame **blamep, const char *path,
282 84451b3e 2018-07-10 stsp struct got_object_id *start_commit_id, struct got_repository *repo,
283 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
284 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void *cancel_arg)
285 404c43c4 2018-06-21 stsp {
286 404c43c4 2018-06-21 stsp const struct got_error *err = NULL;
287 404c43c4 2018-06-21 stsp struct got_object *obj = NULL;
288 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
289 404c43c4 2018-06-21 stsp struct got_blob_object *blob = NULL;
290 404c43c4 2018-06-21 stsp struct got_blame *blame = NULL;
291 c27a5e66 2020-11-18 stsp struct got_object_id *id = NULL;
292 c27a5e66 2020-11-18 stsp int lineno;
293 293f6400 2018-09-20 stsp struct got_commit_graph *graph = NULL;
294 404c43c4 2018-06-21 stsp
295 404c43c4 2018-06-21 stsp *blamep = NULL;
296 404c43c4 2018-06-21 stsp
297 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
298 404c43c4 2018-06-21 stsp if (err)
299 c27a5e66 2020-11-18 stsp goto done;
300 27d434c2 2018-09-15 stsp
301 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
302 27d434c2 2018-09-15 stsp if (err)
303 27d434c2 2018-09-15 stsp goto done;
304 27d434c2 2018-09-15 stsp
305 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
306 2c2d5c5f 2020-06-07 stsp err = got_error_path(path, GOT_ERR_OBJ_TYPE);
307 404c43c4 2018-06-21 stsp goto done;
308 404c43c4 2018-06-21 stsp }
309 404c43c4 2018-06-21 stsp
310 404c43c4 2018-06-21 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
311 404c43c4 2018-06-21 stsp if (err)
312 404c43c4 2018-06-21 stsp goto done;
313 404c43c4 2018-06-21 stsp
314 404c43c4 2018-06-21 stsp blame = calloc(1, sizeof(*blame));
315 c27a5e66 2020-11-18 stsp if (blame == NULL) {
316 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
317 c27a5e66 2020-11-18 stsp goto done;
318 c27a5e66 2020-11-18 stsp }
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 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
323 404c43c4 2018-06-21 stsp goto done;
324 404c43c4 2018-06-21 stsp }
325 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
326 62d463ca 2020-10-20 naddy &blame->line_offsets, blame->f, blob);
327 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
328 404c43c4 2018-06-21 stsp goto done;
329 b02560ec 2019-08-19 stsp
330 c27a5e66 2020-11-18 stsp blame->cfg = got_diff_get_config(GOT_DIFF_ALGORITHM_PATIENCE);
331 fe621944 2020-11-10 stsp
332 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
333 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
334 b02560ec 2019-08-19 stsp blame->nlines--;
335 404c43c4 2018-06-21 stsp
336 404c43c4 2018-06-21 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
337 404c43c4 2018-06-21 stsp if (blame->lines == NULL) {
338 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
339 404c43c4 2018-06-21 stsp goto done;
340 404c43c4 2018-06-21 stsp }
341 404c43c4 2018-06-21 stsp
342 c27a5e66 2020-11-18 stsp blame->nlines2 = blame->nlines;
343 c27a5e66 2020-11-18 stsp blame->linemap2 = calloc(blame->nlines2, sizeof(*blame->linemap2));
344 c27a5e66 2020-11-18 stsp if (blame->linemap2 == NULL) {
345 c27a5e66 2020-11-18 stsp err = got_error_from_errno("calloc");
346 c27a5e66 2020-11-18 stsp goto done;
347 c27a5e66 2020-11-18 stsp }
348 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines2; lineno++)
349 c27a5e66 2020-11-18 stsp blame->linemap2[lineno] = lineno;
350 c27a5e66 2020-11-18 stsp
351 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, 1);
352 404c43c4 2018-06-21 stsp if (err)
353 c27a5e66 2020-11-18 stsp goto done;
354 c27a5e66 2020-11-18 stsp
355 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, start_commit_id, repo,
356 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
357 293f6400 2018-09-20 stsp if (err)
358 404c43c4 2018-06-21 stsp goto done;
359 656b1f76 2019-05-11 jcs for (;;) {
360 c27a5e66 2020-11-18 stsp struct got_object_id *next_id;
361 c27a5e66 2020-11-18 stsp err = got_commit_graph_iter_next(&next_id, graph, repo,
362 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
363 404c43c4 2018-06-21 stsp if (err) {
364 c27a5e66 2020-11-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
365 4c9641fd 2019-08-21 stsp err = NULL;
366 c27a5e66 2020-11-18 stsp break;
367 c27a5e66 2020-11-18 stsp }
368 c27a5e66 2020-11-18 stsp goto done;
369 293f6400 2018-09-20 stsp }
370 c27a5e66 2020-11-18 stsp if (next_id) {
371 c27a5e66 2020-11-18 stsp id = next_id;
372 c27a5e66 2020-11-18 stsp err = blame_commit(blame, id, path, repo, cb, arg);
373 293f6400 2018-09-20 stsp if (err) {
374 293f6400 2018-09-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
375 293f6400 2018-09-20 stsp err = NULL;
376 c27a5e66 2020-11-18 stsp goto done;
377 293f6400 2018-09-20 stsp }
378 06ca4d09 2018-11-19 stsp if (blame->nannotated == blame->nlines)
379 06ca4d09 2018-11-19 stsp break;
380 404c43c4 2018-06-21 stsp }
381 293f6400 2018-09-20 stsp }
382 ed77f2ae 2018-06-21 stsp
383 06ca4d09 2018-11-19 stsp if (id && blame->nannotated < blame->nlines) {
384 293f6400 2018-09-20 stsp /* Annotate remaining non-annotated lines with last commit. */
385 c27a5e66 2020-11-18 stsp for (lineno = 0; lineno < blame->nlines; lineno++) {
386 293f6400 2018-09-20 stsp err = annotate_line(blame, lineno, id, cb, arg);
387 293f6400 2018-09-20 stsp if (err)
388 293f6400 2018-09-20 stsp goto done;
389 404c43c4 2018-06-21 stsp }
390 404c43c4 2018-06-21 stsp }
391 404c43c4 2018-06-21 stsp
392 404c43c4 2018-06-21 stsp done:
393 293f6400 2018-09-20 stsp if (graph)
394 293f6400 2018-09-20 stsp got_commit_graph_close(graph);
395 27d434c2 2018-09-15 stsp free(obj_id);
396 404c43c4 2018-06-21 stsp if (obj)
397 404c43c4 2018-06-21 stsp got_object_close(obj);
398 404c43c4 2018-06-21 stsp if (blob)
399 404c43c4 2018-06-21 stsp got_object_blob_close(blob);
400 1828273a 2018-07-09 stsp if (err) {
401 1828273a 2018-07-09 stsp if (blame)
402 1828273a 2018-07-09 stsp blame_close(blame);
403 1828273a 2018-07-09 stsp } else
404 404c43c4 2018-06-21 stsp *blamep = blame;
405 404c43c4 2018-06-21 stsp
406 404c43c4 2018-06-21 stsp return err;
407 404c43c4 2018-06-21 stsp }
408 84451b3e 2018-07-10 stsp
409 84451b3e 2018-07-10 stsp const struct got_error *
410 0d8ff7d5 2019-08-14 stsp got_blame(const char *path, struct got_object_id *commit_id,
411 84451b3e 2018-07-10 stsp struct got_repository *repo,
412 84451b3e 2018-07-10 stsp const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
413 6fb7cd11 2019-08-22 stsp void *arg, got_cancel_cb cancel_cb, void* cancel_arg)
414 84451b3e 2018-07-10 stsp {
415 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL, *close_err = NULL;
416 84451b3e 2018-07-10 stsp struct got_blame *blame;
417 84451b3e 2018-07-10 stsp char *abspath;
418 84451b3e 2018-07-10 stsp
419 84451b3e 2018-07-10 stsp if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
420 638f9024 2019-05-13 stsp return got_error_from_errno2("asprintf", path);
421 84451b3e 2018-07-10 stsp
422 6fb7cd11 2019-08-22 stsp err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
423 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
424 84451b3e 2018-07-10 stsp free(abspath);
425 26206841 2018-07-12 stsp if (blame)
426 fb43ecf1 2019-02-11 stsp close_err = blame_close(blame);
427 fb43ecf1 2019-02-11 stsp return err ? err : close_err;
428 84451b3e 2018-07-10 stsp }