From b2a7b22676329cde3d44e7490f950ddb93de6ac6 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 13 Feb 2026 21:22:21 +0100 Subject: [PATCH] make `haze git checkout` pick the correct branch by default --- README.md | 14 ++++++++++++++ src/args.rs | 16 ++++++---------- src/main.rs | 8 +++++++- src/sources.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 src/sources.rs diff --git a/README.md b/README.md index b839e16..e04a241 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,20 @@ haze [match] edit haze [match] reload ``` +The php configuration can edit changed with `haze edit /php.ini` + +#### Checkout a branch for all local apps + +```bash +haze git checkout [branch] +``` + +Checks out the branch in all git repositories within the apps folder. + +Defaults to the branch matching the current checked out server versions (e.g. `master` or `stable33`). + +`master` and `main` can be used interchangeably. + ## Federation Multiple instances can reach each other by using their instance name as domain diff --git a/src/args.rs b/src/args.rs index 4b6a5c3..bcf9365 100644 --- a/src/args.rs +++ b/src/args.rs @@ -123,8 +123,10 @@ pub enum GitOperation { /// Checkout a branch in all apps /// /// "main" and "master" can be used interchangeably. - #[strum(props(Args = "[branch] branch to checkout"))] - Checkout { branch: String }, + #[strum(props( + Args = "[branch] branch to checkout, defaults to the branch matching the current server version" + ))] + Checkout { branch: Option }, } impl SubCommand for GitOperation { @@ -316,10 +318,7 @@ impl HazeArgs { HazeCommand::Unpin => Ok(HazeArgs::Unpin { filter }), HazeCommand::Proxy => Ok(HazeArgs::Proxy), HazeCommand::Checkout => { - let branch = args - .next() - .map(S::into) - .ok_or_else(|| Report::msg("No branch provided"))?; + let branch = args.next().map(S::into); Ok(HazeArgs::Git { operation: GitOperation::Checkout { branch }, }) @@ -330,10 +329,7 @@ impl HazeArgs { .ok_or_else(|| Report::msg("No git operation provided"))?; match operation.as_ref() { "checkout" => { - let branch = args - .next() - .map(S::into) - .ok_or_else(|| Report::msg("No branch provided"))?; + let branch = args.next().map(S::into); Ok(HazeArgs::Git { operation: GitOperation::Checkout { branch }, }) diff --git a/src/main.rs b/src/main.rs index 6509a40..091afd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use crate::php::PhpVersion; use crate::proxy::proxy; use crate::service::ServiceTrait; use crate::service::{RedisTls, Service}; +use crate::sources::Sources; use bollard::Docker; use itertools::Itertools; use miette::{IntoDiagnostic, Report, Result, WrapErr}; @@ -35,6 +36,7 @@ mod network; mod php; mod proxy; mod service; +mod sources; static FORWARD_ENV: &[&str] = &[ "OCC_LOG", @@ -385,7 +387,11 @@ async fn main() -> Result { } HazeArgs::Git { operation } => match operation { GitOperation::Checkout { branch } => { - checkout_all(&config.sources_root, &branch)?; + let sources = Sources::new(&config.sources_root)?; + checkout_all( + &config.sources_root, + &branch.unwrap_or_else(|| sources.get_server_version_branch()), + )?; } GitOperation::Pull => { pull_all(&config.sources_root)?; diff --git a/src/sources.rs b/src/sources.rs new file mode 100644 index 0000000..0a19e08 --- /dev/null +++ b/src/sources.rs @@ -0,0 +1,51 @@ +use miette::{Context, IntoDiagnostic, Report, Result}; +use std::fs::read_to_string; +use std::path::{Path, PathBuf}; + +pub struct Sources { + #[allow(dead_code)] + base_dir: PathBuf, + server_version: u8, + server_branched_off: bool, +} + +impl Sources { + pub fn new>(base_dir: P) -> Result { + let base_dir = base_dir.as_ref(); + let versions_source = read_to_string(base_dir.join("version.php")) + .into_diagnostic() + .wrap_err("failed to read version.php")?; + let version_line = versions_source + .lines() + .find(|line| line.starts_with("$OC_Version")) + .ok_or_else(|| Report::msg("failed to find line containing $OC_Version"))?; + let version_str_line = versions_source + .lines() + .find(|line| line.starts_with("$OC_VersionString")) + .ok_or_else(|| Report::msg("failed to find line containing $OC_VersionString"))?; + let (major, _) = version_line + .split_once('[') + .and_then(|(_, line)| line.split_once(',')) + .ok_or_else(|| Report::msg("failed to find version number in line"))?; + let server_version = major + .trim() + .parse() + .into_diagnostic() + .wrap_err("failed to parse version number")?; + let server_branched_off = !version_str_line.contains("dev"); + + Ok(Sources { + base_dir: base_dir.into(), + server_version, + server_branched_off, + }) + } + + pub fn get_server_version_branch(&self) -> String { + if self.server_branched_off { + format!("stable{}", self.server_version) + } else { + "master".into() + } + } +}