package vcs import ( "fmt" "github.com/go-git/go-git/v5/plumbing" "tangled.org/core/knotserver/git" "tangled.org/core/knotserver/pijul" ) // Open opens a repository at path with the given ref (branch/tag/hash). // It auto-detects the VCS type and returns the appropriate adapter. // For git, ref is resolved as a revision; for pijul, ref is the channel name. func Open(path, ref string) (ReadRepo, error) { vcsType, err := DetectVCS(path) if err != nil { return nil, err } switch vcsType { case "git": g, err := git.Open(path, ref) if err != nil { return nil, err } return newGitReadAdapter(g), nil case "pijul": p, err := pijul.Open(path, ref) if err != nil { return nil, err } return newPijulReadAdapter(p), nil default: return nil, fmt.Errorf("unsupported VCS type: %s", vcsType) } } // PlainOpen opens a repository at path without setting a specific ref. // Returns a MutableRepo since no ref is pinned. func PlainOpen(path string) (MutableRepo, error) { vcsType, err := DetectVCS(path) if err != nil { return nil, err } switch vcsType { case "git": g, err := git.PlainOpen(path) if err != nil { return nil, err } return newGitMutableAdapter(g), nil case "pijul": p, err := pijul.PlainOpen(path) if err != nil { return nil, err } return newPijulMutableAdapter(p), nil default: return nil, fmt.Errorf("unsupported VCS type: %s", vcsType) } } // AsGit returns the underlying *git.GitRepo if repo is a git adapter, or nil. // Use this for git-specific operations that don't belong in the VCS interface. func AsGit(repo ReadRepo) *git.GitRepo { switch r := repo.(type) { case *gitReadAdapter: return r.Git() case *gitMutableAdapter: return r.Git() } return nil } // AsPijul returns the underlying *pijul.PijulRepo if repo is a pijul adapter, or nil. // Use this for pijul-specific operations that don't belong in the VCS interface. func AsPijul(repo ReadRepo) *pijul.PijulRepo { switch r := repo.(type) { case *pijulReadAdapter: return r.Pijul() case *pijulMutableAdapter: return r.Pijul() } return nil } // gitHash converts a hex string to a plumbing.Hash. func gitHash(s string) plumbing.Hash { return plumbing.NewHash(s) }