Skip to content

Commit fb30270

Browse files
authored
Rollup merge of rust-lang#117524 - unleashed:bootstrap-create-hooks-dir, r=Mark-Simulacrum
bootstrap/setup: create hooks directory if non-existing When running `./x setup` on a local repository I chose to install a `pre-push` git hook, but this happened: ```shell Would you like to install the git hook?: [y/N] y error: could not create hook .git/hooks/pre-push: do you already have the git hook installed? No such file or directory (os error 2) thread 'main' panicked at src/core/build_steps/setup.rs:462:9: install_git_hook_maybe(&config) failed with No such file or directory (os error 2) ``` This was caused because the `.git/hooks` directory did not exist in my local repository. Creating it manually and re-running the command works fine. This PR tests for the above directory and if it does not exist then it _tries_ to create it before hard linking the pre-push hook - we use `fs::create_dir()` and disregard the result (ie. it could fail if the directory was created in the meantime) and proceed to call `fs::hard_link()` all the same.
2 parents db66598 + 274a6f3 commit fb30270

File tree

1 file changed

+6
-1
lines changed
  • src/bootstrap/src/core/build_steps

1 file changed

+6
-1
lines changed

src/bootstrap/src/core/build_steps/setup.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
469469
assert!(output.status.success(), "failed to run `git`");
470470
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
471471
}));
472-
let dst = git.join("hooks").join("pre-push");
472+
let hooks_dir = git.join("hooks");
473+
let dst = hooks_dir.join("pre-push");
473474
if dst.exists() {
474475
// The git hook has already been set up, or the user already has a custom hook.
475476
return Ok(());
@@ -486,6 +487,10 @@ undesirable, simply delete the `pre-push` file from .git/hooks."
486487
println!("Ok, skipping installation!");
487488
return Ok(());
488489
}
490+
if !hooks_dir.exists() {
491+
// We need to (try to) create the hooks directory first.
492+
let _ = fs::create_dir(hooks_dir);
493+
}
489494
let src = config.src.join("src").join("etc").join("pre-push.sh");
490495
match fs::hard_link(src, &dst) {
491496
Err(e) => {

0 commit comments

Comments
 (0)