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 void
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;
91 line = &blame->lines[lineno - 1];
92 if (line->annotated)
93 return;
95 memcpy(&line->id, id, sizeof(line->id));
96 line->annotated = 1;
97 }
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);
109 if (err)
110 goto done;
111 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
112 err = got_error(GOT_ERR_OBJ_TYPE);
113 goto done;
116 err = got_object_open_by_path(&pobj, repo, pid, path);
117 if (err) {
118 if (err->code == GOT_ERR_NO_OBJ) {
119 /* Blob's history began in previous commit. */
120 err = got_error(GOT_ERR_ITER_COMPLETED);
122 goto done;
124 if (got_object_get_type(pobj) != GOT_OBJ_TYPE_BLOB) {
125 /*
126 * Encountered a non-blob at the path (probably a tree).
127 * Blob's history began in previous commit.
128 */
129 err = got_error(GOT_ERR_ITER_COMPLETED);
130 goto done;
133 /* If blob hashes match then don't bother with diffing. */
134 if (got_object_id_cmp(&obj->id, &pobj->id) == 0)
135 goto done;
137 err = got_object_blob_open(&blob, repo, obj, 8192);
138 if (err)
139 goto done;
141 err = got_object_blob_open(&pblob, repo, pobj, 8192);
142 if (err)
143 goto done;
145 err = got_diff_blob_lines_changed(&changes, blob, pblob);
146 if (err)
147 goto done;
149 if (changes) {
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;
154 int lineno;
155 for (lineno = a; lineno <= b; lineno++)
156 annotate_line(blame, lineno, id);
159 done:
160 if (obj)
161 got_object_close(obj);
162 if (pobj)
163 got_object_close(pobj);
164 if (blob)
165 got_object_blob_close(blob);
166 if (pblob)
167 got_object_blob_close(pblob);
168 return err;
171 static void
172 blame_close(struct got_blame *blame)
174 if (blame->f)
175 fclose(blame->f);
176 free(blame->lines);
177 free(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;
190 int lineno;
192 *blamep = NULL;
194 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
195 if (err)
196 return err;
197 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
198 err = got_error(GOT_ERR_OBJ_TYPE);
199 goto done;
202 err = got_object_blob_open(&blob, repo, obj, 8192);
203 if (err)
204 goto done;
206 blame = calloc(1, sizeof(*blame));
207 if (blame == NULL)
208 return got_error_from_errno();
210 blame->f = got_opentemp();
211 if (blame->f == NULL) {
212 err = got_error_from_errno();
213 goto done;
215 err = dump_blob_and_count_lines(&blame->nlines, blame->f, blob);
216 if (err)
217 goto done;
219 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
220 if (blame->lines == NULL) {
221 err = got_error_from_errno();
222 goto done;
225 /* Loop over first-parent history and try to blame commits. */
226 err = got_object_open_as_commit(&commit, repo, start_commit_id);
227 if (err)
228 goto done;
229 id = got_object_id_dup(start_commit_id);
230 if (id == NULL) {
231 err = got_error_from_errno();
232 goto done;
234 while (1) {
235 struct got_object_qid *pid;
237 pid = SIMPLEQ_FIRST(&commit->parent_ids);
238 if (pid == NULL)
239 break;
241 err = blame_commit(blame, id, pid->id, path, repo);
242 if (err) {
243 if (err->code == GOT_ERR_ITER_COMPLETED)
244 err = NULL;
245 break;
248 free(id);
249 id = got_object_id_dup(pid->id);
250 if (id == NULL) {
251 err = got_error_from_errno();
252 goto done;
254 got_object_commit_close(commit);
255 err = got_object_open_as_commit(&commit, repo, id);
256 if (err)
257 break;
260 /* Annotate remaining non-annotated lines with last commit. */
261 for (lineno = 1; lineno <= blame->nlines; lineno++)
262 annotate_line(blame, lineno, id);
264 done:
265 free(id);
266 if (obj)
267 got_object_close(obj);
268 if (blob)
269 got_object_blob_close(blob);
270 if (commit)
271 got_object_commit_close(commit);
272 if (err)
273 blame_close(blame);
274 else
275 *blamep = blame;
277 return err;
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;
286 return NULL;
289 static char *
290 parse_next_line(FILE *f, size_t *len)
292 char *line;
293 size_t linelen;
294 size_t lineno;
295 const char delim[3] = { '\0', '\0', '\0'};
297 line = fparseln(f, &linelen, &lineno, delim, 0);
298 if (len)
299 *len = linelen;
300 return line;
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;
309 int lineno;
310 char *abspath;
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);
316 if (err) {
317 free(abspath);
318 return err;
321 for (lineno = 1; lineno <= blame->nlines; lineno++) {
322 struct got_object_id *id;
323 char *line, *id_str;
325 line = parse_next_line(blame->f, NULL);
326 if (line == NULL)
327 break;
329 err = blame_line(&id, blame, lineno);
330 if (err)
331 break;
333 err = got_object_id_str(&id_str, id);
334 if (err) {
335 free(line);
336 break;
339 fprintf(outfile, "%.8s %s\n", id_str, line);
340 free(line);
341 free(id_str);
344 blame_close(blame);
345 free(abspath);
346 return err;