Skip to content

Commit

Permalink
Handle which branch updates should be taken from in code rather than …
Browse files Browse the repository at this point in the history
…build script
  • Loading branch information
glandium committed Aug 31, 2024
1 parent aff4ed3 commit c9de886
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ version = "0.1"
optional = true

[dev-dependencies]
semver = "1.0"
tempfile = "3"

[profile.release]
Expand All @@ -170,7 +171,3 @@ self-update = ["semver", "shared_child", "dep:tar", "dep:xz2", "dep:zip", "windo
compile_commands = []
# Enable libgit development options.
gitdev = []

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(version_check_branch)']
29 changes: 0 additions & 29 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,33 +305,4 @@ fn main() {
}

println!("cargo:rerun-if-env-changed=CINNABAR_MAKE_FLAGS");

#[cfg(any(feature = "version-check", feature = "self-update"))]
{
// The expected lifecycle is:
// - 0.x.0a on branch next
// - 0.x.0b on branch master
// - 0.x.0b1 on branch release (optionally)
// - 0.(x+1).0a on branch next (possibly later)
// - 0.x.0rc1 on branch release (optionally)
// - 0.x.0 on branch release
// - 0.x.1a on branch master
// - 0.x.1 on branch release
//
// Releases will only check tags. Others will check branch heads.
let pre = env("CARGO_PKG_VERSION_PRE");
let patch = env("CARGO_PKG_VERSION_PATCH");
if let "a" | "b" = &*pre {
println!("cargo:rustc-cfg=version_check_branch");
println!(
"cargo:rustc-env=VERSION_CHECK_BRANCH={}",
match (&*patch, &*pre) {
("0", "a") => "next",
("0", "b") | (_, "a") => "master",
_ => panic!("Unsupported version"),
}
);
}
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
}
}
64 changes: 64 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,67 @@ pub const FULL_VERSION: &str = if BUILD_COMMIT.is_empty() {
if MODIFIED { "-modified" } else { "" }
)
};

#[cfg(any(feature = "version-check", feature = "self-update"))]
#[derive(PartialEq, Eq, Debug)]
pub enum BuildBranch {
Release,
Master,
Next,
}

#[cfg(any(feature = "version-check", feature = "self-update"))]
impl BuildBranch {
pub const fn from_version(version: &str) -> BuildBranch {
let version = version.as_bytes();
// TODO: Use version.last_chunk when MSRV >= 1.80
// or version.split_last_chunk when MSRV >= 1.77
const fn last_chunk<const N: usize>(b: &[u8]) -> Option<&[u8]> {
if b.len() >= N {
Some(b.split_at(b.len() - N).1)
} else {
None
}
}
if matches!(last_chunk::<4>(version), Some(b".0-a")) {
BuildBranch::Next
} else if matches!(last_chunk::<2>(version), Some(b"-a" | b"-b")) {
BuildBranch::Master
} else {
BuildBranch::Release
}
}

pub const fn as_str(&self) -> &'static str {
match self {
BuildBranch::Release => "release",
BuildBranch::Master => "master",
BuildBranch::Next => "next",
}
}
}

#[cfg(any(feature = "version-check", feature = "self-update"))]
#[test]
fn test_build_branch() {
use semver::Version;

// The following tests outline the expected lifecycle.
let from_version = |v| {
assert!(Version::parse(v).is_ok());
BuildBranch::from_version(v)
};
assert_eq!(from_version("0.2.0-a"), BuildBranch::Next);
assert_eq!(from_version("0.2.0-b"), BuildBranch::Master);
assert_eq!(from_version("0.2.0-b1"), BuildBranch::Release); // optionally
assert_eq!(from_version("0.2.0-beta1"), BuildBranch::Release); // alternative form
assert_eq!(from_version("0.2.0-beta1"), BuildBranch::Release); // alternative form
assert_eq!(from_version("0.3.0-a"), BuildBranch::Next); // possibly later
assert_eq!(from_version("0.2.0-rc1"), BuildBranch::Release); // optionally
assert_eq!(from_version("0.2.0"), BuildBranch::Release); // optionally
assert_eq!(from_version("0.2.1-a"), BuildBranch::Master);
assert_eq!(from_version("0.2.1"), BuildBranch::Release);
}

#[cfg(any(feature = "version-check", feature = "self-update"))]
pub const BUILD_BRANCH: BuildBranch = BuildBranch::from_version(SHORT_VERSION);
17 changes: 7 additions & 10 deletions src/version_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ use crate::git::CommitId;
#[cfg(feature = "version-check")]
use crate::util::DurationExt;
use crate::util::{FromBytes, OsStrExt, ReadExt, SliceExt};
use crate::version::{BUILD_COMMIT, SHORT_VERSION};
use crate::version::BuildBranch::*;
use crate::version::{BUILD_BRANCH, BUILD_COMMIT, SHORT_VERSION};
#[cfg(feature = "version-check")]
use crate::{check_enabled, get_config, Checks};

const ALL_TAG_REFS: &str = "refs/tags/*";
#[cfg(version_check_branch)]
const VERSION_CHECK_REF: &str = env!("VERSION_CHECK_BRANCH");
#[cfg(not(version_check_branch))]
const VERSION_CHECK_REF: &str = ALL_TAG_REFS;
#[cfg(feature = "version-check")]
const VERSION_CHECK_CONFIG: &str = "cinnabar.version-check";

Expand All @@ -49,10 +46,10 @@ impl<'a> From<&'a str> for VersionRequest<'a> {

impl<'a> Default for VersionRequest<'a> {
fn default() -> Self {
if VERSION_CHECK_REF == ALL_TAG_REFS {
if BUILD_BRANCH == Release {
VersionRequest::Tagged
} else {
VersionRequest::Branch(VERSION_CHECK_REF)
VersionRequest::Branch(BUILD_BRANCH.as_str())
}
}
}
Expand Down Expand Up @@ -159,7 +156,7 @@ impl VersionChecker {
impl Drop for VersionChecker {
fn drop(&mut self) {
match self.take_result() {
Some(VersionInfo::Tagged(version, _)) if VERSION_CHECK_REF == ALL_TAG_REFS => {
Some(VersionInfo::Tagged(version, _)) if BUILD_BRANCH == Release => {
if self.show_current {
warn!(
target: "root",
Expand All @@ -180,11 +177,11 @@ impl Drop for VersionChecker {
);
}
}
Some(VersionInfo::Commit(_)) if VERSION_CHECK_REF != ALL_TAG_REFS => {
Some(VersionInfo::Commit(_)) if BUILD_BRANCH != Release => {
warn!(
target: "root",
"The {} branch of git-cinnabar was updated. {}",
VERSION_CHECK_REF,
BUILD_BRANCH.as_str(),
if cfg!(feature = "self-update") {
"You may run `git cinnabar self-update` to update."
} else {
Expand Down

0 comments on commit c9de886

Please sign in to comment.