Skip to content

Commit 8128aed

Browse files
committed
Add pre-commit code formatting
1 parent 54390be commit 8128aed

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ cargo install cargo-edit
2424
cargo add panic_halt
2525
```
2626

27+
## Set Up Git Hooks
28+
29+
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:
30+
```bash
31+
./scripts/create-git-hooks
32+
```
33+
34+
This will create symlinks to the Git hooks, preserving any hooks that you may have already configured.
35+
2736
## Running
2837

2938
For a debug build
@@ -34,7 +43,7 @@ For a release build
3443
```
3544
cargo run --release
3645
```
37-
46+
3847
## Debugging
3948

4049
To debug you can use either `cargo run` which will try and launch a gdb session connecting to a local OpenOCD instance

scripts/create-git-hooks

+29
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

+8
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

+12
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)