beads/internal/storage/dolt/versioned.go
2026-03-24 12:01:16 -07:00

73 lines
2.5 KiB
Go

package dolt
import (
"context"
"database/sql"
"fmt"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
"github.com/steveyegge/beads/internal/types"
)
// History returns the complete version history for an issue.
func (s *DoltStore) History(ctx context.Context, issueID string) ([]*storage.HistoryEntry, error) {
var result []*storage.HistoryEntry
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.HistoryInTx(ctx, tx, issueID)
if err != nil {
return wrapQueryError("get issue history", err)
}
return nil
})
return result, err
}
// AsOf returns the state of an issue at a specific commit hash or branch ref.
// Implements storage.VersionedStorage.
func (s *DoltStore) AsOf(ctx context.Context, issueID string, ref string) (*types.Issue, error) {
return s.getIssueAsOf(ctx, issueID, ref)
}
// Diff returns changes between two commits/branches.
// Implements storage.VersionedStorage.
func (s *DoltStore) Diff(ctx context.Context, fromRef, toRef string) ([]*storage.DiffEntry, error) {
var result []*storage.DiffEntry
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.DiffInTx(ctx, tx, fromRef, toRef)
return err
})
return result, err
}
// ListBranches returns the names of all branches.
// Implements storage.VersionedStorage.
func (s *DoltStore) ListBranches(ctx context.Context) ([]string, error) {
return versioncontrolops.ListBranches(ctx, s.db)
}
// GetCurrentCommit returns the hash of the current HEAD commit.
// Implements storage.VersionedStorage.
func (s *DoltStore) GetCurrentCommit(ctx context.Context) (string, error) {
var hash string
err := s.db.QueryRowContext(ctx, "SELECT DOLT_HASHOF('HEAD')").Scan(&hash)
if err != nil {
return "", fmt.Errorf("failed to get current commit: %w", err)
}
return hash, nil
}
// GetConflicts returns any merge conflicts in the current state.
// Implements storage.VersionedStorage.
func (s *DoltStore) GetConflicts(ctx context.Context) ([]storage.Conflict, error) {
return versioncontrolops.GetConflicts(ctx, s.db)
}
// CommitExists checks whether a commit hash exists in the repository.
// Returns false for empty strings, malformed input, or non-existent commits.
func (s *DoltStore) CommitExists(ctx context.Context, commitHash string) (bool, error) {
return versioncontrolops.CommitExists(ctx, s.db, commitHash)
}