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"
40 if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
57 const struct got_error *
58 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
59 const char *label1, const char *label2, FILE *outfile)
61 struct got_diff_state ds;
62 struct got_diff_args args;
63 const struct got_error *err = NULL;
64 FILE *f1 = NULL, *f2 = NULL;
65 char hex1[SHA1_DIGEST_STRING_LENGTH];
66 char hex2[SHA1_DIGEST_STRING_LENGTH];
73 return got_error(GOT_ERR_FILE_OPEN);
81 return got_error(GOT_ERR_FILE_OPEN);
89 hdrlen = blob1->hdrlen;
91 err = got_object_blob_read_block(blob1, &len);
94 /* Skip blob object header first time around. */
95 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
100 hdrlen = blob2->hdrlen;
102 err = got_object_blob_read_block(blob2, &len);
105 /* Skip blob object header first time around. */
106 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
115 memset(&ds, 0, sizeof(ds));
116 /* XXX should stat buffers be passed in args instead of ds? */
117 ds.stb1.st_mode = S_IFREG;
118 ds.stb1.st_size = blob1->zb.z.total_out;
119 ds.stb1.st_mtime = 0; /* XXX */
121 ds.stb2.st_mode = S_IFREG;
122 ds.stb2.st_size = blob2->zb.z.total_out;
123 ds.stb2.st_mtime = 0; /* XXX */
125 memset(&args, 0, sizeof(args));
126 args.diff_format = D_UNIFIED;
127 args.label[0] = label1 ?
128 label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
129 args.label[1] = label2 ?
130 label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
132 err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
139 static const struct got_error *
140 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
141 struct got_tree_object *tree2)
148 same_id(struct got_object_id *id1, struct got_object_id *id2)
150 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
153 static const struct got_error *
154 diff_added_blob(struct got_object_id *id, struct got_repository *repo)
156 const struct got_error *err;
157 struct got_blob_object *blob;
158 struct got_object *obj;
160 err = got_object_open(&obj, repo, id);
163 err = got_object_blob_open(&blob, repo, obj, 512);
167 return got_diff_blob(NULL, blob, NULL, NULL, stdout);
170 static const struct got_error *
171 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
172 struct got_repository *repo)
174 const struct got_error *err;
175 struct got_object *obj1 = NULL;
176 struct got_object *obj2 = NULL;
177 struct got_blob_object *blob1 = NULL;
178 struct got_blob_object *blob2 = NULL;
180 err = got_object_open(&obj1, repo, id1);
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
183 if (obj1->type != GOT_OBJ_TYPE_BLOB) {
184 err = got_error(GOT_ERR_OBJ_TYPE);
188 err = got_object_open(&obj2, repo, id2);
190 err= got_error(GOT_ERR_BAD_OBJ_HDR);
193 if (obj2->type != GOT_OBJ_TYPE_BLOB) {
194 err = got_error(GOT_ERR_BAD_OBJ_DATA);
198 err = got_object_blob_open(&blob1, repo, obj1, 512);
200 err = got_error(GOT_ERR_FILE_OPEN);
204 err = got_object_blob_open(&blob2, repo, obj2, 512);
206 err = got_error(GOT_ERR_FILE_OPEN);
210 err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
213 got_object_close(obj1);
214 got_object_close(obj2);
215 got_object_blob_close(blob1);
216 got_object_blob_close(blob2);
220 static const struct got_error *
221 diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
223 const struct got_error *err;
224 struct got_blob_object *blob;
225 struct got_object *obj;
227 err = got_object_open(&obj, repo, id);
230 err = got_object_blob_open(&blob, repo, obj, 512);
234 return got_diff_blob(blob, NULL, NULL, NULL, stdout);
237 static const struct got_error *
238 diff_added_tree(struct got_object_id *id)
243 static const struct got_error *
244 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
249 static const struct got_error *
250 diff_deleted_tree(struct got_object_id *id)
255 static const struct got_error *
256 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
261 static const struct got_error *
262 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
263 struct got_repository *repo)
265 const struct got_error *err;
266 struct got_tree_entry *te2;
268 err = match_entry_by_name(&te2, te1, tree2);
272 if (S_ISDIR(te1->mode))
273 return diff_deleted_tree(&te1->id);
274 return diff_deleted_blob(&te1->id, repo);
277 if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
278 if (!same_id(&te1->id, &te2->id))
279 return diff_modified_tree(&te1->id, &te2->id);
280 } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
281 if (!same_id(&te1->id, &te2->id))
282 return diff_modified_blob(&te1->id, &te2->id, repo);
285 return diff_kind_mismatch(&te1->id, &te2->id);
288 static const struct got_error *
289 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
290 struct got_repository *repo)
292 const struct got_error *err;
293 struct got_tree_entry *te1;
295 err = match_entry_by_name(&te1, te2, tree1);
298 if (te1 != NULL) /* handled by diff_entry_old_new() */
301 if (S_ISDIR(te2->mode))
302 return diff_added_tree(&te2->id);
303 return diff_added_blob(&te2->id, repo);
306 const struct got_error *
307 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
308 struct got_repository *repo)
310 const struct got_error *err = NULL;
311 struct got_tree_entry *te1;
312 struct got_tree_entry *te2;
314 if (tree1->nentries == 0 && tree2->nentries == 0)
317 te1 = SIMPLEQ_FIRST(&tree1->entries);
318 te2 = SIMPLEQ_FIRST(&tree2->entries);
322 err = diff_entry_old_new(te1, tree2, repo);
328 err = diff_entry_new_old(te2, tree1, repo);
334 te1 = SIMPLEQ_NEXT(te1, entry);
336 te2 = SIMPLEQ_NEXT(te2, entry);
337 } while (te1 || te2);