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"
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;
52 return got_error(GOT_ERR_FILE_OPEN);
60 return got_error(GOT_ERR_FILE_OPEN);
66 idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
67 hdrlen = blob1->hdrlen;
69 err = got_object_blob_read_block(blob1, &len);
72 /* Skip blob object header first time around. */
73 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
80 idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
81 hdrlen = blob2->hdrlen;
83 err = got_object_blob_read_block(blob2, &len);
86 /* Skip blob object header first time around. */
87 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, 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;
102 ds.stb1.st_size = blob1->zb.z.total_out;
103 ds.stb1.st_mtime = 0; /* XXX */
105 ds.stb2.st_mode = S_IFREG;
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);
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)
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);
146 err = got_object_blob_open(&blob, repo, obj, 8192);
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);
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
166 if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
167 err = got_error(GOT_ERR_OBJ_TYPE);
171 err = got_object_open(&obj2, repo, id2);
173 err= got_error(GOT_ERR_BAD_OBJ_HDR);
176 if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
177 err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 err = got_object_blob_open(&blob1, repo, obj1, 8192);
183 err = got_error(GOT_ERR_FILE_OPEN);
187 err = got_object_blob_open(&blob2, repo, obj2, 8192);
189 err = got_error(GOT_ERR_FILE_OPEN);
193 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
197 got_object_close(obj1);
199 got_object_close(obj2);
201 got_object_blob_close(blob1);
203 got_object_blob_close(blob2);
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);
217 err = got_object_blob_open(&blob, repo, obj, 8192);
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);
235 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
236 err = got_error(GOT_ERR_OBJ_TYPE);
240 err = got_object_tree_open(&tree, repo, treeobj);
244 err = got_diff_tree(NULL, tree, repo);
248 got_object_tree_close(tree);
250 got_object_close(treeobj);
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);
268 if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
269 err = got_error(GOT_ERR_OBJ_TYPE);
273 err = got_object_open(&treeobj2, repo, id2);
277 if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
278 err = got_error(GOT_ERR_OBJ_TYPE);
282 err = got_object_tree_open(&tree1, repo, treeobj1);
286 err = got_object_tree_open(&tree2, repo, treeobj2);
290 err = got_diff_tree(tree1, tree2, repo);
294 got_object_tree_close(tree1);
296 got_object_tree_close(tree2);
298 got_object_close(treeobj1);
300 got_object_close(treeobj2);
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);
315 if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
316 err = got_error(GOT_ERR_OBJ_TYPE);
320 err = got_object_tree_open(&tree, repo, treeobj);
324 err = got_diff_tree(tree, NULL, repo);
328 got_object_tree_close(tree);
330 got_object_close(treeobj);
334 static const struct got_error *
335 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
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);
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() */
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;
392 te1 = SIMPLEQ_FIRST(&tree1->entries);
394 te2 = SIMPLEQ_FIRST(&tree2->entries);
398 err = diff_entry_old_new(te1, tree2, repo);
404 err = diff_entry_new_old(te2, tree1, repo);
410 te1 = SIMPLEQ_NEXT(te1, entry);
412 te2 = SIMPLEQ_NEXT(te2, entry);
413 } while (te1 || te2);