feat(cli): discover ThothII projects and installations

This commit is contained in:
2026-08-15 22:24:15 +02:00
parent 4fc03574e1
commit 66614fe8cf
6 changed files with 583 additions and 6 deletions
+79
View File
@@ -0,0 +1,79 @@
// Package project finds a ThothII source checkout from a directory within it.
package project
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// Root identifies one ThothII checkout. Path is resolved for stable identity across symlinks.
type Root struct {
Path string
IsWorktree bool
}
// Discover walks from start towards the filesystem root until it finds the ThothII source-layout
// contract. A regular .git file identifies a linked Git worktree; a .git directory identifies a
// main checkout.
func Discover(start string) (Root, error) {
if start == "" {
return Root{}, errors.New("ThothII project root could not be discovered from an empty directory")
}
abs, err := filepath.Abs(start)
if err != nil {
return Root{}, errors.New("ThothII project root could not resolve the current directory")
}
resolved, err := filepath.EvalSymlinks(abs)
if err != nil {
return Root{}, fmt.Errorf("ThothII project root could not resolve %s", filepath.Clean(abs))
}
info, err := os.Stat(resolved)
if err != nil || !info.IsDir() {
return Root{}, fmt.Errorf("ThothII project root could not read %s", filepath.Clean(abs))
}
for directory := filepath.Clean(resolved); ; directory = filepath.Dir(directory) {
if isRoot(directory) {
return Root{Path: directory, IsWorktree: isLinkedWorktree(directory)}, nil
}
parent := filepath.Dir(directory)
if parent == directory {
break
}
}
return Root{}, fmt.Errorf("ThothII project root not found from %s", filepath.Clean(abs))
}
func isRoot(directory string) bool {
if !isGitMarker(filepath.Join(directory, ".git")) || !isRegularFile(filepath.Join(directory, "compose.yaml")) || !isDirectory(filepath.Join(directory, "deploy")) {
return false
}
for _, name := range []string{"backend", "frontend", "harness", "tools"} {
if !isDirectory(filepath.Join(directory, name)) {
return false
}
}
return true
}
func isLinkedWorktree(directory string) bool {
info, err := os.Lstat(filepath.Join(directory, ".git"))
return err == nil && info.Mode().IsRegular()
}
func isGitMarker(path string) bool {
info, err := os.Lstat(path)
return err == nil && (info.IsDir() || info.Mode().IsRegular())
}
func isRegularFile(path string) bool {
info, err := os.Stat(path)
return err == nil && info.Mode().IsRegular()
}
func isDirectory(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
@@ -0,0 +1,100 @@
package project
import (
"os"
"path/filepath"
"testing"
)
func TestDiscoverFindsRepositoryRootFromNestedDirectory(t *testing.T) {
root := writeProjectRoot(t, false)
nested := filepath.Join(root, "tools", "tht", "internal")
if err := os.MkdirAll(nested, 0o755); err != nil {
t.Fatal(err)
}
got, err := Discover(nested)
if err != nil {
t.Fatal(err)
}
want := resolvedPath(t, root)
if got.Path != want {
t.Fatalf("Discover() root = %q, want %q", got.Path, want)
}
if got.IsWorktree {
t.Fatal("Discover() marked a main checkout as a worktree")
}
}
func TestDiscoverRecognizesLinkedGitWorktree(t *testing.T) {
root := writeProjectRoot(t, true)
got, err := Discover(root)
if err != nil {
t.Fatal(err)
}
want := resolvedPath(t, root)
if got.Path != want {
t.Fatalf("Discover() root = %q, want %q", got.Path, want)
}
if !got.IsWorktree {
t.Fatal("Discover() did not mark a linked checkout as a worktree")
}
}
func TestDiscoverResolvesSymlinkForRootIdentity(t *testing.T) {
root := writeProjectRoot(t, false)
alias := filepath.Join(t.TempDir(), "checkout")
if err := os.Symlink(root, alias); err != nil {
t.Fatal(err)
}
got, err := Discover(filepath.Join(alias, "frontend"))
if err != nil {
t.Fatal(err)
}
want := resolvedPath(t, root)
if got.Path != want {
t.Fatalf("Discover() root = %q, want canonical %q", got.Path, want)
}
}
func TestDiscoverRejectsDirectoriesThatAreNotThothIIProjects(t *testing.T) {
_, err := Discover(t.TempDir())
if err == nil {
t.Fatal("Discover() error = nil, want root discovery failure")
}
}
func writeProjectRoot(t *testing.T, linkedWorktree bool) string {
t.Helper()
root := t.TempDir()
for _, directory := range []string{"backend", "frontend", "harness", "tools", "deploy"} {
if err := os.MkdirAll(filepath.Join(root, directory), 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(root, "compose.yaml"), []byte("services: {}\n"), 0o600); err != nil {
t.Fatal(err)
}
gitPath := filepath.Join(root, ".git")
if linkedWorktree {
if err := os.WriteFile(gitPath, []byte("gitdir: /tmp/thothii-worktree.git\n"), 0o600); err != nil {
t.Fatal(err)
}
return root
}
if err := os.Mkdir(gitPath, 0o755); err != nil {
t.Fatal(err)
}
return root
}
func resolvedPath(t *testing.T, path string) string {
t.Helper()
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
t.Fatal(err)
}
return resolved
}