Skip to content

Commit ba62c04

Browse files
felibloj178
andauthored
Add pretty_format_json as builtin hook (#915)
## Description Following the information provided in [880](#880). This is an implementation of the `pretty-format-json` hook. The [pretty-format-json](https://grep.app/search?f.path.pattern=.pre-commit-config.yaml&q=pretty-format-json) has 1k grep.app hits. It's the final unimplemented hook used by `airflow` as mentioned in [this comment](#880 (comment)). ---- ## Notes - **Preserve JSON Order** - Added the `preserve_order` feature to `serde_json`. This prevents `serde` from automatically ordering JSON data, which is important for comparisons with older implementations. - This change does not appear to affect other tests/features. - By default, `serde` uses a `BTreeMap` for `Object`. Enabling `preserve_order` switches it to `IndexMap`. - This keeps the sorting logic within the `sort-keys` argument. - **Git-Style Diff with `similar`** - Added the `similar` library to perform git-style diff checks. - Binary size increase is minimal. - Useful as a general utility and could be leveraged in other parts of the project, maybe needed to be relocated? - Added color highlighting to the diffs, improving readability compared to original `pre-commit`. ```bash cargo clean cargo build --release ``` - Master branch:  **8.49 MB** - Feature branch: **8.55 MB** - **+66,176 bytes** (**+0.74%**) increase from master to feature branch _Q: why is my size so much smaller than both sizes mentioned [here](#884 (comment) ----- ### Performance Ran these commands on the `airflow` repository: ```bash # old pre-commit run pretty-format-json --all-files --verbose Format JSON files........................................................Passed - hook id: pretty-format-json - duration: 0.03s # Old prek implementation (python) prek run pretty-format-json --all-files --verbose Format JSON files........................................................Passed - hook id: pretty-format-json - duration: 0.03s # New implementation (rust) prek run pretty-format-json --all-files --verbose Format JSON files........................................................Passed - hook id: pretty-format-json - duration: 0.00s ``` ## Output Old pre-commit: <img width="601" height="376" alt="image" src="https://github.com/user-attachments/assets/f0553015-24cf-4d23-98e8-2759f912c9b3" /> New: <img width="707" height="430" alt="image" src="https://github.com/user-attachments/assets/fa7ce303-caa9-4139-9e77-6e58db5f0725" /> Where both would autofix to the same: <img width="349" height="304" alt="image" src="https://github.com/user-attachments/assets/67baf526-787b-4aa6-a0fe-7859a2a5fb8b" /> --------- Co-authored-by: Felix Blom <70511386+Felix-Blom@users.noreply.github.com> Co-authored-by: Jo <10510431+j178@users.noreply.github.com>
1 parent dbca904 commit ba62c04

File tree

10 files changed

+1288
-0
lines changed

10 files changed

+1288
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ serde_stacker = { version = "0.1.12" }
8585
serde-saphyr = { version = "0.0.21", default-features = false }
8686
shlex = { version = "1.3.0" }
8787
globset = { version = "0.4.18" }
88+
similar = { version = "2.7.0" }
8889
strum = { version = "0.28.0", features = ["derive"] }
8990
target-lexicon = { version = "0.13.0" }
9091
tempfile = { version = "3.25.0" }

crates/prek/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ serde_json = { workspace = true }
7373
serde_stacker = { workspace = true }
7474
serde-saphyr = { workspace = true }
7575
shlex = { workspace = true }
76+
similar = { workspace = true }
7677
strum = { workspace = true }
7778
target-lexicon = { workspace = true }
7879
tempfile = { workspace = true }

crates/prek/src/hooks/builtin_hooks/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub(crate) enum BuiltinHooks {
4848
FixByteOrderMarker,
4949
MixedLineEnding,
5050
NoCommitToBranch,
51+
PrettyFormatJson,
5152
TrailingWhitespace,
5253
}
5354

@@ -97,6 +98,7 @@ impl BuiltinHooks {
9798
}
9899
Self::MixedLineEnding => pre_commit_hooks::mixed_line_ending(hook, filenames).await,
99100
Self::NoCommitToBranch => pre_commit_hooks::no_commit_to_branch(hook).await,
101+
Self::PrettyFormatJson => pre_commit_hooks::pretty_format_json(hook, filenames).await,
100102
Self::TrailingWhitespace => {
101103
pre_commit_hooks::fix_trailing_whitespace(hook, filenames).await
102104
}
@@ -362,6 +364,18 @@ impl BuiltinHook {
362364
..Default::default()
363365
},
364366
},
367+
BuiltinHooks::PrettyFormatJson => BuiltinHook {
368+
id: "pretty-format-json".to_string(),
369+
name: "pretty format json".to_string(),
370+
entry: "pretty-format-json".to_string(),
371+
priority: None,
372+
options: HookOptions {
373+
description: Some("checks that JSON files are pretty-formatted.".to_string()),
374+
types: Some(tags::TAG_SET_JSON),
375+
stages: Some([Stage::PreCommit, Stage::PrePush, Stage::Manual].into()),
376+
..Default::default()
377+
},
378+
},
365379
BuiltinHooks::TrailingWhitespace => BuiltinHook {
366380
id: "trailing-whitespace".to_string(),
367381
name: "trim trailing whitespace".to_string(),

crates/prek/src/hooks/pre_commit_hooks/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod fix_end_of_file;
2424
mod fix_trailing_whitespace;
2525
mod mixed_line_ending;
2626
mod no_commit_to_branch;
27+
mod pretty_format_json;
2728
mod shebangs;
2829

2930
pub(crate) use check_added_large_files::check_added_large_files;
@@ -45,6 +46,7 @@ pub(crate) use fix_end_of_file::fix_end_of_file;
4546
pub(crate) use fix_trailing_whitespace::fix_trailing_whitespace;
4647
pub(crate) use mixed_line_ending::mixed_line_ending;
4748
pub(crate) use no_commit_to_branch::no_commit_to_branch;
49+
pub(crate) use pretty_format_json::pretty_format_json;
4850

4951
/// Hooks from `https://github.com/pre-commit/pre-commit-hooks`.
5052
#[derive(strum::EnumString)]
@@ -68,6 +70,10 @@ pub(crate) enum PreCommitHooks {
6870
MixedLineEnding,
6971
DetectPrivateKey,
7072
NoCommitToBranch,
73+
// `pretty-format-json` is intentionally builtin-only for now. Do not enable
74+
// automatic fast-path replacement until parity coverage against upstream
75+
// Python is broad enough to trust it as the default implementation.
76+
// PrettyFormatJson,
7177
TrailingWhitespace,
7278
}
7379

0 commit comments

Comments
 (0)