update and rename #26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto-Format | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
CARGO_TERM_COLOR: always | |
jobs: | |
auto-format: | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash | |
steps: | |
- name: Initializing git repository | |
run: | | |
git init --initial-branch=main | |
git remote add origin ${{ github.event.repository.html_url }} | |
- name: Fetching base and head commit (push) | |
if: github.event_name == 'push' | |
env: | |
PUSHED_COMMITS: ${{ toJSON(github.event.commits) }} | |
run: | | |
if [[ ${{ github.event.before }} == 0000000000000000000000000000000000000000 ]] | |
then | |
before=$(echo "$PUSHED_COMMITS" | jq .[0].id --raw-output) | |
git fetch --no-tags --prune --no-recurse-submodules --depth=2 origin $before ${{ github.event.after }} | |
echo "BASE=$(git rev-parse $before~1)" >> $GITHUB_ENV | |
else | |
git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin ${{ github.event.before }} ${{ github.event.after }} | |
echo "BASE=${{ github.event.before }}" >> $GITHUB_ENV | |
fi | |
echo "HEAD=${{ github.event.after }}" >> $GITHUB_ENV | |
- name: Fetching base and head commit (pull_request) | |
if: github.event_name == 'pull_request' | |
run: | | |
git fetch --no-tags --prune --no-recurse-submodules --depth=$((${{ github.event.pull_request.commits }} + 1)) origin ${{ github.event.pull_request.head.sha }} | |
git fetch --no-tags --prune --no-recurse-submodules --depth=10 origin ${{ github.event.pull_request.base.sha }} | |
git checkout --progress --force ${{ github.event.pull_request.head.sha }} | |
while [[ -n $(git rev-list shallow ^${{ github.event.pull_request.base.sha }}) ]] | |
do | |
git fetch --no-tags --prune --no-recurse-submodules --deepen=10 origin ${{ github.event.pull_request.base.sha }} | |
done | |
base=$(git rev-list ${{ github.event.pull_request.head.sha }} ^${{ github.event.pull_request.base.sha }} | tail --lines 1 | xargs -I {} git rev-parse {}~1) | |
echo "BASE=$base" >> $GITHUB_ENV | |
echo "HEAD=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV | |
- name: Auto-Format with pretty-quick | |
env: | |
BASE_BRANCH: ${{ github.base_ref }} | |
run: npx -p prettier@3.0.2 -p pretty-quick@3.1.3 pretty-quick --staged --branch base | |
- name: Commit auto-formatted files | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
uses: stefanzweifel/git-auto-commit-action@v4 | |
if: ${{ github.event_name == 'pull_request'}} | |
with: | |
commit_message: "style: format files" |