-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·110 lines (91 loc) · 3.14 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·110 lines (91 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# shellcheck shell=bash
# =============================================================================
# bin/pre-commit
# Syncs pre-commit config from gautada/cicd and runs pre-commit --all-files.
#
# Usage:
# curl -sSfL https://raw.githubusercontent.com/gautada/cicd/main/bin/pre-commit | bash
# curl -sSfL https://raw.githubusercontent.com/gautada/cicd/main/bin/pre-commit | bash -s -- --pull-only
#
# Options:
# --pull-only Pull config files from gautada/cicd only; skip install and run.
#
# Requirements:
# - Run from the root of a git checkout
# - pre-commit must be installed (pipx install --global pre-commit)
# - curl must be available
# =============================================================================
set -euo pipefail
CICD_RAW="https://raw.githubusercontent.com/gautada/cicd/main/templates/pre-commit"
PULL_ONLY=false
# ---- Parse arguments --------------------------------------------------------
for arg in "$@"; do
case "$arg" in
--pull-only) PULL_ONLY=true ;;
*) echo "ERROR: Unknown argument: $arg" >&2; exit 1 ;;
esac
done
# Config files to pull from gautada/cicd:/templates/pre-commit/
CONFIG_FILES=(
".flake8"
".hadolint.yaml"
".htmlhintrc"
".jscpd.json"
".markdownlint.yaml"
".pre-commit-config.yaml"
".shellcheckrc"
".sqlfluff"
".yamllint.yaml"
)
# ---- Preflight checks -------------------------------------------------------
# Verify git repo
if ! git rev-parse --show-toplevel &>/dev/null; then
echo "ERROR: Not inside a git repository." >&2
exit 1
fi
REPO_ROOT=$(git rev-parse --show-toplevel)
if [[ "$PWD" != "$REPO_ROOT" ]]; then
echo "ERROR: Run from the root of the git checkout." >&2
echo " Expected: $REPO_ROOT" >&2
exit 1
fi
# Verify curl is available
if ! command -v curl &>/dev/null; then
echo "ERROR: curl is required but not found." >&2
exit 1
fi
# ---- Enforce canonical .gitignore -------------------------------------------
echo "==> Syncing canonical .gitignore template..."
curl -sSfL \
https://raw.githubusercontent.com/gautada/cicd/refs/heads/main/templates/gitignore/.gitignore \
-o .gitignore
echo ""
# ---- Sync config files from gautada/cicd ------------------------------------
echo "==> Syncing pre-commit config from gautada/cicd/templates/pre-commit..."
for f in "${CONFIG_FILES[@]}"; do
echo " ↓ $f"
if ! curl -sSfL "$CICD_RAW/$f" -o "$f"; then
echo "ERROR: Failed to download $f from $CICD_RAW" >&2
exit 1
fi
done
echo ""
# ---- Exit early if --pull-only ----------------------------------------------
if [[ "$PULL_ONLY" == true ]]; then
echo "==> Pull complete. Skipping install and run (--pull-only)."
exit 0
fi
# ---- Install/update hooks ---------------------------------------------------
# Verify pre-commit is installed
if ! command -v pre-commit &>/dev/null; then
echo "ERROR: pre-commit is not installed." >&2
echo " Install via: pipx install --global pre-commit" >&2
exit 1
fi
echo "==> Installing pre-commit hooks..."
pre-commit install
echo ""
# ---- Run linters ------------------------------------------------------------
echo "==> Running pre-commit on all files..."
pre-commit run --all-files