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);