s
This commit is contained in:
+36
-4
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
@@ -20,9 +21,10 @@ type Config struct {
|
||||
DBUri string
|
||||
SkipAuth bool
|
||||
|
||||
prepared atomic.Bool
|
||||
driver string
|
||||
dsn string
|
||||
preparedDbUri string
|
||||
prepared atomic.Bool
|
||||
driver string
|
||||
dsn string
|
||||
}
|
||||
|
||||
func (c *Config) RepoRoot() string {
|
||||
@@ -31,6 +33,9 @@ func (c *Config) RepoRoot() string {
|
||||
|
||||
// Prepare checks that the config is valid and creates the repo root if needed.
|
||||
func (c *Config) Prepare() error {
|
||||
if c.prepared.Load() {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
if c.DBUri == "" {
|
||||
return errors.New("simplegit: -db is required (e.g. sqlite://state.db)")
|
||||
@@ -38,7 +43,7 @@ func (c *Config) Prepare() error {
|
||||
if err := os.MkdirAll(c.RepoRoot(), 0o755); err != nil {
|
||||
return fmt.Errorf("simplegit: Failed to create data root: %s (%v)", c.RepoRoot(), err)
|
||||
}
|
||||
c.driver, c.dsn, c.DBUri, err = ParseDBURI(c.Root, c.DBUri)
|
||||
c.driver, c.dsn, c.preparedDbUri, err = ParseDBURI(c.Root, c.DBUri)
|
||||
if err != nil {
|
||||
return errors.New("simplegit: ParseDBURI failed")
|
||||
}
|
||||
@@ -65,3 +70,30 @@ func (c *Config) DSN() string {
|
||||
}
|
||||
return c.dsn
|
||||
}
|
||||
|
||||
func (c *Config) DBURI() string {
|
||||
if !c.prepared.Load() {
|
||||
panic("Config.PreparedDBUri called before Prepare")
|
||||
}
|
||||
return c.preparedDbUri
|
||||
}
|
||||
|
||||
// normalizeName strips one trailing ".git" so both "myrepo" and "myrepo.git"
|
||||
// refer to the same repo.
|
||||
func normalizeName(name string) string {
|
||||
return strings.TrimSuffix(name, ".git")
|
||||
}
|
||||
|
||||
// relPath returns the repo path relative to root: "owner/name.git".
|
||||
func relPath(owner, name string) string {
|
||||
return owner + "/" + normalizeName(name) + ".git"
|
||||
}
|
||||
|
||||
// RepoPath returns the absolute on-disk path for owner/name, but only if the
|
||||
// repo is registered in the DB -- the DB is the authority, not the filesystem.
|
||||
// In skip-auth mode the DB check is skipped (any path under root resolves).
|
||||
func (s *Config) RepoPath(owner, name string) (string, error) {
|
||||
name = normalizeName(name)
|
||||
rel := relPath(owner, name)
|
||||
return Resolve(s.RepoRoot(), rel)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user