@@ -6,11 +6,11 @@ use std::path::Path;
6
6
/// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool
7
7
/// for formatting and should therefor only be used in a normal clone of clippy
8
8
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" ;
11
11
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 ( ) {
14
14
return ;
15
15
}
16
16
@@ -23,11 +23,14 @@ pub fn run(force_override: bool) {
23
23
// that we can check in a file with execution permissions and the sync it to create
24
24
// a file with the flag set. We then copy this file here. The copy function will also
25
25
// 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
+ } ,
28
31
Err ( err) => println ! (
29
32
"error: unable to copy `{}` to `{}` ({})" ,
30
- HOOK_SOURCE_PATH , HOOK_TARGET_PATH , err
33
+ HOOK_SOURCE_FILE , HOOK_TARGET_FILE , err
31
34
) ,
32
35
}
33
36
}
@@ -41,21 +44,33 @@ fn check_precondition(force_override: bool) -> Result<(), ()> {
41
44
}
42
45
43
46
// 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 ) ;
45
48
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 ) ;
48
51
}
52
+ return Err ( ( ) ) ;
53
+ }
54
+
55
+ Ok ( ( ) )
56
+ }
49
57
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 :)" ) ;
57
63
}
64
+ } else {
65
+ println ! ( "No pre-commit hook was found. You're good to go :)" ) ;
58
66
}
67
+ }
59
68
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
+ }
61
76
}
0 commit comments