Skip to content

Commit

Permalink
Merge pull request #160 from zeenix/sync-contributing
Browse files Browse the repository at this point in the history
πŸ“ CONTRIBUTING: Sync with zbus' CONTRIBUTING.md
  • Loading branch information
zeenix authored Oct 28, 2024
2 parents 7885347 + d494f7d commit c62ebc4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 61 deletions.
8 changes: 8 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
GITHOOKS_DIR=$( cd -- "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd )
source $GITHOOKS_DIR/util.sh

ensure_rustup_installed
ensure_rustfmt_installed

check_formatting
8 changes: 8 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
GITHOOKS_DIR=$( cd -- "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd )
source $GITHOOKS_DIR/util.sh

ensure_rustup_installed
ensure_clippy_installed

check_clippy
91 changes: 91 additions & 0 deletions .githooks/util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/bash
#
# Utility functions for git hook scripts.

if test -t 1 && test -n "$(tput colors)" && test "$(tput colors)" -ge 8; then
bold="$(tput bold)"
normal="$(tput sgr0)"
green="$(tput setaf 2)"
red="$(tput setaf 1)"
blue="$(tput setaf 4)"

function hook_failure {
echo "${red}${bold}FAILED:${normal} ${1}${normal}"
exit 1
}

function hook_info {
echo "${blue}${1}${normal}"
}

function hook_success {
echo "${green}${bold}SUCCESS:${normal} ${1}${normal}"
echo
echo
}

else
function hook_failure {
echo "FAILED: ${1}"
exit 1
}

function hook_info {
echo "{$1}"
}

function hook_success {
echo "SUCCESS: ${1}"
echo
echo
}
fi

function ensure_rustup_installed() {
hook_info "πŸ“¦οΈ Ensuring that rustup is installed"
if ! which rustup &> /dev/null; then
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH=$PATH:$HOME/.cargo/bin
if ! which rustup &> /dev/null; then
hook_failure "Failed to install rustup"
else
hook_success "rustup installed."
fi
else
hook_success "rustup is already installed."
fi
}

function ensure_rustfmt_installed() {
hook_info "πŸ“¦οΈ Ensuring that nightly rustfmt is installed"
if ! rustup component list --toolchain nightly|grep 'rustfmt-preview.*(installed)' &> /dev/null; then
rustup component add rustfmt-preview --toolchain nightly
hook_success "rustfmt installed."
else
hook_success "rustfmt is already installed."
fi
}

function ensure_clippy_installed() {
hook_info "πŸ“¦οΈ Ensuring that clippy is installed"
if ! rustup component list --toolchain stable|grep 'clippy.*(installed)' &> /dev/null; then
rustup component add clippy
hook_success "clippy installed."
else
hook_success "clippy is already installed."
fi
}

function check_formatting() {
hook_info "🎨 Running 'cargo +nightly fmt -- --check'"
cargo +nightly fmt -- --check \
&& hook_success "Project is formatted" \
|| hook_failure "Cargo format detected errors."
}

function check_clippy() {
hook_info "πŸ” Running 'cargo clippy -- -D warnings'"
cargo clippy -- -D warnings \
&& hook_success "Clippy detected no issues" \
|| hook_failure "Cargo clippy detected errors."
}
71 changes: 10 additions & 61 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Same rules apply here as for bug reports and feature requests. Plus:
* Please try your best to follow [these guidelines](https://wiki.gnome.org/Git/CommitMessages) for
commit messages.
* We also prefer adding [emoji prefixes to commit messages](https://gitmoji.carloscuesta.me/). Since
the the `gitmoji` CLI tool can be very [slow](https://github.com/zeenix/gimoji#rationale), we
recommend using [`gimoji`](https://github.com/zeenix/gimoji) instead.
the `gitmoji` CLI tool can be very [slow](https://github.com/zeenix/gimoji#rationale), we
recommend using [`gimoji`](https://github.com/zeenix/gimoji) instead. You can also pick an emoji
direcitly from [here](https://gitmoji.dev/).
* Add details to each commit about the changes it contains. PR description is for summarizing the
overall changes in the PR, while commit logs are for describing the specific changes of the
commit in question.
Expand Down Expand Up @@ -74,65 +75,13 @@ Please note that there are times when clippy is wrong and you know what you are
cases, it's acceptable to tell clippy to
[ignore the specific error or warning in the code](https://github.com/rust-lang/rust-clippy#allowingdenying-lints).

If you intend to contribute often or think that's very likely, we recommend you setup the following git
hooks:

* Pre-commit hook that goes in the `.git/hooks/pre-commit` file:

```sh
if ! which rustup &> /dev/null; then
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH=$PATH:$HOME/.cargo/bin
if ! which rustup &> /dev/null; then
echo "Failed to install rustup"
fi
fi

if ! rustup component list --toolchain nightly|grep 'rustfmt-preview.*(installed)' &> /dev/null; then
echo "Installing nightly rustfmt.."
rustup component add rustfmt-preview --toolchain nightly
echo "rustfmt installed."
fi

echo "--Checking style--"
cargo +nightly fmt --all -- --check
if test $? != 0; then
echo "--Checking style fail--"
echo "Please fix the above issues, either manually or by running: cargo +nightly fmt --all"

exit -1
else
echo "--All very stylish 😎--"
fi
```

* Pre-push hook that goes in the `.git/hooks/pre-push` file:

```sh
if ! which rustup &> /dev/null; then
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH=$PATH:$HOME/.cargo/bin
if ! which rustup &> /dev/null; then
echo "Failed to install rustup"
fi
fi

if ! rustup component list --toolchain stable|grep 'clippy.*(installed)' &> /dev/null; then
echo "Installing clippy.."
rustup component add clippy
echo "clippy installed."
fi

echo "--Analysing code πŸ”--"
cargo clippy -- -D warnings
if test $? != 0; then
echo "--Issues with code. See warnings/errors above--"

exit -1
else
echo "--Code looks good πŸ‘--"
fi
```
If you intend to contribute often or think that's very likely, we recommend you setup the git hook
scripts contained within this repository. You can enable them with:

```sh
git config --local core.hooksPath .githooks/
```


## Conduct

Expand Down

0 comments on commit c62ebc4

Please sign in to comment.