mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-28 17:53:56 +00:00
Reimplement format-hook using a rust binary
This commit is contained in:
parent
1e554d551f
commit
cbce28a348
@ -1,10 +1,10 @@
|
|||||||
[alias]
|
[alias]
|
||||||
# Automatically generates the ast and syntax kinds files
|
# Automatically generates the ast and syntax kinds files
|
||||||
gen-syntax = "run --package tools -- gen-syntax"
|
gen-syntax = "run --package tools --bin tools -- gen-syntax"
|
||||||
gen-tests = "run --package tools -- gen-tests"
|
gen-tests = "run --package tools --bin tools -- gen-tests"
|
||||||
install-code = "run --package tools -- install-code"
|
install-code = "run --package tools --bin tools -- install-code"
|
||||||
format = "run --package tools -- format"
|
format = "run --package tools --bin tools -- format"
|
||||||
format-hook = "run --package tools -- format-hook"
|
format-hook = "run --package tools --bin tools -- format-hook"
|
||||||
|
|
||||||
render-test = "run --package ra_cli -- render-test"
|
render-test = "run --package ra_cli -- render-test"
|
||||||
parse = "run --package ra_cli -- parse"
|
parse = "run --package ra_cli -- parse"
|
||||||
|
37
crates/tools/src/bin/pre-commit.rs
Normal file
37
crates/tools/src/bin/pre-commit.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
use std::{
|
||||||
|
process::{Command},
|
||||||
|
};
|
||||||
|
|
||||||
|
use tools::{Result, run_rustfmt, run, project_root};
|
||||||
|
use failure::bail;
|
||||||
|
|
||||||
|
fn main() -> tools::Result<()> {
|
||||||
|
run_rustfmt(tools::Overwrite)?;
|
||||||
|
update_staged()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_staged() -> Result<()> {
|
||||||
|
let root = project_root();
|
||||||
|
let output = Command::new("git")
|
||||||
|
.arg("diff")
|
||||||
|
.arg("--name-only")
|
||||||
|
.arg("--cached")
|
||||||
|
.current_dir(&root)
|
||||||
|
.output()?;
|
||||||
|
if !output.status.success() {
|
||||||
|
bail!(
|
||||||
|
"`git diff --name-only --cached` exited with {}",
|
||||||
|
output.status
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for line in String::from_utf8(output.stdout)?.lines() {
|
||||||
|
run(
|
||||||
|
&format!(
|
||||||
|
"git update-index --add {}",
|
||||||
|
root.join(line).to_string_lossy()
|
||||||
|
),
|
||||||
|
".",
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
fs::OpenOptions,
|
fs::copy,
|
||||||
io::{Write, Error, ErrorKind}
|
io::{Error, ErrorKind}
|
||||||
};
|
};
|
||||||
#[cfg(unix)]
|
|
||||||
use std::os::unix::fs::OpenOptionsExt;
|
|
||||||
|
|
||||||
use failure::bail;
|
use failure::bail;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn project_root() -> PathBuf {
|
pub fn project_root() -> PathBuf {
|
||||||
Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
Path::new(&env!("CARGO_MANIFEST_DIR"))
|
||||||
.ancestors()
|
.ancestors()
|
||||||
.nth(2)
|
.nth(2)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -122,24 +120,14 @@ fn install_rustfmt() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn install_format_hook() -> Result<()> {
|
pub fn install_format_hook() -> Result<()> {
|
||||||
let path = Path::new("./.git/hooks/pre-commit");
|
let result_path = Path::new("./.git/hooks/pre-commit");
|
||||||
if !path.exists() {
|
if !result_path.exists() {
|
||||||
let mut open_options = OpenOptions::new();
|
run("cargo build --package tools --bin pre-commit", ".")?;
|
||||||
#[cfg(unix)]
|
if cfg!(windows) {
|
||||||
{
|
copy("./target/debug/pre-commit.exe", result_path)?;
|
||||||
// Set as executable
|
} else {
|
||||||
open_options.mode(0o770);
|
copy("./target/debug/pre-commit", result_path)?;
|
||||||
}
|
}
|
||||||
let mut file = open_options.write(true).create(true).open(path)?;
|
|
||||||
write!(
|
|
||||||
file,
|
|
||||||
r#"#!/bin/sh
|
|
||||||
|
|
||||||
cargo format
|
|
||||||
for path in $( git diff --name-only --cached ); do
|
|
||||||
git update-index --add $path
|
|
||||||
done"#
|
|
||||||
)?;
|
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
|
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user