1#!/bin/sh
2# Author: Sven Lechner (SirWindfield)
3# License: GPLv3
4
5require_clean_work_tree() {
6    # Update index
7    git update-index -q --ignore-submodules --refresh
8    err=0
9
10    # Disallow unstaged changes in the working tree
11    if ! git diff-files --quiet --ignore-submodules --; then
12        echo >&2 "Cannot commit: you have unstaged changes."
13        git diff-files --name-status -r --ignore-submodules -- >&2
14        err=1
15    fi
16
17    if [ $err = 1 ]; then
18        echo >&2 "Please stage or stash them."
19        exit 1
20    fi
21}
22
23echo "→ Checking for local changes..."
24require_clean_work_tree
25
26echo "→ Formatting Rust code..."
27cargo fmt
28if [ $? -ne 0 ]; then
29    exit 1
30fi
31
32for path in $(git diff --name-only --cached); do
33    git update-index --add $path
34done
35
36echo "→ Building pre-commit build artifacts..."
37cargo check --quiet --no-default-features --features "rodio_backend,dbus_keyring"
38if [ $? -ne 0 ]; then
39    exit 1
40fi
41cargo build --quiet --no-default-features --features "rodio_backend,dbus_keyring"
42
43# Linting is only done with the rodio backend and the keyring feature as those should be
44# compilable for every supported platform without external library needs.
45echo "→ Linting Rust code..."
46cargo clippy --no-default-features --features "rodio_backend,dbus_keyring" -- -D warnings
47
48echo "→ Testing Rust code..."
49cargo test --no-default-features --features "rodio_backend,dbus_keyring"
50