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 goto done;
111 err = got_object_blob_open(&blob, repo, obj, 8192);
112 if (err)
113 goto done;
115 err = got_object_blob_open(&pblob, repo, pobj, 8192);
116 if (err)
117 goto done;
119 err = got_diff_blob_lines_changed(&changes, blob, pblob);
120 if (err)
121 goto done;
123 if (changes) {
124 struct got_diff_change *change;
125 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
126 int a = change->cv.a;
127 int b = change->cv.b;
128 int lineno;
129 for (lineno = a; lineno <= b; lineno++) {
130 err = annotate_line(blame, lineno, id, cb, arg);
131 if (err)
132 goto done;
135 } else if (cb)
136 err = cb(arg, blame->nlines, -1, id);
137 done:
138 if (obj)
139 got_object_close(obj);
140 if (pobj)
141 got_object_close(pobj);
142 if (blob)
143 got_object_blob_close(blob);
144 if (pblob)
145 got_object_blob_close(pblob);
146 return err;
149 static void
150 blame_close(struct got_blame *blame)
152 if (blame->f)
153 fclose(blame->f);
154 free(blame->lines);
155 free(blame);
158 static const struct got_error *
159 blame_open(struct got_blame **blamep, const char *path,
160 struct got_object_id *start_commit_id, struct got_repository *repo,
161 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
162 void *arg)
164 const struct got_error *err = NULL;
165 struct got_object *obj = NULL;
166 struct got_blob_object *blob = NULL;
167 struct got_blame *blame = NULL;
168 struct got_commit_object *commit = NULL;
169 struct got_object_id *id = NULL;
170 int lineno;
172 *blamep = NULL;
174 err = got_object_open_by_path(&obj, repo, start_commit_id, path);
175 if (err)
176 return err;
177 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
178 err = got_error(GOT_ERR_OBJ_TYPE);
179 goto done;
182 err = got_object_blob_open(&blob, repo, obj, 8192);
183 if (err)
184 goto done;
186 blame = calloc(1, sizeof(*blame));
187 if (blame == NULL)
188 return got_error_from_errno();
190 blame->f = got_opentemp();
191 if (blame->f == NULL) {
192 err = got_error_from_errno();
193 goto done;
195 err = got_object_blob_dump_to_file(NULL, &blame->nlines, blame->f,
196 blob);
197 if (err)
198 goto done;
200 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
201 if (blame->lines == NULL) {
202 err = got_error_from_errno();
203 goto done;
206 /* Loop over first-parent history and try to blame commits. */
207 err = got_object_open_as_commit(&commit, repo, start_commit_id);
208 if (err)
209 goto done;
210 id = got_object_id_dup(start_commit_id);
211 if (id == NULL) {
212 err = got_error_from_errno();
213 goto done;
215 while (1) {
216 struct got_object_qid *pid;
218 pid = SIMPLEQ_FIRST(&commit->parent_ids);
219 if (pid == NULL)
220 break;
222 err = blame_commit(blame, id, pid->id, path, repo, cb, arg);
223 if (err) {
224 if (err->code == GOT_ERR_ITER_COMPLETED)
225 err = NULL;
226 break;
229 free(id);
230 id = got_object_id_dup(pid->id);
231 if (id == NULL) {
232 err = got_error_from_errno();
233 goto done;
235 got_object_commit_close(commit);
236 err = got_object_open_as_commit(&commit, repo, id);
237 if (err)
238 goto done;
241 /* Annotate remaining non-annotated lines with last commit. */
242 for (lineno = 1; lineno <= blame->nlines; lineno++) {
243 err = annotate_line(blame, lineno, id, cb, arg);
244 if (err)
245 goto done;
248 done:
249 free(id);
250 if (obj)
251 got_object_close(obj);
252 if (blob)
253 got_object_blob_close(blob);
254 if (commit)
255 got_object_commit_close(commit);
256 if (err) {
257 if (blame)
258 blame_close(blame);
259 } else
260 *blamep = blame;
262 return err;
265 static const struct got_error *
266 blame_line(struct got_object_id **id, struct got_blame *blame, int lineno)
268 if (lineno < 1 || lineno > blame->nlines)
269 return got_error(GOT_ERR_RANGE);
270 *id = &blame->lines[lineno - 1].id;
271 return NULL;
274 static char *
275 parse_next_line(FILE *f, size_t *len)
277 char *line;
278 size_t linelen;
279 size_t lineno;
280 const char delim[3] = { '\0', '\0', '\0'};
282 line = fparseln(f, &linelen, &lineno, delim, 0);
283 if (len)
284 *len = linelen;
285 return line;
288 const struct got_error *
289 got_blame(const char *path, struct got_object_id *start_commit_id,
290 struct got_repository *repo, FILE *outfile)
292 const struct got_error *err = NULL;
293 struct got_blame *blame;
294 int lineno;
295 char *abspath;
297 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
298 return got_error_from_errno();
300 err = blame_open(&blame, abspath, start_commit_id, repo, NULL, NULL);
301 if (err) {
302 free(abspath);
303 return err;
306 for (lineno = 1; lineno <= blame->nlines; lineno++) {
307 struct got_object_id *id;
308 char *line, *id_str;
310 line = parse_next_line(blame->f, NULL);
311 if (line == NULL)
312 break;
314 err = blame_line(&id, blame, lineno);
315 if (err)
316 break;
318 err = got_object_id_str(&id_str, id);
319 if (err) {
320 free(line);
321 break;
324 fprintf(outfile, "%.8s %s\n", id_str, line);
325 free(line);
326 free(id_str);
329 blame_close(blame);
330 free(abspath);
331 return err;
334 const struct got_error *
335 got_blame_incremental(const char *path, struct got_object_id *commit_id,
336 struct got_repository *repo,
337 const struct got_error *(*cb)(void *, int, int, struct got_object_id *),
338 void *arg)
340 const struct got_error *err = NULL;
341 struct got_blame *blame;
342 char *abspath;
344 if (asprintf(&abspath, "%s%s", path[0] == '/' ? "" : "/", path) == -1)
345 return got_error_from_errno();
347 err = blame_open(&blame, abspath, commit_id, repo, cb, arg);
348 free(abspath);
349 if (err == NULL)
350 blame_close(blame);
351 return err;