Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <sha1.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <util.h>
26 #include <zlib.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_blame.h"
31 #include "got_opentemp.h"
33 #include "got_lib_inflate.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_object.h"
36 #include "got_lib_diff.h"
37 #include "got_lib_diffoffset.h"
38 #include "got_commit_graph.h"
40 struct got_blame_line {
41 int annotated;
42 struct got_object_id id;
43 off_t offset;
44 };
46 struct got_blame_diff_offsets {
47 struct got_diffoffset_chunks *chunks;
48 struct got_object_id *commit_id;
49 SLIST_ENTRY(got_blame_diff_offsets) entry;
50 };
52 SLIST_HEAD(got_blame_diff_offsets_list, got_blame_diff_offsets);
54 struct got_blame {
55 FILE *f;
56 size_t filesize;
57 int nlines;
58 int nannotated;
59 struct got_blame_line *lines; /* one per line */
60 off_t *line_offsets; /* one per line */
61 int ncommits;
62 struct got_blame_diff_offsets_list diff_offsets_list;
63 };
65 static void
66 free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
67 {
68 if (diff_offsets->chunks)
69 got_diffoffset_free(diff_offsets->chunks);
70 free(diff_offsets->commit_id);
71 free(diff_offsets);
72 }
74 static const struct got_error *
75 alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
76 struct got_object_id *commit_id)
77 {
78 const struct got_error *err = NULL;
80 *diff_offsets = calloc(1, sizeof(**diff_offsets));
81 if (*diff_offsets == NULL)
82 return got_error_from_errno("calloc");
84 (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
85 if ((*diff_offsets)->commit_id == NULL) {
86 err = got_error_from_errno("got_object_id_dup");
87 free_diff_offsets(*diff_offsets);
88 *diff_offsets = NULL;
89 return err;
90 }
92 err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
93 if (err) {
94 free_diff_offsets(*diff_offsets);
95 return err;
96 }
98 return NULL;
99 }
101 static const struct got_error *
102 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
103 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
104 void *arg)
106 const struct got_error *err = NULL;
107 struct got_blame_line *line;
109 if (lineno < 1 || lineno > blame->nlines)
110 return NULL;
112 line = &blame->lines[lineno - 1];
113 if (line->annotated)
114 return NULL;
116 memcpy(&line->id, id, sizeof(line->id));
117 line->annotated = 1;
118 blame->nannotated++;
119 if (cb)
120 err = cb(arg, blame->nlines, lineno, id);
121 return err;
124 static int
125 get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
126 int lineno)
128 struct got_blame_diff_offsets *diff_offsets;
130 SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
131 lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
133 return lineno;
136 static const struct got_error *
137 blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
138 struct got_object_id *commit_id,
139 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
140 void *arg)
142 const struct got_error *err = NULL;
143 struct got_diff_change *change;
144 struct got_blame_diff_offsets *diff_offsets;
146 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
147 int c = change->cv.c;
148 int d = change->cv.d;
149 int new_lineno = c;
150 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
151 int ln;
153 for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
154 err = annotate_line(blame,
155 get_blamed_line(&blame->diff_offsets_list, ln),
156 commit_id, cb, arg);
157 if (err)
158 return err;
159 if (blame->nlines == blame->nannotated)
160 return NULL;
164 err = alloc_diff_offsets(&diff_offsets, commit_id);
165 if (err)
166 return err;
167 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
168 int a = change->cv.a;
169 int b = change->cv.b;
170 int c = change->cv.c;
171 int d = change->cv.d;
172 int old_lineno = a;
173 int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
174 int new_lineno = c;
175 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
177 err = got_diffoffset_add(diff_offsets->chunks,
178 old_lineno, old_length, new_lineno, new_length);
179 if (err) {
180 free_diff_offsets(diff_offsets);
181 return err;
184 SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
186 return NULL;
189 static const struct got_error *
190 blame_commit(struct got_blame *blame, struct got_object_id *id,
191 struct got_object_id *pid, const char *path, struct got_repository *repo,
192 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
193 void *arg)
195 const struct got_error *err = NULL;
196 struct got_object *obj = NULL, *pobj = NULL;
197 struct got_object_id *obj_id = NULL, *pobj_id = NULL;
198 struct got_blob_object *blob = NULL, *pblob = NULL;
199 struct got_diff_changes *changes = NULL;
201 err = got_object_id_by_path(&obj_id, repo, id, path);
202 if (err)
203 goto done;
205 err = got_object_open(&obj, repo, obj_id);
206 if (err)
207 goto done;
209 if (obj->type != GOT_OBJ_TYPE_BLOB) {
210 err = got_error(GOT_ERR_OBJ_TYPE);
211 goto done;
214 err = got_object_id_by_path(&pobj_id, repo, pid, path);
215 if (err) {
216 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
217 /* Blob's history began in previous commit. */
218 err = got_error(GOT_ERR_ITER_COMPLETED);
220 goto done;
223 /* If IDs match then don't bother with diffing. */
224 if (got_object_id_cmp(obj_id, pobj_id) == 0) {
225 if (cb)
226 err = cb(arg, blame->nlines, -1, id);
227 goto done;
230 err = got_object_open(&pobj, repo, pobj_id);
231 if (err)
232 goto done;
234 if (pobj->type != GOT_OBJ_TYPE_BLOB) {
235 /*
236 * Encountered a non-blob at the path (probably a tree).
237 * Blob's history began in previous commit.
238 */
239 err = got_error(GOT_ERR_ITER_COMPLETED);
240 goto done;
243 err = got_object_blob_open(&blob, repo, obj, 8192);
244 if (err)
245 goto done;
247 err = got_object_blob_open(&pblob, repo, pobj, 8192);
248 if (err)
249 goto done;
251 err = got_diff_blob_lines_changed(&changes, pblob, blob);
252 if (err)
253 goto done;
255 if (changes) {
256 err = blame_changes(blame, changes, id, cb, arg);
257 got_diff_free_changes(changes);
258 } else if (cb)
259 err = cb(arg, blame->nlines, -1, id);
260 done:
261 free(obj_id);
262 free(pobj_id);
263 if (obj)
264 got_object_close(obj);
265 if (pobj)
266 got_object_close(pobj);
267 if (blob)
268 got_object_blob_close(blob);
269 if (pblob)
270 got_object_blob_close(pblob);
271 return err;
274 static const struct got_error *
275 blame_close(struct got_blame *blame)
277 const struct got_error *err = NULL;
278 struct got_blame_diff_offsets *diff_offsets;
280 if (blame->f && fclose(blame->f) != 0)
281 err = got_error_from_errno("fclose");
282 free(blame->lines);
283 while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
284 diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
285 SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
286 free_diff_offsets(diff_offsets);
288 free(blame);
289 return err;
292 static const struct got_error *
293 blame_open(struct got_blame **blamep, const char *path,
294 struct got_object_id *start_commit_id, struct got_repository *repo,
295 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
296 void *arg)
298 const struct got_error *err = NULL;
299 struct got_object *obj = NULL;
300 struct got_object_id *obj_id = NULL;
301 struct got_blob_object *blob = NULL;
302 struct got_blame *blame = NULL;
303 struct got_object_id *id = NULL;
304 int lineno;
305 struct got_commit_graph *graph = NULL;
307 *blamep = NULL;
309 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
310 if (err)
311 return err;
313 err = got_object_open(&obj, repo, obj_id);
314 if (err)
315 goto done;
317 if (obj->type != GOT_OBJ_TYPE_BLOB) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
322 err = got_object_blob_open(&blob, repo, obj, 8192);
323 if (err)
324 goto done;
326 blame = calloc(1, sizeof(*blame));
327 if (blame == NULL)
328 return got_error_from_errno("calloc");
330 blame->f = got_opentemp();
331 if (blame->f == NULL) {
332 err = got_error_from_errno("got_opentemp");
333 goto done;
335 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
336 &blame->line_offsets, blame->f, blob);
337 if (err)
338 goto done;
340 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
341 if (blame->lines == NULL) {
342 err = got_error_from_errno("calloc");
343 goto done;
346 err = got_commit_graph_open(&graph, start_commit_id, path, 0, repo);
347 if (err)
348 return err;
349 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
350 if (err)
351 goto done;
353 id = NULL;
354 for (;;) {
355 struct got_object_id *next_id;
357 err = got_commit_graph_iter_next(&next_id, graph);
358 if (err) {
359 if (err->code == GOT_ERR_ITER_COMPLETED) {
360 err = NULL;
361 break;
363 if (err->code != GOT_ERR_ITER_NEED_MORE)
364 break;
365 err = got_commit_graph_fetch_commits(graph, 1, repo);
366 if (err)
367 break;
368 else
369 continue;
371 if (next_id == NULL)
372 break;
373 if (id) {
374 err = blame_commit(blame, id, next_id, path, repo,
375 cb, arg);
376 if (err) {
377 if (err->code == GOT_ERR_ITER_COMPLETED)
378 err = NULL;
379 break;
381 if (blame->nannotated == blame->nlines)
382 break;
384 id = next_id;
387 if (id && blame->nannotated < blame->nlines) {
388 /* Annotate remaining non-annotated lines with last commit. */
389 for (lineno = 1; lineno <= blame->nlines; lineno++) {
390 err = annotate_line(blame, lineno, id, cb, arg);
391 if (err)
392 goto done;
396 done:
397 if (graph)
398 got_commit_graph_close(graph);
399 free(obj_id);
400 if (obj)
401 got_object_close(obj);
402 if (blob)
403 got_object_blob_close(blob);
404 if (err) {
405 if (blame)
406 blame_close(blame);
407 } else
408 *blamep = blame;
410 return err;
413 static const struct got_error *
414 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
416 if (lineno < 1 || lineno > blame->nlines)
417 return got_error(GOT_ERR_RANGE);
418 *id = &blame->lines[lineno - 1].id;
419 return NULL;
422 static char *
423 parse_next_line(FILE *f, size_t *len)
425 char *line;
426 size_t linelen;
427 size_t lineno;
428 const char delim[3] = { '\0', '\0', '\0'};
430 line = fparseln(f, &linelen, &lineno, delim, 0);
431 if (len)
432 *len = linelen;
433 return line;
436 const struct got_error *
437 got_blame(const char *path, struct got_object_id *start_commit_id,
438 struct got_repository *repo, FILE *outfile)
440 const struct got_error *err = NULL, *close_err = NULL;
441 struct got_blame *blame;
442 int lineno;
443 char *abspath;
445 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
446 return got_error_from_errno2("asprintf", path);
448 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
449 if (err) {
450 free(abspath);
451 return err;
454 for (lineno = 1; lineno <= blame->nlines; lineno++) {
455 struct got_object_id *id;
456 char *line, *id_str;
458 line = parse_next_line(blame->f, NULL);
459 if (line == NULL)
460 break;
462 err = blame_line(&id, blame, lineno);
463 if (err) {
464 free(line);
465 break;
468 err = got_object_id_str(&id_str, id);
469 /* Do not free id; It points into blame->lines. */
470 if (err) {
471 free(line);
472 break;
475 fprintf(outfile, "%.8s %s\n", id_str, line);
476 free(line);
477 free(id_str);
480 close_err = blame_close(blame);
481 free(abspath);
482 return err ? err : close_err;
485 const struct got_error *
486 got_blame_incremental(const char *path, struct got_object_id *commit_id,
487 struct got_repository *repo,
488 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
489 void *arg)
491 const struct got_error *err = NULL, *close_err = NULL;
492 struct got_blame *blame;
493 char *abspath;
495 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
496 return got_error_from_errno2("asprintf", path);
498 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
499 free(abspath);
500 if (blame)
501 close_err = blame_close(blame);
502 return err ? err : close_err;