Help us shape the future of Go! Take the 2026 Go Developer Survey

files

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package files discovers in-scope files and safely resolves indexed paths.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotIndexed is returned when a path was not part of a scan's file index.
	ErrNotIndexed = errors.New("path is not indexed")
	// ErrPathEscape is returned when resolving a path would leave the scan root.
	ErrPathEscape = errors.New("path escapes scan root")
)

Functions

func ResolveFile added in v0.1.6

func ResolveFile(root, rel string) (string, error)

ResolveFile returns the absolute path of the regular file at the slash-separated path rel below root, after evaluating symlinks. It rejects absolute paths and paths that leave root, before or after evaluation, with ErrPathEscape. Unlike Index.Resolve, any file below root resolves; callers decide which paths may be read.

func Within added in v0.1.6

func Within(root, candidate string) bool

Within reports whether candidate is root or lies below it. Both paths must be absolute and symlink-evaluated.

Types

type Entry

type Entry struct {
	Name     string  `json:"name"`
	Path     string  `json:"path"`
	IsDir    bool    `json:"isDir"`
	Children []Entry `json:"children,omitempty"`
}

Entry is a directory or supported file in a scan tree. Path is slash-separated and relative to the scan root; the root entry has an empty Path.

type IgnoreRules added in v0.1.6

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

IgnoreRules answers git ignore questions for paths below one root. It reads the sources git uses (see rootIgnoreRules), loading each directory's .gitignore on first use. It is not safe for concurrent use.

func NewIgnoreRules added in v0.1.6

func NewIgnoreRules(root string) *IgnoreRules

NewIgnoreRules returns the ignore rules for the absolute, symlink-evaluated directory root.

func (*IgnoreRules) Ignored added in v0.1.6

func (r *IgnoreRules) Ignored(rel string, isDir bool) bool

Ignored reports whether the slash-separated root-relative path is ignored, itself or through an ignored ancestor directory, which git never re-includes below. A .git entry is always ignored.

type Index

type Index struct {
	Tree     Entry     `json:"tree"`
	Files    []string  `json:"files"`
	Warnings []Warning `json:"-"`
	// contains filtered or unexported fields
}

Index is the deterministic result of a scan. Files contains the paths of all supported files in tree traversal order. Warnings contains recoverable errors encountered below the root and is intentionally omitted from JSON output.

func Scan

func Scan(root string, maxDepth int, excluded []string, accepts func(string) bool) (*Index, error)

Scan builds an index rooted at root. The root directory is depth zero, so a maxDepth of zero includes supported files directly within root. Negative maxDepth values scan without a depth limit. Directory names in excluded are matched by base name at every scanned level. .git entries of any type are never indexed.

Symlinks are not included. Failure to open or inspect the root is fatal; failures below it are recorded in Index.Warnings and the rest of the scan is returned.

func (*Index) Ignored added in v0.1.6

func (i *Index) Ignored(name string) bool

Ignored reports whether an indexed file is excluded by gitignore rules: the root repository's core.excludesFile and info/exclude, including for linked worktrees, and the .gitignore files from the root down. Ignored files stay browsable; content search and code navigation skip them.

func (*Index) Resolve

func (i *Index) Resolve(name string) (string, error)

Resolve returns an absolute path for an indexed supported file. It rejects unindexed paths and evaluates the current filesystem path to ensure it still resides within the scan root.

func (*Index) RootPath

func (i *Index) RootPath() string

RootPath returns the absolute, symlink-evaluated directory used by the scan.

type NameFilter added in v0.1.6

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

NameFilter selects files by case-insensitive base-name globs in path.Match syntax. It knows no file formats; every matching file is in scope.

func AllNames added in v0.1.6

func AllNames() NameFilter

AllNames is the default filter, equivalent to the single pattern "*".

func NewNameFilter added in v0.1.6

func NewNameFilter(patterns []string) (NameFilter, error)

NewNameFilter validates patterns. Empty patterns, malformed globs, and patterns containing "/" (which can never match a base name) are rejected.

func (NameFilter) Match added in v0.1.6

func (f NameFilter) Match(name string) bool

Match reports whether the base name of name matches any pattern.

func (NameFilter) Patterns added in v0.1.6

func (f NameFilter) Patterns() []string

Patterns returns the validated patterns as given.

type Warning

type Warning struct {
	Path string
	Err  error
}

Warning describes an entry that could not be inspected during a scan. Path is slash-separated and relative to the scan root.

func WalkUnignored added in v0.1.6

func WalkUnignored(ctx context.Context, root string, rules *IgnoreRules, visit func(rel string, entry fs.DirEntry) error) ([]Warning, error)

WalkUnignored calls visit with the slash-separated root-relative path of every regular file and symlink below root that rules do not ignore, in directory order. Symlinks are reported, never followed. Ignored directories, .git entries, and nested repositories (directories other than root holding a .git entry) are pruned. No depth, filename, or exclusion scope applies.

root must be absolute and symlink-evaluated. Failure to read root, a visit error, or ctx ending stops the walk with that error; unreadable directories below root are returned as warnings.

func (Warning) Error

func (w Warning) Error() string

Jump to

Keyboard shortcuts

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