mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-03-07 00:37:25 +00:00
Added the cargo dev remove
command for convenience
This commit is contained in:
parent
3d0984975e
commit
b48f041bef
2 changed files with 46 additions and 20 deletions
|
@ -38,9 +38,14 @@ fn main() {
|
||||||
},
|
},
|
||||||
("setup", Some(sub_command)) => match sub_command.subcommand() {
|
("setup", Some(sub_command)) => match sub_command.subcommand() {
|
||||||
("intellij", Some(matches)) => setup::intellij::run(matches.value_of("rustc-repo-path")),
|
("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)) => {
|
("serve", Some(matches)) => {
|
||||||
let port = matches.value_of("port").unwrap().parse().unwrap();
|
let port = matches.value_of("port").unwrap().parse().unwrap();
|
||||||
let lint = matches.value_of("lint");
|
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(
|
||||||
SubCommand::with_name("serve")
|
SubCommand::with_name("serve")
|
||||||
.about("Launch a local 'ALL the Clippy Lints' website in a browser")
|
.about("Launch a local 'ALL the Clippy Lints' website in a browser")
|
||||||
|
|
|
@ -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
|
/// 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
|
/// for formatting and should therefor only be used in a normal clone of clippy
|
||||||
const REPO_GIT_DIR: &str = ".git";
|
const REPO_GIT_DIR: &str = ".git";
|
||||||
const HOOK_SOURCE_PATH: &str = "util/etc/pre-commit.sh";
|
const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh";
|
||||||
const HOOK_TARGET_PATH: &str = ".git/hooks/pre-commit";
|
const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit";
|
||||||
|
|
||||||
pub fn run(force_override: bool) {
|
pub fn install_hook(force_override: bool) {
|
||||||
if let Err(_) = check_precondition(force_override) {
|
if check_precondition(force_override).is_err() {
|
||||||
return;
|
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
|
// 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
|
// a file with the flag set. We then copy this file here. The copy function will also
|
||||||
// include the `execute` permission.
|
// include the `execute` permission.
|
||||||
match fs::copy(HOOK_SOURCE_PATH, HOOK_TARGET_PATH) {
|
match fs::copy(HOOK_SOURCE_FILE, HOOK_TARGET_FILE) {
|
||||||
Ok(_) => println!("Git hook successfully installed :)"),
|
Ok(_) => {
|
||||||
|
println!("note: the hook can be removed with `cargo dev remove git-hook`");
|
||||||
|
println!("Git hook successfully installed :)");
|
||||||
|
},
|
||||||
Err(err) => println!(
|
Err(err) => println!(
|
||||||
"error: unable to copy `{}` to `{}` ({})",
|
"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
|
// 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 path.exists() {
|
||||||
if !force_override {
|
if force_override || super::ask_yes_no_question("Do you want to override the existing pre-commit hook it?") {
|
||||||
println!("warn: The found `.git` directory already has a commit hook");
|
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(());
|
return Err(());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return Err(());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue