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);
56 struct got_blame_line *lines; /* one per line */
58 struct got_blame_diff_offsets_list diff_offsets_list;
62 free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
64 if (diff_offsets->chunks)
65 got_diffoffset_free(diff_offsets->chunks);
66 free(diff_offsets->commit_id);
70 static const struct got_error *
71 alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
72 struct got_object_id *commit_id)
74 const struct got_error *err = NULL;
76 *diff_offsets = calloc(1, sizeof(**diff_offsets));
77 if (*diff_offsets == NULL)
78 return got_error_from_errno();
80 (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
81 if ((*diff_offsets)->commit_id == NULL) {
82 err = got_error_from_errno();
83 free_diff_offsets(*diff_offsets);
88 err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
90 free_diff_offsets(*diff_offsets);
97 static const struct got_error *
98 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
99 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
102 const struct got_error *err = NULL;
103 struct got_blame_line *line;
105 if (lineno < 1 || lineno > blame->nlines)
106 return got_error(GOT_ERR_RANGE);
108 line = &blame->lines[lineno - 1];
112 memcpy(&line->id, id, sizeof(line->id));
115 err = cb(arg, blame->nlines, lineno, id);
120 get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
123 struct got_blame_diff_offsets *diff_offsets;
125 SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
126 lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
131 static const struct got_error *
132 blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
133 struct got_object_id *commit_id,
134 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
137 const struct got_error *err = NULL;
138 struct got_diff_change *change;
139 struct got_blame_diff_offsets *diff_offsets;
141 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
142 int c = change->cv.c;
143 int d = change->cv.d;
145 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
148 for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
149 err = annotate_line(blame,
150 get_blamed_line(&blame->diff_offsets_list, ln),
157 err = alloc_diff_offsets(&diff_offsets, commit_id);
160 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
161 int a = change->cv.a;
162 int b = change->cv.b;
163 int c = change->cv.c;
164 int d = change->cv.d;
166 int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
168 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
170 err = got_diffoffset_add(diff_offsets->chunks,
171 old_lineno, old_length, new_lineno, new_length);
173 free_diff_offsets(diff_offsets);
177 SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
182 static const struct got_error *
183 blame_commit(struct got_blame *blame, struct got_object_id *id,
184 struct got_object_id *pid, const char *path, struct got_repository *repo,
185 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
188 const struct got_error *err = NULL;
189 struct got_object *obj = NULL, *pobj = NULL;
190 struct got_object_id *obj_id = NULL, *pobj_id = NULL;
191 struct got_blob_object *blob = NULL, *pblob = NULL;
192 struct got_diff_changes *changes = NULL;
194 err = got_object_id_by_path(&obj_id, repo, id, path);
198 err = got_object_open(&obj, repo, obj_id);
202 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
203 err = got_error(GOT_ERR_OBJ_TYPE);
207 err = got_object_id_by_path(&pobj_id, repo, pid, path);
209 if (err->code == GOT_ERR_NO_OBJ) {
210 /* Blob's history began in previous commit. */
211 err = got_error(GOT_ERR_ITER_COMPLETED);
216 /* If IDs match then don't bother with diffing. */
217 if (got_object_id_cmp(obj_id, pobj_id) == 0) {
219 err = cb(arg, blame->nlines, -1, id);
223 err = got_object_open(&pobj, repo, pobj_id);
227 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
229 * Encountered a non-blob at the path (probably a tree).
230 * Blob's history began in previous commit.
232 err = got_error(GOT_ERR_ITER_COMPLETED);
236 err = got_object_blob_open(&blob, repo, obj, 8192);
240 err = got_object_blob_open(&pblob, repo, pobj, 8192);
244 err = got_diff_blob_lines_changed(&changes, pblob, blob);
249 err = blame_changes(blame, changes, id, cb, arg);
250 got_diff_free_changes(changes);
252 err = cb(arg, blame->nlines, -1, id);
257 got_object_close(obj);
259 got_object_close(pobj);
261 got_object_blob_close(blob);
263 got_object_blob_close(pblob);
268 blame_close(struct got_blame *blame)
270 struct got_blame_diff_offsets *diff_offsets;
275 while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
276 diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
277 SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
278 free_diff_offsets(diff_offsets);
283 static const struct got_error *
284 blame_open(struct got_blame **blamep, const char *path,
285 struct got_object_id *start_commit_id, struct got_repository *repo,
286 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
289 const struct got_error *err = NULL;
290 struct got_object *obj = NULL;
291 struct got_object_id *obj_id = NULL;
292 struct got_blob_object *blob = NULL;
293 struct got_blame *blame = NULL;
294 struct got_object_id *id = NULL;
296 struct got_commit_graph *graph = NULL;
300 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
304 err = got_object_open(&obj, repo, obj_id);
308 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
309 err = got_error(GOT_ERR_OBJ_TYPE);
313 err = got_object_blob_open(&blob, repo, obj, 8192);
317 blame = calloc(1, sizeof(*blame));
319 return got_error_from_errno();
321 blame->f = got_opentemp();
322 if (blame->f == NULL) {
323 err = got_error_from_errno();
326 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
331 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
332 if (blame->lines == NULL) {
333 err = got_error_from_errno();
337 err = got_commit_graph_open(&graph, start_commit_id, path, 0, repo);
340 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
346 struct got_object_id *next_id;
348 err = got_commit_graph_iter_next(&next_id, graph);
350 if (err->code == GOT_ERR_ITER_COMPLETED) {
354 if (err->code != GOT_ERR_ITER_NEED_MORE)
356 err = got_commit_graph_fetch_commits(graph, 1, repo);
365 err = blame_commit(blame, id, next_id, path, repo,
368 if (err->code == GOT_ERR_ITER_COMPLETED)
377 /* Annotate remaining non-annotated lines with last commit. */
378 for (lineno = 1; lineno <= blame->nlines; lineno++) {
379 err = annotate_line(blame, lineno, id, cb, arg);
387 got_commit_graph_close(graph);
390 got_object_close(obj);
392 got_object_blob_close(blob);
402 static const struct got_error *
403 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
405 if (lineno < 1 || lineno > blame->nlines)
406 return got_error(GOT_ERR_RANGE);
407 *id = &blame->lines[lineno - 1].id;
412 parse_next_line(FILE *f, size_t *len)
417 const char delim[3] = { '\0', '\0', '\0'};
419 line = fparseln(f, &linelen, &lineno, delim, 0);
425 const struct got_error *
426 got_blame(const char *path, struct got_object_id *start_commit_id,
427 struct got_repository *repo, FILE *outfile)
429 const struct got_error *err = NULL;
430 struct got_blame *blame;
434 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
435 return got_error_from_errno();
437 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
443 for (lineno = 1; lineno <= blame->nlines; lineno++) {
444 struct got_object_id *id;
447 line = parse_next_line(blame->f, NULL);
451 err = blame_line(&id, blame, lineno);
457 err = got_object_id_str(&id_str, id);
458 /* Do not free id; It points into blame->lines. */
464 fprintf(outfile, "%.8s %s\n", id_str, line);
474 const struct got_error *
475 got_blame_incremental(const char *path, struct got_object_id *commit_id,
476 struct got_repository *repo,
477 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
480 const struct got_error *err = NULL;
481 struct got_blame *blame;
484 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
485 return got_error_from_errno();
487 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);