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 7d283eee 2017-11-29 stsp #include <sha1.h>
24 7d283eee 2017-11-29 stsp #include <zlib.h>
25 7d283eee 2017-11-29 stsp
26 7d283eee 2017-11-29 stsp #include "got_repository.h"
27 7d283eee 2017-11-29 stsp #include "got_object.h"
28 7d283eee 2017-11-29 stsp #include "got_error.h"
29 7d283eee 2017-11-29 stsp
30 7d283eee 2017-11-29 stsp #include "diff.h"
31 7d283eee 2017-11-29 stsp
32 7d283eee 2017-11-29 stsp static const struct got_error *
33 7d283eee 2017-11-29 stsp open_tempfile(FILE **sfp, char **sfn)
34 7d283eee 2017-11-29 stsp {
35 7d283eee 2017-11-29 stsp static const int sfnlen = 20;
36 7d283eee 2017-11-29 stsp int fd;
37 7d283eee 2017-11-29 stsp
38 ed9e98a8 2017-11-29 stsp *sfn = calloc(sfnlen, sizeof(char));
39 7d283eee 2017-11-29 stsp if (*sfn == NULL)
40 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_MEM);
41 7d283eee 2017-11-29 stsp strlcpy(*sfn, "/tmp/got.XXXXXXXXXX", sfnlen);
42 7d283eee 2017-11-29 stsp if ((fd = mkstemp(*sfn)) == -1 ||
43 7d283eee 2017-11-29 stsp ((*sfp) = fdopen(fd, "w+")) == NULL) {
44 7d283eee 2017-11-29 stsp if (fd != -1) {
45 7d283eee 2017-11-29 stsp unlink(*sfn);
46 7d283eee 2017-11-29 stsp close(fd);
47 7d283eee 2017-11-29 stsp }
48 7d283eee 2017-11-29 stsp free(*sfn);
49 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_FILE_OPEN);
50 7d283eee 2017-11-29 stsp }
51 7d283eee 2017-11-29 stsp return NULL;
52 7d283eee 2017-11-29 stsp }
53 7d283eee 2017-11-29 stsp
54 7d283eee 2017-11-29 stsp const struct got_error *
55 7d283eee 2017-11-29 stsp got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
56 474b4f94 2017-11-30 stsp const char *label1, const char *label2, FILE *outfile)
57 7d283eee 2017-11-29 stsp {
58 ed9e98a8 2017-11-29 stsp struct got_diff_state ds;
59 8ba9a219 2017-11-29 stsp struct got_diff_args args;
60 7d283eee 2017-11-29 stsp const struct got_error *err = NULL;
61 7d283eee 2017-11-29 stsp FILE *f1, *f2;
62 7d283eee 2017-11-29 stsp char *n1, *n2;
63 7d283eee 2017-11-29 stsp size_t len, hdrlen;
64 f78b0693 2017-11-29 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
65 f78b0693 2017-11-29 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
66 7d283eee 2017-11-29 stsp int res;
67 7d283eee 2017-11-29 stsp
68 7d283eee 2017-11-29 stsp err = open_tempfile(&f1, &n1);
69 7d283eee 2017-11-29 stsp if (err != NULL)
70 7d283eee 2017-11-29 stsp return err;
71 7d283eee 2017-11-29 stsp
72 7d283eee 2017-11-29 stsp err = open_tempfile(&f2, &n2);
73 7d283eee 2017-11-29 stsp if (err != NULL) {
74 7d283eee 2017-11-29 stsp fclose(f1);
75 7d283eee 2017-11-29 stsp free(n1);
76 7d283eee 2017-11-29 stsp return err;
77 7d283eee 2017-11-29 stsp }
78 7d283eee 2017-11-29 stsp
79 7d283eee 2017-11-29 stsp
80 7d283eee 2017-11-29 stsp hdrlen = blob1->hdrlen;
81 7d283eee 2017-11-29 stsp do {
82 7d283eee 2017-11-29 stsp err = got_object_blob_read_block(blob1, &len);
83 7d283eee 2017-11-29 stsp if (err)
84 7d283eee 2017-11-29 stsp goto done;
85 7d283eee 2017-11-29 stsp /* Skip blob object header first time around. */
86 7d283eee 2017-11-29 stsp fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
87 7d283eee 2017-11-29 stsp hdrlen = 0;
88 7d283eee 2017-11-29 stsp } while (len != 0);
89 7d283eee 2017-11-29 stsp
90 7d283eee 2017-11-29 stsp hdrlen = blob2->hdrlen;
91 7d283eee 2017-11-29 stsp do {
92 7d283eee 2017-11-29 stsp err = got_object_blob_read_block(blob2, &len);
93 7d283eee 2017-11-29 stsp if (err)
94 7d283eee 2017-11-29 stsp goto done;
95 7d283eee 2017-11-29 stsp /* Skip blob object header first time around. */
96 7d283eee 2017-11-29 stsp fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
97 7d283eee 2017-11-29 stsp hdrlen = 0;
98 7d283eee 2017-11-29 stsp } while (len != 0);
99 7d283eee 2017-11-29 stsp
100 7d283eee 2017-11-29 stsp fflush(f1);
101 7d283eee 2017-11-29 stsp fflush(f2);
102 7d283eee 2017-11-29 stsp
103 ed9e98a8 2017-11-29 stsp memset(&ds, 0, sizeof(ds));
104 8ba9a219 2017-11-29 stsp memset(&args, 0, sizeof(args));
105 8ba9a219 2017-11-29 stsp
106 8ba9a219 2017-11-29 stsp args.diff_format = D_UNIFIED;
107 62136d3a 2017-11-29 stsp args.label[0] = label1 ?
108 62136d3a 2017-11-29 stsp label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
109 62136d3a 2017-11-29 stsp args.label[1] = label2 ?
110 62136d3a 2017-11-29 stsp label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
111 62136d3a 2017-11-29 stsp
112 8ba9a219 2017-11-29 stsp err = got_diffreg(&res, n1, n2, 0, &args, &ds);
113 7d283eee 2017-11-29 stsp done:
114 7d283eee 2017-11-29 stsp unlink(n1);
115 7d283eee 2017-11-29 stsp unlink(n2);
116 7d283eee 2017-11-29 stsp fclose(f1);
117 7d283eee 2017-11-29 stsp fclose(f2);
118 7d283eee 2017-11-29 stsp free(n1);
119 7d283eee 2017-11-29 stsp free(n2);
120 7d283eee 2017-11-29 stsp return err;
121 7d283eee 2017-11-29 stsp }
122 474b4f94 2017-11-30 stsp
123 474b4f94 2017-11-30 stsp static const struct got_error *
124 474b4f94 2017-11-30 stsp match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
125 474b4f94 2017-11-30 stsp struct got_tree_object *tree2)
126 474b4f94 2017-11-30 stsp {
127 474b4f94 2017-11-30 stsp *te = NULL;
128 474b4f94 2017-11-30 stsp return NULL;
129 474b4f94 2017-11-30 stsp }
130 474b4f94 2017-11-30 stsp
131 474b4f94 2017-11-30 stsp static int
132 474b4f94 2017-11-30 stsp same_id(struct got_object_id *id1, struct got_object_id *id2)
133 474b4f94 2017-11-30 stsp {
134 474b4f94 2017-11-30 stsp return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
135 474b4f94 2017-11-30 stsp }
136 474b4f94 2017-11-30 stsp
137 474b4f94 2017-11-30 stsp static const struct got_error *
138 474b4f94 2017-11-30 stsp diff_added_blob(struct got_object_id *id)
139 474b4f94 2017-11-30 stsp {
140 474b4f94 2017-11-30 stsp return NULL;
141 474b4f94 2017-11-30 stsp }
142 474b4f94 2017-11-30 stsp
143 474b4f94 2017-11-30 stsp static const struct got_error *
144 474b4f94 2017-11-30 stsp diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2)
145 474b4f94 2017-11-30 stsp {
146 474b4f94 2017-11-30 stsp return NULL;
147 474b4f94 2017-11-30 stsp }
148 474b4f94 2017-11-30 stsp
149 474b4f94 2017-11-30 stsp static const struct got_error *
150 474b4f94 2017-11-30 stsp diff_deleted_blob(struct got_object_id *id)
151 474b4f94 2017-11-30 stsp {
152 474b4f94 2017-11-30 stsp return NULL;
153 474b4f94 2017-11-30 stsp }
154 474b4f94 2017-11-30 stsp
155 474b4f94 2017-11-30 stsp static const struct got_error *
156 474b4f94 2017-11-30 stsp diff_added_tree(struct got_object_id *id)
157 474b4f94 2017-11-30 stsp {
158 474b4f94 2017-11-30 stsp return NULL;
159 474b4f94 2017-11-30 stsp }
160 474b4f94 2017-11-30 stsp
161 474b4f94 2017-11-30 stsp static const struct got_error *
162 474b4f94 2017-11-30 stsp diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
163 474b4f94 2017-11-30 stsp {
164 474b4f94 2017-11-30 stsp return NULL;
165 474b4f94 2017-11-30 stsp }
166 474b4f94 2017-11-30 stsp
167 474b4f94 2017-11-30 stsp static const struct got_error *
168 474b4f94 2017-11-30 stsp diff_deleted_tree(struct got_object_id *id)
169 474b4f94 2017-11-30 stsp {
170 474b4f94 2017-11-30 stsp return NULL;
171 474b4f94 2017-11-30 stsp }
172 474b4f94 2017-11-30 stsp
173 474b4f94 2017-11-30 stsp static const struct got_error *
174 474b4f94 2017-11-30 stsp diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
175 474b4f94 2017-11-30 stsp {
176 474b4f94 2017-11-30 stsp return NULL;
177 474b4f94 2017-11-30 stsp }
178 474b4f94 2017-11-30 stsp
179 474b4f94 2017-11-30 stsp static const struct got_error *
180 474b4f94 2017-11-30 stsp diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2)
181 474b4f94 2017-11-30 stsp {
182 474b4f94 2017-11-30 stsp const struct got_error *err;
183 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
184 474b4f94 2017-11-30 stsp
185 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te2, te1, tree2);
186 474b4f94 2017-11-30 stsp if (err)
187 474b4f94 2017-11-30 stsp return err;
188 474b4f94 2017-11-30 stsp if (te2 == NULL) {
189 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode))
190 474b4f94 2017-11-30 stsp return diff_deleted_tree(&te1->id);
191 474b4f94 2017-11-30 stsp return diff_deleted_blob(&te1->id);
192 474b4f94 2017-11-30 stsp }
193 474b4f94 2017-11-30 stsp
194 474b4f94 2017-11-30 stsp if (S_ISDIR(te1->mode) == S_ISDIR(te2->mode)) {
195 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
196 474b4f94 2017-11-30 stsp return diff_modified_tree(&te1->id, &te2->id);
197 474b4f94 2017-11-30 stsp } else if (S_ISREG(te1->mode) == S_ISREG(te2->mode)) {
198 474b4f94 2017-11-30 stsp if (!same_id(&te1->id, &te2->id))
199 474b4f94 2017-11-30 stsp return diff_modified_blob(&te1->id, &te2->id);
200 474b4f94 2017-11-30 stsp } else
201 474b4f94 2017-11-30 stsp return diff_kind_mismatch(&te1->id, &te2->id);
202 474b4f94 2017-11-30 stsp
203 474b4f94 2017-11-30 stsp return NULL;
204 474b4f94 2017-11-30 stsp }
205 474b4f94 2017-11-30 stsp
206 474b4f94 2017-11-30 stsp static const struct got_error *
207 474b4f94 2017-11-30 stsp diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1)
208 474b4f94 2017-11-30 stsp {
209 474b4f94 2017-11-30 stsp const struct got_error *err;
210 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
211 474b4f94 2017-11-30 stsp
212 474b4f94 2017-11-30 stsp err = match_entry_by_name(&te1, te2, tree1);
213 474b4f94 2017-11-30 stsp if (err)
214 474b4f94 2017-11-30 stsp return err;
215 474b4f94 2017-11-30 stsp if (te1 != NULL) /* handled by diff_entry_old_new() */
216 474b4f94 2017-11-30 stsp return NULL;
217 474b4f94 2017-11-30 stsp
218 474b4f94 2017-11-30 stsp if (S_ISDIR(te2->mode))
219 474b4f94 2017-11-30 stsp return diff_added_tree(&te2->id);
220 474b4f94 2017-11-30 stsp return diff_added_blob(&te2->id);
221 474b4f94 2017-11-30 stsp }
222 474b4f94 2017-11-30 stsp
223 474b4f94 2017-11-30 stsp const struct got_error *
224 474b4f94 2017-11-30 stsp got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
225 474b4f94 2017-11-30 stsp struct got_repository *repo)
226 474b4f94 2017-11-30 stsp {
227 474b4f94 2017-11-30 stsp const struct got_error *err = NULL;
228 474b4f94 2017-11-30 stsp struct got_tree_entry *te1;
229 474b4f94 2017-11-30 stsp struct got_tree_entry *te2;
230 474b4f94 2017-11-30 stsp
231 474b4f94 2017-11-30 stsp if (tree1->nentries == 0 && tree2->nentries == 0)
232 474b4f94 2017-11-30 stsp return NULL;
233 474b4f94 2017-11-30 stsp
234 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_FIRST(&tree1->entries);
235 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_FIRST(&tree2->entries);
236 474b4f94 2017-11-30 stsp
237 474b4f94 2017-11-30 stsp do {
238 474b4f94 2017-11-30 stsp if (te1) {
239 474b4f94 2017-11-30 stsp err = diff_entry_old_new(te1, tree2);
240 474b4f94 2017-11-30 stsp if (err)
241 474b4f94 2017-11-30 stsp break;
242 474b4f94 2017-11-30 stsp }
243 474b4f94 2017-11-30 stsp
244 474b4f94 2017-11-30 stsp if (te2) {
245 474b4f94 2017-11-30 stsp err = diff_entry_new_old(te2, tree1);
246 474b4f94 2017-11-30 stsp if (err)
247 474b4f94 2017-11-30 stsp break;
248 474b4f94 2017-11-30 stsp }
249 474b4f94 2017-11-30 stsp
250 474b4f94 2017-11-30 stsp if (te1)
251 474b4f94 2017-11-30 stsp te1 = SIMPLEQ_NEXT(te1, entry);
252 474b4f94 2017-11-30 stsp if (te2)
253 474b4f94 2017-11-30 stsp te2 = SIMPLEQ_NEXT(te2, entry);
254 474b4f94 2017-11-30 stsp } while (te1 || te2);
255 474b4f94 2017-11-30 stsp
256 474b4f94 2017-11-30 stsp return err;
257 474b4f94 2017-11-30 stsp }