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"
39 struct got_blame_line {
41 struct got_object_id id;
44 struct got_blame_diff_offsets {
45 struct got_diffoffset_chunks *chunks;
46 struct got_object_id *commit_id;
47 SLIST_ENTRY(got_blame_diff_offsets) entry;
50 SLIST_HEAD(got_blame_diff_offsets_list, got_blame_diff_offsets);
55 struct got_blame_line *lines; /* one per line */
57 struct got_blame_diff_offsets_list diff_offsets_list;
61 free_diff_offsets(struct got_blame_diff_offsets *diff_offsets)
63 if (diff_offsets->chunks)
64 got_diffoffset_free(diff_offsets->chunks);
65 free(diff_offsets->commit_id);
69 static const struct got_error *
70 alloc_diff_offsets(struct got_blame_diff_offsets **diff_offsets,
71 struct got_object_id *commit_id)
73 const struct got_error *err = NULL;
75 *diff_offsets = calloc(1, sizeof(**diff_offsets));
76 if (*diff_offsets == NULL)
77 return got_error_from_errno();
79 (*diff_offsets)->commit_id = got_object_id_dup(commit_id);
80 if ((*diff_offsets)->commit_id == NULL) {
81 err = got_error_from_errno();
82 free_diff_offsets(*diff_offsets);
87 err = got_diffoffset_alloc(&(*diff_offsets)->chunks);
89 free_diff_offsets(*diff_offsets);
96 static const struct got_error *
97 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
98 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
101 const struct got_error *err = NULL;
102 struct got_blame_line *line;
104 if (lineno < 1 || lineno > blame->nlines)
105 return got_error(GOT_ERR_RANGE);
107 line = &blame->lines[lineno - 1];
111 memcpy(&line->id, id, sizeof(line->id));
114 err = cb(arg, blame->nlines, lineno, id);
119 get_blamed_line(struct got_blame_diff_offsets_list *diff_offsets_list,
122 struct got_blame_diff_offsets *diff_offsets;
124 SLIST_FOREACH(diff_offsets, diff_offsets_list, entry)
125 lineno = got_diffoffset_get(diff_offsets->chunks, lineno);
130 static const struct got_error *
131 blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
132 struct got_object_id *commit_id,
133 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
136 const struct got_error *err = NULL;
137 struct got_diff_change *change;
138 struct got_blame_diff_offsets *diff_offsets;
140 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
141 int c = change->cv.c;
142 int d = change->cv.d;
144 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
147 for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
148 err = annotate_line(blame,
149 get_blamed_line(&blame->diff_offsets_list, ln),
156 err = alloc_diff_offsets(&diff_offsets, commit_id);
159 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
160 int a = change->cv.a;
161 int b = change->cv.b;
162 int c = change->cv.c;
163 int d = change->cv.d;
165 int old_length = (a < b ? b - a + 1 : (a == b ? 1 : 0));
167 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
169 err = got_diffoffset_add(diff_offsets->chunks,
170 old_lineno, old_length, new_lineno, new_length);
172 free_diff_offsets(diff_offsets);
176 SLIST_INSERT_HEAD(&blame->diff_offsets_list, diff_offsets, entry);
181 static const struct got_error *
182 blame_commit(struct got_blame *blame, struct got_object_id *id,
183 struct got_object_id *pid, const char *path, struct got_repository *repo,
184 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
187 const struct got_error *err = NULL;
188 struct got_object *obj = NULL, *pobj = NULL;
189 struct got_blob_object *blob = NULL, *pblob = NULL;
190 struct got_diff_changes *changes = NULL;
192 err = got_object_open_by_path(&obj, repo, id, path);
195 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
196 err = got_error(GOT_ERR_OBJ_TYPE);
200 err = got_object_open_by_path(&pobj, repo, pid, path);
202 if (err->code == GOT_ERR_NO_OBJ) {
203 /* Blob's history began in previous commit. */
204 err = got_error(GOT_ERR_ITER_COMPLETED);
208 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
210 * Encountered a non-blob at the path (probably a tree).
211 * Blob's history began in previous commit.
213 err = got_error(GOT_ERR_ITER_COMPLETED);
217 /* If blob hashes match then don't bother with diffing. */
218 if (got_object_id_cmp(&obj->id, &pobj->id) == 0) {
220 err = cb(arg, blame->nlines, -1, id);
224 err = got_object_blob_open(&blob, repo, obj, 8192);
228 err = got_object_blob_open(&pblob, repo, pobj, 8192);
232 err = got_diff_blob_lines_changed(&changes, pblob, blob);
237 err = blame_changes(blame, changes, id, cb, arg);
238 got_diff_free_changes(changes);
240 err = cb(arg, blame->nlines, -1, id);
243 got_object_close(obj);
245 got_object_close(pobj);
247 got_object_blob_close(blob);
249 got_object_blob_close(pblob);
254 blame_close(struct got_blame *blame)
256 struct got_blame_diff_offsets *diff_offsets;
261 while (!SLIST_EMPTY(&blame->diff_offsets_list)) {
262 diff_offsets = SLIST_FIRST(&blame->diff_offsets_list);
263 SLIST_REMOVE_HEAD(&blame->diff_offsets_list, entry);
264 free_diff_offsets(diff_offsets);
269 static const struct got_error *
270 blame_open(struct got_blame **blamep, const char *path,
271 struct got_object_id *start_commit_id, struct got_repository *repo,
272 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
275 const struct got_error *err = NULL;
276 struct got_object *obj = NULL;
277 struct got_blob_object *blob = NULL;
278 struct got_blame *blame = NULL;
279 struct got_commit_object *commit = NULL;
280 struct got_object_id *id = NULL;
285 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
288 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
289 err = got_error(GOT_ERR_OBJ_TYPE);
293 err = got_object_blob_open(&blob, repo, obj, 8192);
297 blame = calloc(1, sizeof(*blame));
299 return got_error_from_errno();
301 blame->f = got_opentemp();
302 if (blame->f == NULL) {
303 err = got_error_from_errno();
306 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
311 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
312 if (blame->lines == NULL) {
313 err = got_error_from_errno();
317 /* Loop over first-parent history and try to blame commits. */
318 err = got_object_open_as_commit(&commit, repo, start_commit_id);
321 id = got_object_id_dup(start_commit_id);
323 err = got_error_from_errno();
327 struct got_object_qid *pid;
329 pid = SIMPLEQ_FIRST(&commit->parent_ids);
333 err = blame_commit(blame, id, pid->id, path, repo, cb, arg);
335 if (err->code == GOT_ERR_ITER_COMPLETED)
341 id = got_object_id_dup(pid->id);
343 err = got_error_from_errno();
346 got_object_commit_close(commit);
347 err = got_object_open_as_commit(&commit, repo, id);
352 /* Annotate remaining non-annotated lines with last commit. */
353 for (lineno = 1; lineno <= blame->nlines; lineno++) {
354 err = annotate_line(blame, lineno, id, cb, arg);
362 got_object_close(obj);
364 got_object_blob_close(blob);
366 got_object_commit_close(commit);
376 static const struct got_error *
377 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
379 if (lineno < 1 || lineno > blame->nlines)
380 return got_error(GOT_ERR_RANGE);
381 *id = &blame->lines[lineno - 1].id;
386 parse_next_line(FILE *f, size_t *len)
391 const char delim[3] = { '\0', '\0', '\0'};
393 line = fparseln(f, &linelen, &lineno, delim, 0);
399 const struct got_error *
400 got_blame(const char *path, struct got_object_id *start_commit_id,
401 struct got_repository *repo, FILE *outfile)
403 const struct got_error *err = NULL;
404 struct got_blame *blame;
408 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
409 return got_error_from_errno();
411 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
417 for (lineno = 1; lineno <= blame->nlines; lineno++) {
418 struct got_object_id *id;
421 line = parse_next_line(blame->f, NULL);
425 err = blame_line(&id, blame, lineno);
431 err = got_object_id_str(&id_str, id);
432 /* Do not free id; It points into blame->lines. */
438 fprintf(outfile, "%.8s %s\n", id_str, line);
448 const struct got_error *
449 got_blame_incremental(const char *path, struct got_object_id *commit_id,
450 struct got_repository *repo,
451 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
454 const struct got_error *err = NULL;
455 struct got_blame *blame;
458 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
459 return got_error_from_errno();
461 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);