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"
38 struct got_blame_line {
40 struct got_object_id id;
46 struct got_blame_line *lines; /* one per line */
49 static const struct got_error *
50 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
51 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
54 const struct got_error *err = NULL;
55 struct got_blame_line *line;
57 if (lineno < 1 || lineno > blame->nlines)
58 return got_error(GOT_ERR_RANGE);
60 line = &blame->lines[lineno - 1];
64 memcpy(&line->id, id, sizeof(line->id));
67 err = cb(arg, blame->nlines, lineno, id);
71 static const struct got_error *
72 blame_commit(struct got_blame *blame, struct got_object_id *id,
73 struct got_object_id *pid, const char *path, struct got_repository *repo,
74 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
77 const struct got_error *err = NULL;
78 struct got_object *obj = NULL, *pobj = NULL;
79 struct got_blob_object *blob = NULL, *pblob = NULL;
80 struct got_diff_changes *changes = NULL;
82 err = got_object_open_by_path(&obj, repo, id, path);
85 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
86 err = got_error(GOT_ERR_OBJ_TYPE);
90 err = got_object_open_by_path(&pobj, repo, pid, path);
92 if (err->code == GOT_ERR_NO_OBJ) {
93 /* Blob's history began in previous commit. */
94 err = got_error(GOT_ERR_ITER_COMPLETED);
98 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
100 * Encountered a non-blob at the path (probably a tree).
101 * Blob's history began in previous commit.
103 err = got_error(GOT_ERR_ITER_COMPLETED);
107 /* If blob hashes match then don't bother with diffing. */
108 if (got_object_id_cmp(&obj->id, &pobj->id) == 0) {
110 err = cb(arg, blame->nlines, -1, id);
114 err = got_object_blob_open(&blob, repo, obj, 8192);
118 err = got_object_blob_open(&pblob, repo, pobj, 8192);
122 err = got_diff_blob_lines_changed(&changes, blob, pblob);
127 struct got_diff_change *change;
128 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
129 int a = change->cv.a;
130 int b = change->cv.b;
132 for (lineno = a; lineno <= b; lineno++) {
133 err = annotate_line(blame, lineno, id, cb, arg);
139 err = cb(arg, blame->nlines, -1, id);
142 got_object_close(obj);
144 got_object_close(pobj);
146 got_object_blob_close(blob);
148 got_object_blob_close(pblob);
153 blame_close(struct got_blame *blame)
161 static const struct got_error *
162 blame_open(struct got_blame **blamep, const char *path,
163 struct got_object_id *start_commit_id, struct got_repository *repo,
164 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
167 const struct got_error *err = NULL;
168 struct got_object *obj = NULL;
169 struct got_blob_object *blob = NULL;
170 struct got_blame *blame = NULL;
171 struct got_commit_object *commit = NULL;
172 struct got_object_id *id = NULL;
177 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
180 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
181 err = got_error(GOT_ERR_OBJ_TYPE);
185 err = got_object_blob_open(&blob, repo, obj, 8192);
189 blame = calloc(1, sizeof(*blame));
191 return got_error_from_errno();
193 blame->f = got_opentemp();
194 if (blame->f == NULL) {
195 err = got_error_from_errno();
198 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
203 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
204 if (blame->lines == NULL) {
205 err = got_error_from_errno();
209 /* Loop over first-parent history and try to blame commits. */
210 err = got_object_open_as_commit(&commit, repo, start_commit_id);
213 id = got_object_id_dup(start_commit_id);
215 err = got_error_from_errno();
219 struct got_object_qid *pid;
221 pid = SIMPLEQ_FIRST(&commit->parent_ids);
225 err = blame_commit(blame, id, pid->id, path, repo, cb, arg);
227 if (err->code == GOT_ERR_ITER_COMPLETED)
233 id = got_object_id_dup(pid->id);
235 err = got_error_from_errno();
238 got_object_commit_close(commit);
239 err = got_object_open_as_commit(&commit, repo, id);
244 /* Annotate remaining non-annotated lines with last commit. */
245 for (lineno = 1; lineno <= blame->nlines; lineno++) {
246 err = annotate_line(blame, lineno, id, cb, arg);
254 got_object_close(obj);
256 got_object_blob_close(blob);
258 got_object_commit_close(commit);
268 static const struct got_error *
269 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
271 if (lineno < 1 || lineno > blame->nlines)
272 return got_error(GOT_ERR_RANGE);
273 *id = &blame->lines[lineno - 1].id;
278 parse_next_line(FILE *f, size_t *len)
283 const char delim[3] = { '\0', '\0', '\0'};
285 line = fparseln(f, &linelen, &lineno, delim, 0);
291 const struct got_error *
292 got_blame(const char *path, struct got_object_id *start_commit_id,
293 struct got_repository *repo, FILE *outfile)
295 const struct got_error *err = NULL;
296 struct got_blame *blame;
300 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
301 return got_error_from_errno();
303 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
309 for (lineno = 1; lineno <= blame->nlines; lineno++) {
310 struct got_object_id *id;
313 line = parse_next_line(blame->f, NULL);
317 err = blame_line(&id, blame, lineno);
321 err = got_object_id_str(&id_str, id);
327 fprintf(outfile, "%.8s %s\n", id_str, line);
337 const struct got_error *
338 got_blame_incremental(const char *path, struct got_object_id *commit_id,
339 struct got_repository *repo,
340 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
343 const struct got_error *err = NULL;
344 struct got_blame *blame;
347 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
348 return got_error_from_errno();
350 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);