Skip to content

Commit 1a5ed46

Browse files
committed
fix sha test
Needed to init the library before calling hash functions.
1 parent 1476225 commit 1a5ed46

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

tests/sha.zig

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const testing = std.testing;
33

44
const c = @cImport({
5+
@cInclude("git2.h");
56
@cInclude("hash.h");
67
});
78

@@ -25,23 +26,29 @@ fn calcHashFile(
2526
};
2627
const actual = try allocator.alloc(u8, size);
2728

28-
var r: c_int = undefined;
29+
errdefer {
30+
std.log.err("{s}", .{c.git_error_last().*.message});
31+
}
32+
33+
if (c.git_libgit2_init() < 0) return error.Unexpected;
34+
defer _ = c.git_libgit2_shutdown();
35+
2936
var ctx: c.git_hash_ctx = undefined;
30-
r = c.git_hash_ctx_init(&ctx, algorithm);
31-
if (r != 0) return error.Unexpected;
37+
if (c.git_hash_ctx_init(&ctx, algorithm) != 0)
38+
return error.Unexpected;
3239
defer c.git_hash_ctx_cleanup(&ctx);
3340

3441
const reader = file.reader();
3542
while (true) {
3643
var buf: [2048]u8 = undefined;
3744
const len = try reader.read(&buf);
3845
if (len == 0) break;
39-
r = c.git_hash_update(&ctx, &buf, len);
40-
if (r != 0) return error.Unexpected;
46+
if (c.git_hash_update(&ctx, &buf, len) != 0)
47+
return error.Unexpected;
4148
}
4249

43-
r = c.git_hash_final(actual.ptr, &ctx);
44-
if (r != 0) return error.Unexpected;
50+
if (c.git_hash_final(actual.ptr, &ctx) != 0)
51+
return error.Unexpected;
4552

4653
return actual;
4754
}

0 commit comments

Comments
 (0)