Repository navigation
Add forbid-new-submodules as builtin hook
#1853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed8307c
Add `forbid-new-submodules` builtin hook
RicardoVercetti ca50aa0
Reformat: make clippy happy
RicardoVercetti 8dbb5c9
fix lint CI error
RicardoVercetti 37bb701
fix failing CI tests
RicardoVercetti ac36197
Update builtins doc & match hook description with pre-commit's
RicardoVercetti 09f15bc
Include filenames & from_ref, to_ref commit hashes in command
RicardoVercetti e26bcff
more detailed remove submodules message
RicardoVercetti 2215c24
revert accidental pre-commit-config.yaml changes
2f8b7a4
Tweak and add tests
j178 a153166
Tweaks
j178 5615b47
Fix `-z` paring
j178 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
crates/prek/src/hooks/pre_commit_hooks/forbid_new_submodules.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| use std::borrow::Cow; | ||
| use std::path::Path; | ||
|
|
||
| use itertools::Itertools; | ||
| use prek_consts::env_vars::EnvVars; | ||
|
|
||
| use crate::git; | ||
| use crate::hook::Hook; | ||
|
|
||
| pub(crate) async fn forbid_new_submodules( | ||
| hook: &Hook, | ||
| filenames: &[&Path], | ||
| ) -> Result<(i32, Vec<u8>), anyhow::Error> { | ||
| let diff_arg = if let (Ok(from_ref), Ok(to_ref)) = ( | ||
| EnvVars::var("PRE_COMMIT_FROM_REF"), | ||
| EnvVars::var("PRE_COMMIT_TO_REF"), | ||
| ) { | ||
| Cow::Owned(format!("{from_ref}...{to_ref}")) | ||
| } else { | ||
| Cow::Borrowed("--staged") | ||
|
j178 marked this conversation as resolved.
Resolved
|
||
| }; | ||
|
|
||
| let stdout = git::git_cmd("git diff")? | ||
| .current_dir(hook.work_dir()) | ||
| .arg("diff") | ||
| .arg("--relative") | ||
| .arg("--diff-filter=A") | ||
|
j178 marked this conversation as resolved.
Resolved
|
||
| .arg("--no-ext-diff") | ||
| .arg("--raw") | ||
| .arg("-z") | ||
| .arg(diff_arg.as_ref()) | ||
|
j178 marked this conversation as resolved.
Resolved
|
||
| .arg("--") | ||
| .args(filenames) | ||
| .check(true) | ||
|
j178 marked this conversation as resolved.
Resolved
|
||
| .output() | ||
| .await? | ||
| .stdout; | ||
|
|
||
| let new_submodules = collect_new_submodules(&stdout); | ||
| if new_submodules.is_empty() { | ||
| Ok((0, Vec::new())) | ||
| } else { | ||
| let message = render_message(&new_submodules); | ||
| Ok((1, message.into_bytes())) | ||
| } | ||
| } | ||
|
|
||
| fn render_message(new_submodules: &[&str]) -> String { | ||
| let mut message = new_submodules | ||
| .iter() | ||
| .map(|filename| format!("{filename}: new submodule introduced")) | ||
| .join("\n"); | ||
|
|
||
| message.push_str("\n\n"); | ||
| message.push_str(indoc::indoc! {" | ||
| This commit introduces new git submodules. | ||
| Did you unintentionally `git add .`? | ||
| To fix this, run `git rm <submodule>`. | ||
| Also check `.gitmodules` for any unintended changes. | ||
| "}); | ||
|
|
||
| message | ||
| } | ||
|
|
||
| fn collect_new_submodules(stdout: &[u8]) -> Vec<&str> { | ||
| let mut entries = stdout | ||
| .split(|&b| b == b'\0') | ||
| .filter(|entry| !entry.is_empty()); | ||
| let mut new_submodules = Vec::new(); | ||
|
|
||
| while let Some(metadata) = entries.next() { | ||
| let Some(filename) = entries.next() else { | ||
| break; | ||
| }; | ||
|
|
||
| let Ok(metadata) = std::str::from_utf8(metadata) else { | ||
| continue; | ||
| }; | ||
| let Ok(filename) = std::str::from_utf8(filename) else { | ||
| continue; | ||
| }; | ||
|
|
||
| // https://git-scm.com/docs/gitdatamodel#Documentation/gitdatamodel.txt-tree | ||
| if metadata.split_whitespace().nth(1) == Some("160000") { | ||
| new_submodules.push(filename); | ||
| } | ||
| } | ||
|
|
||
| new_submodules | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #[test] | ||
| fn collect_new_submodules_ignores_non_submodules() { | ||
| let stdout = b":000000 100644 0000000 abcdef1 A\0.gitmodules\0"; | ||
|
|
||
| let new_submodules = super::collect_new_submodules(stdout); | ||
|
|
||
| assert!(new_submodules.is_empty()); | ||
| } | ||
|
j178 marked this conversation as resolved.
Resolved
|
||
|
|
||
| #[test] | ||
| fn collect_new_submodules_finds_new_submodules() { | ||
| let stdout = b":000000 100644 0000000 abcdef1 A\0.gitmodules\0\ | ||
| :000000 160000 0000000 abcdef2 A\0project2/sub module\0\ | ||
| :000000 160000 0000000 abcdef3 A\0vendor/dep\0"; | ||
|
|
||
| let new_submodules = super::collect_new_submodules(stdout); | ||
|
|
||
| assert_eq!(new_submodules, vec!["project2/sub module", "vendor/dep"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn render_message() { | ||
| let message = super::render_message(&["project2/sub module", "vendor/dep"]); | ||
|
|
||
| assert_eq!( | ||
| message, | ||
| indoc::indoc! {" | ||
| project2/sub module: new submodule introduced | ||
| vendor/dep: new submodule introduced | ||
|
|
||
| This commit introduces new git submodules. | ||
| Did you unintentionally `git add .`? | ||
| To fix this, run `git rm <submodule>`. | ||
| Also check `.gitmodules` for any unintended changes. | ||
| "} | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.