Skip to content

Commit 338d912

Browse files
committed
fix: separate check from format commands to prevent accidental modifications
BREAKING CHANGE: Renamed 'fix' command to 'format' for clarity - Check commands are now read-only (idempotent) - they only report issues - Format commands modify files - clearly separated and labeled - Added format-all with explicit DANGEROUS warning and 5-second delay - Protected golden snapshots in tests/snapshots/ from all hooks - Clear documentation explaining check vs format distinction This prevents developers from accidentally formatting the entire codebase when they just wanted to check for issues. Check commands are safe to run anytime, while format commands explicitly modify files.
1 parent 12423af commit 338d912

File tree

3 files changed

+108
-39
lines changed

3 files changed

+108
-39
lines changed

.atmos.d/dev.yaml

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,86 @@ commands:
4646
4747
echo "✅ Development environment setup complete!"
4848
echo ""
49-
echo "Run 'atmos dev check' to run pre-commit hooks on staged files"
50-
echo "Run 'atmos dev check-pr' to run pre-commit hooks on PR changes"
51-
echo "Run 'atmos dev check-all' to run pre-commit hooks on all files"
49+
echo "Run 'atmos dev check' to check staged files (read-only)"
50+
echo "Run 'atmos dev check-pr' to check PR changes (read-only)"
51+
echo "Run 'atmos dev format' to auto-format staged files"
52+
echo "Run 'atmos dev format-pr' to auto-format PR changes"
5253
5354
- name: check
54-
description: Run pre-commit hooks on staged files (for local development)
55+
description: Check staged files for issues (read-only, no modifications)
5556
steps:
56-
- pre-commit run
57+
- |
58+
echo "🔍 Checking staged files (read-only)..."
59+
# Run checks without modifying files
60+
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.go' | grep -v -E '^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/)' || true)
61+
if [ -n "$STAGED_GO_FILES" ]; then
62+
echo "Checking Go formatting..."
63+
echo "$STAGED_GO_FILES" | xargs -I {} gofumpt -d {} 2>/dev/null | head -20 || true
64+
echo "Running golangci-lint..."
65+
golangci-lint run --new-from-rev=origin/main --config=.golangci.yml $STAGED_GO_FILES || true
66+
fi
67+
echo "💡 To auto-fix issues, run: atmos dev format"
5768
5869
- name: check-pr
59-
description: Run pre-commit hooks on PR changes (compares with main branch)
70+
description: Check PR changes for issues (read-only, no modifications)
6071
steps:
6172
- |
62-
if [ -z "${GITHUB_BASE_REF:-}" ]; then
63-
# Local development - compare with main branch
64-
echo "🔍 Checking files changed from main branch..."
65-
pre-commit run --from-ref origin/main --to-ref HEAD
66-
else
67-
# CI environment - use GitHub PR refs
68-
echo "🔍 Checking files changed in PR..."
69-
pre-commit run --from-ref "origin/${GITHUB_BASE_REF}" --to-ref HEAD
73+
echo "🔍 Checking PR changes (read-only)..."
74+
BASE_REF="${GITHUB_BASE_REF:-main}"
75+
# Get changed Go files
76+
CHANGED_GO_FILES=$(git diff origin/$BASE_REF...HEAD --name-only -- '*.go' | grep -v -E '^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/)' || true)
77+
if [ -n "$CHANGED_GO_FILES" ]; then
78+
echo "Checking Go formatting..."
79+
echo "$CHANGED_GO_FILES" | xargs -I {} gofumpt -d {} 2>/dev/null | head -20 || true
80+
echo "Running golangci-lint..."
81+
golangci-lint run --new-from-rev=origin/$BASE_REF --config=.golangci.yml || true
7082
fi
83+
echo "💡 To auto-fix issues, run: atmos dev format-pr"
7184
7285
- name: check-all
73-
description: Run pre-commit hooks on all files (use sparingly)
86+
description: Check all files for issues (read-only, no modifications)
7487
steps:
75-
- pre-commit run --all-files
88+
- |
89+
echo "🔍 Checking all files (read-only)..."
90+
echo "⚠️ This may take a while..."
91+
# Check formatting without modifying
92+
echo "Checking Go formatting..."
93+
find . -name '*.go' -not -path './vendor/*' -not -path './tests/test-cases/*' -not -path './tests/testdata/*' -not -path './tests/snapshots/*' | xargs gofumpt -d 2>/dev/null | head -50 || true
94+
echo "Running comprehensive linting..."
95+
golangci-lint run --config=.golangci.yml || true
96+
echo "💡 To auto-fix issues, run: atmos dev format-all (⚠️ DANGEROUS)"
97+
98+
- name: format
99+
description: Auto-format staged files
100+
steps:
101+
- |
102+
echo "🔧 Auto-formatting staged files..."
103+
pre-commit run || true
104+
echo "✅ Formatting applied to staged files"
76105
77-
- name: fix
78-
description: Auto-fix Go formatting issues
106+
- name: format-pr
107+
description: Auto-format PR changes
79108
steps:
80109
- |
81-
echo "🔧 Auto-fixing Go formatting issues..."
82-
gofumpt -w .
83-
go mod tidy
84-
echo "✅ Formatting fixes applied"
110+
echo "🔧 Auto-formatting PR changes..."
111+
BASE_REF="${GITHUB_BASE_REF:-main}"
112+
pre-commit run --from-ref origin/$BASE_REF --to-ref HEAD || true
113+
echo "✅ Formatting applied to PR changes"
114+
115+
- name: format-all
116+
description: ⚠️ DANGEROUS - Auto-format ALL files (excludes golden snapshots)
117+
steps:
118+
- |
119+
echo "⚠️ WARNING: This will modify many files!"
120+
echo " Excludes: vendor/, tests/test-cases/, tests/testdata/, tests/snapshots/"
121+
echo " Golden snapshots and fixtures are protected."
122+
echo ""
123+
echo " Press Ctrl+C to cancel, or wait 5 seconds to continue..."
124+
sleep 5
125+
echo "🔧 Auto-formatting all files..."
126+
# Run pre-commit on all files with our exclude patterns
127+
pre-commit run --all-files || true
128+
echo "✅ Formatting complete. Review changes with: git diff"
85129
86130
- name: lint
87131
description: Run golangci-lint
@@ -119,10 +163,17 @@ commands:
119163
echo "Available Atmos dev commands:"
120164
echo ""
121165
echo " atmos dev setup - Set up local development environment"
122-
echo " atmos dev check - Run pre-commit hooks on staged files (local dev)"
123-
echo " atmos dev check-pr - Run pre-commit hooks on PR changes"
124-
echo " atmos dev check-all - Run pre-commit hooks on all files"
125-
echo " atmos dev fix - Auto-fix Go formatting issues"
166+
echo ""
167+
echo " Checking (read-only, no modifications):"
168+
echo " atmos dev check - Check staged files for issues"
169+
echo " atmos dev check-pr - Check PR changes for issues"
170+
echo " atmos dev check-all - Check all files for issues"
171+
echo ""
172+
echo " Formatting (modifies files):"
173+
echo " atmos dev format - Auto-format staged files"
174+
echo " atmos dev format-pr - Auto-format PR changes"
175+
echo " atmos dev format-all - ⚠️ DANGEROUS: Auto-format ALL files"
176+
echo ""
126177
echo " atmos dev lint - Run golangci-lint"
127178
echo " atmos dev test - Run tests"
128179
echo " atmos dev build - Build the Atmos binary"

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
hooks:
66
- id: go-fumpt
77
args: [-w]
8-
exclude: ^(vendor/|tests/test-cases/|tests/testdata/)
8+
exclude: ^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/)
99

1010
- id: go-build-mod
1111
name: Check Go build
@@ -26,16 +26,16 @@ repos:
2626
types: [go]
2727
pass_filenames: false
2828
require_serial: true
29-
exclude: ^(vendor/|tests/test-cases/|tests/testdata/)
29+
exclude: ^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/)
3030

3131
# General file hygiene
3232
- repo: https://github.com/pre-commit/pre-commit-hooks
3333
rev: v4.5.0
3434
hooks:
3535
- id: trailing-whitespace
36-
exclude: ^(vendor/|tests/test-cases/|tests/testdata/|.*\.md)
36+
exclude: ^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/|.*\.md)
3737
- id: end-of-file-fixer
38-
exclude: ^(vendor/|tests/test-cases/|tests/testdata/)
38+
exclude: ^(vendor/|tests/test-cases/|tests/testdata/|tests/snapshots/)
3939
- id: check-yaml
4040
exclude: ^(examples/|tests/test-cases/|vendor/)
4141
args: [--allow-multiple-documents]

docs/development.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ We use Atmos custom commands for development (dogfooding our own tool). This ens
2828
### Available Commands
2929

3030
```bash
31-
atmos dev help # Show all available dev commands
32-
atmos dev check # Run pre-commit hooks on staged files (local dev)
33-
atmos dev check-pr # Run pre-commit hooks on PR changes
34-
atmos dev check-all # Run pre-commit hooks on all files
31+
# Checking Commands (Read-only, no modifications)
32+
atmos dev check # Check staged files for issues
33+
atmos dev check-pr # Check PR changes for issues
34+
atmos dev check-all # Check all files for issues
3535
atmos dev lint # Run golangci-lint
36+
37+
# Formatting Commands (Modifies files)
38+
atmos dev format # Auto-format staged files
39+
atmos dev format-pr # Auto-format PR changes
40+
atmos dev format-all # ⚠️ DANGEROUS: Auto-format ALL files
41+
42+
# Build and Test Commands
3643
atmos dev test # Run tests
3744
atmos dev build # Build the Atmos binary
3845
atmos dev quick # Quick build and test
39-
atmos dev fix # Auto-fix Go formatting issues
46+
atmos dev help # Show all available dev commands
4047
```
4148

4249
### Alternative Make Commands
@@ -53,12 +60,23 @@ make lint # Run golangci-lint on changed files
5360

5461
We use pre-commit hooks to ensure code quality. The following hooks run automatically on `git commit`:
5562

56-
### Running Checks
63+
### Checking vs Formatting
5764

58-
Different commands for different contexts:
59-
- **`atmos dev check`** - Checks only staged files (best for local development before commit)
65+
**Important distinction:**
66+
- **Check commands** are read-only and will NOT modify any files. They only report issues.
67+
- **Format commands** WILL modify files to fix issues automatically.
68+
69+
#### Check Commands (Safe, read-only)
70+
- **`atmos dev check`** - Checks only staged files (best before committing)
6071
- **`atmos dev check-pr`** - Checks files changed from main branch (best for PR reviews)
61-
- **`atmos dev check-all`** - Checks all files in the repository (use sparingly, can be slow)
72+
- **`atmos dev check-all`** - Checks all files in the repository
73+
74+
#### Format Commands (Modifies files)
75+
- **`atmos dev format`** - Auto-formats only staged files
76+
- **`atmos dev format-pr`** - Auto-formats files changed from main branch
77+
- **`atmos dev format-all`** - ⚠️ **DANGEROUS**: Auto-formats ALL files (use with extreme caution)
78+
79+
**Note:** Golden snapshots and test fixtures are always protected from formatting.
6280

6381
### Go-specific Hooks
6482
- **go-fumpt**: Enforces consistent Go formatting (stricter than gofmt)

0 commit comments

Comments
 (0)