Blob


1 /*
2 * Copyright (c) 2018 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"
38 struct got_blame_line {
39 int annotated;
40 struct got_object_id id;
41 };
43 struct got_blame {
44 FILE *f;
45 size_t nlines;
46 struct got_blame_line *lines; /* one per line */
47 };
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 *),
52 void *arg)
53 {
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];
61 if (line->annotated)
62 return NULL;
64 memcpy(&line->id, id, sizeof(line->id));
65 line->annotated = 1;
66 if (cb)
67 err = cb(arg, blame->nlines, lineno, id);
68 return err;
69 }
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 *),
75 void *arg)
76 {
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);
83 if (err)
84 goto done;
85 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
86 err = got_error(GOT_ERR_OBJ_TYPE);
87 goto done;
88 }
90 err = got_object_open_by_path(&pobj, repo, pid, path);
91 if (err) {
92 if (err->code == GOT_ERR_NO_OBJ) {
93 /* Blob's history began in previous commit. */
94 err = got_error(GOT_ERR_ITER_COMPLETED);
95 }
96 goto done;
97 }
98 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
99 /*
100 * Encountered a non-blob at the path (probably a tree).
101 * Blob's history began in previous commit.
102 */
103 err = got_error(GOT_ERR_ITER_COMPLETED);
104 goto done;
107 /* If blob hashes match then don't bother with diffing. */
108 if (got_object_id_cmp(&obj->id, &pobj->id) == 0) {
109 if (cb)
110 err = cb(arg, blame->nlines, -1, id);
111 goto done;
114 err = got_object_blob_open(&blob, repo, obj, 8192);
115 if (err)
116 goto done;
118 err = got_object_blob_open(&pblob, repo, pobj, 8192);
119 if (err)
120 goto done;
122 err = got_diff_blob_lines_changed(&changes, blob, pblob);
123 if (err)
124 goto done;
126 if (changes) {
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;
131 int lineno;
132 for (lineno = a; lineno <= b; lineno++) {
133 err = annotate_line(blame, lineno, id, cb, arg);
134 if (err)
135 goto done;
138 } else if (cb)
139 err = cb(arg, blame->nlines, -1, id);
140 done:
141 if (obj)
142 got_object_close(obj);
143 if (pobj)
144 got_object_close(pobj);
145 if (blob)
146 got_object_blob_close(blob);
147 if (pblob)
148 got_object_blob_close(pblob);
149 return err;
152 static void
153 blame_close(struct got_blame *blame)
155 if (blame->f)
156 fclose(blame->f);
157 free(blame->lines);
158 free(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 *),
165 void *arg)
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;
173 int lineno;
175 *blamep = NULL;
177 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
178 if (err)
179 return err;
180 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
181 err = got_error(GOT_ERR_OBJ_TYPE);
182 goto done;
185 err = got_object_blob_open(&blob, repo, obj, 8192);
186 if (err)
187 goto done;
189 blame = calloc(1, sizeof(*blame));
190 if (blame == NULL)
191 return got_error_from_errno();
193 blame->f = got_opentemp();
194 if (blame->f == NULL) {
195 err = got_error_from_errno();
196 goto done;
198 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
199 blob);
200 if (err)
201 goto done;
203 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
204 if (blame->lines == NULL) {
205 err = got_error_from_errno();
206 goto done;
209 /* Loop over first-parent history and try to blame commits. */
210 err = got_object_open_as_commit(&commit, repo, start_commit_id);
211 if (err)
212 goto done;
213 id = got_object_id_dup(start_commit_id);
214 if (id == NULL) {
215 err = got_error_from_errno();
216 goto done;
218 while (1) {
219 struct got_object_qid *pid;
221 pid = SIMPLEQ_FIRST(&commit->parent_ids);
222 if (pid == NULL)
223 break;
225 err = blame_commit(blame, id, pid->id, path, repo, cb, arg);
226 if (err) {
227 if (err->code == GOT_ERR_ITER_COMPLETED)
228 err = NULL;
229 break;
232 free(id);
233 id = got_object_id_dup(pid->id);
234 if (id == NULL) {
235 err = got_error_from_errno();
236 goto done;
238 got_object_commit_close(commit);
239 err = got_object_open_as_commit(&commit, repo, id);
240 if (err)
241 goto done;
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);
247 if (err)
248 goto done;
251 done:
252 free(id);
253 if (obj)
254 got_object_close(obj);
255 if (blob)
256 got_object_blob_close(blob);
257 if (commit)
258 got_object_commit_close(commit);
259 if (err) {
260 if (blame)
261 blame_close(blame);
262 } else
263 *blamep = blame;
265 return err;
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;
274 return NULL;
277 static char *
278 parse_next_line(FILE *f, size_t *len)
280 char *line;
281 size_t linelen;
282 size_t lineno;
283 const char delim[3] = { '\0', '\0', '\0'};
285 line = fparseln(f, &linelen, &lineno, delim, 0);
286 if (len)
287 *len = linelen;
288 return line;
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;
297 int lineno;
298 char *abspath;
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);
304 if (err) {
305 free(abspath);
306 return err;
309 for (lineno = 1; lineno <= blame->nlines; lineno++) {
310 struct got_object_id *id;
311 char *line, *id_str;
313 line = parse_next_line(blame->f, NULL);
314 if (line == NULL)
315 break;
317 err = blame_line(&id, blame, lineno);
318 if (err)
319 break;
321 err = got_object_id_str(&id_str, id);
322 if (err) {
323 free(line);
324 break;
327 fprintf(outfile, "%.8s %s\n", id_str, line);
328 free(line);
329 free(id_str);
332 blame_close(blame);
333 free(abspath);
334 return err;
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 *),
341 void *arg)
343 const struct got_error *err = NULL;
344 struct got_blame *blame;
345 char *abspath;
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);
351 free(abspath);
352 if (err == NULL)
353 blame_close(blame);
354 return err;