|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 5 | +cd "$repo_root" |
| 6 | + |
| 7 | +staged_swift_files="$(mktemp)" |
| 8 | +tabs_report="$(mktemp)" |
| 9 | +trailing_ws_report="$(mktemp)" |
| 10 | +tabs_tmp="$(mktemp)" |
| 11 | +trailing_tmp="$(mktemp)" |
| 12 | +trap 'rm -f "$staged_swift_files" "$tabs_report" "$trailing_ws_report" "$tabs_tmp" "$trailing_tmp"' EXIT |
| 13 | + |
| 14 | +git diff --cached --name-only --diff-filter=ACMR -- '*.swift' > "$staged_swift_files" || true |
| 15 | + |
| 16 | +if [[ ! -s "$staged_swift_files" ]]; then |
| 17 | + echo "No staged Swift files to format/lint." |
| 18 | + exit 0 |
| 19 | +fi |
| 20 | + |
| 21 | +swift_format_available=0 |
| 22 | +if command -v swift-format >/dev/null 2>&1; then |
| 23 | + swift_format_available=1 |
| 24 | +elif xcrun --find swift-format >/dev/null 2>&1; then |
| 25 | + swift_format_available=2 |
| 26 | +fi |
| 27 | + |
| 28 | +run_swift_format() { |
| 29 | + local mode="$1" |
| 30 | + local file="$2" |
| 31 | + |
| 32 | + if [[ "$swift_format_available" -eq 1 ]]; then |
| 33 | + swift-format "$mode" "$file" |
| 34 | + elif [[ "$swift_format_available" -eq 2 ]]; then |
| 35 | + xcrun swift-format "$mode" "$file" |
| 36 | + fi |
| 37 | +} |
| 38 | + |
| 39 | +# Auto-fix pass for staged Swift files. |
| 40 | +while IFS= read -r file; do |
| 41 | + [[ -z "$file" ]] && continue |
| 42 | + [[ -f "$file" ]] || continue |
| 43 | + |
| 44 | + perl -i -pe 's/[ \t]+$//' "$file" |
| 45 | + if [[ "$swift_format_available" -ne 0 ]]; then |
| 46 | + run_swift_format format "$file" |
| 47 | + fi |
| 48 | + |
| 49 | + git add "$file" |
| 50 | +done < "$staged_swift_files" |
| 51 | + |
| 52 | +# Lint pass for staged Swift files. |
| 53 | +errors=0 |
| 54 | +while IFS= read -r file; do |
| 55 | + [[ -z "$file" ]] && continue |
| 56 | + [[ -f "$file" ]] || continue |
| 57 | + |
| 58 | + if grep -n $'\t' "$file" > "$tabs_tmp" 2>/dev/null; then |
| 59 | + sed "s#^#$file:#" "$tabs_tmp" >> "$tabs_report" |
| 60 | + fi |
| 61 | + |
| 62 | + if grep -n -E "[[:blank:]]$" "$file" > "$trailing_tmp" 2>/dev/null; then |
| 63 | + sed "s#^#$file:#" "$trailing_tmp" >> "$trailing_ws_report" |
| 64 | + fi |
| 65 | +done < "$staged_swift_files" |
| 66 | + |
| 67 | +if [[ -s "$tabs_report" ]]; then |
| 68 | + head -n 30 "$tabs_report" >&2 |
| 69 | + echo "Swift lint failed: tabs detected in staged files." >&2 |
| 70 | + errors=1 |
| 71 | +fi |
| 72 | + |
| 73 | +if [[ -s "$trailing_ws_report" ]]; then |
| 74 | + head -n 30 "$trailing_ws_report" >&2 |
| 75 | + echo "Swift lint failed: trailing whitespace detected in staged files." >&2 |
| 76 | + errors=1 |
| 77 | +fi |
| 78 | + |
| 79 | +if [[ "$swift_format_available" -ne 0 ]]; then |
| 80 | + while IFS= read -r file; do |
| 81 | + [[ -z "$file" ]] && continue |
| 82 | + [[ -f "$file" ]] || continue |
| 83 | + if ! run_swift_format lint "$file"; then |
| 84 | + echo "Swift-format lint failed for $file." >&2 |
| 85 | + errors=1 |
| 86 | + fi |
| 87 | + done < "$staged_swift_files" |
| 88 | +else |
| 89 | + echo "swift-format not found; format/lint step skipped for swift-format." |
| 90 | +fi |
| 91 | + |
| 92 | +if [[ "$errors" -ne 0 ]]; then |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +echo "Staged Swift format/lint checks passed." |
0 commit comments