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"
33 a1fd68d8 2018-01-12 stsp #include "path.h"
35 7d283eee 2017-11-29 stsp const struct got_error *
36 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
37 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
39 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
40 8ba9a219 2017-11-29 stsp struct got_diff_args args;
41 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
42 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
43 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
44 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
45 98abbc84 2017-11-30 stsp char *idstr1 = NULL, *idstr2 = NULL;
46 f9d67749 2017-11-30 stsp size_t len, hdrlen;
47 4e22badc 2017-11-30 stsp int res, flags = 0;
49 4e22badc 2017-11-30 stsp if (blob1) {
50 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
51 4e22badc 2017-11-30 stsp if (f1 == NULL)
52 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
54 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
56 4e22badc 2017-11-30 stsp if (blob2) {
57 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
58 4e22badc 2017-11-30 stsp if (f2 == NULL) {
60 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
63 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
65 98abbc84 2017-11-30 stsp if (blob1) {
66 98abbc84 2017-11-30 stsp idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
67 4e22badc 2017-11-30 stsp hdrlen = blob1->hdrlen;
69 4e22badc 2017-11-30 stsp err = got_object_blob_read_block(blob1, &len);
72 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
73 4e22badc 2017-11-30 stsp fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
75 4e22badc 2017-11-30 stsp } while (len != 0);
77 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
79 98abbc84 2017-11-30 stsp if (blob2) {
80 98abbc84 2017-11-30 stsp idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
81 98abbc84 2017-11-30 stsp hdrlen = blob2->hdrlen;
83 98abbc84 2017-11-30 stsp err = got_object_blob_read_block(blob2, &len);
86 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
87 98abbc84 2017-11-30 stsp fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
89 98abbc84 2017-11-30 stsp } while (len != 0);
91 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
98 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
99 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
100 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
102 98abbc84 2017-11-30 stsp ds.stb1.st_size = blob1->zb.z.total_out;
103 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
105 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
107 98abbc84 2017-11-30 stsp ds.stb2.st_size = blob2->zb.z.total_out;
108 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
110 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
111 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
112 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
113 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
115 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
118 98abbc84 2017-11-30 stsp fclose(f1);
120 98abbc84 2017-11-30 stsp fclose(f2);
121 7d283eee 2017-11-29 stsp return err;
124 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
125 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
127 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
129 a3e2cbea 2017-12-01 stsp SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
130 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
131 a3e2cbea 2017-12-01 stsp return te2;
133 474b4f94 2017-11-30 stsp return NULL;
136 474b4f94 2017-11-30 stsp static const struct got_error *
137 4e22badc 2017-11-30 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo)
139 4e22badc 2017-11-30 stsp const struct got_error *err;
140 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
141 4e22badc 2017-11-30 stsp struct got_object *obj;
143 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
145 4e22badc 2017-11-30 stsp return err;
146 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
147 4e22badc 2017-11-30 stsp if (err != NULL)
148 4e22badc 2017-11-30 stsp return err;
150 4e22badc 2017-11-30 stsp return got_diff_blob(NULL, blob, NULL, NULL, stdout);
153 474b4f94 2017-11-30 stsp static const struct got_error *
154 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
155 6a213ccb 2017-11-30 stsp struct got_repository *repo)
157 6a213ccb 2017-11-30 stsp const struct got_error *err;
158 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
159 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
160 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
161 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
163 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
165 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
166 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
167 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
171 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
173 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
176 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
177 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
182 6a213ccb 2017-11-30 stsp if (err != NULL) {
183 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
187 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
188 6a213ccb 2017-11-30 stsp if (err != NULL) {
189 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
193 6a213ccb 2017-11-30 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
197 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
199 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
201 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
203 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
204 6a213ccb 2017-11-30 stsp return err;
207 474b4f94 2017-11-30 stsp static const struct got_error *
208 365fb436 2017-11-30 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo)
210 365fb436 2017-11-30 stsp const struct got_error *err;
211 365fb436 2017-11-30 stsp struct got_blob_object *blob;
212 365fb436 2017-11-30 stsp struct got_object *obj;
214 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
216 365fb436 2017-11-30 stsp return err;
217 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
218 365fb436 2017-11-30 stsp if (err != NULL)
219 365fb436 2017-11-30 stsp return err;
221 365fb436 2017-11-30 stsp return got_diff_blob(blob, NULL, NULL, NULL, stdout);
224 474b4f94 2017-11-30 stsp static const struct got_error *
225 9c70d4c3 2017-11-30 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo)
227 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
228 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
229 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
231 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
235 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
236 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
240 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
244 9c70d4c3 2017-11-30 stsp err = got_diff_tree(NULL, tree, repo);
248 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
249 9c70d4c3 2017-11-30 stsp if (treeobj)
250 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
251 9c70d4c3 2017-11-30 stsp return err;
254 474b4f94 2017-11-30 stsp static const struct got_error *
255 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
256 789689b5 2017-11-30 stsp struct got_repository *repo)
258 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
259 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
260 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
261 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
262 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
264 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
268 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
269 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
273 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
277 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
278 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
282 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
286 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
290 789689b5 2017-11-30 stsp err = got_diff_tree(tree1, tree2, repo);
294 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
296 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
297 789689b5 2017-11-30 stsp if (treeobj1)
298 789689b5 2017-11-30 stsp got_object_close(treeobj1);
299 789689b5 2017-11-30 stsp if (treeobj2)
300 789689b5 2017-11-30 stsp got_object_close(treeobj2);
301 789689b5 2017-11-30 stsp return err;
304 474b4f94 2017-11-30 stsp static const struct got_error *
305 2c56f2ce 2017-11-30 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo)
307 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
308 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
309 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
311 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
315 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
316 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
320 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
324 2c56f2ce 2017-11-30 stsp err = got_diff_tree(tree, NULL, repo);
328 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
329 2c56f2ce 2017-11-30 stsp if (treeobj)
330 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
331 2c56f2ce 2017-11-30 stsp return err;
334 474b4f94 2017-11-30 stsp static const struct got_error *
335 474b4f94 2017-11-30 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
337 013404a9 2017-11-30 stsp /* XXX TODO */
338 474b4f94 2017-11-30 stsp return NULL;
341 474b4f94 2017-11-30 stsp static const struct got_error *
342 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
343 6a213ccb 2017-11-30 stsp struct got_repository *repo)
345 474b4f94 2017-11-30 stsp const struct got_error *err;
346 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
347 a3e2cbea 2017-12-01 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
349 a3e2cbea 2017-12-01 stsp te2 = match_entry_by_name(te1, tree2);
350 474b4f94 2017-11-30 stsp if (te2 == NULL) {
351 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
352 2c56f2ce 2017-11-30 stsp return diff_deleted_tree(&te1->id, repo);
353 365fb436 2017-11-30 stsp return diff_deleted_blob(&te1->id, repo);
356 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
357 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
358 789689b5 2017-11-30 stsp return diff_modified_tree(&te1->id, &te2->id, repo);
359 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
360 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
361 6a213ccb 2017-11-30 stsp return diff_modified_blob(&te1->id, &te2->id, repo);
364 413ea19d 2017-11-30 stsp return diff_kind_mismatch(&te1->id, &te2->id);
367 474b4f94 2017-11-30 stsp static const struct got_error *
368 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
369 4e22badc 2017-11-30 stsp struct got_repository *repo)
371 474b4f94 2017-11-30 stsp const struct got_error *err;
372 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
374 a3e2cbea 2017-12-01 stsp te1 = match_entry_by_name(te2, tree1);
375 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
376 474b4f94 2017-11-30 stsp return NULL;
378 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
379 9c70d4c3 2017-11-30 stsp return diff_added_tree(&te2->id, repo);
380 4e22badc 2017-11-30 stsp return diff_added_blob(&te2->id, repo);
383 474b4f94 2017-11-30 stsp const struct got_error *
384 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
385 474b4f94 2017-11-30 stsp struct got_repository *repo)
387 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
388 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
389 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
392 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
394 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
398 6a213ccb 2017-11-30 stsp err = diff_entry_old_new(te1, tree2, repo);
404 4e22badc 2017-11-30 stsp err = diff_entry_new_old(te2, tree1, repo);
410 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
412 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
413 474b4f94 2017-11-30 stsp } while (te1 || te2);
415 474b4f94 2017-11-30 stsp return err;