Skip to content

Commit bbf7bed

Browse files
committed
Added the cargo dev remove command for convenience
1 parent ac08436 commit bbf7bed

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

clippy_dev/src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ fn main() {
3838
},
3939
("setup", Some(sub_command)) => match sub_command.subcommand() {
4040
("intellij", Some(matches)) => setup::intellij::run(matches.value_of("rustc-repo-path")),
41-
("git-hook", Some(matches)) => setup::git_hook::run(matches.is_present("force-override")),
41+
("git-hook", Some(matches)) => setup::git_hook::install_hook(matches.is_present("force-override")),
4242
_ => {},
4343
},
44+
("remove", Some(sub_command)) => {
45+
if let ("git-hook", Some(_)) = sub_command.subcommand() {
46+
setup::git_hook::remove_hook();
47+
}
48+
},
4449
("serve", Some(matches)) => {
4550
let port = matches.value_of("port").unwrap().parse().unwrap();
4651
let lint = matches.value_of("lint");
@@ -173,6 +178,12 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
173178
),
174179
),
175180
)
181+
.subcommand(
182+
SubCommand::with_name("remove")
183+
.about("Support for undoing changes done by the setup command")
184+
.setting(AppSettings::ArgRequiredElseHelp)
185+
.subcommand(SubCommand::with_name("git-hook").about("Remove any existing pre-commit git hook")),
186+
)
176187
.subcommand(
177188
SubCommand::with_name("serve")
178189
.about("Launch a local 'ALL the Clippy Lints' website in a browser")

clippy_dev/src/setup/git_hook.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::path::Path;
66
/// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool
77
/// for formatting and should therefor only be used in a normal clone of clippy
88
const REPO_GIT_DIR: &str = ".git";
9-
const HOOK_SOURCE_PATH: &str = "util/etc/pre-commit.sh";
10-
const HOOK_TARGET_PATH: &str = ".git/hooks/pre-commit";
9+
const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh";
10+
const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit";
1111

12-
pub fn run(force_override: bool) {
13-
if let Err(_) = check_precondition(force_override) {
12+
pub fn install_hook(force_override: bool) {
13+
if check_precondition(force_override).is_err() {
1414
return;
1515
}
1616

@@ -23,11 +23,14 @@ pub fn run(force_override: bool) {
2323
// that we can check in a file with execution permissions and the sync it to create
2424
// a file with the flag set. We then copy this file here. The copy function will also
2525
// include the `execute` permission.
26-
match fs::copy(HOOK_SOURCE_PATH, HOOK_TARGET_PATH) {
27-
Ok(_) => println!("Git hook successfully installed :)"),
26+
match fs::copy(HOOK_SOURCE_FILE, HOOK_TARGET_FILE) {
27+
Ok(_) => {
28+
println!("note: the hook can be removed with `cargo dev remove git-hook`");
29+
println!("Git hook successfully installed :)");
30+
},
2831
Err(err) => println!(
2932
"error: unable to copy `{}` to `{}` ({})",
30-
HOOK_SOURCE_PATH, HOOK_TARGET_PATH, err
33+
HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err
3134
),
3235
}
3336
}
@@ -41,21 +44,33 @@ fn check_precondition(force_override: bool) -> Result<(), ()> {
4144
}
4245

4346
// Make sure that we don't override an existing hook by accident
44-
let path = Path::new(HOOK_TARGET_PATH);
47+
let path = Path::new(HOOK_TARGET_FILE);
4548
if path.exists() {
46-
if !force_override {
47-
println!("warn: The found `.git` directory already has a commit hook");
49+
if force_override || super::ask_yes_no_question("Do you want to override the existing pre-commit hook it?") {
50+
return delete_git_hook_file(path);
4851
}
52+
return Err(());
53+
}
54+
55+
Ok(())
56+
}
4957

50-
if force_override || super::ask_yes_no_question("Do you want to override it?") {
51-
if fs::remove_file(path).is_err() {
52-
println!("error: unable to delete existing pre-commit git hook");
53-
return Err(());
54-
}
55-
} else {
56-
return Err(());
58+
pub fn remove_hook() {
59+
let path = Path::new(HOOK_TARGET_FILE);
60+
if path.exists() {
61+
if delete_git_hook_file(path).is_ok() {
62+
println!("Git hook successfully removed :)");
5763
}
64+
} else {
65+
println!("No pre-commit hook was found. You're good to go :)");
5866
}
67+
}
5968

60-
Ok(())
69+
fn delete_git_hook_file(path: &Path) -> Result<(), ()> {
70+
if fs::remove_file(path).is_err() {
71+
println!("error: unable to delete existing pre-commit git hook");
72+
Err(())
73+
} else {
74+
Ok(())
75+
}
6176
}

0 commit comments

Comments
 (0)