Skip to content

Commit 9d664b2

Browse files
committed
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.
1 parent b03ccac commit 9d664b2

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
@@ -191,9 +191,9 @@ fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> {
191191
let mut input = String::new();
192192
println!(
193193
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
194-
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit
195-
to ensure your code is up to par. If you decide later that this behavior is undesirable,
196-
simply delete the `pre-commit` file from .git/hooks."
194+
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` before
195+
pushing your code to ensure your code is up to par. If you decide later that this behavior is
196+
undesirable, simply delete the `pre-push` file from .git/hooks."
197197
);
198198

199199
let should_install = loop {
@@ -213,21 +213,21 @@ simply delete the `pre-commit` file from .git/hooks."
213213
};
214214

215215
if should_install {
216-
let src = src_path.join("src").join("etc").join("pre-commit.sh");
216+
let src = src_path.join("src").join("etc").join("pre-push.sh");
217217
let git = t!(Command::new("git").args(&["rev-parse", "--git-common-dir"]).output().map(
218218
|output| {
219219
assert!(output.status.success(), "failed to run `git`");
220220
PathBuf::from(t!(String::from_utf8(output.stdout)).trim())
221221
}
222222
));
223-
let dst = git.join("hooks").join("pre-commit");
223+
let dst = git.join("hooks").join("pre-push");
224224
match fs::hard_link(src, &dst) {
225225
Err(e) => println!(
226226
"error: could not create hook {}: do you already have the git hook installed?\n{}",
227227
dst.display(),
228228
e
229229
),
230-
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`"),
230+
Ok(_) => println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-push`"),
231231
};
232232
} else {
233233
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)