Skip to content

Commit e22ac32

Browse files
authored
Merge pull request #10 from Jim-Hodapp-Coaching/add-pre-commit-format
2 parents 5f46a75 + 2c4618a commit e22ac32

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ You can find documentation on installing Rust [here](https://www.rust-lang.org/t
99

1010
You will also need a local copy of [Ambi](https://github.com/jhodapp/ambi) running ( default port 4000 ).
1111

12+
## Set Up Git Hooks
13+
14+
The ambi_mock_client repository makes use of several Git hooks to ensure that code quality standards are met and consistent. To automatically configure these hooks for your local workspace, you can run the following:
15+
```bash
16+
./scripts/create-git-hooks
17+
```
18+
19+
This will create symlinks to the Git hooks, preserving any hooks that you may have already configured.
20+
1221
### Using cargo run
1322
```BASH
1423
> cargo build

scripts/create-git-hooks

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This script was taken from a StackOverflow answer that can be found at
2+
# https://stackoverflow.com/a/3464399
3+
#
4+
# This is used under the CC BY-SA 4.0 license:
5+
# https://creativecommons.org/licenses/by-sa/4.0/
6+
#
7+
# This file has been modified from its original form to include a BASE_DIR variable
8+
# and to include creation of the Git hooks directory if it does not already exist
9+
10+
11+
#!/bin/bash
12+
HOOK_NAMES="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-receive update post-receive post-update pre-auto-gc"
13+
BASE_DIR=$(git rev-parse --show-toplevel)
14+
HOOK_DIR=$BASE_DIR/.git/hooks
15+
16+
if [ ! -d $HOOK_DIR ]; then
17+
mkdir $HOOK_DIR
18+
fi
19+
20+
for hook in $HOOK_NAMES; do
21+
# If the hook already exists, is executable, and is not a symlink
22+
if [ ! -h $HOOK_DIR/$hook -a -x $HOOK_DIR/$hook ]; then
23+
mv $HOOK_DIR/$hook $HOOK_DIR/$hook.local
24+
fi
25+
# create the symlink, overwriting the file if it exists
26+
# probably the only way this would happen is if you're using an old version of git
27+
# -- back when the sample hooks were not executable, instead of being named ____.sample
28+
ln -s -f $BASE_DIR/scripts/hooks-wrapper $HOOK_DIR/$hook
29+
done

scripts/git/pre-commit

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -eo pipefail
4+
5+
cargo fmt
6+
7+
# Stage changes to files that were already staged
8+
git update-index --again

scripts/hooks-wrapper

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This script was taken from a StackOverflow answer that can be found at
2+
# https://stackoverflow.com/a/3464399
3+
# This is used under the CC BY-SA 4.0 license:
4+
# https://creativecommons.org/licenses/by-sa/4.0/
5+
6+
#!/bin/bash
7+
if [ -x $0.local ]; then
8+
$0.local "$@" || exit $?
9+
fi
10+
if [ -x scripts/git/$(basename $0) ]; then
11+
scripts/git/$(basename $0) "$@" || exit $?
12+
fi

0 commit comments

Comments
 (0)