aboutsummaryrefslogtreecommitdiff
path: root/src/root.zig
blob: 73222be120582f762f4c84be8af86f668c059acb (plain)
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
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();
}