# MORPH-SPEC Git Hooks

Pre-commit hooks for automatic validation before committing.

## Available Hooks

| Hook | Description | Action |
|------|-------------|--------|
| `pre-commit-agents.sh` | Validates agents.json with JSON Schema | ❌ Blocks commit |
| `pre-commit-costs.sh` | Validates Azure costs don't exceed limits | ❌ Blocks commit |
| `pre-commit-specs.sh` | Validates spec.md has required sections | ❌ Blocks commit |
| `pre-commit-tests.sh` | Checks if new code has tests | ⚠️ Warns only |
| `pre-commit-all.sh` | Runs all hooks in sequence | ❌ Blocks if any fail |

## Installation

### Option 1: Install Individual Hooks

```bash
# Install cost validation
ln -s ../../content/.morph/hooks/pre-commit-costs.sh .git/hooks/pre-commit

# Or symlink all at once
ln -s ../../content/.morph/hooks/pre-commit-all.sh .git/hooks/pre-commit
```

### Option 2: Copy Hooks

```bash
# Copy master hook
cp content/.morph/hooks/pre-commit-all.sh .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```

### Option 3: Use Existing Hooks

If you already have `.git/hooks/pre-commit`, add this to the end:

```bash
# Run MORPH-SPEC validations
bash content/.morph/hooks/pre-commit-all.sh || exit 1
```

## What Each Hook Does

### pre-commit-agents.sh

**Validates:** `agents.json` configuration

**Checks:**
- JSON Schema compliance
- Required fields (id, name, emoji, type, etc.)
- Keyword patterns (alphanumeric + hyphens/underscores)
- No duplicate keywords
- autoActivation for specialist agents

**Example Output:**
```
🤖 Validating agent configuration...
Detected changes to agents.json

=== MORPH-SPEC Agent Validator ===

Version: 1.0.0
Core Agents: 6
Specialist Agents: 4

✓ Validation PASSED
```

### pre-commit-costs.sh

**Validates:** Bicep files for Azure costs

**Checks:**
- Parses modified `.bicep` files
- Calculates monthly costs
- Blocks if > `requiresADR` limit (default: $10)
- Checks if ADR exists in decisions.md

**Example Output:**
```
💰 Calculating Azure costs...

╔════════════════════════════════════════════════╗
║ sqlDatabase (Basic)              $4.99/mo     ║
║ containerApp (Consumption)       $0.00/mo     ║
╠════════════════════════════════════════════════╣
║ Total: $4.99                                   ║
║ Requires ADR: NO                               ║
╚════════════════════════════════════════════════╝

✓ Cost validation passed
```

### pre-commit-specs.sh

**Validates:** `spec.md` files have required sections

**Checks:**
- `## 📋 Metadata`
- `## 🎯 Overview`
- `## 🏗️ Technical Design`
- `## ✅ Acceptance Criteria`
- Warns if no user stories found

**Example Output:**
```
🔍 Validating spec files...
Checking: .morph/outputs/my-feature/spec.md
  ✓ All required sections found
✓ All spec files are valid
```

### pre-commit-tests.sh

**Validates:** New C# code has corresponding tests

**Checks:**
- Finds modified `.cs` files (excluding tests)
- Looks for `*Tests.cs` or `Test*.cs`
- Skips trivial files (DTOs, records, models)
- **Warns only, doesn't block**

**Example Output:**
```
🧪 Checking test coverage...
  ✓ Test found for: Services/UserService.cs
  ⚠️  No test found for: Services/PaymentService.cs

⚠️  WARNING: Some files don't have tests
   Override with: git commit --no-verify
```

### pre-commit-all.sh (Master Hook)

**Runs:** All hooks in sequence

**Output:**
```
╔════════════════════════════════════════════════╗
║     MORPH-SPEC PRE-COMMIT VALIDATION           ║
╚════════════════════════════════════════════════╝

─────────────────────────────────────────────────
🤖 Validating agent configuration...
✓ No changes to agents.json

─────────────────────────────────────────────────
💰 Calculating Azure costs...
✓ No Bicep files modified

─────────────────────────────────────────────────
🔍 Validating spec files...
✓ No spec files modified

─────────────────────────────────────────────────
🧪 Checking test coverage...
  ✓ Test found for: Services/UserService.cs

═════════════════════════════════════════════════
✅ ALL PRE-COMMIT VALIDATIONS PASSED
```

## Bypassing Hooks

If you need to commit without validation (e.g., WIP commit):

```bash
git commit --no-verify -m "WIP: work in progress"
```

**⚠️ Use sparingly** - hooks exist to prevent errors.

## Customizing Hooks

### Adjust Cost Limits

Edit `content/.morph/config/config.json`:

```json
{
  "costs": {
    "limits": {
      "requiresADR": 15  // Change from $10 to $15
    }
  }
}
```

### Disable Specific Hooks

Edit `.git/hooks/pre-commit` and comment out unwanted hooks:

```bash
HOOKS=(
  "pre-commit-agents.sh"
  # "pre-commit-costs.sh"  # Disabled
  "pre-commit-specs.sh"
  "pre-commit-tests.sh"
)
```

## Troubleshooting

### Hook Not Running

```bash
# Check if hook exists
ls -la .git/hooks/pre-commit

# Check if executable
chmod +x .git/hooks/pre-commit

# Test manually
bash .git/hooks/pre-commit
```

### Hook Fails Unexpectedly

```bash
# Run individual hook for debugging
bash content/.morph/hooks/pre-commit-costs.sh

# Check hook output
bash -x content/.morph/hooks/pre-commit-costs.sh
```

## Benefits

- ✅ **Prevents Errors:** Catches issues before commit
- ✅ **Enforces Quality:** Requires tests, specs, ADRs
- ✅ **Cost Control:** Blocks expensive infrastructure
- ✅ **Fast Feedback:** Fails early in dev workflow
- ✅ **Consistent:** Same validation for entire team

---

*MORPH-SPEC Framework - Git Hooks*
