test tree diff (not yet working correctly)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
diff --git a/lib/diff.c b/lib/diff.c
index 8fed0ef..93179e1 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -65,6 +65,7 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
FILE *f1 = NULL, *f2 = NULL;
char hex1[SHA1_DIGEST_STRING_LENGTH];
char hex2[SHA1_DIGEST_STRING_LENGTH];
+ char *idstr1 = NULL, *idstr2 = NULL;
size_t len, hdrlen;
int res, flags = 0;
@@ -84,9 +85,8 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
} else
flags |= D_EMPTY2;
- if (blob1 == NULL) {
- f1 = NULL;
- } else {
+ if (blob1) {
+ idstr1 = got_object_id_str(&blob1->id, hex1, sizeof(hex1));
hdrlen = blob1->hdrlen;
do {
err = got_object_blob_read_block(blob1, &len);
@@ -96,44 +96,51 @@ got_diff_blob(struct got_blob_object *blob1, struct got_blob_object *blob2,
fwrite(blob1->zb.outbuf + hdrlen, len - hdrlen, 1, f1);
hdrlen = 0;
} while (len != 0);
- }
+ } else
+ idstr1 = "/dev/null";
- hdrlen = blob2->hdrlen;
- do {
- err = got_object_blob_read_block(blob2, &len);
- if (err)
- goto done;
- /* Skip blob object header first time around. */
- fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
- hdrlen = 0;
- } while (len != 0);
-
- fflush(f1);
- fflush(f2);
- /* rewind(f1); */
- /* rewind(f2);*/
+ if (blob2) {
+ idstr2 = got_object_id_str(&blob2->id, hex2, sizeof(hex2));
+ hdrlen = blob2->hdrlen;
+ do {
+ err = got_object_blob_read_block(blob2, &len);
+ if (err)
+ goto done;
+ /* Skip blob object header first time around. */
+ fwrite(blob2->zb.outbuf + hdrlen, len - hdrlen, 1, f2);
+ hdrlen = 0;
+ } while (len != 0);
+ } else
+ idstr2 = "/dev/null";
+
+ if (f1)
+ fflush(f1);
+ if (f2)
+ fflush(f2);
memset(&ds, 0, sizeof(ds));
/* XXX should stat buffers be passed in args instead of ds? */
ds.stb1.st_mode = S_IFREG;
- ds.stb1.st_size = blob1->zb.z.total_out;
+ if (blob1)
+ ds.stb1.st_size = blob1->zb.z.total_out;
ds.stb1.st_mtime = 0; /* XXX */
ds.stb2.st_mode = S_IFREG;
- ds.stb2.st_size = blob2->zb.z.total_out;
+ if (blob2)
+ ds.stb2.st_size = blob2->zb.z.total_out;
ds.stb2.st_mtime = 0; /* XXX */
memset(&args, 0, sizeof(args));
args.diff_format = D_UNIFIED;
- args.label[0] = label1 ?
- label1 : got_object_id_str(&blob1->id, hex1, sizeof(hex1));
- args.label[1] = label2 ?
- label2 : got_object_id_str(&blob2->id, hex2, sizeof(hex2));
+ args.label[0] = label1 ? label1 : idstr1;
+ args.label[1] = label2 ? label2 : idstr2;
err = got_diffreg(&res, f1, f2, flags, &args, &ds, outfile);
done:
- fclose(f1);
- fclose(f2);
+ if (f1)
+ fclose(f1);
+ if (f2)
+ fclose(f2);
return err;
}
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index c589fb4..134e586 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -289,6 +289,63 @@ repo_diff_blob(const char *repo_path)
return (err == NULL);
}
+static int
+repo_diff_tree(const char *repo_path)
+{
+ const char *blob1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
+ const char *blob2_sha1 = "cb4ba67a335b2b7ecac88867063596bd9e1ab485";
+ const struct got_error *err;
+ struct got_repository *repo;
+ struct got_object_id id1;
+ struct got_object_id id2;
+ struct got_object *obj1;
+ struct got_object *obj2;
+ struct got_tree_object *tree1;
+ struct got_tree_object *tree2;
+ char hex[SHA1_DIGEST_STRING_LENGTH];
+ int i;
+ size_t len;
+
+ if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
+ return 0;
+ if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
+ return 0;
+
+ err = got_repo_open(&repo, repo_path);
+ if (err != NULL || repo == NULL)
+ return 0;
+
+ err = got_object_open(&obj1, repo, &id1);
+ if (err != NULL || obj1 == NULL)
+ return 0;
+ if (obj1->type != GOT_OBJ_TYPE_TREE)
+ return 0;
+ err = got_object_open(&obj2, repo, &id2);
+ if (err != NULL || obj2 == NULL)
+ return 0;
+ if (obj2->type != GOT_OBJ_TYPE_TREE)
+ return 0;
+
+ err = got_object_tree_open(&tree1, repo, obj1);
+ if (err != NULL)
+ return 0;
+
+ err = got_object_tree_open(&tree2, repo, obj2);
+ if (err != NULL)
+ return 0;
+
+ putchar('\n');
+ got_diff_tree(tree1, tree2, repo);
+ putchar('\n');
+
+ got_object_tree_close(tree1);
+ got_object_tree_close(tree2);
+ got_object_close(obj1);
+ got_object_close(obj2);
+ got_repo_close(repo);
+ return (err == NULL);
+}
+
int
main(int argc, const char *argv[])
{
@@ -307,6 +364,7 @@ main(int argc, const char *argv[])
RUN_TEST(repo_read_log(repo_path), "read_log");
RUN_TEST(repo_read_blob(repo_path), "read_blob");
RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
+ RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
return failure ? 1 : 0;
}