1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-04 01:24:09 +02:00

make haze git checkout pick the correct branch by default

This commit is contained in:
Robin Appelman 2026-02-13 21:22:21 +01:00
commit b2a7b22676
4 changed files with 78 additions and 11 deletions

51
src/sources.rs Normal file
View file

@ -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<P: AsRef<Path>>(base_dir: P) -> Result<Self> {
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()
}
}
}