Added the cargo dev remove command for convenience

This commit is contained in:
xFrednet 2021-06-16 18:59:28 +02:00 committed by flip1995
parent 3d0984975e
commit b48f041bef
No known key found for this signature in database
GPG key ID: 1CA0DF2AF59D68A5
2 changed files with 46 additions and 20 deletions

View file

@ -38,9 +38,14 @@ fn main() {
},
("setup", Some(sub_command)) => match sub_command.subcommand() {
("intellij", Some(matches)) => setup::intellij::run(matches.value_of("rustc-repo-path")),
("git-hook", Some(matches)) => setup::git_hook::run(matches.is_present("force-override")),
("git-hook", Some(matches)) => setup::git_hook::install_hook(matches.is_present("force-override")),
_ => {},
},
("remove", Some(sub_command)) => {
if let ("git-hook", Some(_)) = sub_command.subcommand() {
setup::git_hook::remove_hook();
}
},
("serve", Some(matches)) => {
let port = matches.value_of("port").unwrap().parse().unwrap();
let lint = matches.value_of("lint");
@ -173,6 +178,12 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
),
),
)
.subcommand(
SubCommand::with_name("remove")
.about("Support for undoing changes done by the setup command")
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(SubCommand::with_name("git-hook").about("Remove any existing pre-commit git hook")),
)
.subcommand(
SubCommand::with_name("serve")
.about("Launch a local 'ALL the Clippy Lints' website in a browser")

View file

@ -6,11 +6,11 @@ use std::path::Path;
/// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool
/// for formatting and should therefor only be used in a normal clone of clippy
const REPO_GIT_DIR: &str = ".git";
const HOOK_SOURCE_PATH: &str = "util/etc/pre-commit.sh";
const HOOK_TARGET_PATH: &str = ".git/hooks/pre-commit";
const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh";
const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit";
pub fn run(force_override: bool) {
if let Err(_) = check_precondition(force_override) {
pub fn install_hook(force_override: bool) {
if check_precondition(force_override).is_err() {
return;
}
@ -23,11 +23,14 @@ pub fn run(force_override: bool) {
// that we can check in a file with execution permissions and the sync it to create
// a file with the flag set. We then copy this file here. The copy function will also
// include the `execute` permission.
match fs::copy(HOOK_SOURCE_PATH, HOOK_TARGET_PATH) {
Ok(_) => println!("Git hook successfully installed :)"),
match fs::copy(HOOK_SOURCE_FILE, HOOK_TARGET_FILE) {
Ok(_) => {
println!("note: the hook can be removed with `cargo dev remove git-hook`");
println!("Git hook successfully installed :)");
},
Err(err) => println!(
"error: unable to copy `{}` to `{}` ({})",
HOOK_SOURCE_PATH, HOOK_TARGET_PATH, err
HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err
),
}
}
@ -41,21 +44,33 @@ fn check_precondition(force_override: bool) -> Result<(), ()> {
}
// Make sure that we don't override an existing hook by accident
let path = Path::new(HOOK_TARGET_PATH);
let path = Path::new(HOOK_TARGET_FILE);
if path.exists() {
if !force_override {
println!("warn: The found `.git` directory already has a commit hook");
if force_override || super::ask_yes_no_question("Do you want to override the existing pre-commit hook it?") {
return delete_git_hook_file(path);
}
if force_override || super::ask_yes_no_question("Do you want to override it?") {
if fs::remove_file(path).is_err() {
println!("error: unable to delete existing pre-commit git hook");
return Err(());
}
} else {
return Err(());
}
}
Ok(())
}
pub fn remove_hook() {
let path = Path::new(HOOK_TARGET_FILE);
if path.exists() {
if delete_git_hook_file(path).is_ok() {
println!("Git hook successfully removed :)");
}
} else {
println!("No pre-commit hook was found. You're good to go :)");
}
}
fn delete_git_hook_file(path: &Path) -> Result<(), ()> {
if fs::remove_file(path).is_err() {
println!("error: unable to delete existing pre-commit git hook");
Err(())
} else {
Ok(())
}
}