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_zbuf.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 dump_blob_and_count_lines(size_t *nlines, FILE *outfile,
51 struct got_blob_object *blob)
53 const struct got_error *err = NULL;
58 hdrlen = got_object_blob_get_hdrlen(blob);
61 err = got_object_blob_read_block(&len, blob);
66 buf = got_object_blob_get_read_buf(blob);
67 for (i = 0; i < len; i++) {
71 /* Skip blob object header first time around. */
72 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
84 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id)
86 struct got_blame_line *line;
88 if (lineno < 1 || lineno > blame->nlines)
91 line = &blame->lines[lineno - 1];
95 memcpy(&line->id, id, sizeof(line->id));
99 static const struct got_error *
100 blame_commit(struct got_blame *blame, struct got_object_id *id,
101 struct got_object_id *pid, const char *path, struct got_repository *repo)
103 const struct got_error *err = NULL;
104 struct got_object *obj = NULL, *pobj = NULL;
105 struct got_blob_object *blob = NULL, *pblob = NULL;
106 struct got_diff_changes *changes = NULL;
108 err = got_object_open_by_path(&obj, repo, id, path);
111 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
112 err = got_error(GOT_ERR_OBJ_TYPE);
116 err = got_object_open_by_path(&pobj, repo, pid, path);
118 if (err->code == GOT_ERR_NO_OBJ) {
119 /* Blob's history began in previous commit. */
120 err = got_error(GOT_ERR_ITER_COMPLETED);
124 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
126 * Encountered a non-blob at the path (probably a tree).
127 * Blob's history began in previous commit.
129 err = got_error(GOT_ERR_ITER_COMPLETED);
133 /* If blob hashes match then don't bother with diffing. */
134 if (got_object_id_cmp(&obj->id, &pobj->id) == 0)
137 err = got_object_blob_open(&blob, repo, obj, 8192);
141 err = got_object_blob_open(&pblob, repo, pobj, 8192);
145 err = got_diff_blob_lines_changed(&changes, blob, pblob);
150 struct got_diff_change *change;
151 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
152 int a = change->cv.a;
153 int b = change->cv.b;
155 for (lineno = a; lineno <= b; lineno++)
156 annotate_line(blame, lineno, id);
161 got_object_close(obj);
163 got_object_close(pobj);
165 got_object_blob_close(blob);
167 got_object_blob_close(pblob);
172 blame_close(struct got_blame *blame)
180 static const struct got_error *
181 blame_open(struct got_blame **blamep, const char *path,
182 struct got_object_id *start_commit_id, struct got_repository *repo)
184 const struct got_error *err = NULL;
185 struct got_object *obj = NULL;
186 struct got_blob_object *blob = NULL;
187 struct got_blame *blame = NULL;
188 struct got_commit_object *commit = NULL;
189 struct got_object_id *id = NULL;
194 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
197 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
198 err = got_error(GOT_ERR_OBJ_TYPE);
202 err = got_object_blob_open(&blob, repo, obj, 8192);
206 blame = calloc(1, sizeof(*blame));
208 return got_error_from_errno();
210 blame->f = got_opentemp();
211 if (blame->f == NULL) {
212 err = got_error_from_errno();
215 err = dump_blob_and_count_lines(&blame->nlines, blame->f, blob);
219 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
220 if (blame->lines == NULL) {
221 err = got_error_from_errno();
225 /* Loop over first-parent history and try to blame commits. */
226 err = got_object_open_as_commit(&commit, repo, start_commit_id);
229 id = got_object_id_dup(start_commit_id);
231 err = got_error_from_errno();
235 struct got_object_qid *pid;
237 pid = SIMPLEQ_FIRST(&commit->parent_ids);
241 err = blame_commit(blame, id, pid->id, path, repo);
243 if (err->code == GOT_ERR_ITER_COMPLETED)
249 id = got_object_id_dup(pid->id);
251 err = got_error_from_errno();
254 got_object_commit_close(commit);
255 err = got_object_open_as_commit(&commit, repo, id);
260 /* Annotate remaining non-annotated lines with last commit. */
261 for (lineno = 1; lineno < blame->nlines; lineno++)
262 annotate_line(blame, lineno, id);
267 got_object_close(obj);
269 got_object_blob_close(blob);
271 got_object_commit_close(commit);
280 static const struct got_error *
281 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
283 if (lineno < 1 || lineno > blame->nlines)
284 return got_error(GOT_ERR_RANGE);
285 *id = &blame->lines[lineno - 1].id;
290 parse_next_line(FILE *f, size_t *len)
295 const char delim[3] = { '\0', '\0', '\0'};
297 line = fparseln(f, &linelen, &lineno, delim, 0);
303 const struct got_error *
304 got_blame(const char *path, struct got_object_id *start_commit_id,
305 struct got_repository *repo, FILE *outfile)
307 const struct got_error *err = NULL;
308 struct got_blame *blame;
312 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
313 return got_error_from_errno();
315 err = blame_open(&blame, abspath, start_commit_id, repo);
321 for (lineno = 1; lineno < blame->nlines; lineno++) {
322 struct got_object_id *id;
325 line = parse_next_line(blame->f, NULL);
329 err = blame_line(&id, blame, lineno);
333 err = got_object_id_str(&id_str, id);
339 fprintf(outfile, "%.8s %s\n", id_str, line);