const std = @import("std"); const testing = std.testing; const Allocator = std.mem.Allocator; const git = @cImport({ @cInclude("git2.h"); }); const GitError = error{ InitError, DeinitError, OpenError, }; pub fn initGit() GitError!void { const code = git.git_libgit2_init(); if (code < 0) { return GitError.InitError; } } pub fn deInitGit() !void { 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(alloc: Allocator) !Repository { return Repository{ .allocator = alloc, }; } pub fn open(self: Repository, path: []const u8) GitError!void { const code = git.git_repository_open(@constCast(&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(); try deInitGit(); } test "open repository" { try initGit(); var repository = try Repository.init(std.testing.allocator); try repository.open("."); try deInitGit(); }