Skip to content

Commit

Permalink
Add pre-commit code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Serneum committed Apr 8, 2022
1 parent 5f46a75 commit bad8bdc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,15 @@ You can find documentation on installing Rust [here](https://www.rust-lang.org/t

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

## Set Up Git Hooks

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:
```bash
./scripts/create-git-hooks
```

This will create symlinks to the Git hooks, preserving any hooks that you may have already configured.

### Using cargo run
```BASH
> cargo build
Expand Down
28 changes: 28 additions & 0 deletions scripts/create-git-hooks
@@ -0,0 +1,28 @@
# This script was taken from a StackOverflow answer that can be found at
# https://stackoverflow.com/a/3464399
#
# This is used under the CC BY-SA 4.0 license:
# https://creativecommons.org/licenses/by-sa/4.0/
#
# This file has been modified from its original form to include a BASE_DIR variable


#!/bin/bash
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"
BASE_DIR=$(git rev-parse --show-toplevel)
HOOK_DIR=$BASE_DIR/.git/hooks

if [ ! -d $HOOK_DIR ]; then
mkdir $HOOK_DIR
fi

for hook in $HOOK_NAMES; do
# If the hook already exists, is executable, and is not a symlink
if [ ! -h $HOOK_DIR/$hook -a -x $HOOK_DIR/$hook ]; then
mv $HOOK_DIR/$hook $HOOK_DIR/$hook.local
fi
# create the symlink, overwriting the file if it exists
# probably the only way this would happen is if you're using an old version of git
# -- back when the sample hooks were not executable, instead of being named ____.sample
ln -s -f $BASE_DIR/scripts/hooks-wrapper $HOOK_DIR/$hook
done
8 changes: 8 additions & 0 deletions scripts/git/pre-commit
@@ -0,0 +1,8 @@
#!/bin/bash

set -eo pipefail

cargo fmt

# Stage changes to files that were already staged
git update-index --again
12 changes: 12 additions & 0 deletions scripts/hooks-wrapper
@@ -0,0 +1,12 @@
# This script was taken from a StackOverflow answer that can be found at
# https://stackoverflow.com/a/3464399
# This is used under the CC BY-SA 4.0 license:
# https://creativecommons.org/licenses/by-sa/4.0/

#!/bin/bash
if [ -x $0.local ]; then
$0.local "$@" || exit $?
fi
if [ -x scripts/git/$(basename $0) ]; then
scripts/git/$(basename $0) "$@" || exit $?
fi

0 comments on commit bad8bdc

Please sign in to comment.