Skip to content

Commit 9eeee2a

Browse files
author
kaptron
committed
Updates to lefthook and git hook setup
1 parent 63965f4 commit 9eeee2a

File tree

10 files changed

+107
-154
lines changed

10 files changed

+107
-154
lines changed

.lefthook/post-checkout/01-bundle-checkinstall

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# modified from https://gist.github.com/brysgo/9980344
3+
4+
CHECKING_OUT_BRANCH=$3
5+
OLD_BRANCH=$1
6+
NEW_BRANCH=$2
7+
8+
if [ $CHECKING_OUT_BRANCH -eq 1 ]; then
9+
FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
10+
11+
# check if we need to do a bundle
12+
BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tGemfile.lock'`
13+
if [ ! -z "$BUNDLE_CHANGED" ]; then
14+
echo "Your Gemfile.lock has changed, running bundle"
15+
bundle
16+
fi
17+
18+
# check if we need to do a yarn install
19+
PACKAGE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tyarn.lock'`
20+
if [ ! -z "$PACKAGE_CHANGED" ]; then
21+
echo "Your yarn.lock has changed, running yarn"
22+
yarn
23+
fi
24+
25+
# always check out the branch's correct version of the submodule
26+
git submodule update app/javascript/ui/shared
27+
fi

.lefthook/post-checkout/02-db-migrate

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,30 @@ CHECKING_OUT_BRANCH=$3
55
OLD_BRANCH=$1
66
NEW_BRANCH=$2
77

8-
if [ $CHECKING_OUT_BRANCH -eq 1 ]
9-
then
10-
FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
11-
MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep --color=never 'D\tdb/migrate/([0-9]+)' | sort -r`
12-
MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep --color=never 'A\tdb/migrate/([0-9]+)'`
8+
if [ $CHECKING_OUT_BRANCH -eq 1 ]; then
9+
FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
10+
MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep --color=never 'D\tdb/migrate/([0-9]+)' | sort -r`
11+
MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep --color=never 'A\tdb/migrate/([0-9]+)'`
1312

14-
# check if we need to do a bundle
15-
BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tGemfile.lock'`
16-
if [ ! -z "$BUNDLE_CHANGED" ]; then
17-
echo "Your Gemfile.lock has changed, running bundle"
18-
bundle
19-
fi
13+
if [ ! -z "$MIGRATIONS_REMOVED" ]; then
14+
echo "Rolling back missing migrations"
15+
for migration in $MIGRATIONS_REMOVED
16+
do
17+
if [ $migration == "D" ]; then
18+
continue
19+
fi
20+
git checkout "$OLD_BRANCH" -- "$migration"
21+
VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
22+
bundle exec rake db:migrate:down VERSION="$VERSION"
23+
git reset
24+
rm "$migration"
25+
done
26+
bundle exec rake db:test:prepare
27+
git checkout db/schema.rb
28+
fi
2029

21-
# check if we need to do a yarn install
22-
PACKAGE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tyarn.lock'`
23-
if [ ! -z "$PACKAGE_CHANGED" ]; then
24-
echo "Your yarn.lock has changed, running yarn"
25-
yarn
26-
fi
27-
28-
if [ ! -z "$MIGRATIONS_REMOVED" ]; then
29-
echo "Rolling back missing migrations"
30-
for migration in $MIGRATIONS_REMOVED
31-
do
32-
if [ $migration == "D" ]; then
33-
continue
34-
fi
35-
git checkout "$OLD_BRANCH" -- "$migration"
36-
VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
37-
bundle exec rake db:migrate:down VERSION="$VERSION"
38-
git reset
39-
rm "$migration"
40-
done
41-
bundle exec rake db:test:prepare
42-
git checkout db/schema.rb
43-
fi
44-
45-
if [ ! -z "$MIGRATIONS_ADDED" ]; then
46-
echo "New migrations have been added running migrations"
47-
bundle exec rake db:migrate db:test:prepare
48-
fi
49-
50-
# always check out the branch's correct version of the submodule
51-
git submodule update app/javascript/ui/shared
30+
if [ ! -z "$MIGRATIONS_ADDED" ]; then
31+
echo "New migrations have been added running migrations"
32+
bundle exec rake db:migrate db:test:prepare
33+
fi
5234
fi

lefthook.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pre-commit:
66
files: git diff --name-only development
77
glob: "*.js"
88
exclude: '__tests__/ui/grid/__snapshots__/*'
9-
run: yarn run lint {files}
9+
run: yarn run lint --fix {files}
1010
rubocop:
1111
files: git diff --name-only development
1212
# exclude: "excluded_filename.rb"
@@ -16,9 +16,9 @@ pre-commit:
1616
pre-push: # githook name
1717
parallel: false
1818
commands: # list of commands
19-
rubocop:
20-
tags: backend
21-
run: bundle exec rubocop --fail-level error
19+
# rubocop:
20+
# tags: backend
21+
# run: bundle exec rubocop --fail-level error
2222
# rspec:
2323
# tags: rspec backend
2424
# run: bundle exec crystalball --fail-fast
@@ -28,12 +28,9 @@ pre-push: # githook name
2828
post-checkout:
2929
piped: true # Exit early if preceding command fails
3030
scripts:
31-
01-bundle-checkinstall:
31+
01-check-packages:
3232
tags: backend
3333
02-db-migrate:
3434
tags: backend
3535
03-crystalball-update:
3636
tags: rspec backend
37-
38-
39-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"cypress": "./node_modules/.bin/cypress open",
66
"cypress-ci": "./node_modules/.bin/cypress run",
7-
"lint": "./node_modules/.bin/eslint .",
7+
"lint": "./node_modules/.bin/eslint",
88
"styleguide": "yarn styleguidist server",
99
"test": "jest --watch"
1010
},

script/add-pre-commit-hook.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

script/dev-setup

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ set -e
55
cd "$(dirname "$0")/.."
66
DIR=$(pwd)
77

8-
GIT_DIR=$(git rev-parse --git-dir)
9-
HOOK="$GIT_DIR/hooks/pre-commit"
10-
11-
if [ ! -f "$HOOK" ]; then
12-
$DIR/script/add-pre-commit-hook.sh
13-
fi
14-
158
git submodule init
169
git submodule update
1710

@@ -24,3 +17,6 @@ yarn install
2417
bundle install
2518
# setup runs db:create, db:schema:load and db:seed
2619
bin/rails db:setup
20+
# install git hooks
21+
bundle exec lefthook install
22+
script/override-post-checkout-hook

script/format-staged

Lines changed: 0 additions & 18 deletions
This file was deleted.

script/override-post-checkout-hook

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
GIT_DIR=$(git rev-parse --git-dir)
4+
HOOK="$GIT_DIR/hooks/post-checkout"
5+
#
6+
# if [ -f "$HOOK" ]; then
7+
# if ! grep -q 'script/pre-commit' "$HOOK"; then
8+
# echo "Found an existing pre-commit hook, adding script/pre-commit to it"
9+
# echo "Please double-check $HOOK to ensure that it will run"
10+
# echo script/pre-commit >> "$HOOK"
11+
# fi
12+
# else
13+
# echo "Installing pre-commit hook to $HOOK"
14+
# mkdir -p "$GIT_DIR"/hooks
15+
# echo script/pre-commit >> "$HOOK"
16+
# chmod +x "$HOOK"
17+
# fi
18+
19+
echo "overriding post-checkout hook..."
20+
cp script/post-checkout "$HOOK"
21+
chmod +x "$HOOK"

script/post-checkout

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,31 @@
1-
# modified from https://gist.github.com/brysgo/9980344
1+
#!/bin/sh
22

3-
CHECKING_OUT_BRANCH=$3
4-
OLD_BRANCH=$1
5-
NEW_BRANCH=$2
6-
7-
if [ $CHECKING_OUT_BRANCH -eq 1 ]
8-
then
9-
FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
10-
MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep --color=never 'D\tdb/migrate/([0-9]+)' | sort -r`
11-
MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep --color=never 'A\tdb/migrate/([0-9]+)'`
12-
13-
# check if we need to do a bundle
14-
BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tGemfile.lock'`
15-
if [ ! -z "$BUNDLE_CHANGED" ]; then
16-
echo "Your Gemfile.lock has changed, running bundle"
17-
bundle
18-
fi
19-
20-
# check if we need to do a yarn install
21-
PACKAGE_CHANGED=`echo "$FILES_CHANGED" | egrep --color=never 'M\tyarn.lock'`
22-
if [ ! -z "$PACKAGE_CHANGED" ]; then
23-
echo "Your yarn.lock has changed, running yarn"
24-
yarn
25-
fi
3+
if [ "{$LEFTHOOK}" = "0" ]; then
4+
exit 0
5+
fi
266

27-
if [ ! -z "$MIGRATIONS_REMOVED" ]; then
28-
echo "Rolling back missing migrations"
29-
for migration in $MIGRATIONS_REMOVED
30-
do
31-
if [ $migration == "D" ]; then
32-
continue
33-
fi
34-
git checkout "$OLD_BRANCH" -- "$migration"
35-
VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
36-
bundle exec rake db:migrate:down VERSION="$VERSION"
37-
git reset
38-
rm "$migration"
39-
done
40-
bundle exec rake db:test:prepare
41-
git checkout db/schema.rb
42-
fi
7+
# added setup to prompt user for changes
8+
exec < /dev/tty
9+
echo "Do you want to proceed with lefthook scripts? y/n"
10+
read -n 1 -r
11+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
12+
exit 0
13+
fi
14+
echo ""
15+
echo "running post-checkout..."
16+
# -----
4317

44-
if [ ! -z "$MIGRATIONS_ADDED" ]; then
45-
echo "New migrations have been added running migrations"
46-
bundle exec rake db:migrate db:test:prepare
47-
fi
18+
cmd="lefthook run post-checkout $@"
4819

49-
# always check out the branch's correct version of the submodule
50-
git submodule update app/javascript/ui/shared
20+
if lefthook >/dev/null 2>&1
21+
then
22+
exec $cmd
23+
elif bundle exec lefthook >/dev/null 2>&1
24+
then
25+
bundle exec $cmd
26+
elif npx lefthook >/dev/null 2>&1
27+
then
28+
npx $cmd
29+
else
30+
echo "Can't find lefthook in PATH"
5131
fi

0 commit comments

Comments
 (0)