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 718b3ab0 2018-03-17 stsp #include "got_lib_diff.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_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 f934cf2c 2018-02-12 stsp size_t size1, size2;
48 4e22badc 2017-11-30 stsp int res, flags = 0;
50 4e22badc 2017-11-30 stsp if (blob1) {
51 a1fd68d8 2018-01-12 stsp f1 = got_opentemp();
52 4e22badc 2017-11-30 stsp if (f1 == NULL)
53 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
55 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
57 4e22badc 2017-11-30 stsp if (blob2) {
58 a1fd68d8 2018-01-12 stsp f2 = got_opentemp();
59 4e22badc 2017-11-30 stsp if (f2 == NULL) {
61 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
64 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
67 98abbc84 2017-11-30 stsp if (blob1) {
68 f934cf2c 2018-02-12 stsp idstr1 = got_object_blob_id_str(blob1, hex1, sizeof(hex1));
69 f934cf2c 2018-02-12 stsp hdrlen = got_object_blob_get_hdrlen(blob1);
71 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob1);
74 eba5c6bb 2018-02-11 stsp if (len == 0)
76 f934cf2c 2018-02-12 stsp size1 += len;
77 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
78 f934cf2c 2018-02-12 stsp fwrite(got_object_blob_get_read_buf(blob1) + hdrlen, len - hdrlen, 1, f1);
80 4e22badc 2017-11-30 stsp } while (len != 0);
82 98abbc84 2017-11-30 stsp idstr1 = "/dev/null";
85 98abbc84 2017-11-30 stsp if (blob2) {
86 f934cf2c 2018-02-12 stsp idstr2 = got_object_blob_id_str(blob2, hex2, sizeof(hex2));
87 f934cf2c 2018-02-12 stsp hdrlen = got_object_blob_get_hdrlen(blob2);
89 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob2);
92 eba5c6bb 2018-02-11 stsp if (len == 0)
94 f934cf2c 2018-02-12 stsp size2 += len;
95 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
96 f934cf2c 2018-02-12 stsp fwrite(got_object_blob_get_read_buf(blob2) + hdrlen, len - hdrlen, 1, f2);
98 98abbc84 2017-11-30 stsp } while (len != 0);
100 98abbc84 2017-11-30 stsp idstr2 = "/dev/null";
103 eba5c6bb 2018-02-11 stsp fflush(f1);
104 eba5c6bb 2018-02-11 stsp rewind(f1);
107 98abbc84 2017-11-30 stsp fflush(f2);
108 eba5c6bb 2018-02-11 stsp rewind(f2);
111 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
112 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
113 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
115 f934cf2c 2018-02-12 stsp ds.stb1.st_size = size1;
116 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
118 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
120 f934cf2c 2018-02-12 stsp ds.stb2.st_size = size2;
121 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
123 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
124 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
125 98abbc84 2017-11-30 stsp args.label[0] = label1 ? label1 : idstr1;
126 98abbc84 2017-11-30 stsp args.label[1] = label2 ? label2 : idstr2;
127 c2c21d46 2018-03-27 stsp args.diff_context = 3;
129 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
132 98abbc84 2017-11-30 stsp fclose(f1);
134 98abbc84 2017-11-30 stsp fclose(f2);
135 7d283eee 2017-11-29 stsp return err;
138 a3e2cbea 2017-12-01 stsp struct got_tree_entry *
139 a3e2cbea 2017-12-01 stsp match_entry_by_name(struct got_tree_entry *te1, struct got_tree_object *tree2)
141 a3e2cbea 2017-12-01 stsp struct got_tree_entry *te2;
143 a3e2cbea 2017-12-01 stsp SIMPLEQ_FOREACH(te2, &tree2->entries, entry) {
144 a3e2cbea 2017-12-01 stsp if (strcmp(te1->name, te2->name) == 0)
145 a3e2cbea 2017-12-01 stsp return te2;
147 474b4f94 2017-11-30 stsp return NULL;
150 474b4f94 2017-11-30 stsp static const struct got_error *
151 74671950 2018-02-11 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo,
152 74671950 2018-02-11 stsp FILE *outfile)
154 4e22badc 2017-11-30 stsp const struct got_error *err;
155 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
156 4e22badc 2017-11-30 stsp struct got_object *obj;
158 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
160 4e22badc 2017-11-30 stsp return err;
161 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
162 4e22badc 2017-11-30 stsp if (err != NULL)
163 4e22badc 2017-11-30 stsp return err;
165 74671950 2018-02-11 stsp return got_diff_blob(NULL, blob, NULL, NULL, outfile);
168 474b4f94 2017-11-30 stsp static const struct got_error *
169 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
170 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
172 6a213ccb 2017-11-30 stsp const struct got_error *err;
173 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
174 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
175 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
176 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
178 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
180 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
181 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
182 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
186 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
188 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
191 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
192 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
196 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
197 6a213ccb 2017-11-30 stsp if (err != NULL) {
198 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
202 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
203 6a213ccb 2017-11-30 stsp if (err != NULL) {
204 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
208 74671950 2018-02-11 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
212 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
214 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
216 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
218 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
219 6a213ccb 2017-11-30 stsp return err;
222 474b4f94 2017-11-30 stsp static const struct got_error *
223 74671950 2018-02-11 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
224 74671950 2018-02-11 stsp FILE *outfile)
226 365fb436 2017-11-30 stsp const struct got_error *err;
227 365fb436 2017-11-30 stsp struct got_blob_object *blob;
228 365fb436 2017-11-30 stsp struct got_object *obj;
230 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
232 365fb436 2017-11-30 stsp return err;
233 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
234 365fb436 2017-11-30 stsp if (err != NULL)
235 365fb436 2017-11-30 stsp return err;
237 74671950 2018-02-11 stsp return got_diff_blob(blob, NULL, NULL, NULL, outfile);
240 474b4f94 2017-11-30 stsp static const struct got_error *
241 74671950 2018-02-11 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo,
242 74671950 2018-02-11 stsp FILE *outfile)
244 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
245 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
246 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
248 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
252 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
253 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
257 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
261 74671950 2018-02-11 stsp err = got_diff_tree(NULL, tree, repo, outfile);
265 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
266 9c70d4c3 2017-11-30 stsp if (treeobj)
267 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
268 9c70d4c3 2017-11-30 stsp return err;
271 474b4f94 2017-11-30 stsp static const struct got_error *
272 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
273 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
275 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
276 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
277 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
278 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
279 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
281 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
285 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
286 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
290 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
294 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
295 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
299 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
303 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
307 74671950 2018-02-11 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
311 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
313 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
314 789689b5 2017-11-30 stsp if (treeobj1)
315 789689b5 2017-11-30 stsp got_object_close(treeobj1);
316 789689b5 2017-11-30 stsp if (treeobj2)
317 789689b5 2017-11-30 stsp got_object_close(treeobj2);
318 789689b5 2017-11-30 stsp return err;
321 474b4f94 2017-11-30 stsp static const struct got_error *
322 74671950 2018-02-11 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
324 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
325 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
326 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
328 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
332 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
333 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
337 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
341 74671950 2018-02-11 stsp err = got_diff_tree(tree, NULL, repo, outfile);
345 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
346 2c56f2ce 2017-11-30 stsp if (treeobj)
347 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
348 2c56f2ce 2017-11-30 stsp return err;
351 474b4f94 2017-11-30 stsp static const struct got_error *
352 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
353 74671950 2018-02-11 stsp FILE *outfile)
355 013404a9 2017-11-30 stsp /* XXX TODO */
356 474b4f94 2017-11-30 stsp return NULL;
359 474b4f94 2017-11-30 stsp static const struct got_error *
360 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
361 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
363 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
365 a3e2cbea 2017-12-01 stsp te2 = match_entry_by_name(te1, tree2);
366 474b4f94 2017-11-30 stsp if (te2 == NULL) {
367 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
368 59ece79d 2018-02-12 stsp return diff_deleted_tree(te1->id, repo, outfile);
369 59ece79d 2018-02-12 stsp return diff_deleted_blob(te1->id, repo, outfile);
372 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
373 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
374 59ece79d 2018-02-12 stsp return diff_modified_tree(te1->id, te2->id, repo,
376 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
377 59ece79d 2018-02-12 stsp if (got_object_id_cmp(te1->id, te2->id) != 0)
378 59ece79d 2018-02-12 stsp return diff_modified_blob(te1->id, te2->id, repo,
382 59ece79d 2018-02-12 stsp return diff_kind_mismatch(te1->id, te2->id, outfile);
385 474b4f94 2017-11-30 stsp static const struct got_error *
386 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
387 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
389 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
391 a3e2cbea 2017-12-01 stsp te1 = match_entry_by_name(te2, tree1);
392 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
393 474b4f94 2017-11-30 stsp return NULL;
395 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
396 59ece79d 2018-02-12 stsp return diff_added_tree(te2->id, repo, outfile);
397 59ece79d 2018-02-12 stsp return diff_added_blob(te2->id, repo, outfile);
400 474b4f94 2017-11-30 stsp const struct got_error *
401 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
402 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
404 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
405 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
406 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
409 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
411 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
415 74671950 2018-02-11 stsp err = diff_entry_old_new(te1, tree2, repo, outfile);
421 74671950 2018-02-11 stsp err = diff_entry_new_old(te2, tree1, repo, outfile);
427 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
429 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
430 474b4f94 2017-11-30 stsp } while (te1 || te2);
432 474b4f94 2017-11-30 stsp return err;