@@ -46,6 +46,8 @@ pub fn setup(src_path: &Path, include_name: &str) {
46
46
_ => return ,
47
47
} ;
48
48
49
+ t ! ( install_git_hook_maybe( src_path) ) ;
50
+
49
51
println ! ( "To get started, try one of the following commands:" ) ;
50
52
for cmd in suggestions {
51
53
println ! ( "- `x.py {}`" , cmd) ;
@@ -86,3 +88,41 @@ d) Install Rust from source"
86
88
} ;
87
89
Ok ( template. to_owned ( ) )
88
90
}
91
+
92
+ // install a git hook to automatically run tidy --bless, if they want
93
+ fn install_git_hook_maybe ( src_path : & Path ) -> io:: Result < ( ) > {
94
+ let mut input = String :: new ( ) ;
95
+ println ! (
96
+ "Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
97
+ If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit
98
+ to ensure your code is up to par. If you decide later that this behavior is undesirable,
99
+ simply delete the `pre-commit` file from .git/hooks."
100
+ ) ;
101
+
102
+ let should_install = loop {
103
+ print ! ( "Would you like to install the git hook?: [y/N] " ) ;
104
+ io:: stdout ( ) . flush ( ) ?;
105
+ io:: stdin ( ) . read_line ( & mut input) ?;
106
+ break match input. trim ( ) . to_lowercase ( ) . as_str ( ) {
107
+ "y" | "yes" => true ,
108
+ // is this the right way to check for "entered nothing"?
109
+ "n" | "no" | "" => false ,
110
+ _ => {
111
+ println ! ( "error: unrecognized option '{}'" , input. trim( ) ) ;
112
+ println ! ( "note: press Ctrl+C to exit" ) ;
113
+ continue ;
114
+ }
115
+ } ;
116
+ } ;
117
+
118
+ if should_install {
119
+ let src = src_path. join ( "/etc/pre-commit.rs" ) ;
120
+ let dst = src_path. join ( "/.git/hooks/pre-commit" ) ;
121
+ fs:: hard_link ( src, dst) ?;
122
+ println ! ( "Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`" ) ;
123
+ } else {
124
+ println ! ( "Ok, skipping installation!" ) ;
125
+ } ;
126
+
127
+ Ok ( ( ) )
128
+ }
0 commit comments