Documentation
¶
Index ¶
- Constants
- Variables
- func AppendGitExcludeArgs(args []string, ignoredLockFiles []string) []string
- func IsLockFile(path string, ignoredLockFiles []string) bool
- func OpenInRoot(root, path string) (*os.File, error)
- func ReadBoundedFile(ctx context.Context, root, path string, opts ReadFileOptions) (string, error)
- func ReadLimitedLine(ctx context.Context, r *bufio.Reader, limit int) ([]byte, error)
- func RunGit(ctx context.Context, dir string, args ...string) ([]byte, error)
- func RunGitWithLimit(ctx context.Context, dir string, limit int64, args ...string) ([]byte, bool, error)
- func SafeRel(base, target string) string
- func SanitizeFileName(s string) string
- func SanitizeFileNameWithDefault(s, defaultName string) string
- func TruncateLines(lines []string, maxBytes int) string
- func TruncateString(s string, maxBytes int) string
- func ValidateAndRel(root, target string) (string, error)
- func ValidatePathInRoot(root, path string) (string, error)
- func WriteFileWithDirs(path string, data []byte) error
- type ReadFileOptions
- type TailBuffer
Constants ¶
const MaxGitLimitBytes = 10 * 1024 * 1024
MaxGitLimitBytes defines the hard ceiling (10 MB) for RunGitWithLimit to prevent excessive memory allocation.
Variables ¶
var DefaultLockFiles = []string{
"go.sum",
"package-lock.json",
"yarn.lock",
"pnpm-lock.yaml",
"bun.lockb",
"deno.lock",
"Cargo.lock",
"poetry.lock",
"uv.lock",
"Gemfile.lock",
"composer.lock",
"pubspec.lock",
"mix.lock",
"flake.lock",
"MODULE.bazel.lock",
}
DefaultLockFiles is a list of common lockfile names used by various package managers across different ecosystems.
Functions ¶
func AppendGitExcludeArgs ¶
AppendGitExcludeArgs appends git pathspec exclude arguments for each entry in ignoredLockFiles to the provided args slice and returns the updated slice. For example, if "go.sum" is in the list, it appends ":(exclude)*go.sum".
func IsLockFile ¶
IsLockFile reports whether path names one of the ignored lockfiles — either at the repository root (path == name) or in any subdirectory (path ends in "/name").
func OpenInRoot ¶
OpenInRoot validates the path is within the root (if root is not empty) and opens the file.
func ReadBoundedFile ¶
ReadBoundedFile reads a file from the root according to the provided options. It handles line ranges, tailing, and memory bounds to ensure safe reading.
func ReadLimitedLine ¶
ReadLimitedLine reads a single line from r, up to limit bytes. It respects context cancellation.
func RunGit ¶
RunGit invokes `git <args>` in the given working directory (or the current directory when dir is empty) and returns combined stdout+stderr. Callers wrap the returned error with their own context; RunGit itself does not format the error so callers can inspect the exit code via errors.As (*exec.ExitError) where relevant.
func RunGitWithLimit ¶ added in v0.6.0
func RunGitWithLimit(ctx context.Context, dir string, limit int64, args ...string) ([]byte, bool, error)
RunGitWithLimit invokes `git <args>` in the given working directory, capturing combined stdout+stderr up to limit bytes. If the output exceeds limit, it kills the command process to stop further execution and returns the read bytes and truncated=true. The limit must be non-negative and <= MaxGitLimitBytes (10 MB).
func SafeRel ¶
SafeRel returns the relative path from base to target. If an error occurs, it returns the original target path.
func SanitizeFileName ¶
SanitizeFileName replaces any non-alphanumeric character with an underscore, ensuring the string is safe for use as a filesystem component.
func SanitizeFileNameWithDefault ¶
SanitizeFileNameWithDefault sanitizes s and returns defaultName if the result is empty or consists entirely of underscores.
func TruncateLines ¶
TruncateLines joins lines into a single string, ensuring the result is no longer than maxBytes. It appends the suffix "\n... (truncated)" if the result is truncated. It ensures the truncation point is at a valid UTF-8 rune boundary.
func TruncateString ¶
TruncateString ensures s is no longer than maxBytes. If it is longer, it is truncated and the suffix "\n... (truncated)" is appended. The total length will not exceed maxBytes. It ensures the truncation point is at a valid UTF-8 rune boundary.
func ValidateAndRel ¶
ValidateAndRel validates that target is within root and returns its relative path from root.
func ValidatePathInRoot ¶
ValidatePathInRoot ensures that the given path is within the root directory. It resolves symlinks to prevent escaping the root, including cases where multiple non-existent components follow a symlink.
func WriteFileWithDirs ¶
WriteFileWithDirs creates any missing parent directories before writing the file with 0o644 permissions. It uses 0o755 for any created directories.
Types ¶
type ReadFileOptions ¶
type ReadFileOptions struct {
// LineStart is the 1-based line number to start reading from.
LineStart int
// LineEnd is the 1-based line number to stop reading at (inclusive).
LineEnd int
// TailLines is the number of lines to read from the end of the file.
// If set, LineStart and LineEnd are ignored.
TailLines int
// MaxOutputBytes is the maximum number of bytes to return.
MaxOutputBytes int
// MaxLineLength is the maximum length of a single line.
MaxLineLength int
// MaxTailBufferBytes is the maximum memory to use for the tail buffer.
MaxTailBufferBytes int
// FailIfTooLarge, when true, causes ReadBoundedFile to return an error if
// the file content (when reading whole file) exceeds MaxWholeFileReadBytes.
FailIfTooLarge bool
// MaxWholeFileReadBytes is the maximum bytes allowed for a whole file read.
MaxWholeFileReadBytes int
}
ReadFileOptions configures how a file should be read by ReadBoundedFile.
type TailBuffer ¶
type TailBuffer struct {
// contains filtered or unexported fields
}
TailBuffer maintains a fixed number of lines with a maximum byte size.
func NewTailBuffer ¶
func NewTailBuffer(limit, maxBytes int) *TailBuffer
NewTailBuffer creates a new TailBuffer.
func (*TailBuffer) Add ¶
func (b *TailBuffer) Add(line []byte)
Add appends a line to the buffer, potentially removing the oldest line(s).
func (*TailBuffer) Lines ¶
func (b *TailBuffer) Lines() [][]byte
Lines returns the lines currently in the buffer in order.