Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <sha1.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <util.h>
26 #include <zlib.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_commit_graph.h"
39 struct got_blame_line {
40 int annotated;
41 struct got_object_id id;
42 };
44 struct got_blame {
45 FILE *f;
46 size_t filesize;
47 int nlines;
48 int nannotated;
49 struct got_blame_line *lines; /* one per line */
50 off_t *line_offsets; /* one per line */
51 int ncommits;
52 };
54 static const struct got_error *
55 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id,
56 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
57 void *arg)
58 {
59 const struct got_error *err = NULL;
60 struct got_blame_line *line;
62 if (lineno < 1 || lineno > blame->nlines)
63 return NULL;
65 line = &blame->lines[lineno - 1];
66 if (line->annotated)
67 return NULL;
69 memcpy(&line->id, id, sizeof(line->id));
70 line->annotated = 1;
71 blame->nannotated++;
72 if (cb)
73 err = cb(arg, blame->nlines, lineno, id);
74 return err;
75 }
77 static const struct got_error *
78 blame_changes(struct got_blame *blame, struct got_diff_changes *changes,
79 struct got_object_id *commit_id,
80 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
81 void *arg)
82 {
83 const struct got_error *err = NULL;
84 struct got_diff_change *change;
86 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
87 int c = change->cv.c;
88 int d = change->cv.d;
89 int new_lineno = (c < d ? c : d);
90 int new_length = (c < d ? d - c + 1 : (c == d ? 1 : 0));
91 int ln;
93 for (ln = new_lineno; ln < new_lineno + new_length; ln++) {
94 err = annotate_line(blame, ln, commit_id, cb, arg);
95 if (err)
96 return err;
97 if (blame->nlines == blame->nannotated)
98 break;
99 }
102 return NULL;
105 static const struct got_error *
106 blame_commit(struct got_blame *blame, struct got_object_id *parent_id,
107 struct got_object_id *id, const char *path, struct got_repository *repo,
108 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
109 void *arg)
111 const struct got_error *err = NULL;
112 struct got_object *obj = NULL;
113 struct got_object_id *obj_id = NULL;
114 struct got_commit_object *commit = NULL;
115 struct got_blob_object *blob = NULL;
116 struct got_diff_changes *changes = NULL;
118 err = got_object_open_as_commit(&commit, repo, parent_id);
119 if (err)
120 return err;
122 err = got_object_id_by_path(&obj_id, repo, parent_id, path);
123 if (err) {
124 if (err->code == GOT_ERR_NO_TREE_ENTRY)
125 err = NULL;
126 goto done;
129 err = got_object_open(&obj, repo, obj_id);
130 if (err)
131 goto done;
133 if (obj->type != GOT_OBJ_TYPE_BLOB) {
134 err = got_error(GOT_ERR_OBJ_TYPE);
135 goto done;
138 err = got_object_blob_open(&blob, repo, obj, 8192);
139 if (err)
140 goto done;
142 if (fseek(blame->f, 0L, SEEK_SET) == -1) {
143 err = got_ferror(blame->f, GOT_ERR_IO);
144 goto done;
147 err = got_diff_blob_file_lines_changed(&changes, blob, blame->f,
148 blame->filesize);
149 if (err)
150 goto done;
152 if (changes) {
153 err = blame_changes(blame, changes, id, cb, arg);
154 got_diff_free_changes(changes);
155 } else if (cb)
156 err = cb(arg, blame->nlines, -1, id);
157 done:
158 if (commit)
159 got_object_commit_close(commit);
160 free(obj_id);
161 if (obj)
162 got_object_close(obj);
163 if (blob)
164 got_object_blob_close(blob);
165 return err;
168 static const struct got_error *
169 blame_close(struct got_blame *blame)
171 const struct got_error *err = NULL;
173 if (blame->f && fclose(blame->f) != 0)
174 err = got_error_from_errno("fclose");
175 free(blame->lines);
176 free(blame);
177 return err;
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,
183 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
184 void *arg)
186 const struct got_error *err = NULL;
187 struct got_object *obj = NULL;
188 struct got_object_id *obj_id = NULL;
189 struct got_blob_object *blob = NULL;
190 struct got_blame *blame = NULL;
191 struct got_object_id *id = NULL, *pid = NULL;
192 int lineno;
193 struct got_commit_graph *graph = NULL;
195 *blamep = NULL;
197 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
198 if (err)
199 return err;
201 err = got_object_open(&obj, repo, obj_id);
202 if (err)
203 goto done;
205 if (obj->type != GOT_OBJ_TYPE_BLOB) {
206 err = got_error(GOT_ERR_OBJ_TYPE);
207 goto done;
210 err = got_object_blob_open(&blob, repo, obj, 8192);
211 if (err)
212 goto done;
214 blame = calloc(1, sizeof(*blame));
215 if (blame == NULL)
216 return got_error_from_errno("calloc");
218 blame->f = got_opentemp();
219 if (blame->f == NULL) {
220 err = got_error_from_errno("got_opentemp");
221 goto done;
223 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
224 &blame->line_offsets, blame->f, blob);
225 if (err || blame->nlines == 0)
226 goto done;
228 /* Don't include \n at EOF in the blame line count. */
229 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
230 blame->nlines--;
232 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
233 if (blame->lines == NULL) {
234 err = got_error_from_errno("calloc");
235 goto done;
238 err = got_commit_graph_open(&graph, start_commit_id, path, 1, repo);
239 if (err)
240 return err;
241 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
242 if (err)
243 goto done;
244 id = start_commit_id;
245 for (;;) {
246 err = got_commit_graph_iter_next(&pid, graph);
247 if (err) {
248 if (err->code == GOT_ERR_ITER_COMPLETED) {
249 err = NULL;
250 break;
252 if (err->code != GOT_ERR_ITER_NEED_MORE)
253 break;
254 err = got_commit_graph_fetch_commits(graph, 1, repo);
255 if (err)
256 break;
257 continue;
259 if (pid) {
260 err = blame_commit(blame, pid, id, path, repo, cb, arg);
261 if (err) {
262 if (err->code == GOT_ERR_ITER_COMPLETED)
263 err = NULL;
264 break;
266 if (blame->nannotated == blame->nlines)
267 break;
269 id = pid;
272 if (id && blame->nannotated < blame->nlines) {
273 /* Annotate remaining non-annotated lines with last commit. */
274 for (lineno = 1; lineno <= blame->nlines; lineno++) {
275 err = annotate_line(blame, lineno, id, cb, arg);
276 if (err)
277 goto done;
281 done:
282 if (graph)
283 got_commit_graph_close(graph);
284 free(obj_id);
285 if (obj)
286 got_object_close(obj);
287 if (blob)
288 got_object_blob_close(blob);
289 if (err) {
290 if (blame)
291 blame_close(blame);
292 } else
293 *blamep = blame;
295 return err;
298 const struct got_error *
299 got_blame(const char *path, struct got_object_id *commit_id,
300 struct got_repository *repo,
301 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
302 void *arg)
304 const struct got_error *err = NULL, *close_err = NULL;
305 struct got_blame *blame;
306 char *abspath;
308 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
309 return got_error_from_errno2("asprintf", path);
311 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
312 free(abspath);
313 if (blame)
314 close_err = blame_close(blame);
315 return err ? err : close_err;