@@ -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
88const 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