const std = @import("std"); const testing = std.testing; const Allocator = std.mem.Allocator; const galloc = @import("galloc.zig"); const git = @cImport({ @cInclude("git2.h"); }); const GitError = error{ InitError, DeinitError, OpenError, }; const git_allocator = extern struct { gmalloc: ?*const fn (size: usize, payload: ?*anyopaque) callconv(.C) ?*anyopaque, grealloc: ?*const fn (ptr: ?*anyopaque, size: usize, payload: ?*anyopaque) callconv(.C) ?*anyopaque, gfree: ?*const fn (ptr: ?*anyopaque) callconv(.C) void, }; pub var alloc: galloc.GitAllocator = undefined; fn malloc(size: usize, _: ?*anyopaque) callconv(.C) ?*anyopaque { std.debug.print("MALLOC {}\n", .{size}); return alloc.malloc(size); } fn relloc(ptr: ?*anyopaque, size: usize, _: ?*anyopaque) callconv(.C) ?*anyopaque { std.debug.print("REALLOC {} {?}\n", .{ size, ptr }); const new_ptr = alloc.realloc(ptr, size); return new_ptr; } fn free(ptr: ?*anyopaque) callconv(.C) void { std.debug.print("FREE\n", .{}); alloc.free(ptr); } pub fn initGit(a: std.mem.Allocator) GitError!void { alloc = galloc.GitAllocator.init(a); const cAlloc = git_allocator{ .gmalloc = malloc, .grealloc = relloc, .gfree = free, }; _ = git.git_libgit2_opts(git.GIT_OPT_SET_ALLOCATOR, &cAlloc); const code = git.git_libgit2_init(); if (code < 0) { return GitError.InitError; } } pub fn deInitGit() !void { defer alloc.deinit(); const code = git.git_libgit2_shutdown(); if (code < 0) { return GitError.DeinitError; } } pub const Repository = struct { allocator: Allocator = undefined, repository: ?*git.git_repository = null, pub fn init(a: Allocator) !Repository { return Repository{ .allocator = a, }; } pub fn open(self: *Repository, path: []const u8) GitError!void { const code = git.git_repository_open(@ptrCast(&self.repository), path.ptr); if (code < 0) { return GitError.OpenError; } } pub fn deinit(self: *Repository) void { if (self.repository) |repo| { git.git_repository_free(repo); } } }; test "init deinit" { try initGit(testing.allocator); try deInitGit(); } test "open repository" { try initGit(testing.allocator); var repository = try Repository.init(std.testing.allocator); try repository.open("."); repository.deinit(); try deInitGit(); }