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_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 {
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 dump_blob_and_count_lines(size_t *nlines, FILE *outfile,
51 struct got_blob_object *blob)
52 {
53 const struct got_error *err = NULL;
54 size_t len, hdrlen;
55 const uint8_t *buf;
56 int i;
58 hdrlen = got_object_blob_get_hdrlen(blob);
59 *nlines = 0;
60 do {
61 err = got_object_blob_read_block(&len, blob);
62 if (err)
63 return err;
64 if (len == 0)
65 break;
66 buf = got_object_blob_get_read_buf(blob);
67 for (i = 0; i < len; i++) {
68 if (buf[i] == '\n')
69 (*nlines)++;
70 }
71 /* Skip blob object header first time around. */
72 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
73 hdrlen = 0;
74 } while (len != 0);
77 fflush(outfile);
78 rewind(outfile);
80 return NULL;
81 }
83 static const struct got_error *
84 annotate_line(struct got_blame *blame, int lineno, struct got_object_id *id)
85 {
86 struct got_blame_line *line;
88 if (lineno < 1 || lineno > blame->nlines)
89 return got_error(GOT_ERR_RANGE);
91 line = &blame->lines[lineno - 1];
92 if (line->annotated)
93 return NULL;
95 memcpy(&line->id, id, sizeof(line->id));
96 line->annotated = 1;
97 return NULL;
98 }
100 static const struct got_error *
101 blame_commit(struct got_blame *blame, struct got_object_id *id,
102 struct got_object_id *pid, const char *path, struct got_repository *repo)
104 const struct got_error *err = NULL;
105 struct got_object *obj = NULL, *pobj = NULL;
106 struct got_blob_object *blob = NULL, *pblob = NULL;
107 struct got_diff_changes *changes = NULL;
109 err = got_object_open_by_path(&obj, repo, id, path);
110 if (err)
111 goto done;
112 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
113 err = got_error(GOT_ERR_OBJ_TYPE);
114 goto done;
117 err = got_object_open_by_path(&pobj, repo, pid, path);
118 if (err) {
119 if (err->code == GOT_ERR_NO_OBJ) {
120 /* Blob's history began in previous commit. */
121 err = got_error(GOT_ERR_ITER_COMPLETED);
123 goto done;
125 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
126 /*
127 * Encountered a non-blob at the path (probably a tree).
128 * Blob's history began in previous commit.
129 */
130 err = got_error(GOT_ERR_ITER_COMPLETED);
131 goto done;
134 /* If blob hashes match then don't bother with diffing. */
135 if (got_object_id_cmp(&obj->id, &pobj->id) == 0)
136 goto done;
138 err = got_object_blob_open(&blob, repo, obj, 8192);
139 if (err)
140 goto done;
142 err = got_object_blob_open(&pblob, repo, pobj, 8192);
143 if (err)
144 goto done;
146 err = got_diff_blob_lines_changed(&changes, blob, pblob);
147 if (err)
148 goto done;
150 if (changes) {
151 struct got_diff_change *change;
152 char *id_str;
153 err = got_object_id_str(&id_str, id);
154 if (err)
155 goto done;
157 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
158 int a = change->cv.a;
159 int b = change->cv.b;
160 int lineno;
161 for (lineno = a; lineno <= b; lineno++) {
162 err = annotate_line(blame, lineno, id);
163 if (err)
164 goto done;
167 free(id_str);
169 done:
170 if (obj)
171 got_object_close(obj);
172 if (pobj)
173 got_object_close(pobj);
174 if (blob)
175 got_object_blob_close(blob);
176 if (pblob)
177 got_object_blob_close(pblob);
178 return err;
181 static void
182 blame_close(struct got_blame *blame)
184 if (blame->f)
185 fclose(blame->f);
186 free(blame->lines);
187 free(blame);
190 static const struct got_error *
191 blame_open(struct got_blame **blamep, const char *path,
192 struct got_object_id *start_commit_id, struct got_repository *repo)
194 const struct got_error *err = NULL;
195 struct got_object *obj = NULL;
196 struct got_blob_object *blob = NULL;
197 struct got_blame *blame = NULL;
198 struct got_commit_object *commit = NULL;
199 struct got_object_id *id = NULL;
200 int lineno;
202 *blamep = NULL;
204 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
205 if (err)
206 return err;
207 if (got_object_get_type(obj) != 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();
220 blame->f = got_opentemp();
221 if (blame->f == NULL) {
222 err = got_error_from_errno();
223 goto done;
225 err = dump_blob_and_count_lines(&blame->nlines, blame->f, blob);
226 if (err)
227 goto done;
229 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
230 if (blame->lines == NULL) {
231 err = got_error_from_errno();
232 goto done;
235 /* Loop over first-parent history and try to blame commits. */
236 err = got_object_open_as_commit(&commit, repo, start_commit_id);
237 if (err)
238 goto done;
239 id = got_object_id_dup(start_commit_id);
240 if (id == NULL) {
241 err = got_error_from_errno();
242 goto done;
244 while (1) {
245 struct got_object_qid *pid;
246 struct got_commit_object *pcommit;
248 pid = SIMPLEQ_FIRST(&commit->parent_ids);
249 if (pid == NULL)
250 break;
252 err = got_object_open_as_commit(&pcommit, repo, pid->id);
253 if (err)
254 break;
256 err = blame_commit(blame, id, pid->id, path, repo);
257 if (err) {
258 if (err->code == GOT_ERR_ITER_COMPLETED)
259 err = NULL;
260 got_object_commit_close(pcommit);
261 break;
263 free(id);
264 id = got_object_id_dup(pid->id);
265 got_object_commit_close(commit);
266 commit = pcommit;
267 if (id == NULL) {
268 err = got_error_from_errno();
269 goto done;
273 /* Annotate remaining non-annotated lines with last commit. */
274 for (lineno = 1; lineno < blame->nlines; lineno++) {
275 err = annotate_line(blame, lineno, id);
276 if (err)
277 break;
280 done:
281 free(id);
282 if (obj)
283 got_object_close(obj);
284 if (blob)
285 got_object_blob_close(blob);
286 if (commit)
287 got_object_commit_close(commit);
288 if (err)
289 blame_close(blame);
290 else
291 *blamep = blame;
293 return err;
296 static const struct got_error *
297 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
299 if (lineno < 1 || lineno > blame->nlines)
300 return got_error(GOT_ERR_RANGE);
301 *id = &blame->lines[lineno - 1].id;
302 return NULL;
305 static char *
306 parse_next_line(FILE *f, size_t *len)
308 char *line;
309 size_t linelen;
310 size_t lineno;
311 const char delim[3] = { '\0', '\0', '\0'};
313 line = fparseln(f, &linelen, &lineno, delim, 0);
314 if (len)
315 *len = linelen;
316 return line;
319 const struct got_error *
320 got_blame(const char *path, struct got_object_id *start_commit_id,
321 struct got_repository *repo, FILE *outfile)
323 const struct got_error *err = NULL;
324 struct got_blame *blame;
325 int lineno;
326 char *abspath;
328 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
329 return got_error_from_errno();
331 err = blame_open(&blame, abspath, start_commit_id, repo);
332 if (err) {
333 free(abspath);
334 return err;
337 for (lineno = 1; lineno < blame->nlines; lineno++) {
338 struct got_object_id *id;
339 char *line, *id_str;
341 line = parse_next_line(blame->f, NULL);
342 if (line == NULL)
343 break;
345 err = blame_line(&id, blame, lineno);
346 if (err)
347 break;
349 err = got_object_id_str(&id_str, id);
350 if (err) {
351 free(line);
352 break;
355 fprintf(outfile, "%.8s %s\n", id_str, line);
356 free(line);
357 free(id_str);
360 blame_close(blame);
361 free(abspath);
362 return err;