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 static const struct got_error *
148 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
149 struct got_tree_object *tree2)
156 same_id(struct got_object_id *id1, struct got_object_id *id2)
158 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
161 static const struct got_error *
162 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
164 const struct got_error *err;
165 struct got_blob_object *blob;
166 struct got_object *obj;
168 err = got_object_open(&obj, repo, id);
171 err = got_object_blob_open(&blob, repo, obj, 8192);
175 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
178 static const struct got_error *
179 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
180 struct got_repository *repo)
182 const struct got_error *err;
183 struct got_object *obj1 = NULL;
184 struct got_object *obj2 = NULL;
185 struct got_blob_object *blob1 = NULL;
186 struct got_blob_object *blob2 = NULL;
188 err = got_object_open(&obj1, repo, id1);
190 return got_error(GOT_ERR_BAD_OBJ_HDR);
191 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
192 err = got_error(GOT_ERR_OBJ_TYPE);
196 err = got_object_open(&obj2, repo, id2);
198 err= got_error(GOT_ERR_BAD_OBJ_HDR);
201 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
202 err = got_error(GOT_ERR_BAD_OBJ_DATA);
206 err = got_object_blob_open(&blob1, repo, obj1, 8192);
208 err = got_error(GOT_ERR_FILE_OPEN);
212 err = got_object_blob_open(&blob2, repo, obj2, 8192);
214 err = got_error(GOT_ERR_FILE_OPEN);
218 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
221 got_object_close(obj1);
222 got_object_close(obj2);
223 got_object_blob_close(blob1);
224 got_object_blob_close(blob2);
228 static const struct got_error *
229 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
231 const struct got_error *err;
232 struct got_blob_object *blob;
233 struct got_object *obj;
235 err = got_object_open(&obj, repo, id);
238 err = got_object_blob_open(&blob, repo, obj, 8192);
242 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
245 static const struct got_error *
246 diff_added_tree(struct got_object_id *id, struct got_repository *repo)
248 const struct got_error *err = NULL;
249 struct got_object *treeobj = NULL;
250 struct got_tree_object *tree = NULL;
252 err = got_object_open(&treeobj, repo, id);
256 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
257 err = got_error(GOT_ERR_OBJ_TYPE);
261 err = got_object_tree_open(&tree, repo, treeobj);
265 err = got_diff_tree(NULL, tree, repo);
269 got_object_tree_close(tree);
271 got_object_close(treeobj);
275 static const struct got_error *
276 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
277 struct got_repository *repo)
279 const struct got_error *err = NULL;
280 struct got_object *treeobj1 = NULL;
281 struct got_object *treeobj2 = NULL;
282 struct got_tree_object *tree1 = NULL;
283 struct got_tree_object *tree2 = NULL;
285 err = got_object_open(&treeobj1, repo, id1);
289 if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
290 err = got_error(GOT_ERR_OBJ_TYPE);
294 err = got_object_open(&treeobj2, repo, id2);
298 if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
299 err = got_error(GOT_ERR_OBJ_TYPE);
303 err = got_object_tree_open(&tree1, repo, treeobj1);
307 err = got_object_tree_open(&tree2, repo, treeobj2);
311 err = got_diff_tree(tree1, tree2, repo);
315 got_object_tree_close(tree1);
317 got_object_tree_close(tree2);
319 got_object_close(treeobj1);
321 got_object_close(treeobj2);
325 static const struct got_error *
326 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
328 const struct got_error *err = NULL;
329 struct got_object *treeobj = NULL;
330 struct got_tree_object *tree = NULL;
332 err = got_object_open(&treeobj, repo, id);
336 if (treeobj->type != GOT_OBJ_TYPE_TREE) {
337 err = got_error(GOT_ERR_OBJ_TYPE);
341 err = got_object_tree_open(&tree, repo, treeobj);
345 err = got_diff_tree(tree, NULL, repo);
349 got_object_tree_close(tree);
351 got_object_close(treeobj);
355 static const struct got_error *
356 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
362 static const struct got_error *
363 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
364 struct got_repository *repo)
366 const struct got_error *err;
367 struct got_tree_entry *te2;
369 err = match_entry_by_name(&te2, te1, tree2);
373 if (S_ISDIR(te1->mode))
374 return diff_deleted_tree(&te1->id, repo);
375 return diff_deleted_blob(&te1->id, repo);
378 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
379 if (!same_id(&te1->id, &te2->id))
380 return diff_modified_tree(&te1->id, &te2->id, repo);
381 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
382 if (!same_id(&te1->id, &te2->id))
383 return diff_modified_blob(&te1->id, &te2->id, repo);
386 return diff_kind_mismatch(&te1->id, &te2->id);
389 static const struct got_error *
390 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
391 struct got_repository *repo)
393 const struct got_error *err;
394 struct got_tree_entry *te1;
396 err = match_entry_by_name(&te1, te2, tree1);
399 if (te1 != NULL) /* handled by diff_entry_old_new() */
402 if (S_ISDIR(te2->mode))
403 return diff_added_tree(&te2->id, repo);
404 return diff_added_blob(&te2->id, repo);
407 const struct got_error *
408 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
409 struct got_repository *repo)
411 const struct got_error *err = NULL;
412 struct got_tree_entry *te1 = NULL;
413 struct got_tree_entry *te2 = NULL;
416 te1 = SIMPLEQ_FIRST(&tree1->entries);
418 te2 = SIMPLEQ_FIRST(&tree2->entries);
422 err = diff_entry_old_new(te1, tree2, repo);
428 err = diff_entry_new_old(te2, tree1, repo);
434 te1 = SIMPLEQ_NEXT(te1, entry);
436 te2 = SIMPLEQ_NEXT(te2, entry);
437 } while (te1 || te2);