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 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob1);
72 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
73 eb651edf 2018-02-11 stsp fwrite(blob1->read_buf + 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 eb651edf 2018-02-11 stsp err = got_object_blob_read_block(&len, blob2);
86 98abbc84 2017-11-30 stsp /* Skip blob object header first time around. */
87 eb651edf 2018-02-11 stsp fwrite(blob2->read_buf + 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 74671950 2018-02-11 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo,
138 74671950 2018-02-11 stsp FILE *outfile)
140 4e22badc 2017-11-30 stsp const struct got_error *err;
141 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
142 4e22badc 2017-11-30 stsp struct got_object *obj;
144 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
146 4e22badc 2017-11-30 stsp return err;
147 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
148 4e22badc 2017-11-30 stsp if (err != NULL)
149 4e22badc 2017-11-30 stsp return err;
151 74671950 2018-02-11 stsp return got_diff_blob(NULL, blob, NULL, NULL, outfile);
154 474b4f94 2017-11-30 stsp static const struct got_error *
155 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
156 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
158 6a213ccb 2017-11-30 stsp const struct got_error *err;
159 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
160 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
161 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
162 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
164 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
166 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
167 eef6493a 2018-01-19 stsp if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB) {
168 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
172 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
174 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
177 eef6493a 2018-01-19 stsp if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB) {
178 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
182 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 8192);
183 6a213ccb 2017-11-30 stsp if (err != NULL) {
184 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
188 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 8192);
189 6a213ccb 2017-11-30 stsp if (err != NULL) {
190 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
194 74671950 2018-02-11 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, outfile);
198 a3e2cbea 2017-12-01 stsp got_object_close(obj1);
200 a3e2cbea 2017-12-01 stsp got_object_close(obj2);
202 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob1);
204 a3e2cbea 2017-12-01 stsp got_object_blob_close(blob2);
205 6a213ccb 2017-11-30 stsp return err;
208 474b4f94 2017-11-30 stsp static const struct got_error *
209 74671950 2018-02-11 stsp diff_deleted_blob(struct got_object_id *id, struct got_repository *repo,
210 74671950 2018-02-11 stsp FILE *outfile)
212 365fb436 2017-11-30 stsp const struct got_error *err;
213 365fb436 2017-11-30 stsp struct got_blob_object *blob;
214 365fb436 2017-11-30 stsp struct got_object *obj;
216 365fb436 2017-11-30 stsp err = got_object_open(&obj, repo, id);
218 365fb436 2017-11-30 stsp return err;
219 c7020aea 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
220 365fb436 2017-11-30 stsp if (err != NULL)
221 365fb436 2017-11-30 stsp return err;
223 74671950 2018-02-11 stsp return got_diff_blob(blob, NULL, NULL, NULL, outfile);
226 474b4f94 2017-11-30 stsp static const struct got_error *
227 74671950 2018-02-11 stsp diff_added_tree(struct got_object_id *id, struct got_repository *repo,
228 74671950 2018-02-11 stsp FILE *outfile)
230 9c70d4c3 2017-11-30 stsp const struct got_error *err = NULL;
231 9c70d4c3 2017-11-30 stsp struct got_object *treeobj = NULL;
232 9c70d4c3 2017-11-30 stsp struct got_tree_object *tree = NULL;
234 9c70d4c3 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
238 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
239 9c70d4c3 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
243 9c70d4c3 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
247 74671950 2018-02-11 stsp err = got_diff_tree(NULL, tree, repo, outfile);
251 9c70d4c3 2017-11-30 stsp got_object_tree_close(tree);
252 9c70d4c3 2017-11-30 stsp if (treeobj)
253 9c70d4c3 2017-11-30 stsp got_object_close(treeobj);
254 9c70d4c3 2017-11-30 stsp return err;
257 474b4f94 2017-11-30 stsp static const struct got_error *
258 789689b5 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
259 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
261 789689b5 2017-11-30 stsp const struct got_error *err = NULL;
262 789689b5 2017-11-30 stsp struct got_object *treeobj1 = NULL;
263 789689b5 2017-11-30 stsp struct got_object *treeobj2 = NULL;
264 789689b5 2017-11-30 stsp struct got_tree_object *tree1 = NULL;
265 789689b5 2017-11-30 stsp struct got_tree_object *tree2 = NULL;
267 789689b5 2017-11-30 stsp err = got_object_open(&treeobj1, repo, id1);
271 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj1) != GOT_OBJ_TYPE_TREE) {
272 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
276 789689b5 2017-11-30 stsp err = got_object_open(&treeobj2, repo, id2);
280 eef6493a 2018-01-19 stsp if (got_object_get_type(treeobj2) != GOT_OBJ_TYPE_TREE) {
281 789689b5 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
285 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree1, repo, treeobj1);
289 789689b5 2017-11-30 stsp err = got_object_tree_open(&tree2, repo, treeobj2);
293 74671950 2018-02-11 stsp err = got_diff_tree(tree1, tree2, repo, outfile);
297 789689b5 2017-11-30 stsp got_object_tree_close(tree1);
299 789689b5 2017-11-30 stsp got_object_tree_close(tree2);
300 789689b5 2017-11-30 stsp if (treeobj1)
301 789689b5 2017-11-30 stsp got_object_close(treeobj1);
302 789689b5 2017-11-30 stsp if (treeobj2)
303 789689b5 2017-11-30 stsp got_object_close(treeobj2);
304 789689b5 2017-11-30 stsp return err;
307 474b4f94 2017-11-30 stsp static const struct got_error *
308 74671950 2018-02-11 stsp diff_deleted_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
310 2c56f2ce 2017-11-30 stsp const struct got_error *err = NULL;
311 2c56f2ce 2017-11-30 stsp struct got_object *treeobj = NULL;
312 2c56f2ce 2017-11-30 stsp struct got_tree_object *tree = NULL;
314 2c56f2ce 2017-11-30 stsp err = got_object_open(&treeobj, repo, id);
318 b107e67f 2018-01-19 stsp if (got_object_get_type(treeobj) != GOT_OBJ_TYPE_TREE) {
319 2c56f2ce 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
323 2c56f2ce 2017-11-30 stsp err = got_object_tree_open(&tree, repo, treeobj);
327 74671950 2018-02-11 stsp err = got_diff_tree(tree, NULL, repo, outfile);
331 2c56f2ce 2017-11-30 stsp got_object_tree_close(tree);
332 2c56f2ce 2017-11-30 stsp if (treeobj)
333 2c56f2ce 2017-11-30 stsp got_object_close(treeobj);
334 2c56f2ce 2017-11-30 stsp return err;
337 474b4f94 2017-11-30 stsp static const struct got_error *
338 74671950 2018-02-11 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
339 74671950 2018-02-11 stsp FILE *outfile)
341 013404a9 2017-11-30 stsp /* XXX TODO */
342 474b4f94 2017-11-30 stsp return NULL;
345 474b4f94 2017-11-30 stsp static const struct got_error *
346 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
347 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
349 474b4f94 2017-11-30 stsp const struct got_error *err;
350 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
351 a3e2cbea 2017-12-01 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
353 a3e2cbea 2017-12-01 stsp te2 = match_entry_by_name(te1, tree2);
354 474b4f94 2017-11-30 stsp if (te2 == NULL) {
355 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
356 74671950 2018-02-11 stsp return diff_deleted_tree(&te1->id, repo, outfile);
357 74671950 2018-02-11 stsp return diff_deleted_blob(&te1->id, repo, outfile);
360 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
361 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
362 74671950 2018-02-11 stsp return diff_modified_tree(&te1->id, &te2->id, repo,
364 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
365 a1fd68d8 2018-01-12 stsp if (got_object_id_cmp(&te1->id, &te2->id) != 0)
366 74671950 2018-02-11 stsp return diff_modified_blob(&te1->id, &te2->id, repo,
370 74671950 2018-02-11 stsp return diff_kind_mismatch(&te1->id, &te2->id, outfile);
373 474b4f94 2017-11-30 stsp static const struct got_error *
374 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
375 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
377 474b4f94 2017-11-30 stsp const struct got_error *err;
378 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
380 a3e2cbea 2017-12-01 stsp te1 = match_entry_by_name(te2, tree1);
381 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
382 474b4f94 2017-11-30 stsp return NULL;
384 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
385 74671950 2018-02-11 stsp return diff_added_tree(&te2->id, repo, outfile);
386 74671950 2018-02-11 stsp return diff_added_blob(&te2->id, repo, outfile);
389 474b4f94 2017-11-30 stsp const struct got_error *
390 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
391 74671950 2018-02-11 stsp struct got_repository *repo, FILE *outfile)
393 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
394 789689b5 2017-11-30 stsp struct got_tree_entry *te1 = NULL;
395 789689b5 2017-11-30 stsp struct got_tree_entry *te2 = NULL;
398 789689b5 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
400 789689b5 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
404 74671950 2018-02-11 stsp err = diff_entry_old_new(te1, tree2, repo, outfile);
410 74671950 2018-02-11 stsp err = diff_entry_new_old(te2, tree1, repo, outfile);
416 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
418 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
419 474b4f94 2017-11-30 stsp } while (te1 || te2);
421 474b4f94 2017-11-30 stsp return err;