cas

package
v0.77.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package cas implements a content-addressable storage for git content.

Blobs are copied from cloned repositories to a local store, along with trees. When the same content is requested again, the content is read from the local store, avoiding the need to clone the repository or read from the network.

Index

Constants

View Source
const (
	// DefaultDirPerms represents standard directory permissions (rwxr-xr-x)
	DefaultDirPerms = os.FileMode(0755)
	// StoredFilePerms represents read-only file permissions (r--r--r--)
	StoredFilePerms = os.FileMode(0444)
	// RegularFilePerms represents standard file permissions (rw-r--r--)
	RegularFilePerms = os.FileMode(0644)
)

Variables

View Source
var (
	ErrCommandSpawn        = errors.New("failed to spawn git command")
	ErrNoMatchingReference = errors.New("no matching reference")
	ErrReadTree            = errors.New("failed to read tree")
	ErrNoWorkDir           = errors.New("working directory not set")
)

Git operation errors

Functions

func GetRepoName

func GetRepoName(repo string) string

GetRepoName extracts the repository name from a git URL

Types

type CAS

type CAS struct {
	// contains filtered or unexported fields
}

CAS clones a git repository using content-addressable storage.

func New

func New(opts Options) (*CAS, error)

New creates a new CAS instance with the given options

TODO: Make these options optional

func (*CAS) Clone

func (c *CAS) Clone(ctx context.Context, l *log.Logger, opts *CloneOptions, url string) error

Clone performs the clone operation

TODO: Make options optional

type CASGetter

type CASGetter struct {
	CAS       *CAS
	Logger    *log.Logger
	Opts      *CloneOptions
	Detectors []getter.Detector
}

CASGetter is a go-getter Getter implementation.

func NewCASGetter

func NewCASGetter(l *log.Logger, cas *CAS, opts *CloneOptions) *CASGetter

func (*CASGetter) Detect

func (g *CASGetter) Detect(req *getter.Request) (bool, error)

func (*CASGetter) Get

func (g *CASGetter) Get(ctx context.Context, req *getter.Request) error

func (*CASGetter) GetFile

func (g *CASGetter) GetFile(_ context.Context, req *getter.Request) error

func (*CASGetter) Mode

func (g *CASGetter) Mode(_ context.Context, url *url.URL) (getter.Mode, error)

type CloneOptions

type CloneOptions struct {
	// Dir specifies the target directory for the clone
	// If empty, uses the repository name
	Dir string

	// Branch specifies which branch to clone
	// If empty, uses HEAD
	Branch string

	// IncludedGitFiles specifies the files to preserve from the .git directory
	// If empty, does not preserve any files
	IncludedGitFiles []string
}

CloneOptions configures the behavior of a specific clone operation

type Content

type Content struct {
	// contains filtered or unexported fields
}

Content manages git object storage and linking

func NewContent

func NewContent(store *Store) *Content

NewContent creates a new Content instance

func (*Content) Ensure

func (c *Content) Ensure(l *log.Logger, hash string, data []byte) error

Ensure ensures that a content item exists in the store

func (*Content) EnsureCopy

func (c *Content) EnsureCopy(l *log.Logger, hash, src string) error

EnsureCopy ensures that a content item exists in the store by copying from a file

func (*Content) GetTmpHandle

func (c *Content) GetTmpHandle(hash string) (*os.File, error)

GetTmpHandle returns a file handle to a temporary file where content will be stored.

func (c *Content) Link(hash, targetPath string) error

Link creates a hard link from the store to the target path

func (*Content) Read

func (c *Content) Read(hash string) ([]byte, error)

Read retrieves content from the store by hash

func (*Content) Store

func (c *Content) Store(l *log.Logger, hash string, data []byte) error

Store stores a single content item. This is typically used for trees, As blobs are written directly from git cat-file stdout.

type Error

type Error string

Error types that can be returned by the cas package

const (
	// ErrTempDir is returned when failing to create or close a temporary directory
	ErrTempDir Error = "failed to create or manage temporary directory"
	// ErrCreateDir is returned when failing to create a directory
	ErrCreateDir Error = "failed to create directory"
	// ErrReadFile is returned when failing to read a file
	ErrReadFile Error = "failed to read file"
	// ErrParseTree is returned when failing to parse git tree output
	ErrParseTree Error = "failed to parse git tree output"
	// ErrGitClone is returned when the git clone operation fails
	ErrGitClone Error = "failed to complete git clone"
	// ErrCreateTempDir is returned when failing to create a temporary directory
	ErrCreateTempDir Error = "failed to create temporary directory"
	// ErrCleanupTempDir is returned when failing to clean up a temporary directory
	ErrCleanupTempDir Error = "failed to clean up temporary directory"
)

func (Error) Error

func (e Error) Error() string

type GitRunner

type GitRunner struct {
	WorkDir string
}

GitRunner handles git command execution

func NewGitRunner

func NewGitRunner() *GitRunner

NewGitRunner creates a new GitRunner instance

func (*GitRunner) CatFile

func (g *GitRunner) CatFile(ctx context.Context, hash string, out io.Writer) error

CatFile writes the contents of a git object to a given writer.

func (*GitRunner) Clone

func (g *GitRunner) Clone(ctx context.Context, repo string, bare bool, depth int, branch string) error

Clone performs a git clone operation

func (*GitRunner) CreateTempDir

func (g *GitRunner) CreateTempDir() (string, func() error, error)

CreateTempDir creates a new temporary directory for git operations

func (*GitRunner) LsRemote

func (g *GitRunner) LsRemote(ctx context.Context, repo, ref string) ([]LsRemoteResult, error)

LsRemote runs git ls-remote for a specific reference. If ref is empty, we check HEAD instead.

func (*GitRunner) LsTree

func (g *GitRunner) LsTree(ctx context.Context, reference, path string) (*Tree, error)

LsTree runs git ls-tree and returns the parsed tree

func (*GitRunner) RequiresWorkDir

func (g *GitRunner) RequiresWorkDir() error

RequiresWorkDir returns an error if no working directory is set

func (*GitRunner) SetWorkDir

func (g *GitRunner) SetWorkDir(dir string)

SetWorkDir sets the working directory for git commands

func (*GitRunner) WithWorkDir

func (g *GitRunner) WithWorkDir(workDir string) *GitRunner

WithWorkDir returns a new GitRunner with the specified working directory

type LsRemoteResult

type LsRemoteResult struct {
	Hash string
	Ref  string
}

LsRemoteResult represents the output of git ls-remote

type Options

type Options struct {
	// StorePath specifies a custom path for the content store
	// If empty, uses $HOME/.cache/terragrunt/cas/store
	StorePath string
}

Options configures the behavior of CAS

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages the store directory and locks to prevent concurrent writes

func NewStore

func NewStore(path string) *Store

NewStore creates a new Store instance.

func (*Store) NeedsWrite

func (s *Store) NeedsWrite(hash string, cloneStart time.Time) bool

NeedsWrite checks if a given hash needs to be stored

func (*Store) Path

func (s *Store) Path() string

Path returns the current store path

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree represents a git tree object with its entries

func ParseTree

func ParseTree(output, path string) (*Tree, error)

ParseTree parses the complete output of git ls-tree

func (*Tree) Data

func (t *Tree) Data() []byte

Data returns the tree data

func (*Tree) Entries

func (t *Tree) Entries() []TreeEntry

Entries returns the tree entries

func (*Tree) LinkTree

func (t *Tree) LinkTree(ctx context.Context, store *Store, targetDir string) error

LinkTree writes the tree to a target directory

func (*Tree) Path

func (t *Tree) Path() string

Path returns the tree path

type TreeEntry

type TreeEntry struct {
	Mode string
	Type string
	Hash string
	Path string
}

TreeEntry represents a single entry in a git tree

func ParseTreeEntry

func ParseTreeEntry(line string) (TreeEntry, error)

ParseTreeEntry parses a single line from git ls-tree output

type WrappedError

type WrappedError struct {
	Op      string // Operation that failed
	Path    string // File path if applicable
	Err     error  // Original error
	Context string // Additional context
}

WrappedError provides additional context for errors

func (*WrappedError) Error

func (e *WrappedError) Error() string

func (*WrappedError) Unwrap

func (e *WrappedError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL