Skip to content

Commit 1c1af94

Browse files
authored
Allow optional upgrade hints after semicolon in minimum_prek_version (#1536)
Closes #1490
1 parent 5797d53 commit 1c1af94

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

crates/prek/src/config.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,25 +1126,38 @@ fn deserialize_and_validate_minimum_version<'de, D>(
11261126
where
11271127
D: Deserializer<'de>,
11281128
{
1129-
let s = String::deserialize(deserializer)?;
1130-
if s.is_empty() {
1129+
let raw = String::deserialize(deserializer)?;
1130+
let (version_str, upgrade_hint) = match raw.split_once(';') {
1131+
Some((version, hint)) => {
1132+
let trimmed_hint = hint.trim();
1133+
if trimmed_hint.is_empty() {
1134+
(version, None)
1135+
} else {
1136+
(version, Some(trimmed_hint))
1137+
}
1138+
}
1139+
None => (raw.as_str(), None),
1140+
};
1141+
let version_str = version_str.trim();
1142+
if version_str.is_empty() {
11311143
return Ok(None);
11321144
}
11331145

1134-
let version = s
1146+
let version = version_str
11351147
.parse::<semver::Version>()
11361148
.map_err(serde::de::Error::custom)?;
11371149
let cur_version = version::version()
11381150
.version
11391151
.parse::<semver::Version>()
11401152
.expect("Invalid prek version");
11411153
if version > cur_version {
1154+
let hint = upgrade_hint.unwrap_or("Please consider updating prek.");
11421155
return Err(serde::de::Error::custom(format!(
1143-
"Required minimum prek version `{version}` is greater than current version `{cur_version}`. Please consider updating prek.",
1156+
"Required minimum prek version `{version}` is greater than current version `{cur_version}`. {hint}",
11441157
)));
11451158
}
11461159

1147-
Ok(Some(s))
1160+
Ok(Some(version_str.to_string()))
11481161
}
11491162

11501163
/// Deserializes a vector of strings and validates that each is a known file type tag.
@@ -1689,6 +1702,52 @@ mod tests {
16891702
");
16901703
}
16911704

1705+
#[test]
1706+
fn test_upgrade_hint() {
1707+
// Test that hint is included in error message when version is too new
1708+
let yaml = indoc::indoc! {r"
1709+
repos:
1710+
- repo: local
1711+
hooks:
1712+
- id: test-hook
1713+
name: Test Hook
1714+
entry: echo test
1715+
language: system
1716+
minimum_prek_version: '10.0.0; Use `brew upgrade` to upgrade prek'
1717+
"};
1718+
let err = serde_saphyr::from_str::<Config>(yaml).unwrap_err();
1719+
insta::assert_snapshot!(err, @"
1720+
error: line 8 column 23: Required minimum prek version `10.0.0` is greater than current version `0.3.1`. Use `brew upgrade` to upgrade prek at line 8, column 23
1721+
--> <input>:8:23
1722+
|
1723+
6 | entry: echo test
1724+
7 | language: system
1725+
8 | minimum_prek_version: '10.0.0; Use `brew upgrade` to upgrade prek'
1726+
| ^ Required minimum prek version `10.0.0` is greater than current version `0.3.1`. Use `brew upgrade` to upgrade prek at line 8, column 23
1727+
");
1728+
1729+
let yaml = indoc::indoc! {r"
1730+
repos:
1731+
- repo: local
1732+
hooks:
1733+
- id: test-hook
1734+
name: Test Hook
1735+
entry: echo test
1736+
language: system
1737+
minimum_prek_version: '10.0.0; '
1738+
"};
1739+
let err = serde_saphyr::from_str::<Config>(yaml).unwrap_err();
1740+
insta::assert_snapshot!(err, @"
1741+
error: line 8 column 23: Required minimum prek version `10.0.0` is greater than current version `0.3.1`. Please consider updating prek. at line 8, column 23
1742+
--> <input>:8:23
1743+
|
1744+
6 | entry: echo test
1745+
7 | language: system
1746+
8 | minimum_prek_version: '10.0.0; '
1747+
| ^ Required minimum prek version `10.0.0` is greater than current version `0.3.1`. Please consider updating prek. at line 8, column 23
1748+
");
1749+
}
1750+
16921751
#[test]
16931752
fn test_validate_type_tags() {
16941753
// Valid tags should parse successfully

docs/configuration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@ Example:
301301
minimum_prek_version: '0.2.0'
302302
```
303303

304+
You can append an optional upgrade hint after a semicolon. The hint is shown only
305+
when validation fails.
306+
307+
Example:
308+
309+
```yaml
310+
minimum_prek_version: '0.2.0; Use brew upgrade to upgrade prek'
311+
```
312+
304313
#### `orphan`
305314

306315
<a id="prek-only-orphan"></a>
@@ -821,6 +830,20 @@ Require a minimum `prek` version for this specific hook.
821830
- Type: string (version)
822831
- Default: unset
823832

833+
You can append an optional upgrade hint after a semicolon. The hint is shown only
834+
when validation fails.
835+
836+
Example:
837+
838+
```yaml
839+
hooks:
840+
- id: my-hook
841+
name: My Hook
842+
language: system
843+
entry: echo hello
844+
minimum_prek_version: '0.2.0; Use brew upgrade to upgrade prek'
845+
```
846+
824847
## Environment variables
825848

826849
prek supports the following environment variables:

0 commit comments

Comments
 (0)