Skip to content

Commit f13b0c9

Browse files
committed
fix: do not resolve to nondeterministic cache entry
Also fix the test by actually resolving the latest commit and ensuring output is equal
1 parent f439bdb commit f13b0c9

2 files changed

Lines changed: 10 additions & 28 deletions

File tree

packages/lde-core/src/global/init.lua

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,27 +383,13 @@ function global.repoNameFromUrl(url)
383383
return url:match("([^/]+)%.git$") or url:match("([^/]+)$")
384384
end
385385

386-
--- Clones or retrieves a cached git repo directory. Always resolves to a specific commit.
387-
--- Checks existing cache entries first to avoid network where possible.
386+
--- Clones or retrieves a cached git repo directory. Always resolves to the latest commit.
388387
---@param repoName string
389388
---@param cloneUrl string
390389
---@param branch string?
391390
---@return string repoDir
392391
---@return string commit
393392
function global.getOrCloneRepo(repoName, cloneUrl, branch)
394-
-- Check for any existing cache entry for this repo name
395-
local prefix = sanitize(repoName) .. "-"
396-
local cacheDir = global.getGitCacheDir()
397-
if fs.isdir(cacheDir) then
398-
for entry in fs.readdir(cacheDir) do
399-
if entry.type == "dir" and entry.name:sub(1, #prefix) == prefix then
400-
local commit = entry.name:sub(#prefix + 1)
401-
return path.join(cacheDir, entry.name), commit
402-
end
403-
end
404-
end
405-
406-
-- No cache hit, resolve and download
407393
local ref = branch and ("refs/heads/" .. branch) or "HEAD"
408394
local commit, err = git2.lsRemote(cloneUrl, ref)
409395
if not commit then

packages/lde/tests/main.test.lua

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,20 @@ local fs = require("fs")
44
local env = require("env")
55
local path = require("path")
66
local json = require("json")
7+
local git2 = require("git2-sys")
78

89
local lde = require("lde-core")
910

1011
local ldecli = require("tests.lib.ldecli")
1112

1213
test.it("should not ignore --git in ldx", function()
13-
-- Pre-populate the git cache so no real download happens.
14-
-- Clean up any existing cache entries for this repo name first.
15-
local cacheDir = lde.global.getGitCacheDir()
16-
if fs.isdir(cacheDir) then
17-
for entry in fs.readdir(cacheDir) do
18-
if entry.name:match("^hood%-") then
19-
fs.rmdir(path.join(cacheDir, entry.name))
20-
end
21-
end
22-
end
23-
24-
local repoDir = lde.global.getGitRepoDir("hood", "abc1234567890abcdef1234567890abcdef123456")
14+
local cloneUrl = "https://github.com/codebycruz/hood"
15+
16+
-- Resolve the real commit so the cache key matches what getOrCloneRepo expects.
17+
local commit = assert(git2.lsRemote(cloneUrl, "HEAD"))
18+
19+
-- Pre-populate the cache with a fake repo that lacks a "triangle" package.
20+
local repoDir = lde.global.getGitRepoDir("hood", commit)
2521
fs.rmdir(repoDir)
2622
fs.mkdir(repoDir)
2723
fs.write(path.join(repoDir, "lde.json"), json.encode({
@@ -32,7 +28,7 @@ test.it("should not ignore --git in ldx", function()
3228
fs.mkdir(path.join(repoDir, "src"))
3329
fs.write(path.join(repoDir, "src", "init.lua"), "")
3430

35-
local _, out = ldecli { "x", "triangle", "--git", "https://github.com/codebycruz/hood" }
31+
local _, out = ldecli { "x", "triangle", "--git", cloneUrl }
3632
test.falsy(out:find("not found in lde registry"))
3733
test.includes(out, "No package named 'triangle'")
3834

0 commit comments

Comments
 (0)