Last active
November 1, 2025 18:54
-
-
Save nberlette/222b756bd669fa8f4336db337aa1df05 to your computer and use it in GitHub Desktop.
pre-commit hook to lint + fmt staged files with deno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # pre-commit - deno fmt + deno lint | |
| # --------------------------------------------------------------------- | |
| # Copyright (c) 2025 Nicholas Berlette. All rights reserved. | |
| # Published under the MIT License (https://nick.mit-license.org). | |
| # --------------------------------------------------------------------- | |
| # Runs deno's formatter and linter on staged files before committing. | |
| # | |
| # Place this file in your .git/hooks/ directory to enable pre-commit checks. | |
| # | |
| # Configuration options can be set using git config: | |
| # | |
| # ```gitconfig | |
| # [hooks] | |
| # allownonascii = false # Set to true to allow non-ASCII filenames | |
| # [hooks "fmt"] | |
| # check = true # Set to false to auto-format staged files | |
| # disabled = false # Set to true to disable formatting checks | |
| # patterns = ["*.ts", "*.js"] # File patterns to format (overrides default) | |
| # [hooks "lint"] | |
| # fix = false # Set to true to auto-fix linting issues | |
| # disabled = false # Set to true to disable linting checks | |
| # patterns = ["*.ts", "*.js"] # File patterns to lint (overrides default) | |
| # [hooks "pre-commit"] | |
| # fmt = "deno fmt --check" # Command to run (or true / false) | |
| # lint = "deno lint" # Command to run (or true / false) | |
| # ``` | |
| # | |
| # Note: The default settings of this hook require Deno to be installed, but | |
| # you can easily customize the commands used for formatting and linting to | |
| # adapt it to your project's needs. | |
| # | |
| # For example, to adapt this for a Rust codebase, these configs could be set: | |
| # | |
| # ```sh | |
| # git config hooks.fmt.patterns "*.rs" | |
| # git config hooks.lint.patterns "*.rs" | |
| # git config hooks.pre-commit.fmt "cargo fmt -- --check" | |
| # git config hooks.pre-commit.lint "cargo clippy -- -D warnings" | |
| # ``` | |
| # An example hook script to verify what is about to be committed. | |
| # Called by "git commit" with no arguments. The hook should | |
| # exit with non-zero status after issuing an appropriate message if | |
| # it wants to stop the commit. | |
| function verify_ascii() { | |
| if git rev-parse --verify HEAD >/dev/null 2>&1; then | |
| against=HEAD | |
| else | |
| # Initial commit: diff against an empty tree object | |
| against=$(git hash-object -t tree /dev/null) | |
| fi | |
| # If you want to allow non-ASCII filenames set this variable to true. | |
| allownonascii=$(git config --type=bool hooks.allownonascii) | |
| # Redirect output to stderr. | |
| exec 1>&2 | |
| # Cross platform projects tend to avoid non-ASCII filenames; prevent | |
| # them from being added to the repository. We exploit the fact that the | |
| # printable range starts at the space character and ends with tilde. | |
| if [ "$allownonascii" != "true" ] && | |
| # Note that the use of brackets around a tr range is ok here, (it's | |
| # even required, for portability to Solaris 10's /usr/bin/tr), since | |
| # the square bracket bytes happen to fall in the designated range. | |
| test $(git diff-index --cached --name-only --diff-filter=A -z $against | | |
| LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 | |
| then | |
| cat <<-๐ | |
| Error: Attempt to add a non-ASCII file name. | |
| This can cause problems if you want to work with people on other platforms. | |
| To be portable it is advisable to rename the file. | |
| If you know what you are doing you can disable this check using: | |
| git config hooks.allownonascii true | |
| ๐ | |
| exit 1 | |
| fi | |
| # If there are whitespace errors, print the offending file names and fail. | |
| exec git diff-index --check --cached $against -- | |
| } | |
| DEFAULT_FMT_LINT_PATTERNS=("${DEFAULT_FMT_LINT_PATTERNS[@]:-}") | |
| if [ "${#DEFAULT_FMT_LINT_PATTERNS[@]}" -eq 0 ]; then | |
| DEFAULT_FMT_LINT_PATTERNS=( | |
| '*.ts' '*.tsx' '*.mts' '*.cts' | |
| '*.js' '*.jsx' '*.mjs' '*.cjs' | |
| '*.json' '*.jsonc' '*.jsonl' '*.yaml' '*.yml' '*.toml' '*.md' | |
| '*.css' '*.scss' '*.sass' '*.less' | |
| '*.html' '*.njk' '*.vto' '*.hbs' '*.svelte' '*.astro' '*.vue' | |
| '*.ipynb' '*.sql' | |
| ) | |
| fi | |
| function ensure_deno_installed() { | |
| if ! command -v deno >/dev/null 2>&1; then | |
| export DENO_INSTALL="${DENO_INSTALL:-$HOME/.deno}" | |
| export PATH="$DENO_INSTALL/bin:$PATH" | |
| curl -fsSL https://deno.land/install.sh | sh | |
| fi | |
| if ! command -v deno >/dev/null 2>&1; then | |
| echo -n $'\e[1;31merror\e[0m Deno is not installed. Please install Deno to use this hook.\n' >&2 | |
| exit 1 | |
| fi | |
| } | |
| function fmt_staged() { | |
| # Run deno's formatter on staged files | |
| local disabled=$(git config --get --type bool hooks.fmt.disabled 2>/dev/null) | |
| [ "$disabled" -eq "true" ] && return 0 | |
| local check=$(git config --get --type bool hooks.fmt.check 2>/dev/null || echo -n "true") | |
| [ "$check" -eq "false" ] && check="" | |
| local program=$(git config --get hooks.pre-commit.fmt 2>/dev/null || echo -n "true") | |
| [ "$program" -eq "false" ] && return 0 | |
| [ "$program" -eq "true" ] && program="deno fmt${check:+ --check}" | |
| [[ "$program" == *"deno"* ]] && ensure_deno_installed | |
| local -a pats=($(git config --get-all hooks.fmt.patterns 2>/dev/null)) | |
| if [ ${#pats[@]} -eq 0 ]; then | |
| pats=("${DEFAULT_FMT_LINT_PATTERNS[@]}") | |
| fi | |
| local -a files=($(git diff --cached --name-only -- "${pats[@]}")) | |
| if [ ${#files[@]} -ne 0 ]; then | |
| local message=$'\e[1;31merror\e[0m Formatting issues detected. Commit aborted!\n' | |
| if [[ "$check" == "true" ]]; then | |
| message+=$'\n\e[1;95mhint:\e[0m \e[3mtry setting\e[1;4:4;34;58;5;137mhooks.fmt.check\e[0;3m to \e[92m\'false\'\e[39m to auto-format before committing.\e[0m\n' | |
| fi | |
| $program "${files[@]}" >&2 | |
| if [ $? -ne 0 ]; then | |
| echo -n "${message-}" >&2 | |
| return 1 | |
| fi | |
| # Re-add potentially modified files to the staging area | |
| git add "${files[@]}" | |
| fi | |
| } | |
| function lint_staged() { | |
| # Run deno's linter on staged files | |
| local disabled=$(git config --get --type bool hooks.lint.disabled 2>/dev/null || echo -n "false") | |
| [ "$disabled" -eq "true" ] && return 0 | |
| local fix=$(git config --get --type bool hooks.lint.fix 2>/dev/null || echo -n "false") | |
| [ "$fix" -eq "false" ] && fix="" | |
| local program=$(git config --get hooks.pre-commit.lint 2>/dev/null || echo -n "true") | |
| [ "$program" -eq "false" ] && return 0 | |
| [ "$program" -eq "true" ] && program="deno lint${fix:+ --fix}" | |
| [[ "$program" == *"deno"* ]] && ensure_deno_installed | |
| local -a pats=($(git config --get-all hooks.lint.patterns 2>/dev/null)) | |
| if [ ${#pats[@]} -eq 0 ]; then | |
| pats=("${DEFAULT_FMT_LINT_PATTERNS[@]}") | |
| fi | |
| local -a files=($(git diff --cached --name-only -- "${pats[@]}")) | |
| if [ ${#files[@]} -ne 0 ]; then | |
| local message=$'\e[1;31merror\e[0m Linting issues detected. Commit aborted!\n' | |
| if [ -z "$fix" ]; then | |
| message+=$'\n\e[1;95mhint:\e[0m \e[3mtry setting\e[1;4:4;34;58;5;137mhooks.lint.fix\e[0;3m to \e[92m\'true\'\e[39m to enable auto-fixing.\e[0m\n' | |
| fi | |
| $program "${files[@]}" >&2 | |
| if [ $? -ne 0 ]; then | |
| echo -n "${message-}" >&2 | |
| return 1 | |
| fi | |
| # Re-add potentially modified files to the staging area | |
| git add "${files[@]}" | |
| fi | |
| } | |
| verify_ascii && fmt_staged && lint_staged && exit 0 | |
| exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment