From fd4e4903d70ef21477653d1b50900aca2bd9a5a0 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Sun, 30 Aug 2026 21:21:23 +0200 Subject: [PATCH] radicle, cli: Move Git versions Move `struct Version` from `radicle` to `radicle-cli`, where it is used. --- crates/radicle-cli/src/commands/auth.rs | 6 +- crates/radicle-cli/src/git.rs | 161 ++++++++++++++++++++++-- crates/radicle-node/src/runtime.rs | 3 - crates/radicle/src/git.rs | 149 ---------------------- 4 files changed, 153 insertions(+), 166 deletions(-) diff --git a/crates/radicle-cli/src/commands/auth.rs b/crates/radicle-cli/src/commands/auth.rs index 6b01798e6..eead05f69 100644 --- a/crates/radicle-cli/src/commands/auth.rs +++ b/crates/radicle-cli/src/commands/auth.rs @@ -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(); } diff --git a/crates/radicle-cli/src/git.rs b/crates/radicle-cli/src/git.rs index 8180030c8..a476a3a24 100644 --- a/crates/radicle-cli/src/git.rs +++ b/crates/radicle-cli/src/git.rs @@ -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 { + 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 { + 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 Ok(remote) } -/// Check that the system's git version is supported. Returns an error otherwise. -pub fn check_version() -> Result { - 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, 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()); + } +} diff --git a/crates/radicle-node/src/runtime.rs b/crates/radicle-node/src/runtime.rs index b711cd343..aaa8c8a74 100644 --- a/crates/radicle-node/src/runtime.rs +++ b/crates/radicle-node/src/runtime.rs @@ -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 for Error { diff --git a/crates/radicle/src/git.rs b/crates/radicle/src/git.rs index 45ebcfe4c..8537bc111 100644 --- a/crates/radicle/src/git.rs +++ b/crates/radicle/src/git.rs @@ -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 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 { - 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 { - 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()); - } -}