2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
27 #include "got_repository.h"
28 #include "got_object.h"
29 #include "got_error.h"
41 if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
58 const struct got_error *
59 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
60 const char *label1, const char *label2, FILE *outfile)
62 struct got_diff_state ds;
63 struct got_diff_args args;
64 const struct got_error *err = NULL;
65 FILE *f1 = NULL, *f2 = NULL;
66 char hex1[SHA1_DIGEST_STRING_LENGTH];
67 char hex2[SHA1_DIGEST_STRING_LENGTH];
68 char *idstr1 = NULL, *idstr2 = NULL;
75 return got_error(GOT_ERR_FILE_OPEN);
83 return got_error(GOT_ERR_FILE_OPEN);
89 idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
90 hdrlen = blob1->hdrlen;
92 err = got_object_blob_read_block(blob1, &len);
95 /* Skip blob object header first time around. */
96 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
100 idstr1 = "/dev/null";
103 idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
104 hdrlen = blob2->hdrlen;
106 err = got_object_blob_read_block(blob2, &len);
109 /* Skip blob object header first time around. */
110 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
114 idstr2 = "/dev/null";
121 memset(&ds, 0, sizeof(ds));
122 /* XXX should stat buffers be passed in args instead of ds? */
123 ds.stb1.st_mode = S_IFREG;
125 ds.stb1.st_size = blob1->zb.z.total_out;
126 ds.stb1.st_mtime = 0; /* XXX */
128 ds.stb2.st_mode = S_IFREG;
130 ds.stb2.st_size = blob2->zb.z.total_out;
131 ds.stb2.st_mtime = 0; /* XXX */
133 memset(&args, 0, sizeof(args));
134 args.diff_format = D_UNIFIED;
135 args.label[0] = label1 ? label1 : idstr1;
136 args.label[1] = label2 ? label2 : idstr2;
138 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
147 struct got_tree_entry *
148 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
150 struct got_tree_entry *te2;
152 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
153 if (strcmp(te1->name, te2->name) == 0)
160 same_id(struct got_object_id *id1, struct got_object_id *id2)
162 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
165 static const struct got_error *
166 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
168 const struct got_error *err;
169 struct got_blob_object *blob;
170 struct got_object *obj;
172 err = got_object_open(&obj, repo, id);
175 err = got_object_blob_open(&blob, repo, obj, 8192);
179 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
182 static const struct got_error *
183 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
184 struct got_repository *repo)
186 const struct got_error *err;
187 struct got_object *obj1 = NULL;
188 struct got_object *obj2 = NULL;
189 struct got_blob_object *blob1 = NULL;
190 struct got_blob_object *blob2 = NULL;
192 err = got_object_open(&obj1, repo, id1);
194 return got_error(GOT_ERR_BAD_OBJ_HDR);
195 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
196 err = got_error(GOT_ERR_OBJ_TYPE);
200 err = got_object_open(&obj2, repo, id2);
202 err= got_error(GOT_ERR_BAD_OBJ_HDR);
205 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
206 err = got_error(GOT_ERR_BAD_OBJ_DATA);
210 err = got_object_blob_open(&blob1, repo, obj1, 8192);
212 err = got_error(GOT_ERR_FILE_OPEN);
216 err = got_object_blob_open(&blob2, repo, obj2, 8192);
218 err = got_error(GOT_ERR_FILE_OPEN);
222 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
226 got_object_close(obj1);
228 got_object_close(obj2);
230 got_object_blob_close(blob1);
232 got_object_blob_close(blob2);
236 static const struct got_error *
237 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
239 const struct got_error *err;
240 struct got_blob_object *blob;
241 struct got_object *obj;
243 err = got_object_open(&obj, repo, id);
246 err = got_object_blob_open(&blob, repo, obj, 8192);
250 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
253 static const struct got_error *
254 diff_added_tree(struct got_object_id *id, struct got_repository *repo)
256 const struct got_error *err = NULL;
257 struct got_object *treeobj = NULL;
258 struct got_tree_object *tree = NULL;
260 err = got_object_open(&treeobj, repo, id);
264 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
265 err = got_error(GOT_ERR_OBJ_TYPE);
269 err = got_object_tree_open(&tree, repo, treeobj);
273 err = got_diff_tree(NULL, tree, repo);
277 got_object_tree_close(tree);
279 got_object_close(treeobj);
283 static const struct got_error *
284 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
285 struct got_repository *repo)
287 const struct got_error *err = NULL;
288 struct got_object *treeobj1 = NULL;
289 struct got_object *treeobj2 = NULL;
290 struct got_tree_object *tree1 = NULL;
291 struct got_tree_object *tree2 = NULL;
293 err = got_object_open(&treeobj1, repo, id1);
297 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
298 err = got_error(GOT_ERR_OBJ_TYPE);
302 err = got_object_open(&treeobj2, repo, id2);
306 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
307 err = got_error(GOT_ERR_OBJ_TYPE);
311 err = got_object_tree_open(&tree1, repo, treeobj1);
315 err = got_object_tree_open(&tree2, repo, treeobj2);
319 err = got_diff_tree(tree1, tree2, repo);
323 got_object_tree_close(tree1);
325 got_object_tree_close(tree2);
327 got_object_close(treeobj1);
329 got_object_close(treeobj2);
333 static const struct got_error *
334 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
336 const struct got_error *err = NULL;
337 struct got_object *treeobj = NULL;
338 struct got_tree_object *tree = NULL;
340 err = got_object_open(&treeobj, repo, id);
344 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
345 err = got_error(GOT_ERR_OBJ_TYPE);
349 err = got_object_tree_open(&tree, repo, treeobj);
353 err = got_diff_tree(tree, NULL, repo);
357 got_object_tree_close(tree);
359 got_object_close(treeobj);
363 static const struct got_error *
364 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
370 static const struct got_error *
371 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
372 struct got_repository *repo)
374 const struct got_error *err;
375 struct got_tree_entry *te2;
376 char hex[SHA1_DIGEST_STRING_LENGTH];
378 te2 = match_entry_by_name(te1, tree2);
380 if (S_ISDIR(te1->mode))
381 return diff_deleted_tree(&te1->id, repo);
382 return diff_deleted_blob(&te1->id, repo);
385 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
386 if (!same_id(&te1->id, &te2->id))
387 return diff_modified_tree(&te1->id, &te2->id, repo);
388 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
389 if (!same_id(&te1->id, &te2->id))
390 return diff_modified_blob(&te1->id, &te2->id, repo);
394 return diff_kind_mismatch(&te1->id, &te2->id);
397 static const struct got_error *
398 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
399 struct got_repository *repo)
401 const struct got_error *err;
402 struct got_tree_entry *te1;
404 te1 = match_entry_by_name(te2, tree1);
405 if (te1 != NULL) /* handled by diff_entry_old_new() */
408 if (S_ISDIR(te2->mode))
409 return diff_added_tree(&te2->id, repo);
410 return diff_added_blob(&te2->id, repo);
413 const struct got_error *
414 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
415 struct got_repository *repo)
417 const struct got_error *err = NULL;
418 struct got_tree_entry *te1 = NULL;
419 struct got_tree_entry *te2 = NULL;
422 te1 = SIMPLEQ_FIRST(&tree1->entries);
424 te2 = SIMPLEQ_FIRST(&tree2->entries);
428 err = diff_entry_old_new(te1, tree2, repo);
434 err = diff_entry_new_old(te2, tree1, repo);
440 te1 = SIMPLEQ_NEXT(te1, entry);
442 te2 = SIMPLEQ_NEXT(te2, entry);
443 } while (te1 || te2);