Skip to content

Fix pre-push hook to fail if working tree dirty #5194

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

Open
wants to merge 4 commits into
base: main
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
27 changes: 4 additions & 23 deletions .github/workflows/lint-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ jobs:
- run: yarn --immutable
- run: yarn lint
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty at end of job"
exit 1
fi
run: yarn git:fail-on-changes

validate-changelog:
name: Validate changelog
Expand All @@ -86,12 +81,7 @@ jobs:
- run: yarn --immutable
- run: yarn workspace ${{ matrix.package-name }} changelog:validate
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty at end of job"
exit 1
fi
run: yarn git:fail-on-changes

build:
name: Build
Expand All @@ -116,12 +106,7 @@ jobs:
- run: yarn --immutable
- run: yarn build
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty at end of job"
exit 1
fi
run: yarn git:fail-on-changes

test:
name: Test
Expand Down Expand Up @@ -149,8 +134,4 @@ jobs:
- run: yarn workspace ${{ matrix.package-name }} run test
- name: Require clean working directory
shell: bash
run: |
if ! git diff --exit-code; then
echo "Working tree dirty at end of job"
exit 1
fi
run: yarn git:fail-on-changes
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"changelog:update": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:update",
"changelog:validate": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:validate",
"create-package": "ts-node scripts/create-package",
"git:fail-on-changes": "./scripts/fail-on-changes.sh",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams",
"lint:dependencies": "depcheck && yarn dedupe --check",
"lint:dependencies:fix": "depcheck && yarn dedupe",
Expand All @@ -41,7 +42,7 @@
"workspaces:list-versions": "./scripts/list-workspace-versions.sh"
},
"simple-git-hooks": {
"pre-push": "yarn lint"
"pre-push": "yarn lint && yarn git:fail-on-changes"
},
"resolutions": {
"[email protected]": "^6.5.7",
Expand Down
16 changes: 16 additions & 0 deletions scripts/fail-on-changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

red() {
printf "\x1B[31m"
echo -n "$@"
printf "\x1B[0m"
}

diff="$(git diff --stat --exit-code --color)"
exitcode=$?

if [[ $exitcode -ne 0 ]]; then
red "ERROR: The working tree is dirty. Please commit or remove these changes to continue:" $'\n'
echo "$diff"
exit 1
fi
Loading