From b4c2a9d1b2d7be51d984cf16cbef5002db39b84f Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Wed, 22 Aug 2018 00:52:37 +0800 Subject: [PATCH] Automatic setting up git hooks for the project: rustfmt, etc. (#1380) Also update document for this change on pre-commit hook config --- CONTRIBUTING.md | 13 +++++++++---- src/build/build.rs | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5f061f7c1..a0502049c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -37,11 +37,16 @@ There is a basic git [pre-commit](../.hooks/pre-commit) hook in the repo. The pre-commit hook will not prevent commits if style issues are present but it will indicate any files that need formatting. -To enable this, create a symlink in `.git/hooks` (note the relative path) - - +This pre-commit hook will be **automatically** configured in this project, once you run `cargo build` for the 1st time. + +Or you can config it manually with the following command without building, and check it: ``` -cd .git/hooks -ln -s -f ../../.hooks/pre-commit +git config core.hooksPath ./.hooks +git config --list | grep hook +``` +The output will be: +``` +core.hookspath=./.hooks ``` ## Running rustfmt diff --git a/src/build/build.rs b/src/build/build.rs index 2c3e09d6e..d7f2f7aba 100644 --- a/src/build/build.rs +++ b/src/build/build.rs @@ -25,11 +25,31 @@ use std::fs::{self, File}; use std::io::prelude::*; use std::io::Read; use std::path::{self, Path, PathBuf}; +use std::process::Command; + use tar::Archive; const WEB_WALLET_TAG: &str = "0.3.0.1"; fn main() { + // Setting up git hooks in the project: rustfmt and so on. + let git_hooks = format!( + "git config core.hooksPath {}", + PathBuf::from("./.hooks").to_str().unwrap() + ); + + if cfg!(target_os = "windows") { + Command::new("cmd") + .args(&["/C", &git_hooks]) + .output() + .expect("failed to execute git config for hooks"); + } else { + Command::new("sh") + .args(&["-c", &git_hooks]) + .output() + .expect("failed to execute git config for hooks"); + } + // build and versioning information let mut opts = built::Options::default(); opts.set_dependencies(true);