-- SPDX-FileCopyrightText: © 2022—2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. --[[ Throwaway git fixture for lilgit tests, built with the real git binary. `make()` creates a root directory holding two repositories: root/fix worktree repo (loose objects/refs), registry name "fix" root/bare.git gc'd bare clone (packed objects + packed-refs with peeled tag lines), registry name "bare" Contents: a README with a relative image link, a binary image, Lua source, a markdown subdirectory doc, a plain unknown-extension file, a symlink, a slashed branch `feature/foo`, and an annotated tag `v1.0`. ]] local std = require("std") -- os.execute returns a boolean with LUA52COMPAT, a number otherwise local sh = function(cmd) local r = os.execute(cmd .. " >/dev/null 2>&1") return r == true or r == 0 end local sh_out = function(cmd) local f = io.popen(cmd) if not f then return nil end local out = f:read("*a") f:close() return out and out:match("^%s*(.-)%s*$") end local has_git = function() return sh("git --version") end local PNG_BYTES = "\137PNG\r\n\26\n" .. string.rep("\0\1\2\3", 16) local make = function() local root = "/tmp/lilgit-fix-" .. std.nanoid() local dir = root .. "/fix" local g = "git -C " .. dir .. " -c user.name=test -c user.email=test@test" if not sh("git init -q " .. dir) then return nil end std.fs.write_file(dir .. "/README.md", "# Fixture Repo\n\nSome intro text.\n\n![logo](assets/logo.png)\n") std.fs.mkdir(dir .. "/assets") std.fs.write_file(dir .. "/assets/logo.png", PNG_BYTES) std.fs.mkdir(dir .. "/src") std.fs.write_file(dir .. "/src/main.lua", 'print("hello from fixture")\n') std.fs.mkdir(dir .. "/docs") std.fs.write_file(dir .. "/docs/guide.md", "# Guide\n\nBack to the [readme](../README.md).\n") std.fs.write_file(dir .. "/plain.xyz", "just some text in an unknown extension\n") sh("ln -s README.md " .. dir .. "/link.md") sh(g .. " add -A") if not sh(g .. " commit -q -m first") then return nil end local branch = sh_out("git -C " .. dir .. " symbolic-ref --short HEAD") local head = sh_out("git -C " .. dir .. " rev-parse HEAD") sh(g .. " checkout -q -b feature/foo") std.fs.write_file(dir .. "/feature.txt", "feature branch file\n") sh(g .. " add -A") sh(g .. " commit -q -m feature") local feature = sh_out("git -C " .. dir .. " rev-parse HEAD") sh(g .. " checkout -q " .. branch) sh(g .. " tag -a v1.0 -m release") local tag = sh_out("git -C " .. dir .. " rev-parse v1.0") local tag_commit = sh_out("git -C " .. dir .. " rev-parse v1.0^{}") local readme_blob = sh_out("git -C " .. dir .. " rev-parse " .. branch .. ":README.md") sh(g .. " gc -q") if not sh("git clone -q --bare " .. dir .. " " .. root .. "/bare.git") then return nil end sh("git -C " .. root .. "/bare.git gc -q") sh("git -C " .. root .. "/bare.git pack-refs --all") if not head or #head ~= 40 or not branch then return nil end return { root = root, dir = dir, git_dir = dir .. "/.git", bare_dir = root .. "/bare.git", branch = branch, head = head, feature = feature, tag = tag, tag_commit = tag_commit, readme_blob = readme_blob, } end local destroy = function(fx) if fx and fx.root and fx.root:match("^/tmp/lilgit%-fix%-") then sh("rm -rf " .. fx.root) end end return { has_git = has_git, make = make, destroy = destroy, sh = sh, sh_out = sh_out, }