-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a command to install a git hook to automatically run x.py test tidy --bless
#76356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a009e28
44af74f
7de557b
f53d436
68ca474
d585c96
0845627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,12 @@ pub fn setup(src_path: &Path, include_name: &str) { | |
_ => return, | ||
}; | ||
|
||
println!(); | ||
|
||
t!(install_git_hook_maybe(src_path)); | ||
|
||
println!(); | ||
|
||
println!("To get started, try one of the following commands:"); | ||
for cmd in suggestions { | ||
println!("- `x.py {}`", cmd); | ||
|
@@ -86,3 +92,44 @@ d) Install Rust from source" | |
}; | ||
Ok(template.to_owned()) | ||
} | ||
|
||
// install a git hook to automatically run tidy --bless, if they want | ||
fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> { | ||
let mut input = String::new(); | ||
println!( | ||
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality. | ||
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit | ||
to ensure your code is up to par. If you decide later that this behavior is undesirable, | ||
simply delete the `pre-commit` file from .git/hooks." | ||
); | ||
|
||
let should_install = loop { | ||
print!("Would you like to install the git hook?: [y/N] "); | ||
io::stdout().flush()?; | ||
input.clear(); | ||
io::stdin().read_line(&mut input)?; | ||
break match input.trim().to_lowercase().as_str() { | ||
"y" | "yes" => true, | ||
"n" | "no" | "" => false, | ||
_ => { | ||
println!("error: unrecognized option '{}'", input.trim()); | ||
println!("note: press Ctrl+C to exit"); | ||
continue; | ||
} | ||
}; | ||
}; | ||
|
||
Ok(if should_install { | ||
let src = src_path.join("src").join("etc").join("pre-commit.sh"); | ||
let dst = src_path.join(".git").join("hooks").join("pre-commit"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: this plays badly with worktrees. Do you mind changing it to use
Although I guess the user might not want to install it globally for all worktrees ... I'm not sure what the right behavior is here. Let's leave it for now and we can fix it in a follow-up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i also noticed a similar error trying to run a merge commit...i'm kind of starting to get out of my league at this point tho so i'm more than happy to move it to a follow up 😅 |
||
match fs::hard_link(src, dst) { | ||
Err(e) => println!( | ||
"x.py encountered an error -- do you already have the git hook installed?\n{}", | ||
e | ||
), | ||
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`"), | ||
}; | ||
} else { | ||
println!("Ok, skipping installation!"); | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Call `tidy --bless` before each commit | ||
# Copy this scripts to .git/hooks to activate, | ||
# and remove it from .git/hooks to deactivate. | ||
# | ||
|
||
set -Eeuo pipefail | ||
|
||
ROOT_DIR="$(git rev-parse --show-toplevel)"; | ||
COMMAND="$ROOT_DIR/x.py test tidy --bless"; | ||
caass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then | ||
COMMAND="python $COMMAND" | ||
fi | ||
|
||
echo "Running pre-commit script '$COMMAND'"; | ||
|
||
cd "$ROOT_DIR" | ||
|
||
$COMMAND; |
Uh oh!
There was an error while loading. Please reload this page.