Skip to content

Commit

Permalink
Merge #787
Browse files Browse the repository at this point in the history
787: Add utility to install git hooks. r=Emilgardis a=Alexhuszagh

Adds the `install-git-hooks` to `cross-dev`, which installs pre-commit hooks to ensure clippy and fmt are run on all commits.

Co-authored-by: Alex Huszagh <[email protected]>
  • Loading branch information
bors[bot] and Alexhuszagh authored Jun 14, 2022
2 parents e1676d2 + 4257c41 commit 5fe2145
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Internal

- #787 - add installer for git hooks.
- #786 - Migrate build script to rust: `cargo build-docker-image $TARGET`
- #730 - make FreeBSD builds more resilient.
- #670 - Use serde for deserialization of Cross.toml
Expand Down
24 changes: 24 additions & 0 deletions xtask/src/install_git_hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap::Args;

use std::path::Path;

#[derive(Args, Debug)]
pub struct InstallGitHooks {
/// Provide verbose diagnostic output.
#[clap(short, long)]
verbose: bool,
}

pub fn install_git_hooks(InstallGitHooks { verbose }: InstallGitHooks) -> cross::Result<()> {
let metadata = cross::cargo_metadata_with_args(
Some(Path::new(env!("CARGO_MANIFEST_DIR"))),
None,
verbose,
)?
.ok_or_else(|| eyre::eyre!("could not find cross workspace"))?;
let git_hooks = metadata.workspace_root.join(".git").join("hooks");
let cross_dev = metadata.workspace_root.join("xtask").join("src");
std::fs::copy(cross_dev.join("pre-commit"), git_hooks.join("pre-commit"))?;

Ok(())
}
6 changes: 6 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![deny(missing_debug_implementations, rust_2018_idioms)]

pub mod build_docker_image;
pub mod install_git_hooks;
pub mod target_info;

use std::path::PathBuf;

use clap::{Parser, Subcommand};

use self::build_docker_image::BuildDockerImage;
use self::install_git_hooks::InstallGitHooks;
use self::target_info::TargetInfo;

#[derive(Parser, Debug)]
Expand All @@ -22,6 +24,7 @@ enum Commands {
/// Extract and print info for targets.
TargetInfo(TargetInfo),
BuildDockerImage(BuildDockerImage),
InstallGitHooks(InstallGitHooks),
}

pub fn main() -> cross::Result<()> {
Expand All @@ -36,6 +39,9 @@ pub fn main() -> cross::Result<()> {
let engine = get_container_engine(args.engine.as_deref())?;
build_docker_image::build_docker_image(args, &engine)?;
}
Commands::InstallGitHooks(args) => {
install_git_hooks::install_git_hooks(args)?;
}
}

Ok(())
Expand Down
12 changes: 12 additions & 0 deletions xtask/src/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

echo "Running rustfmt and clippy checks."

set -ex

flags=(--all-features --all-targets --workspace)
cargo fmt -- --check
cargo clippy "${flags[@]}" -- --deny warnings
if cargo +nightly >/dev/null 2>&1; then
cargo +nightly clippy "${flags[@]}" -- --deny warnings
fi

0 comments on commit 5fe2145

Please sign in to comment.