2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.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 {
42 struct got_object_id id;
45 struct got_blame_diff_offsets {
46 struct got_diffoffset_chunks *chunks;
47 struct got_object_id *commit_id;
48 SLIST_ENTRY(got_blame_diff_offsets) entry;
51 SLIST_HEAD(got_blame_diff_offsets_list, got_blame_diff_offsets);
57 struct got_blame_line *lines; /* one per line */
59 struct got_blame_diff_offsets_list diff_offsets_list;
63 free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
65 if (diff_offsets->chunks)
66 got_diffoffset_free(diff_offsets->chunks);
67 free(diff_offsets->commit_id);
71 static const struct got_error *
72 alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
73 struct got_object_id *commit_id)
75 const struct got_error *err = NULL;
77 *diff_offsets = calloc(1, sizeof(**diff_offsets));
78 if (*diff_offsets == NULL)
79 return got_error_from_errno();
81 (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
82 if ((*diff_offsets)->commit_id == NULL) {
83 err = got_error_from_errno();
84 free_diff_offsets(*diff_offsets);
89 err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
91 free_diff_offsets(*diff_offsets);
98 static const struct got_error *
99 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
100 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
103 const struct got_error *err = NULL;
104 struct got_blame_line *line;
106 if (lineno < 1 || lineno > blame->nlines)
109 line = &blame->lines[lineno - 1];
113 memcpy(&line->id, id, sizeof(line->id));
117 err = cb(arg, blame->nlines, lineno, id);
122 get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
125 struct got_blame_diff_offsets *diff_offsets;
127 SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
128 lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
133 static const struct got_error *
134 blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
135 struct got_object_id *commit_id,
136 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
139 const struct got_error *err = NULL;
140 struct got_diff_change *change;
141 struct got_blame_diff_offsets *diff_offsets;
143 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
144 int c = change->cv.c;
145 int d = change->cv.d;
147 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
150 for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
151 err = annotate_line(blame,
152 get_blamed_line(&blame->diff_offsets_list, ln),
156 if (blame->nlines == blame->nannotated)
161 err = alloc_diff_offsets(&diff_offsets, commit_id);
164 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
165 int a = change->cv.a;
166 int b = change->cv.b;
167 int c = change->cv.c;
168 int d = change->cv.d;
170 int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
172 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
174 err = got_diffoffset_add(diff_offsets->chunks,
175 old_lineno, old_length, new_lineno, new_length);
177 free_diff_offsets(diff_offsets);
181 SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
186 static const struct got_error *
187 blame_commit(struct got_blame *blame, struct got_object_id *id,
188 struct got_object_id *pid, const char *path, struct got_repository *repo,
189 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
192 const struct got_error *err = NULL;
193 struct got_object *obj = NULL, *pobj = NULL;
194 struct got_object_id *obj_id = NULL, *pobj_id = NULL;
195 struct got_blob_object *blob = NULL, *pblob = NULL;
196 struct got_diff_changes *changes = NULL;
198 err = got_object_id_by_path(&obj_id, repo, id, path);
202 err = got_object_open(&obj, repo, obj_id);
206 if (obj->type != GOT_OBJ_TYPE_BLOB) {
207 err = got_error(GOT_ERR_OBJ_TYPE);
211 err = got_object_id_by_path(&pobj_id, repo, pid, path);
213 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
214 /* Blob's history began in previous commit. */
215 err = got_error(GOT_ERR_ITER_COMPLETED);
220 /* If IDs match then don't bother with diffing. */
221 if (got_object_id_cmp(obj_id, pobj_id) == 0) {
223 err = cb(arg, blame->nlines, -1, id);
227 err = got_object_open(&pobj, repo, pobj_id);
231 if (pobj->type != GOT_OBJ_TYPE_BLOB) {
233 * Encountered a non-blob at the path (probably a tree).
234 * Blob's history began in previous commit.
236 err = got_error(GOT_ERR_ITER_COMPLETED);
240 err = got_object_blob_open(&blob, repo, obj, 8192);
244 err = got_object_blob_open(&pblob, repo, pobj, 8192);
248 err = got_diff_blob_lines_changed(&changes, pblob, blob);
253 err = blame_changes(blame, changes, id, cb, arg);
254 got_diff_free_changes(changes);
256 err = cb(arg, blame->nlines, -1, id);
261 got_object_close(obj);
263 got_object_close(pobj);
265 got_object_blob_close(blob);
267 got_object_blob_close(pblob);
271 static const struct got_error *
272 blame_close(struct got_blame *blame)
274 const struct got_error *err = NULL;
275 struct got_blame_diff_offsets *diff_offsets;
277 if (blame->f && fclose(blame->f) != 0)
278 err = got_error_from_errno();
280 while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
281 diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
282 SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
283 free_diff_offsets(diff_offsets);
289 static const struct got_error *
290 blame_open(struct got_blame **blamep, const char *path,
291 struct got_object_id *start_commit_id, struct got_repository *repo,
292 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
295 const struct got_error *err = NULL;
296 struct got_object *obj = NULL;
297 struct got_object_id *obj_id = NULL;
298 struct got_blob_object *blob = NULL;
299 struct got_blame *blame = NULL;
300 struct got_object_id *id = NULL;
302 struct got_commit_graph *graph = NULL;
306 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
310 err = got_object_open(&obj, repo, obj_id);
314 if (obj->type != GOT_OBJ_TYPE_BLOB) {
315 err = got_error(GOT_ERR_OBJ_TYPE);
319 err = got_object_blob_open(&blob, repo, obj, 8192);
323 blame = calloc(1, sizeof(*blame));
325 return got_error_from_errno();
327 blame->f = got_opentemp();
328 if (blame->f == NULL) {
329 err = got_error_from_errno();
332 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
337 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
338 if (blame->lines == NULL) {
339 err = got_error_from_errno();
343 err = got_commit_graph_open(&graph, start_commit_id, path, 0, repo);
346 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
352 struct got_object_id *next_id;
354 err = got_commit_graph_iter_next(&next_id, graph);
356 if (err->code == GOT_ERR_ITER_COMPLETED) {
360 if (err->code != GOT_ERR_ITER_NEED_MORE)
362 err = got_commit_graph_fetch_commits(graph, 1, repo);
371 err = blame_commit(blame, id, next_id, path, repo,
374 if (err->code == GOT_ERR_ITER_COMPLETED)
378 if (blame->nannotated == blame->nlines)
384 if (id && blame->nannotated < blame->nlines) {
385 /* Annotate remaining non-annotated lines with last commit. */
386 for (lineno = 1; lineno <= blame->nlines; lineno++) {
387 err = annotate_line(blame, lineno, id, cb, arg);
395 got_commit_graph_close(graph);
398 got_object_close(obj);
400 got_object_blob_close(blob);
410 static const struct got_error *
411 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
413 if (lineno < 1 || lineno > blame->nlines)
414 return got_error(GOT_ERR_RANGE);
415 *id = &blame->lines[lineno - 1].id;
420 parse_next_line(FILE *f, size_t *len)
425 const char delim[3] = { '\0', '\0', '\0'};
427 line = fparseln(f, &linelen, &lineno, delim, 0);
433 const struct got_error *
434 got_blame(const char *path, struct got_object_id *start_commit_id,
435 struct got_repository *repo, FILE *outfile)
437 const struct got_error *err = NULL, *close_err = NULL;
438 struct got_blame *blame;
442 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
443 return got_error_from_errno();
445 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
451 for (lineno = 1; lineno <= blame->nlines; lineno++) {
452 struct got_object_id *id;
455 line = parse_next_line(blame->f, NULL);
459 err = blame_line(&id, blame, lineno);
465 err = got_object_id_str(&id_str, id);
466 /* Do not free id; It points into blame->lines. */
472 fprintf(outfile, "%.8s %s\n", id_str, line);
477 close_err = blame_close(blame);
479 return err ? err : close_err;
482 const struct got_error *
483 got_blame_incremental(const char *path, struct got_object_id *commit_id,
484 struct got_repository *repo,
485 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
488 const struct got_error *err = NULL, *close_err = NULL;
489 struct got_blame *blame;
492 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
493 return got_error_from_errno();
495 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
498 close_err = blame_close(blame);
499 return err ? err : close_err;