Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(git): add git pre-commit and setup script #513

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Setup

__Important:__ Make sure to install the provided git hooks by running `setup-dev-workspace.sh`!

### Install Rust

It is recommended to install rust through [rustup](https://rustup.rs). System package managers often have outdated versions of Rust or don't include rustfmt/clippy.
Expand Down
49 changes: 49 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh
# Author: Sven Lechner (SirWindfield)
# License: GPLv3

require_clean_work_tree() {
# Update index
git update-index -q --ignore-submodules --refresh
err=0

# Disallow unstaged changes in the working tree
if ! git diff-files --quiet --ignore-submodules --; then
echo >&2 "Cannot commit: you have unstaged changes."
git diff-files --name-status -r --ignore-submodules -- >&2
err=1
fi

if [ $err = 1 ]; then
echo >&2 "Please stage or stash them."
exit 1
fi
}

echo "→ Checking for local changes..."
require_clean_work_tree

echo "→ Formatting Rust code..."
cargo fmt
if [ $? -ne 0 ]; then
exit 1
fi

for path in $(git diff --name-only --cached); do
git update-index --add $path
done

echo "→ Building pre-commit build artifacts..."
cargo check --quiet --no-default-features --features "rodio_backend,dbus_keyring"
if [ $? -ne 0 ]; then
exit 1
fi
cargo build --quiet --no-default-features --features "rodio_backend,dbus_keyring"

# Linting is only done with the rodio backend and the keyring feature as those should be
# compilable for every supported platform without external library needs.
echo "→ Linting Rust code..."
cargo clippy --no-default-features --features "rodio_backend,dbus_keyring" -- -D warnings

echo "→ Testing Rust code..."
cargo test --no-default-features --features "rodio_backend,dbus_keyring"
6 changes: 6 additions & 0 deletions setup-dev-workspace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# Author: Sven Lechner (SirWindfield)
# License: GPLv3

# Copies all hooks into the .git/hooks directory
cp hooks/* .git/hooks