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"
32 #include "got_lib_diff.h"
33 #include "got_lib_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)
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;
53 return got_error(GOT_ERR_FILE_OPEN);
61 return got_error(GOT_ERR_FILE_OPEN);
68 idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
69 hdrlen = got_object_blob_get_hdrlen(blob1);
71 err = got_object_blob_read_block(&len, blob1);
77 /* Skip blob object header first time around. */
78 fwrite(got_object_blob_get_read_buf(blob1) + hdrlen, len - hdrlen, 1, f1);
86 idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 hdrlen = got_object_blob_get_hdrlen(blob2);
89 err = got_object_blob_read_block(&len, blob2);
95 /* Skip blob object header first time around. */
96 fwrite(got_object_blob_get_read_buf(blob2) + hdrlen, len - hdrlen, 1, f2);
100 idstr2 = "/dev/null";
111 memset(&ds, 0, sizeof(ds));
112 /* XXX should stat buffers be passed in args instead of ds? */
113 ds.stb1.st_mode = S_IFREG;
115 ds.stb1.st_size = size1;
116 ds.stb1.st_mtime = 0; /* XXX */
118 ds.stb2.st_mode = S_IFREG;
120 ds.stb2.st_size = size2;
121 ds.stb2.st_mtime = 0; /* XXX */
123 memset(&args, 0, sizeof(args));
124 args.diff_format = D_UNIFIED;
125 args.label[0] = label1 ? label1 : idstr1;
126 args.label[1] = label2 ? label2 : idstr2;
127 args.diff_context = 3;
128 flags |= D_PROTOTYPE;
130 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
139 struct got_tree_entry *
140 match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
142 struct got_tree_entry *te2;
144 SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
145 if (strcmp(te1->name, te2->name) == 0)
151 static const struct got_error *
152 diff_added_blob(struct got_object_id *id, struct got_repository *repo,
155 const struct got_error *err;
156 struct got_blob_object *blob;
157 struct got_object *obj;
159 err = got_object_open(&obj, repo, id);
162 err = got_object_blob_open(&blob, repo, obj, 8192);
166 return got_diff_blob(NULL, blob, NULL, NULL, outfile);
169 static const struct got_error *
170 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
171 struct got_repository *repo, FILE *outfile)
173 const struct got_error *err;
174 struct got_object *obj1 = NULL;
175 struct got_object *obj2 = NULL;
176 struct got_blob_object *blob1 = NULL;
177 struct got_blob_object *blob2 = NULL;
179 err = got_object_open(&obj1, repo, id1);
181 return got_error(GOT_ERR_BAD_OBJ_HDR);
182 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
183 err = got_error(GOT_ERR_OBJ_TYPE);
187 err = got_object_open(&obj2, repo, id2);
189 err= got_error(GOT_ERR_BAD_OBJ_HDR);
192 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
193 err = got_error(GOT_ERR_BAD_OBJ_DATA);
197 err = got_object_blob_open(&blob1, repo, obj1, 8192);
199 err = got_error(GOT_ERR_FILE_OPEN);
203 err = got_object_blob_open(&blob2, repo, obj2, 8192);
205 err = got_error(GOT_ERR_FILE_OPEN);
209 err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
213 got_object_close(obj1);
215 got_object_close(obj2);
217 got_object_blob_close(blob1);
219 got_object_blob_close(blob2);
223 static const struct got_error *
224 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
227 const struct got_error *err;
228 struct got_blob_object *blob;
229 struct got_object *obj;
231 err = got_object_open(&obj, repo, id);
234 err = got_object_blob_open(&blob, repo, obj, 8192);
238 return got_diff_blob(blob, NULL, NULL, NULL, outfile);
241 static const struct got_error *
242 diff_added_tree(struct got_object_id *id, struct got_repository *repo,
245 const struct got_error *err = NULL;
246 struct got_object *treeobj = NULL;
247 struct got_tree_object *tree = NULL;
249 err = got_object_open(&treeobj, repo, id);
253 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
254 err = got_error(GOT_ERR_OBJ_TYPE);
258 err = got_object_tree_open(&tree, repo, treeobj);
262 err = got_diff_tree(NULL, tree, repo, outfile);
266 got_object_tree_close(tree);
268 got_object_close(treeobj);
272 static const struct got_error *
273 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
274 struct got_repository *repo, FILE *outfile)
276 const struct got_error *err = NULL;
277 struct got_object *treeobj1 = NULL;
278 struct got_object *treeobj2 = NULL;
279 struct got_tree_object *tree1 = NULL;
280 struct got_tree_object *tree2 = NULL;
282 err = got_object_open(&treeobj1, repo, id1);
286 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
287 err = got_error(GOT_ERR_OBJ_TYPE);
291 err = got_object_open(&treeobj2, repo, id2);
295 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
296 err = got_error(GOT_ERR_OBJ_TYPE);
300 err = got_object_tree_open(&tree1, repo, treeobj1);
304 err = got_object_tree_open(&tree2, repo, treeobj2);
308 err = got_diff_tree(tree1, tree2, repo, outfile);
312 got_object_tree_close(tree1);
314 got_object_tree_close(tree2);
316 got_object_close(treeobj1);
318 got_object_close(treeobj2);
322 static const struct got_error *
323 diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
325 const struct got_error *err = NULL;
326 struct got_object *treeobj = NULL;
327 struct got_tree_object *tree = NULL;
329 err = got_object_open(&treeobj, repo, id);
333 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
334 err = got_error(GOT_ERR_OBJ_TYPE);
338 err = got_object_tree_open(&tree, repo, treeobj);
342 err = got_diff_tree(tree, NULL, repo, outfile);
346 got_object_tree_close(tree);
348 got_object_close(treeobj);
352 static const struct got_error *
353 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
360 static const struct got_error *
361 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
362 struct got_repository *repo, FILE *outfile)
364 struct got_tree_entry *te2 = NULL;
367 te2 = match_entry_by_name(te1, tree2);
369 if (S_ISDIR(te1->mode))
370 return diff_deleted_tree(te1->id, repo, outfile);
371 return diff_deleted_blob(te1->id, repo, outfile);
374 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
375 if (got_object_id_cmp(te1->id, te2->id) != 0)
376 return diff_modified_tree(te1->id, te2->id, repo,
378 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
379 if (got_object_id_cmp(te1->id, te2->id) != 0)
380 return diff_modified_blob(te1->id, te2->id, repo,
384 return diff_kind_mismatch(te1->id, te2->id, outfile);
387 static const struct got_error *
388 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
389 struct got_repository *repo, FILE *outfile)
392 struct got_tree_entry *te1 = match_entry_by_name(te2, tree1);
393 if (te1 != NULL) /* handled by diff_entry_old_new() */
397 if (S_ISDIR(te2->mode))
398 return diff_added_tree(te2->id, repo, outfile);
399 return diff_added_blob(te2->id, repo, outfile);
402 const struct got_error *
403 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
404 struct got_repository *repo, FILE *outfile)
406 const struct got_error *err = NULL;
407 struct got_tree_entry *te1 = NULL;
408 struct got_tree_entry *te2 = NULL;
411 te1 = SIMPLEQ_FIRST(&tree1->entries);
413 te2 = SIMPLEQ_FIRST(&tree2->entries);
417 err = diff_entry_old_new(te1, tree2, repo, outfile);
423 err = diff_entry_new_old(te2, tree1, repo, outfile);
429 te1 = SIMPLEQ_NEXT(te1, entry);
431 te2 = SIMPLEQ_NEXT(te2, entry);
432 } while (te1 || te2);