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

Add pre-commit git hook & bootstrap script #233

Merged

Conversation

yanganto
Copy link
Contributor

@yanganto yanganto commented Jan 8, 2020

@yanganto yanganto self-assigned this Jan 8, 2020
@yanganto yanganto changed the title Add pre-commit git hook & bootstrap script [WIP] Add pre-commit git hook & bootstrap script Jan 8, 2020
@AurevoirXavier
Copy link
Member

AurevoirXavier commented Jan 8, 2020

Export PATH and remove Color Set (compatible with SourceTree or other Git Tools).

#!/bin/sh

export PATH=$PATH:$HOME/.cargo/bin

rustfmt --version &>/dev/null
if [ $? != 0 ]; then
	printf "[pre_commit] rustfmt not available.\n"
	printf "[pre_commit] rustfmt can be installed via -\n"
	printf "[pre_commit] rustup component add rustfmt\n"
	exit 1
fi


problem_files=()

# first collect all the files that need reformatting
for file in $(git diff --name-only --cached); do
    if [ ${file: -3} == ".rs" ]; then
        rustfmt --check $file
        if [ $? != 0 ]; then
            problem_files+=($file)
        fi
    fi
done

if [ ${#problem_files[@]} != 0 ]; then
  # reformat the files that need it and re-stage them.
  printf "[pre_commit] Plaese format the files via -\n"
  printf "[pre_commit] cargo fmt --all\n"
  printf "[pre_commit] If you want to keep your format with special reason,\n"
  printf "[pre_commit] you can use this macro. \n"
  printf "[pre_commit] #[rustfmt::skip]\n"
  exit 1
fi

printf "[pre_commit] rustfmt\n"

exit 0

@AurevoirXavier
Copy link
Member

AurevoirXavier commented Jan 8, 2020

Always failed

git --no-optional-locks -c color.branch=false -c color.diff=false -c color.status=false -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree commit -q -F /var/folders/v6/y_8scs3j797bpyn8gg0d14l00000gn/T/SourceTreeTemp.VLGTAx
Diff in /Users/xavier/Downloads/darwinia/srml/staking/src/lib.rs at line 2027:

[pre_commit] Plaese format the files via -
[pre_commit] cargo fmt --all
[pre_commit] If you want to keep your format with special reason,
[pre_commit] you can use this macro.
[pre_commit] #[rustfmt::skip]
Completed with errors, see above


Okay, the script only do checking.

- add pre-commit git hook
- add bootstrap script to help people setting up
- test rustfmt before running test
@yanganto yanganto changed the title [WIP] Add pre-commit git hook & bootstrap script Add pre-commit git hook & bootstrap script Jan 8, 2020
@yanganto
Copy link
Contributor Author

yanganto commented Jan 8, 2020

The bootstrap.sh is tested by ci environment, please check following travis report.
https://travis-ci.org/darwinia-network/darwinia/jobs/634114860?utm_medium=notification&utm_source=github_status

@yanganto
Copy link
Contributor Author

yanganto commented Jan 8, 2020

Thanks for the review.

@AurevoirXavier AurevoirXavier merged commit 9a08671 into darwinia-network:develop Jan 8, 2020
@yanganto yanganto deleted the trival/rustfmt-hook branch March 26, 2020 03:34
boundless-forest pushed a commit that referenced this pull request Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should we use cargo fmt on .githook?
2 participants