Documentation
¶
Overview ¶
Package githooks provides utilities shared between the mdsmith CLI and the git-hook-sync rule for managing the pre-merge-commit hook, merge-driver assignments in .gitattributes, and discovery of files that contain generated-section directives.
Index ¶
- Constants
- func DiscoverFiles(repoRoot string, maxBytes int64) []string
- func DiscoverFilesForInstall(repoRoot string, maxBytes int64) []string
- func EnableRuleSnippet(ruleName string) string
- func ExtractGitattributesFiles(content string) []string
- func ExtractHookFiles(content string) []string
- func FilesMatch(a, b []string) bool
- func GitRepoRoot(dir string) (string, error)
- func HasMdsmithMergeDriver(repoRoot string) bool
- func NormalizeManagedPath(repoRoot, p string) (string, error)
- func NormalizeManagedPaths(repoRoot string, paths []string) ([]string, error)
- func ResolveHooksDir(repoRoot string) string
Constants ¶
const PreMergeCommitMarker = "# mdsmith merge-driver pre-merge-commit hook"
PreMergeCommitMarker is the comment line written into the pre-merge-commit hook so that mdsmith (and the git-hook-sync rule) can recognise hooks it manages without stomping on user-authored hooks of the same name.
Variables ¶
This section is empty.
Functions ¶
func DiscoverFiles ¶
DiscoverFiles scans repoRoot for Markdown files containing a generated-section directive (catalog, include, toc, …). Returned paths are relative to repoRoot and use forward-slash separators on every platform so they compare correctly against entries written into .gitattributes and the pre-merge-commit hook.
Hidden directories (names starting with ".") are skipped. The returned slice is sorted and may be empty: the caller decides whether to apply a fallback (the install commands do; the git-hook-sync rule does not).
func DiscoverFilesForInstall ¶
DiscoverFilesForInstall is the install-time variant of DiscoverFiles that supplies a sensible default file list when the repository has no directive-bearing files. It returns ["PLAN.md", "README.md"] in that case so a fresh repo still gets a useful hook/.gitattributes configuration after `mdsmith merge-driver install` or `mdsmith pre-merge-commit install`.
The git-hook-sync rule must not use this variant: when the user has no directive-bearing files, the rule should report nothing rather than reference fictional PLAN.md/README.md paths.
func EnableRuleSnippet ¶
EnableRuleSnippet returns the YAML the user can paste into .mdsmith.yml to enable the given rule. mdsmith never rewrites the user's config file automatically; the snippet is printed instead.
func ExtractGitattributesFiles ¶
ExtractGitattributesFiles returns the list of paths assigned to the mdsmith merge driver in .gitattributes content. Each entry is the pathname token from a line of the form `<pathname> merge=mdsmith`. Comment lines (`#`) and lines without a `merge=mdsmith` attribute are ignored.
The parser splits on whitespace, so it does not support pathnames that themselves contain whitespace. NormalizeManagedPath rejects such paths at install time so the installer and the drift checker stay consistent.
func ExtractHookFiles ¶
ExtractHookFiles parses a pre-merge-commit hook script and returns the list of files it invokes `mdsmith fix --` on. Files appear in the order they occur in the hook. Each `fix --` line contributes at most one entry: the first single-quoted token that follows. Comment and blank lines are skipped so a commented-out example or note in the hook does not produce a false managed-file entry.
func FilesMatch ¶
FilesMatch reports whether a and b contain the same set of files, ignoring order and duplicates. A repeated entry on either side is treated the same as a single occurrence so that a `.gitattributes` or hook script that lists the same path twice still compares equal to a deduplicated list.
func GitRepoRoot ¶
GitRepoRoot returns the absolute path of the git repository that contains dir. The lookup runs `git -C dir rev-parse --show-toplevel` so it works correctly when invoked from any subdirectory or when linting an absolute path outside the process working directory.
func HasMdsmithMergeDriver ¶
HasMdsmithMergeDriver reports whether the repository's local git config defines `merge.mdsmith.driver` (i.e. the merge driver itself has been registered for this repo). The lookup is scoped to the repo's local config (`--local`), not global/system config, so a user with a personal merge driver elsewhere cannot accidentally opt every clone into MDS048's drift checks. A missing driver is reported as false rather than as an error so callers can treat the merge-driver setup as "not installed".
func NormalizeManagedPath ¶
NormalizeManagedPath converts p (which may be absolute, relative, or use OS-specific separators) into the canonical form used in .gitattributes and the pre-merge-commit hook: a non-empty repo-relative path with forward-slash separators that does not escape repoRoot.
Whitespace inside the *resulting* repo-relative path is rejected because .gitattributes splits attributes on whitespace and the rule's Fields-based parser cannot recover the original token. The check runs after Rel/ToSlash so an absolute input rooted at a repo whose own path contains whitespace (e.g. a Windows or macOS home dir with spaces) is still accepted, as long as the repo-relative tail is whitespace-free.
Glob and pathspec metacharacters (`*`, `?`, `[`) are also rejected. The install commands write each managed entry into a `[ -e <path> ]` guard inside the pre-merge-commit hook script, and `[ -e ]` treats its argument as a literal filename rather than a glob, so a pattern like `docs/*.md` would always be skipped even when files match. The drift checker likewise compares exact paths.
func NormalizeManagedPaths ¶
NormalizeManagedPaths normalizes each entry via NormalizeManagedPath. It returns the first error encountered, so callers can surface a single clear message rather than a list of failures.
func ResolveHooksDir ¶
ResolveHooksDir returns the directory where git hooks live for the repository at repoRoot. It uses `git rev-parse --git-path hooks` so that worktrees, submodules, and core.hooksPath all resolve correctly. Falls back to <repoRoot>/.git/hooks when git cannot be queried.
Types ¶
This section is empty.