Documentation
¶
Overview ¶
Package roles adds repository context to file occurrences through path classification. Roles are additive; classification never excludes a file. Paths are repository-relative, use slash separators, and end in a slash for directories. Optional evidence explains each matching convention.
Index ¶
- Constants
- Variables
- func IsLegalDirectory(name string) bool
- func IsLegalDirectoryBytes(name []byte) bool
- func LegalFileName(name string) (license, notice bool)
- func LegalFileNameBytes(name []byte) (license, notice bool)
- func Walk(tree fs.FS, options WalkOptions, visit func(string, Result) error) error
- func WalkMatch(tree fs.FS, options WalkOptions, visit func(string, Set) error) error
- type BlobResult
- type Classifier
- func (c *Classifier) Classify(name string) (Result, error)
- func (c *Classifier) ClassifyBlob(name string, contents []byte) (BlobResult, error)
- func (c *Classifier) ClassifyPrefix(name string, prefix []byte) (BlobResult, error)
- func (c *Classifier) ClassifyReader(name string, reader io.Reader) (BlobResult, error)
- func (c *Classifier) Match(name string) (Set, error)
- func (c *Classifier) RootState() State
- func (c *Classifier) Walk(tree fs.FS, options WalkOptions, visit func(string, Result) error) error
- func (c *Classifier) WalkMatch(tree fs.FS, options WalkOptions, visit func(string, Set) error) error
- type Evidence
- type Result
- type Role
- type Set
- type State
- type StateKey
- type VendorRoot
- type WalkOptions
Examples ¶
Constants ¶
const ( MaxHeaderBytes = 8192 MaxHeaderLines = 40 ContentVersion = "2" )
const ( DefaultMaxEntries = 1_000_000 DefaultMaxDepth = 256 )
const CorpusVersion = "6"
CorpusVersion identifies the corpus schema.
Variables ¶
var ErrInvalidPath = errors.New("expected a clean repository-relative path")
ErrInvalidPath indicates an absolute, unclean, empty, or NUL-containing path.
var ErrLimit = errors.New("repository traversal limit reached")
ErrLimit indicates that a walk stopped before visiting the complete tree.
Functions ¶
func IsLegalDirectory ¶
IsLegalDirectory reports whether one directory name confers licence context. It accepts a single name, independent of the repository path contract.
func IsLegalDirectoryBytes ¶
IsLegalDirectoryBytes is the raw Git-name form of IsLegalDirectory.
func LegalFileName ¶
LegalFileName reports licence and notice matches for one filename. These predicates use the same corpus as Classify and allocate no evidence.
func LegalFileNameBytes ¶
LegalFileNameBytes is the raw Git-name form of LegalFileName.
func Walk ¶
Walk classifies regular files and directories in lexical order. It skips symlinks and special files, propagates errors, and never reads file contents. The filesystem must be rooted by the caller. fs.SkipDir and fs.SkipAll from visit have the same meaning as in fs.WalkDir. The root itself is not emitted.
Types ¶
type BlobResult ¶
type BlobResult struct {
Result
HeaderChecked bool `json:"header_checked"`
BytesExamined int `json:"bytes_examined"`
HeaderLimited bool `json:"header_limited"`
}
BlobResult describes bounded content inspection, where applicable. Absence of Generated is not proof that a file was handwritten.
func ClassifyBlob ¶
func ClassifyBlob(name string, contents []byte) (BlobResult, error)
ClassifyBlob adds generated and minified content evidence to path classification. Contents must be the complete file. At most MaxHeaderBytes and MaxHeaderLines are inspected. Unsupported file types receive path-only classification.
func ClassifyPrefix ¶ added in v0.1.3
func ClassifyPrefix(name string, prefix []byte) (BlobResult, error)
ClassifyPrefix adds content evidence from a prefix starting at file offset zero. EOF is unknown, so an inspected prefix always has HeaderLimited set, even if empty. At most MaxHeaderBytes and MaxHeaderLines are inspected. Use ClassifyBlob when the contents are known to be complete.
Example ¶
package main
import (
"fmt"
"github.com/git-pkgs/roles"
)
func main() {
prefix := []byte("# DO NOT EDIT")
result, err := roles.ClassifyPrefix("client.py", prefix)
if err != nil {
panic(err)
}
fmt.Println(result.Has(roles.Generated), result.HeaderLimited)
}
Output: false true
func ClassifyReader ¶
func ClassifyReader(name string, reader io.Reader) (BlobResult, error)
ClassifyReader adds content evidence using bounded input. It reads at most MaxHeaderBytes plus one byte used to detect truncation.
type Classifier ¶
type Classifier struct {
// contains filtered or unexported fields
}
Classifier combines the embedded corpus with immutable vendor-root context. A classifier can be shared by concurrent callers.
func New ¶
func New(roots []VendorRoot) (*Classifier, error)
New validates, normalizes and copies caller-supplied vendor roots. Exact duplicates are ignored, and distinct observations for one path are retained.
func (*Classifier) Classify ¶
func (c *Classifier) Classify(name string) (Result, error)
Classify returns labels and evidence for a file or trailing-slash directory.
func (*Classifier) ClassifyBlob ¶
func (c *Classifier) ClassifyBlob(name string, contents []byte) (BlobResult, error)
ClassifyBlob preserves this classifier's vendor-root context.
func (*Classifier) ClassifyPrefix ¶ added in v0.1.3
func (c *Classifier) ClassifyPrefix(name string, prefix []byte) (BlobResult, error)
ClassifyPrefix preserves this classifier's vendor-root context.
func (*Classifier) ClassifyReader ¶
func (c *Classifier) ClassifyReader(name string, reader io.Reader) (BlobResult, error)
ClassifyReader preserves this classifier's vendor-root context while bounding content reads.
func (*Classifier) Match ¶
func (c *Classifier) Match(name string) (Set, error)
Match returns labels without collecting evidence.
func (*Classifier) RootState ¶
func (c *Classifier) RootState() State
RootState returns this classifier's root state.
func (*Classifier) Walk ¶
func (c *Classifier) Walk(tree fs.FS, options WalkOptions, visit func(string, Result) error) error
Walk reuses inherited matches and includes this classifier's vendor roots.
func (*Classifier) WalkMatch ¶
func (c *Classifier) WalkMatch(tree fs.FS, options WalkOptions, visit func(string, Set) error) error
WalkMatch includes this classifier's vendor roots without collecting evidence.
type Evidence ¶
type Evidence struct {
Rule string `json:"rule"`
Role Role `json:"role"`
Path string `json:"path"`
Subtype string `json:"subtype,omitempty"`
Ecosystem string `json:"ecosystem,omitempty"`
Source string `json:"source"`
EvidencePath string `json:"evidence_path,omitempty"`
}
Evidence identifies a matched corpus rule or caller-supplied vendor root. Source names the rule provider. EvidencePath names the repository input that supplied contextual evidence, when one exists.
type Result ¶
Result contains labels and the independent matches that produced them. Classification functions return non-nil Roles and Evidence slices.
func Classify ¶
Classify returns additive roles and all matching evidence, in stable order. Unknown conventions produce an empty result. A trailing slash denotes a directory. Paths use slash separators; backslashes and non-UTF-8 bytes are literal names.
type Role ¶
type Role string
Role describes a convention at a file or directory occurrence.
const ( Source Role = "source" Test Role = "test" Fixture Role = "fixture" Example Role = "example" Benchmark Role = "benchmark" Fuzz Role = "fuzz" Vendor Role = "vendor" Generated Role = "generated" BuildOutput Role = "build-output" Cache Role = "cache" Documentation Role = "documentation" Legal Role = "legal" Build Role = "build" CI Role = "ci" Packaging Role = "packaging" Tooling Role = "tooling" Configuration Role = "configuration" Minified Role = "minified" )
Keep explicit Role types so the constant-to-bit mapping test includes each role.
type Set ¶
type Set uint32
Set is a compact collection of roles in corpus-defined order.
func Match ¶
Match returns the same labels as Classify without collecting evidence. It returns ErrInvalidPath for paths outside the repository-relative contract.
func (Set) BundledOrDerived ¶ added in v0.1.3
BundledOrDerived reports whether the set contains Vendor, Fixture, Cache or BuildOutput. Generated alone does not qualify. The result does not establish ownership or whether a file is safe to skip.
func (Set) MarshalJSON ¶
MarshalJSON encodes the set as role names in corpus order.
func (*Set) UnmarshalJSON ¶
UnmarshalJSON decodes role names into a set.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State incrementally classifies one repository path. Its zero value is the root state for the package-level classifier.
func RootState ¶
func RootState() State
RootState returns the package-level classifier's root state.
func (State) Enter ¶
Enter returns the state for a child directory component. Git tree names may contain arbitrary non-NUL bytes but cannot contain a slash.
type StateKey ¶
type StateKey struct {
// contains filtered or unexported fields
}
StateKey is a stable, comparable summary of the context that can affect descendant matches. It can be combined with a Git tree object ID for a semantic subtree cache.
type VendorRoot ¶
VendorRoot adds a vendor directory discovered by a caller.
type WalkOptions ¶
WalkOptions bounds traversal. Zero values select the documented defaults.