This commit is contained in:
Jabberwocky238
2026-07-17 05:55:31 -04:00
parent 67c07b3bcf
commit 68cda1972c
19 changed files with 897 additions and 897 deletions
+31 -31
View File
@@ -1,15 +1,15 @@
// Database engine opener for simplegit's private DB.
//
// Two backends, selected by URI scheme:
//
// sqlite://<path> -> modernc.org/sqlite (pure Go, no CGO; single binary)
// postgres://<dsn-url> -> github.com/lib/pq
//
// Both are driven through xorm (see models.go's InitEngine). modernc registers
// its driver under the name "sqlite" by default, but xorm keys its sqlite
// dialect off "sqlite3" -- so we re-register modernc's driver as "sqlite3" in
// init(). This is the same trick simpleci's driver_sqlite_modernc.go uses, and
// it keeps CGO out of the build (unlike mattn/go-sqlite3).
package state
@@ -35,9 +35,9 @@ import (
"strings"
"sync"
_ "github.com/lib/pq" // postgres driver, registered as "postgres"
_ "github.com/lib/pq"
"golang.org/x/crypto/ssh"
"modernc.org/sqlite" // pure-Go sqlite driver; re-registered as "sqlite3" below
"modernc.org/sqlite"
"xorm.io/xorm"
)
@@ -45,7 +45,7 @@ const sqliteDriver = "sqlite3"
type IState interface {
Open(owner, name string) (*gitcmd.Repository, error)
// should check database, if not exist, return err directly
RepoPath(owner, name string) (string, error)
KnownSSHKey(key ssh.PublicKey) (bool, error)
AccessBySSHKey(ctx context.Context, key ssh.PublicKey, repons, reponame string, perm common.Perm) (ok bool, err error)
@@ -69,7 +69,7 @@ type IStateMut interface {
ACLDelete(id int64) error
}
// Compile-time guarantees that LocalState satisfies both views.
var (
_ IState = (*LocalState)(nil)
_ IStateMut = (*LocalState)(nil)
@@ -85,8 +85,8 @@ type LocalState struct {
}
func init() {
// Register modernc's driver under "sqlite3" so xorm picks up its sqlite
// dialect while the actual driver stays CGO-free.
sql.Register(sqliteDriver, &sqlite.Driver{})
}
@@ -106,8 +106,8 @@ func Open(cfg *common.Config) (*LocalState, error) {
}, nil
}
// Signer returns the SSH host key, creating and persisting it on first use.
// Cached so the key is stable for the process lifetime.
func (s *LocalState) Signers() ([]ssh.Signer, error) {
s.signerMu.Lock()
defer s.signerMu.Unlock()
@@ -185,8 +185,8 @@ func fingerprint(key ssh.PublicKey) string {
return "SHA256:" + hex.EncodeToString(sum[:])
}
// findRepo looks up a repo by (owner, name). name may include ".git". A missing
// row yields ErrRepoNotFound.
func (s *LocalState) findRepo(owner, name string) (*Repo, error) {
var r Repo
has, err := s.engine.Where("namespace = ? AND name = ?", owner, strings.TrimSuffix(name, ".git")).Get(&r)
@@ -203,9 +203,9 @@ func (s *LocalState) RepoPath(owner, name string) (string, error) {
return s.cfg.RepoPath(owner, name)
}
// Open returns a gitcmd.Repository for owner/name. The repo must be registered
// (RepoPath checks the DB) and present on disk. In skip-auth mode only the disk
// presence is required.
func (s *LocalState) Open(owner, name string) (*gitcmd.Repository, error) {
path, err := s.RepoPath(owner, name)
if err != nil {
@@ -214,10 +214,10 @@ func (s *LocalState) Open(owner, name string) (*gitcmd.Repository, error) {
return gitcmd.OpenRepository(context.Background(), path)
}
// Engine returns the underlying xorm engine. It is the read egress for the
// ManageService inspection RPCs (ListRepos / GetRepoGrants): those queries live
// in the gitrpc server layer, so state itself stays free of List*/management-
// read methods (only access checks + Updater writes live here). nil in skip-auth
// mode -- but ManageService is disabled under skip-auth, so no caller is reached
// with a nil engine.
func (s *LocalState) Engine() *xorm.Engine { return s.engine }