Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic line ending conversion #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# linguist overrides
*.sql linguist-language=TSQL

# converts to native line endings on checkout for all SQL files
*.sql text=auto
61 changes: 61 additions & 0 deletions .github/workflows/AutoFormatSQL.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: AutoFormatSQL

on:
push:
branches:
- main
paths:
- "**.sql"
permissions:
contents: write
jobs:
trim_whitespace:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: "20"

- name: git log
shell: pwsh
run: |
git log --pretty=format:%H --max-count=10

- name: Trim trailing whitespace and replace Tabs
shell: pwsh
run: |
$changedFiles = git --no-pager diff --name-only --relative --diff-filter=AM "${{ github.event.before }}" "${{ github.sha }}" -- '***.sql'
$tabSize = 4
$outputString = ""
$changedFiles | ForEach-Object {
$fullPath = $_
$Content = Get-Content $fullPath
foreach ($Ctnt in $Content) {
$CharArray = $Ctnt.ToCharArray()
$lineOutput = ""
$slidingOffset = 0
for ($charIdx = 0; $charIdx -lt $CharArray.Length; $charIdx++) {
if ($CharArray[$charIdx] -eq "`t") {
$currentTab = $tabSize - (($charIdx + $slidingOffset) % $tabSize)
$slidingOffset += $currentTab - 1
$lineOutput += (" " * $currentTab)
}
else {
$lineOutput += $CharArray[$charIdx]
}
}
$outputString += $lineOutput.TrimEnd() + "`n" # Add line output with trailing spaces removed to the output string, and add newline
}
Set-Content -Path $fullPath -Value ($outputString.TrimEnd())
}
- name: Commit changes
run: |
if [ -n "$(git status --porcelain)" ]; then
git config user.name "GitHub Actions"
git config user.email "[email protected]"
git add .
git commit -m "Automation: Replace tabs and remove trailing spaces"
git push
fi