radicle, cli: Move Git versions

Move `struct Version` from `radicle` to `radicle-cli`, where it is used.
This commit is contained in:
Lorenz Leutgeb
2026-08-30 23:53:25 +02:00
parent 05a192daff
commit fd4e4903d7
4 changed files with 153 additions and 166 deletions
+3 -3
View File
@@ -23,11 +23,11 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
pub fn init(args: Args) -> anyhow::Result<()> {
term::headline("Initializing your Radicle 👾 identity");
if let Ok(version) = radicle::git::version() {
if version < radicle::git::VERSION_REQUIRED {
if let Ok(version) = crate::git::version() {
if version < crate::git::VERSION_REQUIRED {
term::warning(format!(
"Your Git version is unsupported, please upgrade to {} or later",
radicle::git::VERSION_REQUIRED,
crate::git::VERSION_REQUIRED,
));
term::blank();
}
+150 -11
View File
@@ -21,7 +21,6 @@ use thiserror::Error;
use radicle::crypto::ssh;
use radicle::git;
use radicle::git::raw::{ErrorExt as _, Repository};
use radicle::git::{VERSION_REQUIRED, Version};
use radicle::prelude::{NodeId, RepoId};
use radicle::storage::git::transport;
@@ -31,6 +30,92 @@ pub const CONFIG_GPG_FORMAT: &str = "gpg.format";
pub const CONFIG_GPG_SSH_PROGRAM: &str = "gpg.ssh.program";
pub const CONFIG_GPG_SSH_ALLOWED_SIGNERS: &str = "gpg.ssh.allowedSignersFile";
/// A parsed git version.
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord)]
pub(crate) struct Version {
pub major: u8,
pub minor: u8,
pub patch: u8,
}
impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
/// Minimum required git version.
pub(crate) const VERSION_REQUIRED: Version = Version {
major: 2,
minor: 31,
patch: 0,
};
#[derive(thiserror::Error, Debug)]
pub(crate) enum VersionError {
#[error("malformed git version string")]
Malformed,
#[error("malformed git version string: {0}")]
ParseInt(#[from] std::num::ParseIntError),
#[error("malformed git version string: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
#[error("error retrieving git version: {0}")]
Io(#[from] io::Error),
#[error("error retrieving git version: {0}")]
Other(String),
}
impl std::str::FromStr for Version {
type Err = VersionError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let rest = input
.strip_prefix("git version ")
.ok_or(VersionError::Malformed)?;
let rest = rest.split(' ').next().ok_or(VersionError::Malformed)?;
let rest = rest.trim_end();
let mut parts = rest.split('.');
let major = parts.next().ok_or(VersionError::Malformed)?.parse()?;
let minor = parts.next().ok_or(VersionError::Malformed)?.parse()?;
let patch = match parts.next() {
None => 0,
Some(patch) => patch.parse()?,
};
Ok(Self {
major,
minor,
patch,
})
}
}
/// Get the system's git version.
pub(crate) fn version() -> Result<Version, VersionError> {
let mut command = Command::new("git");
command.arg("version");
#[cfg(windows)]
std::os::windows::process::CommandExt::creation_flags(
&mut command,
radicle_windows::process::creation_flags::CREATE_NO_WINDOW.0,
);
let output = command.output()?;
if output.status.success() {
let output = String::from_utf8(output.stdout)?;
let version = output.parse()?;
return Ok(version);
}
Err(VersionError::Other(
String::from_utf8_lossy(&output.stderr).to_string(),
))
}
/// Git revision parameter. Supports extended SHA-1 syntax.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rev(String);
@@ -333,16 +418,6 @@ pub fn branch_remote(repo: &Repository, branch: &str) -> anyhow::Result<String>
Ok(remote)
}
/// Check that the system's git version is supported. Returns an error otherwise.
pub fn check_version() -> Result<Version, anyhow::Error> {
let git_version = git::version()?;
if git_version < VERSION_REQUIRED {
anyhow::bail!("a minimum git version of {} is required", VERSION_REQUIRED);
}
Ok(git_version)
}
pub fn add_tag(
repo: &Repository,
message: &str,
@@ -387,3 +462,67 @@ pub fn commit_ssh_fingerprint(path: &Path, sha1: &str) -> Result<Option<String>,
Ok(None)
}
#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;
#[test]
fn test_version_ord() {
assert!(
Version {
major: 2,
minor: 34,
patch: 1
} > Version {
major: 2,
minor: 34,
patch: 0
}
);
assert!(
Version {
major: 2,
minor: 24,
patch: 12
} < Version {
major: 2,
minor: 34,
patch: 0
}
);
}
#[test]
fn test_version_from_str() {
assert_eq!(
Version::from_str("git version 2.34.1\n").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 1
})
);
assert_eq!(
Version::from_str("git version 2.34.1 (macOS)").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 1
})
);
assert_eq!(
Version::from_str("git version 2.34").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 0
})
);
assert!(Version::from_str("2.34").is_err());
}
}
-3
View File
@@ -84,9 +84,6 @@ pub enum Error {
and restart the node"
)]
AlreadyRunning(PathBuf),
/// A git version error.
#[error("git version error: {0}")]
GitVersion(#[from] git::VersionError),
}
impl From<service::Error> for Error {
-149
View File
@@ -23,26 +23,6 @@ pub type BranchName = crate::git::fmt::RefString;
/// Default port of the `git` transport protocol.
pub const PROTOCOL_PORT: u16 = 9418;
/// Minimum required git version.
pub const VERSION_REQUIRED: Version = Version {
major: 2,
minor: 31,
patch: 0,
};
/// A parsed git version.
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord)]
pub struct Version {
pub major: u8,
pub minor: u8,
pub patch: u8,
}
impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
/// Verbosity level for Git commands.
#[derive(Default, Clone, Copy)]
@@ -88,71 +68,6 @@ impl From<i8> for Verbosity {
}
}
#[derive(thiserror::Error, Debug)]
pub enum VersionError {
#[error("malformed git version string")]
Malformed,
#[error("malformed git version string: {0}")]
ParseInt(#[from] std::num::ParseIntError),
#[error("malformed git version string: {0}")]
Utf8(#[from] std::string::FromUtf8Error),
#[error("error retrieving git version: {0}")]
Io(#[from] io::Error),
#[error("error retrieving git version: {0}")]
Other(String),
}
impl std::str::FromStr for Version {
type Err = VersionError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let rest = input
.strip_prefix("git version ")
.ok_or(VersionError::Malformed)?;
let rest = rest.split(' ').next().ok_or(VersionError::Malformed)?;
let rest = rest.trim_end();
let mut parts = rest.split('.');
let major = parts.next().ok_or(VersionError::Malformed)?.parse()?;
let minor = parts.next().ok_or(VersionError::Malformed)?.parse()?;
let patch = match parts.next() {
None => 0,
Some(patch) => patch.parse()?,
};
Ok(Self {
major,
minor,
patch,
})
}
}
/// Get the system's git version.
pub fn version() -> Result<Version, VersionError> {
let mut command = Command::new("git");
command.arg("version");
#[cfg(windows)]
std::os::windows::process::CommandExt::creation_flags(
&mut command,
radicle_windows::process::creation_flags::CREATE_NO_WINDOW.0,
);
let output = command.output()?;
if output.status.success() {
let output = String::from_utf8(output.stdout)?;
let version = output.parse()?;
return Ok(version);
}
Err(VersionError::Other(
String::from_utf8_lossy(&output.stderr).to_string(),
))
}
#[derive(thiserror::Error, Debug)]
pub enum RefError {
#[error("ref name is not valid: {source}")]
@@ -862,67 +777,3 @@ impl UserInfo {
format!("{}@{}", self.alias, self.key)
}
}
#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;
#[test]
fn test_version_ord() {
assert!(
Version {
major: 2,
minor: 34,
patch: 1
} > Version {
major: 2,
minor: 34,
patch: 0
}
);
assert!(
Version {
major: 2,
minor: 24,
patch: 12
} < Version {
major: 2,
minor: 34,
patch: 0
}
);
}
#[test]
fn test_version_from_str() {
assert_eq!(
Version::from_str("git version 2.34.1\n").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 1
})
);
assert_eq!(
Version::from_str("git version 2.34.1 (macOS)").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 1
})
);
assert_eq!(
Version::from_str("git version 2.34").ok(),
Some(Version {
major: 2,
minor: 34,
patch: 0
})
);
assert!(Version::from_str("2.34").is_err());
}
}