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);
272 blame_close(struct got_blame *blame)
274 struct got_blame_diff_offsets *diff_offsets;
279 while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
280 diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
281 SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
282 free_diff_offsets(diff_offsets);
287 static const struct got_error *
288 blame_open(struct got_blame **blamep, const char *path,
289 struct got_object_id *start_commit_id, struct got_repository *repo,
290 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
293 const struct got_error *err = NULL;
294 struct got_object *obj = NULL;
295 struct got_object_id *obj_id = NULL;
296 struct got_blob_object *blob = NULL;
297 struct got_blame *blame = NULL;
298 struct got_object_id *id = NULL;
300 struct got_commit_graph *graph = NULL;
304 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
308 err = got_object_open(&obj, repo, obj_id);
312 if (obj->type != GOT_OBJ_TYPE_BLOB) {
313 err = got_error(GOT_ERR_OBJ_TYPE);
317 err = got_object_blob_open(&blob, repo, obj, 8192);
321 blame = calloc(1, sizeof(*blame));
323 return got_error_from_errno();
325 blame->f = got_opentemp();
326 if (blame->f == NULL) {
327 err = got_error_from_errno();
330 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
335 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
336 if (blame->lines == NULL) {
337 err = got_error_from_errno();
341 err = got_commit_graph_open(&graph, start_commit_id, path, 0, repo);
344 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
350 struct got_object_id *next_id;
352 err = got_commit_graph_iter_next(&next_id, graph);
354 if (err->code == GOT_ERR_ITER_COMPLETED) {
358 if (err->code != GOT_ERR_ITER_NEED_MORE)
360 err = got_commit_graph_fetch_commits(graph, 1, repo);
369 err = blame_commit(blame, id, next_id, path, repo,
372 if (err->code == GOT_ERR_ITER_COMPLETED)
376 if (blame->nannotated == blame->nlines)
382 if (id && blame->nannotated < blame->nlines) {
383 /* Annotate remaining non-annotated lines with last commit. */
384 for (lineno = 1; lineno <= blame->nlines; lineno++) {
385 err = annotate_line(blame, lineno, id, cb, arg);
393 got_commit_graph_close(graph);
396 got_object_close(obj);
398 got_object_blob_close(blob);
408 static const struct got_error *
409 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
411 if (lineno < 1 || lineno > blame->nlines)
412 return got_error(GOT_ERR_RANGE);
413 *id = &blame->lines[lineno - 1].id;
418 parse_next_line(FILE *f, size_t *len)
423 const char delim[3] = { '\0', '\0', '\0'};
425 line = fparseln(f, &linelen, &lineno, delim, 0);
431 const struct got_error *
432 got_blame(const char *path, struct got_object_id *start_commit_id,
433 struct got_repository *repo, FILE *outfile)
435 const struct got_error *err = NULL;
436 struct got_blame *blame;
440 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
441 return got_error_from_errno();
443 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
449 for (lineno = 1; lineno <= blame->nlines; lineno++) {
450 struct got_object_id *id;
453 line = parse_next_line(blame->f, NULL);
457 err = blame_line(&id, blame, lineno);
463 err = got_object_id_str(&id_str, id);
464 /* Do not free id; It points into blame->lines. */
470 fprintf(outfile, "%.8s %s\n", id_str, line);
480 const struct got_error *
481 got_blame_incremental(const char *path, struct got_object_id *commit_id,
482 struct got_repository *repo,
483 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
486 const struct got_error *err = NULL;
487 struct got_blame *blame;
490 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
491 return got_error_from_errno();
493 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);