package memvcs
import (
)
const Name = "mem"
type Commit struct {
ID string
Parent string
Message string
Files map[string][]byte
}
type PushRecord struct {
Remote string
Ref string
Tags []string
}
type Repo struct {
mu sync.Mutex
root string
seq int
headRef string
refs map[string]string
commits map[string]Commit
tags map[string]string
pushes []PushRecord
}
func ( map[string][]byte) *Repo {
:= &Repo{
root: "/mem",
headRef: "main",
refs: make(map[string]string),
commits: make(map[string]Commit),
tags: make(map[string]string),
}
.commitLocked("initial commit", cloneFiles())
return
}
func ( *Repo) ( string, map[string][]byte) string {
.seq++
:= fmt.Sprintf("mem-%04d", .seq)
:= .refs[.headRef]
.commits[] = Commit{
ID: ,
Parent: ,
Message: ,
Files: ,
}
.refs[.headRef] =
return
}
func ( *Repo) ( string, map[string][]byte) string {
.mu.Lock()
defer .mu.Unlock()
:= cloneFiles(.commits[.refs[.headRef]].Files)
for , := range {
[cleanPath()] = bytes.Clone()
}
return .commitLocked(, )
}
func ( *Repo) (, string) {
.mu.Lock()
defer .mu.Unlock()
.tags[] = cmp.Or(, .refs[.headRef])
}
func ( *Repo) () []PushRecord {
.mu.Lock()
defer .mu.Unlock()
return slices.Clone(.pushes)
}
func ( *Repo) () string { return Name }
func ( *Repo) () string { return .root }
func ( *Repo) ( context.Context, string) (vcs.Revision, error) {
.mu.Lock()
defer .mu.Unlock()
if == "" || == .headRef {
return vcs.Revision{
ID: .refs[.headRef],
Ref: .headRef,
Label: .headRef,
}, nil
}
if , := .refs[]; {
return vcs.Revision{ID: , Ref: , Label: }, nil
}
if , := .tags[]; {
return vcs.Revision{ID: }, nil
}
if , := .commits[]; {
return vcs.Revision{ID: }, nil
}
return vcs.Revision{}, fmt.Errorf("memvcs: unknown revision %q", )
}
func ( *Repo) ( context.Context) ([]string, error) {
.mu.Lock()
defer .mu.Unlock()
return slices.Sorted(maps.Keys(.tags)), nil
}
func ( *Repo) ( context.Context, , , string) ([]string, error) {
.mu.Lock()
defer .mu.Unlock()
, := .resolveIDLocked()
if ! {
return nil, fmt.Errorf("memvcs: unknown base revision %q", )
}
, := .resolveIDLocked()
if ! {
return nil, fmt.Errorf("memvcs: unknown target revision %q", )
}
:= .commits[].Files
:= .commits[].Files
:= dirPrefix()
:= make(map[string]bool)
for := range {
[] = true
}
for := range {
[] = true
}
var []string
for := range {
if != "" && !strings.HasPrefix(, ) {
continue
}
, := []
, := []
if != || !bytes.Equal(, ) {
= append(, )
}
}
slices.Sort()
return , nil
}
func ( *Repo) ( context.Context, , string) ([]vcs.FileEntry, error) {
.mu.Lock()
defer .mu.Unlock()
, := .resolveIDLocked()
if ! {
return nil, fmt.Errorf("memvcs: unknown revision %q", )
}
return listFromMap(.commits[].Files, ), nil
}
func ( *Repo) ( context.Context, , string) ([]byte, error) {
.mu.Lock()
defer .mu.Unlock()
, := .resolveIDLocked()
if ! {
return nil, fmt.Errorf("memvcs: unknown revision %q", )
}
, := .commits[].Files[cleanPath()]
if ! {
return nil, fs.ErrNotExist
}
return bytes.Clone(), nil
}
func ( *Repo) ( context.Context, vcs.Revision) (vcs.Session, error) {
.mu.Lock()
defer .mu.Unlock()
, := .commits[.ID]
if ! {
return nil, fmt.Errorf("memvcs: unknown revision %q", .ID)
}
return &session{
repo: ,
rev: ,
head: .ID,
workFiles: cloneFiles(.Files),
headFiles: cloneFiles(.Files),
staged: make(map[string]Commit),
tags: make(map[string]string),
}, nil
}
func ( *Repo) ( context.Context, , string, []string) error {
.mu.Lock()
defer .mu.Unlock()
.pushes = append(.pushes, PushRecord{
Remote: cmp.Or(, "default"),
Ref: ,
Tags: slices.Clone(),
})
return nil
}
func ( *Repo) ( string, vcs.PublishResult) string {
return fmt.Sprintf("mem push %s (%d tags)", cmp.Or(, "default"), len(.Tags))
}
func ( *Repo) ( string) (string, bool) {
if == "" {
return .refs[.headRef], true
}
if , := .commits[]; {
return , true
}
if , := .tags[]; {
return , true
}
if , := .refs[]; {
return , true
}
return "", false
}
func ( map[string][]byte, string) []vcs.FileEntry {
:= dirPrefix()
var []vcs.FileEntry
for := range {
if != "" && !strings.HasPrefix(, ) {
continue
}
= append(, vcs.FileEntry{
Path: ,
Mode: fs.FileMode(0o644),
})
}
slices.SortFunc(, func(, vcs.FileEntry) int {
return strings.Compare(.Path, .Path)
})
return
}
func ( map[string][]byte) map[string][]byte {
:= make(map[string][]byte, len())
for , := range {
[cleanPath()] = bytes.Clone()
}
return
}
func ( string) string {
return vcs.CleanRelPath()
}
func ( string) string {
return vcs.DirPrefix()
}