Skip to content

Commit ea306a4

Browse files
authored
Only lint changed files when pushing branch (#189)
The Git hook that's currently configured will lint all files in the project when a branch is pushed. Linting takes a long time, so to speed this up, only lint the files that have changed in the branch. Also, to make the feedback loop even faster, fix those files instead of just linting. Also rename the package script to create the Git hooks from `simple-git-hooks` to `setup-git-hooks` (so if we decide not to use `simple-git-hooks` in the future we don't have to rename it again) and update `bin/setup` script to run this script. Finally, replace `simple-git-hooks` with `husky`. Our hook is now a little bit more complicated as we need to figure out which branch the current branch is based off of, find the files changed in the branch, and run Prettier accordingly. Being able to just commit the hooks to the repo as Husky allows us to do makes it possible to guarantee that we are doing this.
1 parent 37cedd6 commit ea306a4

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

.husky/pre-push

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
3+
. "$(dirname -- "$0")/_/husky.sh"
4+
5+
echo "Fixing changed files with lint violations..."
6+
7+
yarn prettier --write --ignore-unknown $(git diff "$(git merge-base main HEAD)" --name-only) || exit $?
8+
9+
echo
10+
echo "Auditing dependencies..."
11+
12+
yarn audit || exit $?
13+
14+
echo

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.yarn/releases
33
gemfiles
44
vendor/bundle
5+
yarn.lock

bin/setup

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ set -euo pipefail
55
### PROJECT SETUP ##############################################################
66

77
provision-project() {
8-
banner "Adding simple-git-hooks"
9-
yarn simple-git-hooks
8+
banner "Setting up Git hooks"
9+
yarn setup-git-hooks
1010

1111
banner "Installing appraisals"
1212
bundle exec appraisal install

package.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@
44
"description": "A more helpful way to view differences between complex data structures in RSpec",
55
"private": true,
66
"scripts": {
7-
"lint": "prettier --check .",
87
"audit": "yarn npm audit && bundle exec bundle audit --gemfile-lock ${BUNDLE_GEMFILE:-Gemfile}",
9-
"lint:fix": "yarn lint --write"
10-
},
11-
"simple-git-hooks": {
12-
"pre-push": "yarn lint && yarn audit"
8+
"lint": "prettier --check .",
9+
"lint:fix": "yarn lint --write",
10+
"setup-git-hooks": "husky install"
1311
},
1412
"devDependencies": {
1513
"@lavamoat/allow-scripts": "^2.3.0",
1614
"@prettier/plugin-ruby": "^3.2.2",
17-
"prettier": "^2.8.7",
18-
"simple-git-hooks": "^2.8.1"
15+
"husky": "^8.0.3",
16+
"prettier": "^2.8.7"
1917
},
2018
"packageManager": "[email protected]",
2119
"lavamoat": {
22-
"allowScripts": {
23-
"simple-git-hooks": false
24-
}
20+
"allowScripts": {}
2521
}
2622
}

yarn.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,15 @@ __metadata:
521521
languageName: node
522522
linkType: hard
523523

524+
"husky@npm:^8.0.3":
525+
version: 8.0.3
526+
resolution: "husky@npm:8.0.3"
527+
bin:
528+
husky: lib/bin.js
529+
checksum: 837bc7e4413e58c1f2946d38fb050f5d7324c6f16b0fd66411ffce5703b294bd21429e8ba58711cd331951ee86ed529c5be4f76805959ff668a337dbfa82a1b0
530+
languageName: node
531+
linkType: hard
532+
524533
"imurmurhash@npm:^0.1.4":
525534
version: 0.1.4
526535
resolution: "imurmurhash@npm:0.1.4"
@@ -1025,15 +1034,6 @@ __metadata:
10251034
languageName: node
10261035
linkType: hard
10271036

1028-
"simple-git-hooks@npm:^2.8.1":
1029-
version: 2.8.1
1030-
resolution: "simple-git-hooks@npm:2.8.1"
1031-
bin:
1032-
simple-git-hooks: cli.js
1033-
checksum: 920d8b3122a102d4790b511a2f033202511f6a08d5105b4e05f05907d407d99f25490da1037643280d622c1951e0d10abacfbeaee64d5f69f1d0e29cf914141f
1034-
languageName: node
1035-
linkType: hard
1036-
10371037
"sshpk@npm:^1.7.0":
10381038
version: 1.17.0
10391039
resolution: "sshpk@npm:1.17.0"
@@ -1110,8 +1110,8 @@ __metadata:
11101110
dependencies:
11111111
"@lavamoat/allow-scripts": ^2.3.0
11121112
"@prettier/plugin-ruby": ^3.2.2
1113+
husky: ^8.0.3
11131114
prettier: ^2.8.7
1114-
simple-git-hooks: ^2.8.1
11151115
languageName: unknown
11161116
linkType: soft
11171117

0 commit comments

Comments
 (0)