package rosa

import (
	"path"
	"strings"

	"hakurei.app/internal/pkg"
)

var (
	_git       = H("git")
	_nssCACert = H("nss-cacert")
)

// NewViaGit returns a [pkg.Artifact] for cloning a git repository.
func (t Toolchain) NewViaGit(
	url, rev string,
	checksum pkg.Checksum,
) pkg.Artifact {
	return t.New(strings.TrimSuffix(
		path.Base(url),
		".git",
	)+"-src", path.Base(rev), nil, &PackageAttr{
		KnownChecksum: &checksum,

		HostNet: true,
		Paths:   []pkg.ExecPath{resolvconf()},
	}, &GenericHelper{
		Build: `
git \
	-c advice.detachedHead=false \
	clone \
	--depth=1 \
	--revision=` + rev + ` \
	--shallow-submodules \
	--recurse-submodules \
	` + url + ` \
	/work
rm -rf /work/.git
`,
	},
		_nssCACert,
		_git,
	)
}
