2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7d283eee 2017-11-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 7d283eee 2017-11-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 7d283eee 2017-11-29 stsp * copyright notice and this permission notice appear in all copies.
8 7d283eee 2017-11-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7d283eee 2017-11-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7d283eee 2017-11-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7d283eee 2017-11-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7d283eee 2017-11-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7d283eee 2017-11-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7d283eee 2017-11-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
20 7d283eee 2017-11-29 stsp #include <stdio.h>
21 7d283eee 2017-11-29 stsp #include <stdlib.h>
22 7d283eee 2017-11-29 stsp #include <string.h>
23 f9d67749 2017-11-30 stsp #include <limits.h>
24 7d283eee 2017-11-29 stsp #include <sha1.h>
25 7d283eee 2017-11-29 stsp #include <zlib.h>
27 7d283eee 2017-11-29 stsp #include "got_repository.h"
28 7d283eee 2017-11-29 stsp #include "got_object.h"
29 7d283eee 2017-11-29 stsp #include "got_error.h"
30 789689b5 2017-11-30 stsp #include "got_diff.h"
32 7d283eee 2017-11-29 stsp #include "diff.h"
34 f9d67749 2017-11-30 stsp static FILE *
35 f9d67749 2017-11-30 stsp opentemp(void)
37 f9d67749 2017-11-30 stsp char name[PATH_MAX];
41 f9d67749 2017-11-30 stsp if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
42 f9d67749 2017-11-30 stsp return NULL;
44 f9d67749 2017-11-30 stsp fd = mkstemp(name);
46 f9d67749 2017-11-30 stsp return NULL;
48 f9d67749 2017-11-30 stsp unlink(name);
49 a7852263 2017-11-30 stsp f = fdopen(fd, "w+");
50 a7852263 2017-11-30 stsp if (f == NULL) {
52 a7852263 2017-11-30 stsp return NULL;
58 7d283eee 2017-11-29 stsp const struct got_error *
59 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
60 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
62 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
63 8ba9a219 2017-11-29 stsp struct got_diff_args args;
64 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
65 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
66 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
67 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
68 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
69 f9d67749 2017-11-30 stsp size_t len, hdrlen;
70 4e22badc 2017-11-30 stsp int res, flags = 0;
72 4e22badc 2017-11-30 stsp if (blob1) {
73 4e22badc 2017-11-30 stsp f1 = opentemp();
74 4e22badc 2017-11-30 stsp if (f1 == NULL)
75 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
77 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
79 4e22badc 2017-11-30 stsp if (blob2) {
80 4e22badc 2017-11-30 stsp f2 = opentemp();
81 4e22badc 2017-11-30 stsp if (f2 == NULL) {
83 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
86 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
88 98abbc84 2017-11-30 stsp if (blob1) {
89 98abbc84 2017-11-30 stsp idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
90 4e22badc 2017-11-30 stsp hdrlen = blob1->hdrlen;
92 4e22badc 2017-11-30 stsp err = got_object_blob_read_block(blob1, &len);
95 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
96 4e22badc 2017-11-30 stsp fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
98 4e22badc 2017-11-30 stsp } while (len != 0);
100 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
102 98abbc84 2017-11-30 stsp if (blob2) {
103 98abbc84 2017-11-30 stsp idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
104 98abbc84 2017-11-30 stsp hdrlen = blob2->hdrlen;
106 98abbc84 2017-11-30 stsp err = got_object_blob_read_block(blob2, &len);
109 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
110 98abbc84 2017-11-30 stsp fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
111 98abbc84 2017-11-30 stsp hdrlen = 0;
112 98abbc84 2017-11-30 stsp } while (len != 0);
114 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
117 98abbc84 2017-11-30 stsp fflush(f1);
119 98abbc84 2017-11-30 stsp fflush(f2);
121 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
122 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
123 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
125 98abbc84 2017-11-30 stsp ds.stb1.st_size = blob1->zb.z.total_out;
126 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
128 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
130 98abbc84 2017-11-30 stsp ds.stb2.st_size = blob2->zb.z.total_out;
131 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
133 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
134 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
135 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
136 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
138 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
141 98abbc84 2017-11-30 stsp fclose(f1);
143 98abbc84 2017-11-30 stsp fclose(f2);
144 7d283eee 2017-11-29 stsp return err;
147 474b4f94 2017-11-30 stsp static const struct got_error *
148 474b4f94 2017-11-30 stsp match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
149 474b4f94 2017-11-30 stsp struct got_tree_object *tree2)
151 474b4f94 2017-11-30 stsp *te = NULL;
152 474b4f94 2017-11-30 stsp return NULL;
156 474b4f94 2017-11-30 stsp same_id(struct got_object_id *id1, struct got_object_id *id2)
158 474b4f94 2017-11-30 stsp return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
161 474b4f94 2017-11-30 stsp static const struct got_error *
162 4e22badc 2017-11-30 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo)
164 4e22badc 2017-11-30 stsp const struct got_error *err;
165 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
166 4e22badc 2017-11-30 stsp struct got_object *obj;
168 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
170 4e22badc 2017-11-30 stsp return err;
171 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
172 4e22badc 2017-11-30 stsp if (err != NULL)
173 4e22badc 2017-11-30 stsp return err;
175 4e22badc 2017-11-30 stsp return got_diff_blob(NULL, blob, NULL, NULL, stdout);
178 474b4f94 2017-11-30 stsp static const struct got_error *
179 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
180 6a213ccb 2017-11-30 stsp struct got_repository *repo)
182 6a213ccb 2017-11-30 stsp const struct got_error *err;
183 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
184 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
185 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
186 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
188 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
190 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
191 6a213ccb 2017-11-30 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
192 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
196 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
198 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
201 6a213ccb 2017-11-30 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
202 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
206 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
207 6a213ccb 2017-11-30 stsp if (err != NULL) {
208 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
212 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
213 6a213ccb 2017-11-30 stsp if (err != NULL) {
214 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
218 6a213ccb 2017-11-30 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
221 6a213ccb 2017-11-30 stsp got_object_close(obj1);
222 6a213ccb 2017-11-30 stsp got_object_close(obj2);
223 6a213ccb 2017-11-30 stsp got_object_blob_close(blob1);
224 6a213ccb 2017-11-30 stsp got_object_blob_close(blob2);
225 6a213ccb 2017-11-30 stsp return err;
228 474b4f94 2017-11-30 stsp static const struct got_error *
229 365fb436 2017-11-30 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
231 365fb436 2017-11-30 stsp const struct got_error *err;
232 365fb436 2017-11-30 stsp struct got_blob_object *blob;
233 365fb436 2017-11-30 stsp struct got_object *obj;
235 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
237 365fb436 2017-11-30 stsp return err;
238 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
239 365fb436 2017-11-30 stsp if (err != NULL)
240 365fb436 2017-11-30 stsp return err;
242 365fb436 2017-11-30 stsp return got_diff_blob(blob, NULL, NULL, NULL, stdout);
245 474b4f94 2017-11-30 stsp static const struct got_error *
246 9c70d4c3 2017-11-30 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo)
248 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
249 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
250 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
252 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
256 9c70d4c3 2017-11-30 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
257 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
261 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
265 9c70d4c3 2017-11-30 stsp err = got_diff_tree(NULL, tree, repo);
269 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
270 9c70d4c3 2017-11-30 stsp if (treeobj)
271 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
272 9c70d4c3 2017-11-30 stsp return err;
275 474b4f94 2017-11-30 stsp static const struct got_error *
276 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
277 789689b5 2017-11-30 stsp struct got_repository *repo)
279 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
280 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
281 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
282 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
283 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
285 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
289 789689b5 2017-11-30 stsp if (treeobj1->type != GOT_OBJ_TYPE_TREE) {
290 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
294 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
298 789689b5 2017-11-30 stsp if (treeobj2->type != GOT_OBJ_TYPE_TREE) {
299 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
303 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
307 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
311 789689b5 2017-11-30 stsp err = got_diff_tree(tree1, tree2, repo);
315 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
317 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
318 789689b5 2017-11-30 stsp if (treeobj1)
319 789689b5 2017-11-30 stsp got_object_close(treeobj1);
320 789689b5 2017-11-30 stsp if (treeobj2)
321 789689b5 2017-11-30 stsp got_object_close(treeobj2);
322 789689b5 2017-11-30 stsp return err;
325 474b4f94 2017-11-30 stsp static const struct got_error *
326 2c56f2ce 2017-11-30 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
328 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
329 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
330 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
332 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
336 2c56f2ce 2017-11-30 stsp if (treeobj->type != GOT_OBJ_TYPE_TREE) {
337 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
341 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
345 2c56f2ce 2017-11-30 stsp err = got_diff_tree(tree, NULL, repo);
349 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
350 2c56f2ce 2017-11-30 stsp if (treeobj)
351 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
352 2c56f2ce 2017-11-30 stsp return err;
355 474b4f94 2017-11-30 stsp static const struct got_error *
356 474b4f94 2017-11-30 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
358 013404a9 2017-11-30 stsp /* XXX TODO */
359 474b4f94 2017-11-30 stsp return NULL;
362 474b4f94 2017-11-30 stsp static const struct got_error *
363 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
364 6a213ccb 2017-11-30 stsp struct got_repository *repo)
366 474b4f94 2017-11-30 stsp const struct got_error *err;
367 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
369 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te2, te1, tree2);
371 474b4f94 2017-11-30 stsp return err;
372 474b4f94 2017-11-30 stsp if (te2 == NULL) {
373 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
374 2c56f2ce 2017-11-30 stsp return diff_deleted_tree(&te1->id, repo);
375 365fb436 2017-11-30 stsp return diff_deleted_blob(&te1->id, repo);
378 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
379 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
380 789689b5 2017-11-30 stsp return diff_modified_tree(&te1->id, &te2->id, repo);
381 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
382 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
383 6a213ccb 2017-11-30 stsp return diff_modified_blob(&te1->id, &te2->id, repo);
386 413ea19d 2017-11-30 stsp return diff_kind_mismatch(&te1->id, &te2->id);
389 474b4f94 2017-11-30 stsp static const struct got_error *
390 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
391 4e22badc 2017-11-30 stsp struct got_repository *repo)
393 474b4f94 2017-11-30 stsp const struct got_error *err;
394 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
396 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te1, te2, tree1);
398 474b4f94 2017-11-30 stsp return err;
399 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
400 474b4f94 2017-11-30 stsp return NULL;
402 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
403 9c70d4c3 2017-11-30 stsp return diff_added_tree(&te2->id, repo);
404 4e22badc 2017-11-30 stsp return diff_added_blob(&te2->id, repo);
407 474b4f94 2017-11-30 stsp const struct got_error *
408 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
409 474b4f94 2017-11-30 stsp struct got_repository *repo)
411 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
412 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
413 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
416 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
418 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
422 6a213ccb 2017-11-30 stsp err = diff_entry_old_new(te1, tree2, repo);
428 4e22badc 2017-11-30 stsp err = diff_entry_new_old(te2, tree1, repo);
434 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
436 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
437 474b4f94 2017-11-30 stsp } while (te1 || te2);
439 474b4f94 2017-11-30 stsp return err;