aboutsummaryrefslogtreecommitdiff
path: root/src/git.zig
blob: 4803103d1f1ac58c2fe9656745f3dab642869878 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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{
    RepositoryAlreadyInitialized,
    Unkown,

    //libgit specific  error
    GitError,
    GitNotfound,
    GitExists,
    GitAmbiguous,
    GitBufs,
    GitUser,
    GitUnbornBranch,
    GitUnmerged,
    GitNonFastForward,
    GitInvalidSpec,
    GitConflict,
    GitLocked,
    GitModified,
    GitAuth,
    GitCertificate,
    GitApplied,
    GitPeel,
    GitEof,
    GitInvalid,
    GitUncommitted,
    GitDirectory,
    GitMergeConflict,
    GitPassthrough,
    GitIterOver,
    GitRetry,
    GitMismatch,
    GitIndexDirty,
    GitApplyFail,
    GitOwner,
    GitTimeout,
    GitUnchanged,
    GitNotSupported,
    GitReadonly,
};

var alloc: galloc.GitAllocator = undefined;

fn malloc(size: usize, _: ?*anyopaque) callconv(.C) ?*anyopaque {
    return alloc.malloc(size);
}

fn relloc(ptr: ?*anyopaque, size: usize, _: ?*anyopaque) callconv(.C) ?*anyopaque {
    const new_ptr = alloc.realloc(ptr, size);
    return new_ptr;
}

fn free(ptr: ?*anyopaque) callconv(.C) void {
    alloc.free(ptr);
}

fn err(code: c_int) GitError!void {
    if (code >= 0) return;

    return switch (code) {
        git.GIT_ERROR => GitError.GitError,
        git.GIT_ENOTFOUND => GitError.GitNotfound,
        git.GIT_EEXISTS => GitError.GitExists,
        git.GIT_EAMBIGUOUS => GitError.GitAmbiguous,
        git.GIT_EBUFS => GitError.GitBufs,
        git.GIT_EUSER => GitError.GitUser,
        git.GIT_EUNBORNBRANCH => GitError.GitUnbornBranch,
        git.GIT_EUNMERGED => GitError.GitUnmerged,
        git.GIT_ENONFASTFORWARD => GitError.GitNonFastForward,
        git.GIT_EINVALIDSPEC => GitError.GitInvalidSpec,
        git.GIT_ECONFLICT => GitError.GitConflict,
        git.GIT_ELOCKED => GitError.GitLocked,
        git.GIT_EMODIFIED => GitError.GitModified,
        git.GIT_EAUTH => GitError.GitAuth,
        git.GIT_ECERTIFICATE => GitError.GitCertificate,
        git.GIT_EAPPLIED => GitError.GitApplied,
        git.GIT_EPEEL => GitError.GitPeel,
        git.GIT_EEOF => GitError.GitEof,
        git.GIT_EINVALID => GitError.GitInvalid,
        git.GIT_EUNCOMMITTED => GitError.GitUncommitted,
        git.GIT_EDIRECTORY => GitError.GitDirectory,
        git.GIT_EMERGECONFLICT => GitError.GitMergeConflict,
        git.GIT_PASSTHROUGH => GitError.GitPassthrough,
        git.GIT_ITEROVER => GitError.GitIterOver,
        git.GIT_RETRY => GitError.GitRetry,
        git.GIT_EMISMATCH => GitError.GitMismatch,
        git.GIT_EINDEXDIRTY => GitError.GitIndexDirty,
        git.GIT_EAPPLYFAIL => GitError.GitApplyFail,
        git.GIT_EOWNER => GitError.GitOwner,
        git.GIT_TIMEOUT => GitError.GitTimeout,
        git.GIT_EUNCHANGED => GitError.GitUnchanged,
        git.GIT_ENOTSUPPORTED => GitError.GitNotSupported,
        git.GIT_EREADONLY => GitError.GitReadonly,
        else => GitError.Unkown,
    };
}

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 fn init(a: std.mem.Allocator) GitError!void {
    alloc = galloc.GitAllocator.init(a);

    const cAlloc = git_allocator{
        .gmalloc = malloc,
        .grealloc = relloc,
        .gfree = free,
    };

    var code = git.git_libgit2_opts(git.GIT_OPT_SET_ALLOCATOR, &cAlloc);
    try err(code);

    code = git.git_libgit2_init();
    try err(code);
}

pub fn deinit() !void {
    defer alloc.deinit();
    try err(git.git_libgit2_shutdown());
}

pub const Repository = struct {
    repository: ?*git.git_repository = null,

    fn validateInit(self: *Repository) GitError!void {
        if (self.repository != null)
            return GitError.RepositoryAlreadyInitialized;
    }

    pub fn open(self: *Repository, path: []const u8) GitError!void {
        try self.validateInit();
        try err(git.git_repository_open(@ptrCast(&self.repository), path.ptr));
    }

    pub fn init(self: *Repository, path: []const u8, bare: bool) GitError!void {
        try self.validateInit();
        try err(git.git_repository_init(@ptrCast(&self.repository), path.ptr, if (bare) 1 else 0));
    }

    pub fn deinit(self: *Repository) void {
        if (self.repository) |repo| {
            git.git_repository_free(repo);
        }
    }
};

test "init deinit" {
    try init(testing.allocator);
    try deinit();
}

test "open repository" {
    try init(testing.allocator);
    defer deinit() catch {};

    var repository = Repository{};
    defer repository.deinit();

    try repository.open(".");
}

test "init repository" {
    var tmp_dir = testing.tmpDir(.{});
    defer tmp_dir.cleanup();

    const full_path = try tmp_dir.dir.realpathAlloc(testing.allocator, ".");
    defer testing.allocator.free(full_path);

    try init(testing.allocator);
    defer deinit() catch {};

    var repository = Repository{};
    defer repository.deinit();

    try repository.init(full_path, false);
}

test "init repository bare" {
    var tmp_dir = testing.tmpDir(.{});
    defer tmp_dir.cleanup();

    const full_path = try tmp_dir.dir.realpathAlloc(testing.allocator, ".");
    defer testing.allocator.free(full_path);

    try init(testing.allocator);
    defer deinit() catch {};

    var repository = Repository{};
    defer repository.deinit();

    try repository.init(full_path, false);

    // try opening the repository to test if it is properly created.
    var tmp = Repository{};
    defer tmp.deinit();

    try tmp.open(full_path);
}