Skip to content

Instantly share code, notes, and snippets.

@cn-ml
Created May 25, 2023 12:35
Show Gist options
  • Save cn-ml/6221e6c684e0a9795207be5523bae346 to your computer and use it in GitHub Desktop.
Save cn-ml/6221e6c684e0a9795207be5523bae346 to your computer and use it in GitHub Desktop.
Generate the pre-commit schema file dyamically.
from __future__ import annotations
from enum import Enum
from typing import Literal, Optional, Type, TypeAlias, get_args
from pydantic import BaseModel, Extra, Field
from identify.identify import ALL_TAGS
IdentifyTypes = Enum("IdentifyTypes", {tag: tag for tag in ALL_TAGS})
IdentifyTypesLiteral: TypeAlias = Literal["gherkin", "zsh", "adobe-illustrator", "markdown", "gitmodules", "edn", "dart", "bazel", "gitlint", "mention-bot", "terraform", "dockerignore", "dtd", "xquery", "ino", "php8", "makefile", "ts", "idris", "audio", "woff2", "inc", "c++", "verilog", "yaml", "ttf", "geojson", "system-verilog", "python3", "yamlld", "scss", "txsprofile", "eps", "png", "gitconfig", "sln", "pem", "ngdoc", "rust", "haskell", "awk", "m4", "bazelrc", "spec", "c2hs", "purescript", "flake8", "ocaml", "vim", "nunjucks", "nimble", "vdx", "less", "groovy", "aspectj", "toml", "css", "def", "objective-c", "jsonnet", "gitignore", "babelrc", "wkt", "plantuml", "vue", "bib", "swf", "xhtml", "browserslistrc", "text", "pkgbuild", "myst", "jade", "vb", "gpx", "executable", "csv", "file", "idl", "mailmap", "clojurescript", "gdscript", "scheme", "manifest", "npmignore", "sass", "isort", "graphql", "pyi", "shell", "directory", "yang", "icns", "json5", "jupyter", "fish", "ppm", "modulemap", "ruby", "php", "cylc", "svg", "p12", "powershell", "tiff", "python", "jpeg", "icalendar", "erlang", "sbt", "vcxproj", "ash", "wav", "apinotes", "thrift", "cuda", "lean", "urdf", "prisma", "scala", "sql", "otf", "pom", "html", "alpm", "bats", "go-mod", "asm", "xml", "csslintrc", "julia", "tcsh", "eot", "cython", "json", "tex", "lektorproject", "woff", "gyp", "cue", "tsv", "xsl", "ini", "gif", "bitmap", "coveragerc", "go-sum", "literate-haskell", "jinja", "socket", "coffee", "vbproj", "resx", "pofile", "csproj", "lua", "meson", "twig", "pyproj", "rst", "swiftdeps", "perl", "symlink", "image", "bowerrc", "jsonld", "webp", "lazarus-form", "inl", "zip", "swift", "sh", "non-executable", "binary", "stylus", "mustache", "python2", "java-properties", "mdx", "yamllint", "go", "r", "kotlin", "gotmpl", "jshintrc", "dockerfile", "wsgi", "bash", "twisted", "diff", "crystal", "zpt", "pyz", "hgrc", "pascal", "dotenv", "map", "ksh", "jenkins", "asciidoc", "vhdl", "jar", "nix", "dbc", "plain-text", "lazarus", "beancount", "c#", "yin", "handlebars", "cmake", "xsd", "clojure", "pdbrc", "musescore", "cson", "inx", "fits", "codespellrc", "objective-c++", "batch", "mib", "pypirc", "proto", "pdf", "kml", "pug", "java", "wheel", "prettierignore", "plist", "hcl", "asar", "zig", "gyb", "svelte", "avif", "expect", "textproto", "cbsd", "erb", "csh", "c", "nim", "qml", "gzip", "solidity", "tar", "dash", "fortran", "tsx", "pylintrc", "ejs", "header", "vtl", "php7", "salt", "editorconfig", "relax-ng", "jsx", "javascript", "liquid", "gitattributes", "zcml", "lektor", "puppet", "bzip2", "icon"]
AllIdentifyTypes = set(get_args(IdentifyTypesLiteral))
assert AllIdentifyTypes == ALL_TAGS, "IdentifyTypes is not up to date"
class LanguageVersions(BaseModel):
conda: Optional[str] = Field(None, description="Language version to use for Conda")
coursier: Optional[str] = Field(None, description="Language version to use for Coursier")
dart: Optional[str] = Field(None, description="Language version to use for Dart")
docker: Optional[str] = Field(None, description="Language version to use for Docker")
docker_image: Optional[str] = Field(None, description="Language version to use for Docker Images")
dotnet: Optional[str] = Field(None, description="Language version to use for Dotnet")
fail: Optional[str] = Field(None, description="Language version to use for Fail")
golang: Optional[str] = Field(None, description="Language version to use for Golang")
lua: Optional[str] = Field(None, description="Language version to use for Lua")
node: Optional[str] = Field(None, description="Language version to use for Node")
perl: Optional[str] = Field(None, description="Language version to use for Perl")
python: Optional[str] = Field(None, description="Language version to use for Python")
python_venv: Optional[str] = Field(None, description="Language version to use for Python Venv")
r: Optional[str] = Field(None, description="Language version to use for R")
ruby: Optional[str] = Field(None, description="Language version to use for Ruby")
rust: Optional[str] = Field(None, description="Language version to use for Rust")
swift: Optional[str] = Field(None, description="Language version to use for Swift")
pygrep: Optional[str] = Field(None, description="Language version to use for Pygrep")
script: Optional[str] = Field(None, description="Language version to use for Script")
system: Optional[str] = Field(None, description="Language version to use for System")
class HookType(str, Enum):
CommitMsg = "commit-msg"
PostCheckout = "post-checkout"
PostCommit = "post-commit"
PostMerge = "post-merge"
PostRewrite = "post-rewrite"
PreCommit = "pre-commit"
PreMergeCommit = "pre-merge-commit"
PrePush = "pre-push"
PreRebase = "pre-rebase"
PrepareCommitMsg = "prepare-commit-msg"
class PreCommitConfig(BaseModel):
"""
The pre-commit config file describes what repositories and hooks are installed.
"""
repos: list[Repo] = Field(description="a list of repository mappings.")
default_install_hook_types: Optional[set[HookType]] = Field(None, description="a list of --hook-types which will be used by default when running pre-commit install.")
default_language_version: Optional[LanguageVersions] = Field(None, description="a mapping from language to the default language_version that should be used for that language. This will only override individual hooks that do not set language_version.")
default_stages: Optional[set[HookType]] = Field(None, description="a configuration-wide default for the stages property of hooks. This will only override individual hooks that do not set stages.")
files: Optional[str] = Field(None, description="global file include pattern.")
exclude: Optional[str] = Field(None, description="global file exclude pattern.")
fail_fast: Optional[bool] = Field(None, description="set to true to have pre-commit stop running hooks after the first failure.")
minimum_pre_commit_version: Optional[str] = Field(None, description="require a minimum version of pre-commit.")
class Hook(BaseModel):
id: str = Field(description="which hook from the repository to use.")
alias: Optional[str] = Field(description="allows the hook to be referenced using an additional id when using pre-commit run <hookid>.")
name: Optional[str] = Field(description="override the name of the hook - shown during hook execution.")
language_version: Optional[str] = Field(description="override the language version for the hook. See Overriding Language Version.")
files: Optional[str] = Field(description="override the default pattern for files to run on.")
exclude: Optional[str] = Field(description="file exclude pattern.")
types: Optional[set[IdentifyTypes]] = Field(description="override the default file types to run on (AND). See Filtering files with types.")
types_or: Optional[set[IdentifyTypes]] = Field(description="override the default file types to run on (OR). See Filtering files with types. new in 2.9.0.")
exclude_types: Optional[set[IdentifyTypes]] = Field(description="file types to exclude.")
args: Optional[list[str]] = Field(description="list of additional parameters to pass to the hook.")
stages: Optional[set[HookType]] = Field(description="selects which git hook(s) to run for. See Confining hooks to run at certain stages.")
additional_dependencies: Optional[list[str]] = Field(description="a list of dependencies that will be installed in the environment where this hook gets run. One useful application is to install plugins for hooks such as eslint.")
always_run: Optional[bool] = Field(description="if true, this hook will run even if there are no matching files.")
verbose: Optional[bool] = Field(description="if true, forces the output of the hook to be printed even when the hook passes.")
log_file: Optional[str] = Field(description="if present, the hook output will additionally be written to a file when the hook fails or verbose is true.")
class Repo(BaseModel):
repo: str = Field(description="the repository url to git clone from.")
rev: str = Field(description="the revision or tag to clone at.")
hooks: list[Hook] = Field(description="A list of hook mappings.")
def main():
PreCommitConfig.update_forward_refs()
print(PreCommitConfig.schema_json(indent=2))
if __name__ == "__main__":
main()
{
"title": "PreCommitConfig",
"description": "The pre-commit config file describes what repositories and hooks are installed.",
"type": "object",
"properties": {
"repos": {
"title": "Repos",
"description": "a list of repository mappings.",
"type": "array",
"items": {
"$ref": "#/definitions/Repo"
}
},
"default_install_hook_types": {
"description": "a list of --hook-types which will be used by default when running pre-commit install.",
"type": "array",
"items": {
"$ref": "#/definitions/HookType"
},
"uniqueItems": true
},
"default_language_version": {
"title": "Default Language Version",
"description": "a mapping from language to the default language_version that should be used for that language. This will only override individual hooks that do not set language_version.",
"allOf": [
{
"$ref": "#/definitions/LanguageVersions"
}
]
},
"default_stages": {
"description": "a configuration-wide default for the stages property of hooks. This will only override individual hooks that do not set stages.",
"type": "array",
"items": {
"$ref": "#/definitions/HookType"
},
"uniqueItems": true
},
"files": {
"title": "Files",
"description": "global file include pattern.",
"type": "string"
},
"exclude": {
"title": "Exclude",
"description": "global file exclude pattern.",
"type": "string"
},
"fail_fast": {
"title": "Fail Fast",
"description": "set to true to have pre-commit stop running hooks after the first failure.",
"type": "boolean"
},
"minimum_pre_commit_version": {
"title": "Minimum Pre Commit Version",
"description": "require a minimum version of pre-commit.",
"type": "string"
}
},
"required": [
"repos"
],
"definitions": {
"IdentifyTypes": {
"title": "IdentifyTypes",
"description": "An enumeration.",
"enum": [
"wkt",
"wheel",
"verilog",
"map",
"header",
"svelte",
"prettierignore",
"fish",
"purescript",
"inx",
"pkgbuild",
"bowerrc",
"isort",
"editorconfig",
"json5",
"go-sum",
"bazelrc",
"lektor",
"yin",
"qml",
"binary",
"perl",
"rust",
"xsl",
"bitmap",
"csh",
"gzip",
"json",
"edn",
"yamlld",
"c#",
"p12",
"vue",
"text",
"clojure",
"ttf",
"makefile",
"tar",
"xsd",
"lazarus-form",
"toml",
"ngdoc",
"png",
"gyp",
"pofile",
"wsgi",
"twisted",
"woff2",
"csslintrc",
"jshintrc",
"fortran",
"lazarus",
"mib",
"css",
"coffee",
"gherkin",
"idl",
"swift",
"meson",
"expect",
"def",
"lean",
"modulemap",
"vtl",
"xhtml",
"swiftdeps",
"handlebars",
"icon",
"sql",
"javascript",
"gitignore",
"image",
"ash",
"coveragerc",
"spec",
"erlang",
"bash",
"kotlin",
"stylus",
"pem",
"thrift",
"hgrc",
"dockerfile",
"erb",
"wav",
"zcml",
"kml",
"cbsd",
"csv",
"awk",
"nix",
"apinotes",
"tsx",
"zig",
"tiff",
"dash",
"system-verilog",
"ino",
"asar",
"ocaml",
"dockerignore",
"asciidoc",
"haskell",
"csproj",
"eot",
"woff",
"vim",
"cmake",
"cuda",
"lektorproject",
"gitmodules",
"python3",
"otf",
"nim",
"liquid",
"ppm",
"plain-text",
"bzip2",
"bats",
"sass",
"idris",
"ruby",
"audio",
"directory",
"html",
"icalendar",
"pyz",
"jsonld",
"graphql",
"inc",
"cylc",
"shell",
"jsx",
"pom",
"php7",
"bazel",
"scss",
"php",
"pdf",
"tex",
"webp",
"bib",
"non-executable",
"c",
"musescore",
"zsh",
"gitlint",
"pug",
"hcl",
"pypirc",
"geojson",
"c2hs",
"dtd",
"icns",
"jpeg",
"swf",
"eps",
"prisma",
"python",
"scheme",
"yamllint",
"pascal",
"objective-c",
"alpm",
"terraform",
"tsv",
"dart",
"gdscript",
"mention-bot",
"yang",
"adobe-illustrator",
"batch",
"powershell",
"cson",
"npmignore",
"gpx",
"python2",
"jsonnet",
"rst",
"pdbrc",
"diff",
"r",
"socket",
"go-mod",
"vbproj",
"c++",
"asm",
"salt",
"executable",
"solidity",
"ini",
"sln",
"vcxproj",
"groovy",
"ts",
"zpt",
"dbc",
"gyb",
"cue",
"pyproj",
"jupyter",
"vhdl",
"plist",
"urdf",
"pylintrc",
"vdx",
"xquery",
"m4",
"clojurescript",
"yaml",
"literate-haskell",
"codespellrc",
"jade",
"go",
"jenkins",
"browserslistrc",
"aspectj",
"tcsh",
"twig",
"sh",
"proto",
"zip",
"objective-c++",
"babelrc",
"gif",
"java-properties",
"nimble",
"gotmpl",
"scala",
"beancount",
"mdx",
"symlink",
"java",
"svg",
"flake8",
"puppet",
"textproto",
"xml",
"myst",
"resx",
"mailmap",
"gitconfig",
"gitattributes",
"ejs",
"vb",
"plantuml",
"fits",
"dotenv",
"sbt",
"manifest",
"ksh",
"file",
"jar",
"relax-ng",
"lua",
"pyi",
"markdown",
"nunjucks",
"avif",
"jinja",
"txsprofile",
"inl",
"cython",
"mustache",
"less",
"php8",
"crystal",
"julia"
]
},
"HookType": {
"title": "HookType",
"description": "An enumeration.",
"enum": [
"commit-msg",
"post-checkout",
"post-commit",
"post-merge",
"post-rewrite",
"pre-commit",
"pre-merge-commit",
"pre-push",
"pre-rebase",
"prepare-commit-msg"
],
"type": "string"
},
"Hook": {
"title": "Hook",
"type": "object",
"properties": {
"id": {
"title": "Id",
"description": "which hook from the repository to use.",
"type": "string"
},
"alias": {
"title": "Alias",
"description": "allows the hook to be referenced using an additional id when using pre-commit run <hookid>.",
"type": "string"
},
"name": {
"title": "Name",
"description": "override the name of the hook - shown during hook execution.",
"type": "string"
},
"language_version": {
"title": "Language Version",
"description": "override the language version for the hook. See Overriding Language Version.",
"type": "string"
},
"files": {
"title": "Files",
"description": "override the default pattern for files to run on.",
"type": "string"
},
"exclude": {
"title": "Exclude",
"description": "file exclude pattern.",
"type": "string"
},
"types": {
"description": "override the default file types to run on (AND). See Filtering files with types.",
"type": "array",
"items": {
"$ref": "#/definitions/IdentifyTypes"
},
"uniqueItems": true
},
"types_or": {
"description": "override the default file types to run on (OR). See Filtering files with types. new in 2.9.0.",
"type": "array",
"items": {
"$ref": "#/definitions/IdentifyTypes"
},
"uniqueItems": true
},
"exclude_types": {
"description": "file types to exclude.",
"type": "array",
"items": {
"$ref": "#/definitions/IdentifyTypes"
},
"uniqueItems": true
},
"args": {
"title": "Args",
"description": "list of additional parameters to pass to the hook.",
"type": "array",
"items": {
"type": "string"
}
},
"stages": {
"description": "selects which git hook(s) to run for. See Confining hooks to run at certain stages.",
"type": "array",
"items": {
"$ref": "#/definitions/HookType"
},
"uniqueItems": true
},
"additional_dependencies": {
"title": "Additional Dependencies",
"description": "a list of dependencies that will be installed in the environment where this hook gets run. One useful application is to install plugins for hooks such as eslint.",
"type": "array",
"items": {
"type": "string"
}
},
"always_run": {
"title": "Always Run",
"description": "if true, this hook will run even if there are no matching files.",
"type": "boolean"
},
"verbose": {
"title": "Verbose",
"description": "if true, forces the output of the hook to be printed even when the hook passes.",
"type": "boolean"
},
"log_file": {
"title": "Log File",
"description": "if present, the hook output will additionally be written to a file when the hook fails or verbose is true.",
"type": "string"
}
},
"required": [
"id"
]
},
"Repo": {
"title": "Repo",
"type": "object",
"properties": {
"repo": {
"title": "Repo",
"description": "the repository url to git clone from.",
"type": "string"
},
"rev": {
"title": "Rev",
"description": "the revision or tag to clone at.",
"type": "string"
},
"hooks": {
"title": "Hooks",
"description": "A list of hook mappings.",
"type": "array",
"items": {
"$ref": "#/definitions/Hook"
}
}
},
"required": [
"repo",
"rev",
"hooks"
]
},
"LanguageVersions": {
"title": "LanguageVersions",
"type": "object",
"properties": {
"conda": {
"title": "Conda",
"description": "Language version to use for Conda",
"type": "string"
},
"coursier": {
"title": "Coursier",
"description": "Language version to use for Coursier",
"type": "string"
},
"dart": {
"title": "Dart",
"description": "Language version to use for Dart",
"type": "string"
},
"docker": {
"title": "Docker",
"description": "Language version to use for Docker",
"type": "string"
},
"docker_image": {
"title": "Docker Image",
"description": "Language version to use for Docker Images",
"type": "string"
},
"dotnet": {
"title": "Dotnet",
"description": "Language version to use for Dotnet",
"type": "string"
},
"fail": {
"title": "Fail",
"description": "Language version to use for Fail",
"type": "string"
},
"golang": {
"title": "Golang",
"description": "Language version to use for Golang",
"type": "string"
},
"lua": {
"title": "Lua",
"description": "Language version to use for Lua",
"type": "string"
},
"node": {
"title": "Node",
"description": "Language version to use for Node",
"type": "string"
},
"perl": {
"title": "Perl",
"description": "Language version to use for Perl",
"type": "string"
},
"python": {
"title": "Python",
"description": "Language version to use for Python",
"type": "string"
},
"python_venv": {
"title": "Python Venv",
"description": "Language version to use for Python Venv",
"type": "string"
},
"r": {
"title": "R",
"description": "Language version to use for R",
"type": "string"
},
"ruby": {
"title": "Ruby",
"description": "Language version to use for Ruby",
"type": "string"
},
"rust": {
"title": "Rust",
"description": "Language version to use for Rust",
"type": "string"
},
"swift": {
"title": "Swift",
"description": "Language version to use for Swift",
"type": "string"
},
"pygrep": {
"title": "Pygrep",
"description": "Language version to use for Pygrep",
"type": "string"
},
"script": {
"title": "Script",
"description": "Language version to use for Script",
"type": "string"
},
"system": {
"title": "System",
"description": "Language version to use for System",
"type": "string"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment