2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
26 #include "got_repository.h"
27 #include "got_object.h"
28 #include "got_error.h"
32 static const struct got_error *
33 open_tempfile(FILE **sfp, char **sfn)
35 static const int sfnlen = 20;
38 *sfn = calloc(sfnlen, sizeof(char));
40 return got_error(GOT_ERR_NO_MEM);
41 strlcpy(*sfn, "/tmp/got.XXXXXXXXXX", sfnlen);
42 if ((fd = mkstemp(*sfn)) == -1 ||
43 ((*sfp) = fdopen(fd, "w+")) == NULL) {
49 return got_error(GOT_ERR_FILE_OPEN);
54 const struct got_error *
55 got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
56 const char *label1, const char *label2, FILE *outfile)
58 struct got_diff_state ds;
59 struct got_diff_args args;
60 const struct got_error *err = NULL;
64 char hex1[SHA1_DIGEST_STRING_LENGTH];
65 char hex2[SHA1_DIGEST_STRING_LENGTH];
68 err = open_tempfile(&f1, &n1);
72 err = open_tempfile(&f2, &n2);
80 hdrlen = blob1->hdrlen;
82 err = got_object_blob_read_block(blob1, &len);
85 /* Skip blob object header first time around. */
86 fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
90 hdrlen = blob2->hdrlen;
92 err = got_object_blob_read_block(blob2, &len);
95 /* Skip blob object header first time around. */
96 fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
103 memset(&ds, 0, sizeof(ds));
104 memset(&args, 0, sizeof(args));
106 args.diff_format = D_UNIFIED;
107 args.label[0] = label1 ?
108 label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
109 args.label[1] = label2 ?
110 label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
112 err = got_diffreg(&res, n1, n2, 0, &args, &ds);
123 static const struct got_error *
124 match_entry_by_name(struct got_tree_entry **te, struct got_tree_entry *te1,
125 struct got_tree_object *tree2)
132 same_id(struct got_object_id *id1, struct got_object_id *id2)
134 return (memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH) == 0);
137 static const struct got_error *
138 diff_added_blob(struct got_object_id *id)
143 static const struct got_error *
144 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2)
149 static const struct got_error *
150 diff_deleted_blob(struct got_object_id *id)
155 static const struct got_error *
156 diff_added_tree(struct got_object_id *id)
161 static const struct got_error *
162 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2)
167 static const struct got_error *
168 diff_deleted_tree(struct got_object_id *id)
173 static const struct got_error *
174 diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2)
179 static const struct got_error *
180 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2)
182 const struct got_error *err;
183 struct got_tree_entry *te2;
185 err = match_entry_by_name(&te2, te1, tree2);
189 if (S_ISDIR(te1->mode))
190 return diff_deleted_tree(&te1->id);
191 return diff_deleted_blob(&te1->id);
194 if (S_ISDIR(te1->mode) == S_ISDIR(te2->mode)) {
195 if (!same_id(&te1->id, &te2->id))
196 return diff_modified_tree(&te1->id, &te2->id);
197 } else if (S_ISREG(te1->mode) == S_ISREG(te2->mode)) {
198 if (!same_id(&te1->id, &te2->id))
199 return diff_modified_blob(&te1->id, &te2->id);
201 return diff_kind_mismatch(&te1->id, &te2->id);
206 static const struct got_error *
207 diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1)
209 const struct got_error *err;
210 struct got_tree_entry *te1;
212 err = match_entry_by_name(&te1, te2, tree1);
215 if (te1 != NULL) /* handled by diff_entry_old_new() */
218 if (S_ISDIR(te2->mode))
219 return diff_added_tree(&te2->id);
220 return diff_added_blob(&te2->id);
223 const struct got_error *
224 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
225 struct got_repository *repo)
227 const struct got_error *err = NULL;
228 struct got_tree_entry *te1;
229 struct got_tree_entry *te2;
231 if (tree1->nentries == 0 && tree2->nentries == 0)
234 te1 = SIMPLEQ_FIRST(&tree1->entries);
235 te2 = SIMPLEQ_FIRST(&tree2->entries);
239 err = diff_entry_old_new(te1, tree2);
245 err = diff_entry_new_old(te2, tree1);
251 te1 = SIMPLEQ_NEXT(te1, entry);
253 te2 = SIMPLEQ_NEXT(te2, entry);
254 } while (te1 || te2);