Blame


1 7d283eee 2017-11-29 stsp /*
2 7d283eee 2017-11-29 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7d283eee 2017-11-29 stsp *
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.
7 7d283eee 2017-11-29 stsp *
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.
15 7d283eee 2017-11-29 stsp */
16 7d283eee 2017-11-29 stsp
17 7d283eee 2017-11-29 stsp #include <sys/queue.h>
18 1c7f0520 2017-11-29 stsp #include <sys/stat.h>
19 7d283eee 2017-11-29 stsp
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>
26 7d283eee 2017-11-29 stsp
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 7d283eee 2017-11-29 stsp
31 7d283eee 2017-11-29 stsp #include "diff.h"
32 7d283eee 2017-11-29 stsp
33 f9d67749 2017-11-30 stsp static FILE *
34 f9d67749 2017-11-30 stsp opentemp(void)
35 7d283eee 2017-11-29 stsp {
36 f9d67749 2017-11-30 stsp char name[PATH_MAX];
37 7d283eee 2017-11-29 stsp int fd;
38 7d283eee 2017-11-29 stsp
39 f9d67749 2017-11-30 stsp if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
40 f9d67749 2017-11-30 stsp return NULL;
41 f9d67749 2017-11-30 stsp
42 f9d67749 2017-11-30 stsp fd = mkstemp(name);
43 f9d67749 2017-11-30 stsp if (fd < 0)
44 f9d67749 2017-11-30 stsp return NULL;
45 f9d67749 2017-11-30 stsp
46 f9d67749 2017-11-30 stsp unlink(name);
47 f9d67749 2017-11-30 stsp return (fdopen(fd, "w+"));
48 7d283eee 2017-11-29 stsp }
49 7d283eee 2017-11-29 stsp
50 7d283eee 2017-11-29 stsp const struct got_error *
51 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
52 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
53 7d283eee 2017-11-29 stsp {
54 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
55 8ba9a219 2017-11-29 stsp struct got_diff_args args;
56 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
57 4e22badc 2017-11-30 stsp FILE *f1 = NULL, *f2 = NULL;
58 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
59 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
60 f9d67749 2017-11-30 stsp size_t len, hdrlen;
61 4e22badc 2017-11-30 stsp int res, flags = 0;
62 7d283eee 2017-11-29 stsp
63 4e22badc 2017-11-30 stsp if (blob1) {
64 4e22badc 2017-11-30 stsp f1 = opentemp();
65 4e22badc 2017-11-30 stsp if (f1 == NULL)
66 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
67 4e22badc 2017-11-30 stsp } else
68 4e22badc 2017-11-30 stsp flags |= D_EMPTY1;
69 7d283eee 2017-11-29 stsp
70 4e22badc 2017-11-30 stsp if (blob2) {
71 4e22badc 2017-11-30 stsp f2 = opentemp();
72 4e22badc 2017-11-30 stsp if (f2 == NULL) {
73 4e22badc 2017-11-30 stsp fclose(f1);
74 4e22badc 2017-11-30 stsp return got_error(GOT_ERR_FILE_OPEN);
75 4e22badc 2017-11-30 stsp }
76 4e22badc 2017-11-30 stsp } else
77 4e22badc 2017-11-30 stsp flags |= D_EMPTY2;
78 7d283eee 2017-11-29 stsp
79 4e22badc 2017-11-30 stsp if (blob1 == NULL) {
80 4e22badc 2017-11-30 stsp f1 = NULL;
81 4e22badc 2017-11-30 stsp } else {
82 4e22badc 2017-11-30 stsp hdrlen = blob1->hdrlen;
83 4e22badc 2017-11-30 stsp do {
84 4e22badc 2017-11-30 stsp err = got_object_blob_read_block(blob1, &len);
85 4e22badc 2017-11-30 stsp if (err)
86 4e22badc 2017-11-30 stsp goto done;
87 4e22badc 2017-11-30 stsp /* Skip blob object header first time around. */
88 4e22badc 2017-11-30 stsp fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
89 4e22badc 2017-11-30 stsp hdrlen = 0;
90 4e22badc 2017-11-30 stsp } while (len != 0);
91 4e22badc 2017-11-30 stsp }
92 7d283eee 2017-11-29 stsp
93 7d283eee 2017-11-29 stsp hdrlen = blob2->hdrlen;
94 7d283eee 2017-11-29 stsp do {
95 7d283eee 2017-11-29 stsp err = got_object_blob_read_block(blob2, &len);
96 7d283eee 2017-11-29 stsp if (err)
97 7d283eee 2017-11-29 stsp goto done;
98 7d283eee 2017-11-29 stsp /* Skip blob object header first time around. */
99 7d283eee 2017-11-29 stsp fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
100 7d283eee 2017-11-29 stsp hdrlen = 0;
101 7d283eee 2017-11-29 stsp } while (len != 0);
102 7d283eee 2017-11-29 stsp
103 7d283eee 2017-11-29 stsp fflush(f1);
104 7d283eee 2017-11-29 stsp fflush(f2);
105 f9d67749 2017-11-30 stsp /* rewind(f1); */
106 f9d67749 2017-11-30 stsp /* rewind(f2);*/
107 7d283eee 2017-11-29 stsp
108 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
109 f9d67749 2017-11-30 stsp /* XXX should stat buffers be passed in args instead of ds? */
110 f9d67749 2017-11-30 stsp ds.stb1.st_mode = S_IFREG;
111 f9d67749 2017-11-30 stsp ds.stb1.st_size = blob1->zb.z.total_out;
112 f9d67749 2017-11-30 stsp ds.stb1.st_mtime = 0; /* XXX */
113 8ba9a219 2017-11-29 stsp
114 f9d67749 2017-11-30 stsp ds.stb2.st_mode = S_IFREG;
115 f9d67749 2017-11-30 stsp ds.stb2.st_size = blob2->zb.z.total_out;
116 f9d67749 2017-11-30 stsp ds.stb2.st_mtime = 0; /* XXX */
117 f9d67749 2017-11-30 stsp
118 f9d67749 2017-11-30 stsp memset(&args, 0, sizeof(args));
119 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
120 62136d3a 2017-11-29 stsp args.label[0] = label1 ?
121 62136d3a 2017-11-29 stsp label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
122 62136d3a 2017-11-29 stsp args.label[1] = label2 ?
123 62136d3a 2017-11-29 stsp label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
124 62136d3a 2017-11-29 stsp
125 cb74ff21 2017-11-30 stsp err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
126 7d283eee 2017-11-29 stsp done:
127 7d283eee 2017-11-29 stsp fclose(f1);
128 7d283eee 2017-11-29 stsp fclose(f2);
129 7d283eee 2017-11-29 stsp return err;
130 7d283eee 2017-11-29 stsp }
131 474b4f94 2017-11-30 stsp
132 474b4f94 2017-11-30 stsp static const struct got_error *
133 474b4f94 2017-11-30 stsp match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
134 474b4f94 2017-11-30 stsp struct got_tree_object *tree2)
135 474b4f94 2017-11-30 stsp {
136 474b4f94 2017-11-30 stsp *te = NULL;
137 474b4f94 2017-11-30 stsp return NULL;
138 474b4f94 2017-11-30 stsp }
139 474b4f94 2017-11-30 stsp
140 474b4f94 2017-11-30 stsp static int
141 474b4f94 2017-11-30 stsp same_id(struct got_object_id *id1, struct got_object_id *id2)
142 474b4f94 2017-11-30 stsp {
143 474b4f94 2017-11-30 stsp return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
144 474b4f94 2017-11-30 stsp }
145 474b4f94 2017-11-30 stsp
146 474b4f94 2017-11-30 stsp static const struct got_error *
147 4e22badc 2017-11-30 stsp diff_added_blob(struct got_object_id *id, struct got_repository *repo)
148 474b4f94 2017-11-30 stsp {
149 4e22badc 2017-11-30 stsp const struct got_error *err;
150 4e22badc 2017-11-30 stsp struct got_blob_object *blob;
151 4e22badc 2017-11-30 stsp struct got_object *obj;
152 4e22badc 2017-11-30 stsp
153 4e22badc 2017-11-30 stsp err = got_object_open(&obj, repo, id);
154 4e22badc 2017-11-30 stsp if (err)
155 4e22badc 2017-11-30 stsp return err;
156 4e22badc 2017-11-30 stsp err = got_object_blob_open(&blob, repo, obj, 512);
157 4e22badc 2017-11-30 stsp if (err != NULL)
158 4e22badc 2017-11-30 stsp return err;
159 4e22badc 2017-11-30 stsp
160 4e22badc 2017-11-30 stsp return got_diff_blob(NULL, blob, NULL, NULL, stdout);
161 474b4f94 2017-11-30 stsp }
162 474b4f94 2017-11-30 stsp
163 474b4f94 2017-11-30 stsp static const struct got_error *
164 6a213ccb 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
165 6a213ccb 2017-11-30 stsp struct got_repository *repo)
166 474b4f94 2017-11-30 stsp {
167 6a213ccb 2017-11-30 stsp const struct got_error *err;
168 6a213ccb 2017-11-30 stsp struct got_object *obj1 = NULL;
169 6a213ccb 2017-11-30 stsp struct got_object *obj2 = NULL;
170 6a213ccb 2017-11-30 stsp struct got_blob_object *blob1 = NULL;
171 6a213ccb 2017-11-30 stsp struct got_blob_object *blob2 = NULL;
172 6a213ccb 2017-11-30 stsp
173 6a213ccb 2017-11-30 stsp err = got_object_open(&obj1, repo, id1);
174 6a213ccb 2017-11-30 stsp if (err)
175 6a213ccb 2017-11-30 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
176 6a213ccb 2017-11-30 stsp if (obj1->type != GOT_OBJ_TYPE_BLOB) {
177 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_OBJ_TYPE);
178 6a213ccb 2017-11-30 stsp goto done;
179 6a213ccb 2017-11-30 stsp }
180 6a213ccb 2017-11-30 stsp
181 6a213ccb 2017-11-30 stsp err = got_object_open(&obj2, repo, id2);
182 6a213ccb 2017-11-30 stsp if (err) {
183 6a213ccb 2017-11-30 stsp err= got_error(GOT_ERR_BAD_OBJ_HDR);
184 6a213ccb 2017-11-30 stsp goto done;
185 6a213ccb 2017-11-30 stsp }
186 6a213ccb 2017-11-30 stsp if (obj2->type != GOT_OBJ_TYPE_BLOB) {
187 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
188 6a213ccb 2017-11-30 stsp goto done;
189 6a213ccb 2017-11-30 stsp }
190 6a213ccb 2017-11-30 stsp
191 6a213ccb 2017-11-30 stsp err = got_object_blob_open(&blob1, repo, obj1, 512);
192 6a213ccb 2017-11-30 stsp if (err != NULL) {
193 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
194 6a213ccb 2017-11-30 stsp goto done;
195 6a213ccb 2017-11-30 stsp }
196 6a213ccb 2017-11-30 stsp
197 6a213ccb 2017-11-30 stsp err = got_object_blob_open(&blob2, repo, obj2, 512);
198 6a213ccb 2017-11-30 stsp if (err != NULL) {
199 6a213ccb 2017-11-30 stsp err = got_error(GOT_ERR_FILE_OPEN);
200 6a213ccb 2017-11-30 stsp goto done;
201 6a213ccb 2017-11-30 stsp }
202 6a213ccb 2017-11-30 stsp
203 6a213ccb 2017-11-30 stsp err = got_diff_blob(blob1, blob2, NULL, NULL, stdout);
204 6a213ccb 2017-11-30 stsp
205 6a213ccb 2017-11-30 stsp done:
206 6a213ccb 2017-11-30 stsp got_object_close(obj1);
207 6a213ccb 2017-11-30 stsp got_object_close(obj2);
208 6a213ccb 2017-11-30 stsp got_object_blob_close(blob1);
209 6a213ccb 2017-11-30 stsp got_object_blob_close(blob2);
210 6a213ccb 2017-11-30 stsp return err;
211 474b4f94 2017-11-30 stsp }
212 474b4f94 2017-11-30 stsp
213 474b4f94 2017-11-30 stsp static const struct got_error *
214 474b4f94 2017-11-30 stsp diff_deleted_blob(struct got_object_id *id)
215 474b4f94 2017-11-30 stsp {
216 474b4f94 2017-11-30 stsp return NULL;
217 474b4f94 2017-11-30 stsp }
218 474b4f94 2017-11-30 stsp
219 474b4f94 2017-11-30 stsp static const struct got_error *
220 474b4f94 2017-11-30 stsp diff_added_tree(struct got_object_id *id)
221 474b4f94 2017-11-30 stsp {
222 474b4f94 2017-11-30 stsp return NULL;
223 474b4f94 2017-11-30 stsp }
224 474b4f94 2017-11-30 stsp
225 474b4f94 2017-11-30 stsp static const struct got_error *
226 474b4f94 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
227 474b4f94 2017-11-30 stsp {
228 474b4f94 2017-11-30 stsp return NULL;
229 474b4f94 2017-11-30 stsp }
230 474b4f94 2017-11-30 stsp
231 474b4f94 2017-11-30 stsp static const struct got_error *
232 474b4f94 2017-11-30 stsp diff_deleted_tree(struct got_object_id *id)
233 474b4f94 2017-11-30 stsp {
234 474b4f94 2017-11-30 stsp return NULL;
235 474b4f94 2017-11-30 stsp }
236 474b4f94 2017-11-30 stsp
237 474b4f94 2017-11-30 stsp static const struct got_error *
238 474b4f94 2017-11-30 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
239 474b4f94 2017-11-30 stsp {
240 474b4f94 2017-11-30 stsp return NULL;
241 474b4f94 2017-11-30 stsp }
242 474b4f94 2017-11-30 stsp
243 474b4f94 2017-11-30 stsp static const struct got_error *
244 6a213ccb 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
245 6a213ccb 2017-11-30 stsp struct got_repository *repo)
246 474b4f94 2017-11-30 stsp {
247 474b4f94 2017-11-30 stsp const struct got_error *err;
248 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
249 474b4f94 2017-11-30 stsp
250 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te2, te1, tree2);
251 474b4f94 2017-11-30 stsp if (err)
252 474b4f94 2017-11-30 stsp return err;
253 474b4f94 2017-11-30 stsp if (te2 == NULL) {
254 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
255 474b4f94 2017-11-30 stsp return diff_deleted_tree(&te1->id);
256 474b4f94 2017-11-30 stsp return diff_deleted_blob(&te1->id);
257 474b4f94 2017-11-30 stsp }
258 474b4f94 2017-11-30 stsp
259 4209f790 2017-11-30 stsp if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
260 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
261 474b4f94 2017-11-30 stsp return diff_modified_tree(&te1->id, &te2->id);
262 4209f790 2017-11-30 stsp } else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
263 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
264 6a213ccb 2017-11-30 stsp return diff_modified_blob(&te1->id, &te2->id, repo);
265 413ea19d 2017-11-30 stsp }
266 474b4f94 2017-11-30 stsp
267 413ea19d 2017-11-30 stsp return diff_kind_mismatch(&te1->id, &te2->id);
268 474b4f94 2017-11-30 stsp }
269 474b4f94 2017-11-30 stsp
270 474b4f94 2017-11-30 stsp static const struct got_error *
271 4e22badc 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
272 4e22badc 2017-11-30 stsp struct got_repository *repo)
273 474b4f94 2017-11-30 stsp {
274 474b4f94 2017-11-30 stsp const struct got_error *err;
275 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
276 474b4f94 2017-11-30 stsp
277 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te1, te2, tree1);
278 474b4f94 2017-11-30 stsp if (err)
279 474b4f94 2017-11-30 stsp return err;
280 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
281 474b4f94 2017-11-30 stsp return NULL;
282 474b4f94 2017-11-30 stsp
283 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
284 474b4f94 2017-11-30 stsp return diff_added_tree(&te2->id);
285 4e22badc 2017-11-30 stsp return diff_added_blob(&te2->id, repo);
286 474b4f94 2017-11-30 stsp }
287 474b4f94 2017-11-30 stsp
288 474b4f94 2017-11-30 stsp const struct got_error *
289 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
290 474b4f94 2017-11-30 stsp struct got_repository *repo)
291 474b4f94 2017-11-30 stsp {
292 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
293 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
294 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
295 474b4f94 2017-11-30 stsp
296 474b4f94 2017-11-30 stsp if (tree1->nentries == 0 && tree2->nentries == 0)
297 474b4f94 2017-11-30 stsp return NULL;
298 474b4f94 2017-11-30 stsp
299 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
300 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
301 474b4f94 2017-11-30 stsp
302 474b4f94 2017-11-30 stsp do {
303 474b4f94 2017-11-30 stsp if (te1) {
304 6a213ccb 2017-11-30 stsp err = diff_entry_old_new(te1, tree2, repo);
305 474b4f94 2017-11-30 stsp if (err)
306 474b4f94 2017-11-30 stsp break;
307 474b4f94 2017-11-30 stsp }
308 474b4f94 2017-11-30 stsp
309 474b4f94 2017-11-30 stsp if (te2) {
310 4e22badc 2017-11-30 stsp err = diff_entry_new_old(te2, tree1, repo);
311 474b4f94 2017-11-30 stsp if (err)
312 474b4f94 2017-11-30 stsp break;
313 474b4f94 2017-11-30 stsp }
314 474b4f94 2017-11-30 stsp
315 474b4f94 2017-11-30 stsp if (te1)
316 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
317 474b4f94 2017-11-30 stsp if (te2)
318 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
319 474b4f94 2017-11-30 stsp } while (te1 || te2);
320 474b4f94 2017-11-30 stsp
321 474b4f94 2017-11-30 stsp return err;
322 474b4f94 2017-11-30 stsp }