aboutsummaryrefslogtreecommitdiff
path: root/src/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/root.zig')
-rw-r--r--src/root.zig65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/root.zig b/src/root.zig
new file mode 100644
index 0000000..73222be
--- /dev/null
+++ b/src/root.zig
@@ -0,0 +1,65 @@
+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();
+}