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, *pobj = 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 (pobj)
164 got_object_close(pobj);
165 if (blob)
166 got_object_blob_close(blob);
167 return err;
170 static const struct got_error *
171 blame_close(struct got_blame *blame)
173 const struct got_error *err = NULL;
175 if (blame->f && fclose(blame->f) != 0)
176 err = got_error_from_errno("fclose");
177 free(blame->lines);
178 free(blame);
179 return err;
182 static const struct got_error *
183 blame_open(struct got_blame **blamep, const char *path,
184 struct got_object_id *start_commit_id, struct got_repository *repo,
185 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
186 void *arg)
188 const struct got_error *err = NULL;
189 struct got_object *obj = NULL;
190 struct got_object_id *obj_id = NULL;
191 struct got_blob_object *blob = NULL;
192 struct got_blame *blame = NULL;
193 struct got_object_id *id = NULL, *pid = NULL;
194 int lineno;
195 struct got_commit_graph *graph = NULL;
197 *blamep = NULL;
199 err = got_object_id_by_path(&obj_id, repo, start_commit_id, path);
200 if (err)
201 return err;
203 err = got_object_open(&obj, repo, obj_id);
204 if (err)
205 goto done;
207 if (obj->type != GOT_OBJ_TYPE_BLOB) {
208 err = got_error(GOT_ERR_OBJ_TYPE);
209 goto done;
212 err = got_object_blob_open(&blob, repo, obj, 8192);
213 if (err)
214 goto done;
216 blame = calloc(1, sizeof(*blame));
217 if (blame == NULL)
218 return got_error_from_errno("calloc");
220 blame->f = got_opentemp();
221 if (blame->f == NULL) {
222 err = got_error_from_errno("got_opentemp");
223 goto done;
225 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
226 &blame->line_offsets, blame->f, blob);
227 if (err || blame->nlines == 0)
228 goto done;
230 /* Don't include \n at EOF in the blame line count. */
231 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
232 blame->nlines--;
234 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
235 if (blame->lines == NULL) {
236 err = got_error_from_errno("calloc");
237 goto done;
240 err = got_commit_graph_open(&graph, start_commit_id, path, 1, repo);
241 if (err)
242 return err;
243 err = got_commit_graph_iter_start(graph, start_commit_id, repo);
244 if (err)
245 goto done;
246 id = start_commit_id;
247 for (;;) {
248 err = got_commit_graph_iter_next(&pid, graph);
249 if (err) {
250 if (err->code == GOT_ERR_ITER_COMPLETED) {
251 err = NULL;
252 break;
254 if (err->code != GOT_ERR_ITER_NEED_MORE)
255 break;
256 err = got_commit_graph_fetch_commits(graph, 1, repo);
257 if (err)
258 break;
259 continue;
261 if (pid) {
262 err = blame_commit(blame, pid, id, path, repo, cb, arg);
263 if (err) {
264 if (err->code == GOT_ERR_ITER_COMPLETED)
265 err = NULL;
266 break;
268 if (blame->nannotated == blame->nlines)
269 break;
271 id = pid;
274 if (id && blame->nannotated < blame->nlines) {
275 /* Annotate remaining non-annotated lines with last commit. */
276 for (lineno = 1; lineno <= blame->nlines; lineno++) {
277 err = annotate_line(blame, lineno, id, cb, arg);
278 if (err)
279 goto done;
283 done:
284 if (graph)
285 got_commit_graph_close(graph);
286 free(obj_id);
287 if (obj)
288 got_object_close(obj);
289 if (blob)
290 got_object_blob_close(blob);
291 if (err) {
292 if (blame)
293 blame_close(blame);
294 } else
295 *blamep = blame;
297 return err;
300 const struct got_error *
301 got_blame(const char *path, struct got_object_id *commit_id,
302 struct got_repository *repo,
303 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
304 void *arg)
306 const struct got_error *err = NULL, *close_err = NULL;
307 struct got_blame *blame;
308 char *abspath;
310 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
311 return got_error_from_errno2("asprintf", path);
313 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
314 free(abspath);
315 if (blame)
316 close_err = blame_close(blame);
317 return err ? err : close_err;