package filediff import ( "crypto/sha256" "fmt" "io/fs" "path" "strings" ) // Policy no-patch reasons: the change is listed, completely, without a patch // by design rather than because a capture limit ran out, so it never makes a // capture partial. const ( reasonSymlink = "symlink" // a symbolic link; its target is compared reasonLarge = "large" // over the hash bound; compared by size, mtime and mode reasonGenerated = "generated" // a lockfile, minified bundle or source map ) // cacheDirs are tool caches, virtual environments and dependency trees. Their // untracked files are left out of the inventory entirely (never "skipped"): // they are regenerated by tools, not authored by a stage. Tracked files below // them are still compared, without a patch. var cacheDirs = map[string]bool{ "node_modules": true, "bower_components": true, ".git": true, "__pycache__": true, ".venv": true, "venv": true, ".tox": true, ".nox": true, ".mypy_cache": true, ".pytest_cache": true, ".ruff_cache": true, ".hypothesis": true, ".eggs": true, ".next": true, ".nuxt": true, ".svelte-kit": true, ".turbo": true, ".parcel-cache": true, ".pnpm-store": true, ".dart_tool": true, ".gradle": true, ".terraform": true, } // buildDirs are conventional build output directories. Untracked files below // one are left out only when Git tracks nothing there, so a new source file // in a real `build/` source directory is still captured. var buildDirs = map[string]bool{"build": true, "dist": true, "out": true, "target": true, "coverage": true} // generatedNames are lockfiles: tool output whose diff is noise. var generatedNames = map[string]bool{ "package-lock.json": true, "npm-shrinkwrap.json": true, "yarn.lock": true, "pnpm-lock.yaml": true, "bun.lockb": true, "cargo.lock": true, "go.sum": true, "poetry.lock": true, "uv.lock": true, "pipfile.lock": true, "gemfile.lock": true, "composer.lock": true, "pubspec.lock": true, "flake.lock": true, "mix.lock": true, "podfile.lock": true, "packages.lock.json": true, } // inCacheDir reports whether any directory component of name is a cache dir. func inCacheDir(name string) bool { parts := strings.Split(name, "/") for _, part := range parts[:len(parts)-1] { if cacheDirs[part] { return true } } return false } // generatedFile reports a file whose diff is tool output: a lockfile, a // minified bundle, a source map, compiled Python, or anything below a cache // directory. func generatedFile(name string) bool { base := strings.ToLower(path.Base(name)) if generatedNames[base] || strings.HasSuffix(base, ".min.js") || strings.HasSuffix(base, ".min.css") || strings.HasSuffix(base, ".map") || strings.HasSuffix(base, ".pyc") { return true } return inCacheDir(name) } // excludedUntracked reports an untracked path that is tool output rather than // stage work: below a cache directory, compiled Python, or below a build // directory where Git tracks nothing (trackedDirs holds every directory that // contains a tracked file). func excludedUntracked(name string, trackedDirs map[string]bool) bool { if inCacheDir(name) || strings.HasSuffix(name, ".pyc") { return true } parts := strings.Split(name, "/") for i, part := range parts[:len(parts)-1] { if buildDirs[part] && !trackedDirs[strings.Join(parts[:i+1], "/")] { return true } } return false } // trackedDirectories returns every ancestor directory of the tracked names. func trackedDirectories(names []string) map[string]bool { dirs := map[string]bool{} for _, name := range names { for dir := path.Dir(name); dir != "." && dir != "/" && !dirs[dir]; dir = path.Dir(dir) { dirs[dir] = true } } return dirs } // policyReason is the policy no-patch reason of a changed path, preferring // the stage-end side (a file that became a symlink is a symlink change). func policyReason(a file, aOK bool, b file, bOK bool) string { if bOK && b.reason != "" { return b.reason } if aOK && a.reason != "" { return a.reason } return "" } // metaHash fingerprints a file by its metadata alone, without reading it: a // protected (secret) file is never opened, and a huge file is not hashed. func metaHash(info fs.FileInfo) [32]byte { return sha256.Sum256(fmt.Appendf(nil, "meta\x00%d\x00%d\x00%o", info.Size(), info.ModTime().UnixNano(), info.Mode())) } // linkHash fingerprints a symbolic link by its target. func linkHash(target string) [32]byte { return sha256.Sum256([]byte("symlink\x00" + target)) }