Blob


1 /*
2 * Copyright (c) 2017 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <zlib.h>
27 #include "got_repository.h"
28 #include "got_object.h"
29 #include "got_error.h"
30 #include "got_diff.h"
32 #include "diff.h"
33 #include "path.h"
35 const struct got_error *
36 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
37 const char *label1, const char *label2, FILE *outfile)
38 {
39 struct got_diff_state ds;
40 struct got_diff_args args;
41 const struct got_error *err = NULL;
42 FILE *f1 = NULL, *f2 = NULL;
43 char hex1[SHA1_DIGEST_STRING_LENGTH];
44 char hex2[SHA1_DIGEST_STRING_LENGTH];
45 char *idstr1 = NULL, *idstr2 = NULL;
46 size_t len, hdrlen;
47 int res, flags = 0;
49 if (blob1) {
50 f1 = got_opentemp();
51 if (f1 == NULL)
52 return got_error(GOT_ERR_FILE_OPEN);
53 } else
54 flags |= D_EMPTY1;
56 if (blob2) {
57 f2 = got_opentemp();
58 if (f2 == NULL) {
59 fclose(f1);
60 return got_error(GOT_ERR_FILE_OPEN);
61 }
62 } else
63 flags |= D_EMPTY2;
65 if (blob1) {
66 idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
67 hdrlen = blob1->hdrlen;
68 do {
69 err = got_object_blob_read_block(blob1, &len);
70 if (err)
71 goto done;
72 /* Skip blob object header first time around. */
73 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
74 hdrlen = 0;
75 } while (len != 0);
76 } else
77 idstr1 = "/dev/null";
79 if (blob2) {
80 idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
81 hdrlen = blob2->hdrlen;
82 do {
83 err = got_object_blob_read_block(blob2, &len);
84 if (err)
85 goto done;
86 /* Skip blob object header first time around. */
87 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
88 hdrlen = 0;
89 } while (len != 0);
90 } else
91 idstr2 = "/dev/null";
93 if (f1)
94 fflush(f1);
95 if (f2)
96 fflush(f2);
98 memset(&ds, 0, sizeof(ds));
99 /* XXX should stat buffers be passed in args instead of ds? */
100 ds.stb1.st_mode = S_IFREG;
101 if (blob1)
102 ds.stb1.st_size = blob1->zb.z.total_out;
103 ds.stb1.st_mtime = 0; /* XXX */
105 ds.stb2.st_mode = S_IFREG;
106 if (blob2)
107 ds.stb2.st_size = blob2->zb.z.total_out;
108 ds.stb2.st_mtime = 0; /* XXX */
110 memset(&args, 0, sizeof(args));
111 args.diff_format = D_UNIFIED;
112 args.label[0] = label1 ? label1 : idstr1;
113 args.label[1] = label2 ? label2 : idstr2;
115 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
116 done:
117 if (f1)
118 fclose(f1);
119 if (f2)
120 fclose(f2);
121 return err;
124 struct got_tree_entry *
125 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
127 struct got_tree_entry *te2;
129 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
130 if (strcmp(te1->name, te2->name) == 0)
131 return te2;
133 return NULL;
136 static const struct got_error *
137 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
139 const struct got_error *err;
140 struct got_blob_object *blob;
141 struct got_object *obj;
143 err = got_object_open(&obj, repo, id);
144 if (err)
145 return err;
146 err = got_object_blob_open(&blob, repo, obj, 8192);
147 if (err != NULL)
148 return err;
150 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
153 static const struct got_error *
154 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
155 struct got_repository *repo)
157 const struct got_error *err;
158 struct got_object *obj1 = NULL;
159 struct got_object *obj2 = NULL;
160 struct got_blob_object *blob1 = NULL;
161 struct got_blob_object *blob2 = NULL;
163 err = got_object_open(&obj1, repo, id1);
164 if (err)
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
167 err = got_error(GOT_ERR_OBJ_TYPE);
168 goto done;
171 err = got_object_open(&obj2, repo, id2);
172 if (err) {
173 err= got_error(GOT_ERR_BAD_OBJ_HDR);
174 goto done;
176 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
177 err = got_error(GOT_ERR_BAD_OBJ_DATA);
178 goto done;
181 err = got_object_blob_open(&blob1, repo, obj1, 8192);
182 if (err != NULL) {
183 err = got_error(GOT_ERR_FILE_OPEN);
184 goto done;
187 err = got_object_blob_open(&blob2, repo, obj2, 8192);
188 if (err != NULL) {
189 err = got_error(GOT_ERR_FILE_OPEN);
190 goto done;
193 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
195 done:
196 if (obj1)
197 got_object_close(obj1);
198 if (obj2)
199 got_object_close(obj2);
200 if (blob1)
201 got_object_blob_close(blob1);
202 if (blob2)
203 got_object_blob_close(blob2);
204 return err;
207 static const struct got_error *
208 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
210 const struct got_error *err;
211 struct got_blob_object *blob;
212 struct got_object *obj;
214 err = got_object_open(&obj, repo, id);
215 if (err)
216 return err;
217 err = got_object_blob_open(&blob, repo, obj, 8192);
218 if (err != NULL)
219 return err;
221 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
224 static const struct got_error *
225 diff_added_tree(struct got_object_id *id, struct got_repository *repo)
227 const struct got_error *err = NULL;
228 struct got_object *treeobj = NULL;
229 struct got_tree_object *tree = NULL;
231 err = got_object_open(&treeobj, repo, id);
232 if (err)
233 goto done;
235 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
236 err = got_error(GOT_ERR_OBJ_TYPE);
237 goto done;
240 err = got_object_tree_open(&tree, repo, treeobj);
241 if (err)
242 goto done;
244 err = got_diff_tree(NULL, tree, repo);
246 done:
247 if (tree)
248 got_object_tree_close(tree);
249 if (treeobj)
250 got_object_close(treeobj);
251 return err;
254 static const struct got_error *
255 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
256 struct got_repository *repo)
258 const struct got_error *err = NULL;
259 struct got_object *treeobj1 = NULL;
260 struct got_object *treeobj2 = NULL;
261 struct got_tree_object *tree1 = NULL;
262 struct got_tree_object *tree2 = NULL;
264 err = got_object_open(&treeobj1, repo, id1);
265 if (err)
266 goto done;
268 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
269 err = got_error(GOT_ERR_OBJ_TYPE);
270 goto done;
273 err = got_object_open(&treeobj2, repo, id2);
274 if (err)
275 goto done;
277 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
278 err = got_error(GOT_ERR_OBJ_TYPE);
279 goto done;
282 err = got_object_tree_open(&tree1, repo, treeobj1);
283 if (err)
284 goto done;
286 err = got_object_tree_open(&tree2, repo, treeobj2);
287 if (err)
288 goto done;
290 err = got_diff_tree(tree1, tree2, repo);
292 done:
293 if (tree1)
294 got_object_tree_close(tree1);
295 if (tree2)
296 got_object_tree_close(tree2);
297 if (treeobj1)
298 got_object_close(treeobj1);
299 if (treeobj2)
300 got_object_close(treeobj2);
301 return err;
304 static const struct got_error *
305 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
307 const struct got_error *err = NULL;
308 struct got_object *treeobj = NULL;
309 struct got_tree_object *tree = NULL;
311 err = got_object_open(&treeobj, repo, id);
312 if (err)
313 goto done;
315 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
316 err = got_error(GOT_ERR_OBJ_TYPE);
317 goto done;
320 err = got_object_tree_open(&tree, repo, treeobj);
321 if (err)
322 goto done;
324 err = got_diff_tree(tree, NULL, repo);
326 done:
327 if (tree)
328 got_object_tree_close(tree);
329 if (treeobj)
330 got_object_close(treeobj);
331 return err;
334 static const struct got_error *
335 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
337 /* XXX TODO */
338 return NULL;
341 static const struct got_error *
342 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
343 struct got_repository *repo)
345 const struct got_error *err;
346 struct got_tree_entry *te2;
347 char hex[SHA1_DIGEST_STRING_LENGTH];
349 te2 = match_entry_by_name(te1, tree2);
350 if (te2 == NULL) {
351 if (S_ISDIR(te1->mode))
352 return diff_deleted_tree(&te1->id, repo);
353 return diff_deleted_blob(&te1->id, repo);
356 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
357 if (got_object_id_cmp(&te1->id, &te2->id) != 0)
358 return diff_modified_tree(&te1->id, &te2->id, repo);
359 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
360 if (got_object_id_cmp(&te1->id, &te2->id) != 0)
361 return diff_modified_blob(&te1->id, &te2->id, repo);
364 return diff_kind_mismatch(&te1->id, &te2->id);
367 static const struct got_error *
368 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
369 struct got_repository *repo)
371 const struct got_error *err;
372 struct got_tree_entry *te1;
374 te1 = match_entry_by_name(te2, tree1);
375 if (te1 != NULL) /* handled by diff_entry_old_new() */
376 return NULL;
378 if (S_ISDIR(te2->mode))
379 return diff_added_tree(&te2->id, repo);
380 return diff_added_blob(&te2->id, repo);
383 const struct got_error *
384 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
385 struct got_repository *repo)
387 const struct got_error *err = NULL;
388 struct got_tree_entry *te1 = NULL;
389 struct got_tree_entry *te2 = NULL;
391 if (tree1)
392 te1 = SIMPLEQ_FIRST(&tree1->entries);
393 if (tree2)
394 te2 = SIMPLEQ_FIRST(&tree2->entries);
396 do {
397 if (te1) {
398 err = diff_entry_old_new(te1, tree2, repo);
399 if (err)
400 break;
403 if (te2) {
404 err = diff_entry_new_old(te2, tree1, repo);
405 if (err)
406 break;
409 if (te1)
410 te1 = SIMPLEQ_NEXT(te1, entry);
411 if (te2)
412 te2 = SIMPLEQ_NEXT(te2, entry);
413 } while (te1 || te2);
415 return err;