{"modulePath":"github.com/CommitBrief/commitbrief","version":"v1.23.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package diff\n\n## Constants\n\n```go\nconst (\n\tSideRight = \"RIGHT\"\n\tSideLeft  = \"LEFT\"\n)\n```\nGitHub inline-review-comment \\`side\\` values. RIGHT is the post-image (new file), LEFT is the pre-image (old file).\n\n## Functions\n\n```go\nfunc CanonicalPath(file string) string\n```\nCanonicalPath is the single path canonicalisation shared by the finding fingerprint and SymbolFor: backslashes become slashes (on every platform, so a Windows-built path hashes like its POSIX twin) and any number of leading \"./\" prefixes are removed.\n\n```go\nfunc EstimateTokens(s string) int\n```\nEstimateTokens returns a rough token count from byte length using the chars/4 heuristic. Backed by the shared internal/tokens helper so providers, compress, and the diff layer never drift apart. Provider-side exact tokenizers can replace this when they're cheap to call from outside the provider's process.\n\n## Types\n\n```go\ntype Diff struct {\n\tFiles  []FileDiff\n\tOrigin git.Origin\n\tArgs   map[string]string\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc DropPaths(d Diff, files, dirs []string) (Diff, error)\n```\nDropPaths is the inverse of KeepPaths: it removes every file matching the supplied denylists (\\`--exclude-file\\` / \\`--exclude-dir\\`) and keeps the rest. Pattern semantics are identical — same literal/glob bucketing, same gitignore matcher, same union across files, dirs and globs — so a pattern that would select a file under KeepPaths removes exactly that file here.\n\nBoth empty → no filtering (returns d unchanged). Applied after KeepPaths, so exclusion wins: \\`--dir internal --exclude-dir internal/cli\\` reviews everything under internal/ except internal/cli.\n\nAn invalid glob returns a non-nil error, exactly as KeepPaths does — an unusable denylist must not silently pass every file through.\n\n```go\nfunc Filter(d Diff, m *ignore.Matcher) Diff\n```\n\n```go\nfunc KeepPaths(d Diff, files, dirs []string) (Diff, error)\n```\nKeepPaths narrows a Diff to only files matching the supplied allowlists. The inputs are split into a fast literal path and a glob path so existing literal usage keeps its O(1) / prefix behavior byte-for-byte (the backward-compatibility guarantee, ADR-0026).\n\n  - Literal files (no glob metacharacter) → exact-path allowlist (post-change Path or OldPath equality, slash-normalized).\n  - Literal dirs (no glob metacharacter) → prefix allowlist; a file is kept when its path starts with \\`\\\u003cdir\u003e/\\` for any entry (trailing slash on the input is stripped, then re-added by the matcher so prefixes stay strict on path-segment boundaries).\n  - Glob entries (file ∪ dir containing \\`\\*\\`, \\`?\\`, or \\`\\[\\`) → compiled gitignore patterns. A slash-less pattern matches the basename at any depth (\\`\\*.go\\` → \\`internal/x.go\\`); a slash-bearing pattern is root-anchored (\\`internal/\\*\\*/\\*.ts\\`). Reuses go-git's gitignore matcher (already a dependency via internal/ignore) → zero new deps.\n\nBoth empty → no filtering (returns d unchanged). When at least one input is non-empty, union semantics apply: a file is kept if it matches ANY literal file OR sits under ANY literal dir OR matches ANY glob.\n\nAn invalid glob pattern returns a non-nil error rather than silently mis-filtering (e.g. dropping every file). Paths are compared after \\`filepath.ToSlash\\` normalization on both sides so a user passing \\`app\\\\Models\\` on Windows still matches \\`app/Models/User.go\\`.\n\n```go\nfunc Parse(input git.Diff) (Diff, error)\n```\n\n```go\nfunc (d Diff) AddedLines() int\n```\nAddedLines is the count of \\`+\\` lines across all hunks in all files. O(1) — the value is computed once in Parse / Filter / KeepPaths and memoized in the struct.\n\n```go\nfunc (d Diff) Anchors() map[string]FileAnchors\n```\nAnchors builds a per-file index of postable comment positions. Keyed by the new-file path (FileDiff.Path), falling back to OldPath for pure deletions where Path is empty — the same key submitPRReview looks up with a finding's File.\n\n```go\nfunc (d Diff) DeletedLines() int\n```\nDeletedLines is the count of \\`-\\` lines across all hunks in all files. Memoized like AddedLines.\n\n```go\nfunc (d Diff) Empty() bool\n```\n\n```go\nfunc (d Diff) EstimateTokens() int\n```\n\n```go\nfunc (d Diff) FileCount() int\n```\n\n```go\nfunc (d Diff) NumberedString() string\n```\nNumberedString renders the diff like String, but prefixes every hunk line with the line number a GitHub inline comment would anchor to: the new-file number for added and context lines, the old-file number for removed lines. The format is \\`\\\u003cn\u003e| \\\u003cmarker\u003e\\\u003ctext\u003e\\` (marker is \\`+\\`/\\`-\\`/space). LLMs count poorly across long hunks and tend to echo the \\`@@\\` header's start line; handing them the number per line turns the \\`line\\` field of a finding from an estimate into a copy. The cache key still keys off String() — NumberedString is a deterministic function of the same diff, so it carries no extra cache identity.\n\n```go\nfunc (d Diff) String() string\n```\n\n```go\nfunc (d Diff) SymbolFor(file string, line int) string\n```\nSymbolFor returns the normalised enclosing-symbol text for a finding at (file, line) — the stable anchor of a finding fingerprint (ADR-0045).\n\nThe finding's line is looked up in the diff, right (post-image) side first and then the left (pre-image) side, mirroring the anchor order used for inline comments. Inside the covering hunk the nearest preceding line that satisfies git's default funcname rule wins; when the hunk body has none the raw hunk header is used if it satisfies the same rule. Everything else — a line \\\u003c= 0, a file or line outside the diff, no qualifying line — yields \"\". The result is run through normaliseSymbol.\n\nWhen the same path appears in several file sections (a commit-walk diff is a concatenation of per-commit patches) the first matching section in diff order is used.\n\n```go\ntype FileAnchors struct {\n\t// contains filtered or unexported fields\n}\n```\nFileAnchors indexes the line numbers in one file that a GitHub inline review comment can legally attach to. GitHub accepts a comment on any line that appears in the PR diff: on the RIGHT side that is every context and added line (numbered in the new file); on the LEFT side every context and removed line (numbered in the old file). Indexing both lets a finding be pinned to the side it actually lives on instead of unconditionally guessing RIGHT — and lets a finding whose line is outside the diff be detected before the POST 422s.\n\n```go\nfunc (fa FileAnchors) Has(side string, line int) bool\n```\nHas reports whether line is a postable position on the given side.\n\n```go\nfunc (fa FileAnchors) Resolve(line int, preferLeft bool) (side string, ok bool)\n```\nResolve maps a finding's reported line number to a postable GitHub comment side. preferLeft flips the lookup order so a line valid on both sides resolves to LEFT — used for findings about removed code. ok is false when the line matches no postable position; the caller must not POST it (it would 422) and should fall back to the review summary instead of dropping it.\n\n```go\ntype FileDiff struct {\n\tPath    string\n\tOldPath string\n\tMode    Mode\n\tHunks   []Hunk\n\tBinary  bool\n\n\t// PathParts is Path pre-split on `/`, populated once during\n\t// parseDiffHeader. The ignore-matcher backend takes `[]string`\n\t// parts; without this cache every Match call would re-split.\n\t// On a 500-file diff with two filter layers that's 1000+\n\t// redundant allocations.\n\tPathParts []string\n\t// OldPathParts mirrors PathParts for renamed/deleted entries.\n\tOldPathParts []string\n}\n```\n\n```go\nfunc (f FileDiff) String() string\n```\n\n```go\ntype Hunk struct {\n\tOldStart int\n\tOldLines int\n\tNewStart int\n\tNewLines int\n\t// Header is the trimmed function-context text after the closing \"@@\"\n\t// of the hunk header line.\n\tHeader string\n\t// HeaderRaw is the same text with only the single separator space\n\t// removed and nothing else trimmed, so a leading indent survives. Git's\n\t// default funcname rule only accepts a line whose first byte is a\n\t// letter, '_' or '$'; an indented header is therefore a git artefact\n\t// (a custom diff driver) and must not be mistaken for a symbol.\n\t// Symbol detection (SymbolFor) reads this field, never Header.\n\tHeaderRaw string\n\tLines     []HunkLine\n}\n```\n\n```go\nfunc (h Hunk) String() string\n```\n\n```go\ntype HunkLine struct {\n\tKind LineKind\n\tText string\n}\n```\n\n```go\ntype LineKind int\n```\n\n```go\nconst (\n\tLineContext LineKind = iota\n\tLineAdd\n\tLineDel\n)\n```\n\n```go\ntype Mode int\n```\n\n```go\nconst (\n\tModeModified Mode = iota\n\tModeAdded\n\tModeDeleted\n\tModeRenamed\n\tModeCopied\n)\n```\n\n```go\nfunc (m Mode) String() string\n```\n\n","path":"github.com/CommitBrief/commitbrief/internal/diff","name":"diff","synopsis":"","isRedistributable":true}
