package vcs import ( "context" "io" "tangled.org/core/knotserver/pijul" ) // pijulReadAdapter wraps a pijul.PijulRepo to implement ReadRepo. type pijulReadAdapter struct { p *pijul.PijulRepo } func newPijulReadAdapter(p *pijul.PijulRepo) *pijulReadAdapter { return &pijulReadAdapter{p: p} } func (a *pijulReadAdapter) VCSType() string { return "pijul" } func (a *pijulReadAdapter) Path() string { return a.p.Path() } func (a *pijulReadAdapter) History(offset, limit int) ([]HistoryEntry, error) { changes, err := a.p.Changes(offset, limit) if err != nil { return nil, err } entries := make([]HistoryEntry, 0, len(changes)) for _, c := range changes { var author Author if len(c.Authors) > 0 { author = Author{ Name: c.Authors[0].Name, Email: c.Authors[0].Email, } } entries = append(entries, HistoryEntry{ Hash: c.Hash, Author: author, Committer: author, Message: c.Message, Timestamp: c.Timestamp, Parents: c.Dependencies, }) } return entries, nil } func (a *pijulReadAdapter) TotalHistoryEntries() (int, error) { return a.p.TotalChanges() } func (a *pijulReadAdapter) HistoryEntry(hash string) (*HistoryEntry, error) { c, err := a.p.GetChange(hash) if err != nil { return nil, err } var author Author if len(c.Authors) > 0 { author = Author{ Name: c.Authors[0].Name, Email: c.Authors[0].Email, } } return &HistoryEntry{ Hash: c.Hash, Author: author, Committer: author, Message: c.Message, Timestamp: c.Timestamp, Parents: c.Dependencies, }, nil } func (a *pijulReadAdapter) Branches(opts *PaginationOpts) ([]BranchInfo, error) { var pijulOpts *pijul.ChannelOptions if opts != nil { pijulOpts = &pijul.ChannelOptions{ Limit: opts.Limit, Offset: opts.Offset, } } channels, err := a.p.ChannelsWithOptions(pijulOpts) if err != nil { return nil, err } infos := make([]BranchInfo, 0, len(channels)) for _, ch := range channels { infos = append(infos, BranchInfo{ Name: ch.Name, IsDefault: ch.IsCurrent, }) } return infos, nil } func (a *pijulReadAdapter) DefaultBranch() (string, error) { return a.p.FindDefaultChannel() } func (a *pijulReadAdapter) FileTree(ctx context.Context, path string) ([]TreeEntry, error) { files, err := a.p.FileTree(ctx, path) if err != nil { return nil, err } entries := make([]TreeEntry, 0, len(files)) for _, f := range files { entries = append(entries, TreeEntry{ Name: f.Name, Mode: f.Mode, Size: f.Size, }) } return entries, nil } func (a *pijulReadAdapter) FileContentN(path string, cap int64) ([]byte, error) { return a.p.FileContentN(path, cap) } func (a *pijulReadAdapter) RawContent(path string) ([]byte, error) { return a.p.RawContent(path) } func (a *pijulReadAdapter) WriteTar(w io.Writer, prefix string) error { return a.p.WriteTar(w, prefix) } func (a *pijulReadAdapter) Tags(_ *PaginationOpts) ([]TagInfo, error) { // Pijul doesn't have tags. return nil, nil } // pijulMutableAdapter wraps a pijul.PijulRepo to implement MutableRepo. type pijulMutableAdapter struct { *pijulReadAdapter } func newPijulMutableAdapter(p *pijul.PijulRepo) *pijulMutableAdapter { return &pijulMutableAdapter{pijulReadAdapter: newPijulReadAdapter(p)} } func (a *pijulMutableAdapter) SetDefaultBranch(name string) error { return a.p.SetDefaultChannel(name) } func (a *pijulMutableAdapter) DeleteBranch(name string) error { return a.p.DeleteChannel(name) } // Pijul returns the underlying *pijul.PijulRepo for pijul-specific operations // that don't belong in the VCS interface. func (a *pijulReadAdapter) Pijul() *pijul.PijulRepo { return a.p }