Skip to content

Commit aa3bcc0

Browse files
committed
fix: remove ffi.gc entirely
1 parent 337bb52 commit aa3bcc0

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ lib.git_libgit2_init()
114114
---@field indexWrite fun()
115115
---@field indexWriteTree fun(): string Tree SHA
116116
---@field fetch fun(remote: string)
117-
117+
---@field free fun()
118118
local sigLayout = ffi.typeof("struct { char *name; char *email; int64_t when_time; int when_offset; } *")
119119

120120
---@param code integer
@@ -169,7 +169,7 @@ local function openRepo(path, bare)
169169
check(lib.git_repository_open(rp, path))
170170
end
171171
---@type git2.ffi.Repository
172-
local repo = ffi.gc(rp[0], lib.git_repository_free)
172+
local repo = rp[0]
173173

174174
---@type git2.Repo
175175
local M = {}
@@ -256,6 +256,8 @@ local function openRepo(path, bare)
256256
lib.git_remote_free(rmt[0])
257257
end
258258

259+
function M.free() lib.git_repository_free(repo) end
260+
259261
return M
260262
end
261263

tests/git2.test.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,29 @@ test.it("init creates a non-bare repo", function()
3333
test.truthy(repo.path():find(".git"))
3434
test.equal(repo.isBare(), false)
3535
test.equal(repo.headUnborn(), true)
36+
repo.free()
3637
end)
3738

3839
test.it("init bare creates a bare repo", function()
3940
local repo = git2.init(mkTmp("bare"), true)
4041
test.equal(repo.isBare(), true)
42+
repo.free()
4143
end)
4244

4345
test.it("open existing repo works", function()
4446
local src = debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])")
4547
local repo = git2.open(src .. "..")
4648
test.truthy(repo.path())
4749
test.equal(repo.isBare(), false)
50+
repo.free()
4851
end)
4952

5053
test.it("head returns a 40-char sha", function()
5154
local dir = mkTmp("head")
5255
mkCommit(dir, "init")
5356
local repo = git2.open(dir)
5457
test.equal(#repo.head(), 40)
58+
repo.free()
5559
end)
5660

5761
test.it("commitLookup returns correct metadata", function()
@@ -65,11 +69,13 @@ test.it("commitLookup returns correct metadata", function()
6569
test.equal(c.author.name, "T")
6670
test.equal(c.author.email, "t@t.com")
6771
test.truthy(c.time > 0)
72+
repo.free()
6873
end)
6974

7075
test.it("revparse HEAD matches head()", function()
7176
local dir = mkTmp("rev")
7277
mkCommit(dir, "rev")
7378
local repo = git2.open(dir)
7479
test.equal(repo.revparse("HEAD"), repo.head())
80+
repo.free()
7581
end)

0 commit comments

Comments
 (0)