Skip to content

Commit 6fc3311

Browse files
committed
Fix pre-push hook to fail if working tree dirty
Now that we've instructed the lint pipeline to update the ESLint warning thresholds file, if you introduce changes to lint violations prior to pushing, and you have the `pre-push` Git hook installed, you could forget to push changes to this file. This commit extracts the "working tree dirty" check that we perform in the `lint-build-test` GitHub workflow to a script so that we can not only call it in the workflow but also after running `yarn lint` in the `pre-push` hook. Note that you'll need to re-run `yarn simple-git-hooks` for this change to take effect.
1 parent dcc0df0 commit 6fc3311

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

.github/workflows/lint-build-test.yml

+4-23
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ jobs:
5555
- run: yarn --immutable
5656
- run: yarn lint
5757
- name: Require clean working directory
58-
shell: bash
59-
run: |
60-
if ! git diff --exit-code; then
61-
echo "Working tree dirty at end of job"
62-
exit 1
63-
fi
58+
run: yarn git:fail-on-changes
6459

6560
validate-changelog:
6661
name: Validate changelog
@@ -86,12 +81,7 @@ jobs:
8681
- run: yarn --immutable
8782
- run: yarn workspace ${{ matrix.package-name }} changelog:validate
8883
- name: Require clean working directory
89-
shell: bash
90-
run: |
91-
if ! git diff --exit-code; then
92-
echo "Working tree dirty at end of job"
93-
exit 1
94-
fi
84+
run: yarn git:fail-on-changes
9585

9686
build:
9787
name: Build
@@ -116,12 +106,7 @@ jobs:
116106
- run: yarn --immutable
117107
- run: yarn build
118108
- name: Require clean working directory
119-
shell: bash
120-
run: |
121-
if ! git diff --exit-code; then
122-
echo "Working tree dirty at end of job"
123-
exit 1
124-
fi
109+
run: yarn git:fail-on-changes
125110

126111
test:
127112
name: Test
@@ -149,8 +134,4 @@ jobs:
149134
- run: yarn workspace ${{ matrix.package-name }} run test
150135
- name: Require clean working directory
151136
shell: bash
152-
run: |
153-
if ! git diff --exit-code; then
154-
echo "Working tree dirty at end of job"
155-
exit 1
156-
fi
137+
run: yarn git:fail-on-changes

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"changelog:update": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:update",
2222
"changelog:validate": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run changelog:validate",
2323
"create-package": "ts-node scripts/create-package",
24+
"git:fail-on-changes": "./scripts/fail-on-changes.sh",
2425
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams",
2526
"lint:dependencies": "depcheck && yarn dedupe --check",
2627
"lint:dependencies:fix": "depcheck && yarn dedupe",
@@ -41,7 +42,7 @@
4142
"workspaces:list-versions": "./scripts/list-workspace-versions.sh"
4243
},
4344
"simple-git-hooks": {
44-
"pre-push": "yarn lint"
45+
"pre-push": "yarn lint && yarn git:fail-on-changes"
4546
},
4647
"resolutions": {
4748
"[email protected]": "^6.5.7",

scripts/fail-on-changes.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
red() {
4+
printf "\x1B[31m"
5+
echo -n "$@"
6+
printf "\x1B[0m"
7+
}
8+
9+
diff="$(git diff --stat --exit-code --color)"
10+
exitcode=$?
11+
12+
if [[ $exitcode -ne 0 ]]; then
13+
red "ERROR: The working tree is dirty. Please commit or remove these changes to continue:" $'\n'
14+
echo "$diff"
15+
exit 1
16+
fi

0 commit comments

Comments
 (0)