Skip to content

Commit c2d6ddd

Browse files
authored
Rollup merge of rust-lang#88313 - jyn514:pre-push, r=Mark-Simulacrum
Make the pre-commit script pre-push instead This should make it substantially less annoying, and hopefully more people will find it useful. In particular, it will no longer run tidy each time you run `git commit --amend` or rebase a branch. This also warns if you have the old script in pre-commit; see the HACK comment for details. r? ```@Mark-Simulacrum``` cc ```@caass```
2 parents 926e784 + 9d664b2 commit c2d6ddd

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/bootstrap/bin/main.rs

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn main() {
3030
println!("{}", suggestion);
3131
}
3232

33+
let pre_commit = config.src.join(".git").join("hooks").join("pre-commit");
3334
Build::new(config).build();
3435

3536
if suggest_setup {
@@ -42,6 +43,19 @@ fn main() {
4243
println!("{}", suggestion);
4344
}
4445

46+
// Give a warning if the pre-commit script is in pre-commit and not pre-push.
47+
// HACK: Since the commit script uses hard links, we can't actually tell if it was installed by x.py setup or not.
48+
// We could see if it's identical to src/etc/pre-push.sh, but pre-push may have been modified in the meantime.
49+
// Instead, look for this comment, which is almost certainly not in any custom hook.
50+
if std::fs::read_to_string(pre_commit).map_or(false, |contents| {
51+
contents.contains("https://github.com/rust-lang/rust/issues/77620#issuecomment-705144570")
52+
}) {
53+
println!(
54+
"warning: You have the pre-push script installed to .git/hooks/pre-commit. \
55+
Consider moving it to .git/hooks/pre-push instead, which runs less often."
56+
);
57+
}
58+
4559
if suggest_setup || changelog_suggestion.is_some() {
4660
println!("note: this message was printed twice to make it more likely to be seen");
4761
}

src/bootstrap/setup.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> {
271271
let mut input = String::new();
272272
println!(
273273
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
274-
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit
275-
to ensure your code is up to par. If you decide later that this behavior is undesirable,
276-
simply delete the `pre-commit` file from .git/hooks."
274+
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` before
275+
pushing your code to ensure your code is up to par. If you decide later that this behavior is
276+
undesirable, simply delete the `pre-push` file from .git/hooks."
277277
);
278278

279279
let should_install = loop {
@@ -293,21 +293,21 @@ simply delete the `pre-commit` file from .git/hooks."
293293
};
294294

295295
if should_install {
296-
let src = src_path.join("src").join("etc").join("pre-commit.sh");
296+
let src = src_path.join("src").join("etc").join("pre-push.sh");
297297
let git = t!(Command::new("git").args(&["rev-parse", "--git-common-dir"]).output().map(
298298
|output| {
299299
assert!(output.status.success(), "failed to run `git`");
300300
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
301301
}
302302
));
303-
let dst = git.join("hooks").join("pre-commit");
303+
let dst = git.join("hooks").join("pre-push");
304304
match fs::hard_link(src, &dst) {
305305
Err(e) => println!(
306306
"error: could not create hook {}: do you already have the git hook installed?\n{}",
307307
dst.display(),
308308
e
309309
),
310-
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`"),
310+
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-push`"),
311311
};
312312
} else {
313313
println!("Ok, skipping installation!");

src/etc/pre-commit.sh src/etc/pre-push.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
1616
COMMAND="python $COMMAND"
1717
fi
1818

19-
echo "Running pre-commit script '$COMMAND'"
19+
echo "Running pre-push script '$COMMAND'"
2020

2121
cd "$ROOT_DIR"
2222

0 commit comments

Comments
 (0)