Skip to content

Commit 6ad9387

Browse files
Byronjpgrayson
authored andcommitted
fix: add support for git version parsing on MacOS.
Such version strings are special as they look like `2.37.1 (Apple Git-137.1)`, and the parsing was adjusted to account for that.
1 parent c693736 commit 6ad9387

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/stupid/version.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ impl FromStr for StupidVersion {
2525
type Err = anyhow::Error;
2626

2727
fn from_str(s: &str) -> Result<Self, Self::Err> {
28-
let ver_str = s
28+
let mut ver_str = s
2929
.strip_prefix("git version ")
3030
.ok_or_else(|| anyhow!("failed to parse git version"))?;
3131

32+
if let Some((triplet, _extra)) = ver_str.split_once(' ') {
33+
ver_str = triplet;
34+
};
35+
3236
let (dotted, extra) = if let Some((dotted, extra)) = ver_str.split_once('-') {
3337
(dotted, Some(extra.to_string()))
3438
} else {
@@ -81,9 +85,24 @@ mod tests {
8185
},
8286
"2.17.123-rc0",
8387
),
88+
(StupidVersion::new(2, 37, 1), "2.37.1 (Apple Git-137.1)"),
89+
(
90+
StupidVersion {
91+
major: 2,
92+
minor: 37,
93+
micro: 123,
94+
extra: Some("rc0".to_string()),
95+
},
96+
"2.37.123-rc0 (Apple Git-137.1)",
97+
),
8498
] {
8599
let version_str = format!("git version {version_str}");
86-
assert_eq!(version, version_str.parse::<StupidVersion>().unwrap());
100+
assert_eq!(
101+
version,
102+
version_str
103+
.parse::<StupidVersion>()
104+
.unwrap_or_else(|_| panic!("{}", version_str))
105+
);
87106
}
88107
}
89108

0 commit comments

Comments
 (0)