From cef29b1146f362f0e000c965ac79657fed00a3ff Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 30 Sep 2026 21:42:16 +0200 Subject: [PATCH] feat: support repository fallback URLs --- Cargo.lock | 47 ++++++++ Cargo.toml | 1 + README.md | 2 + config.toml | 3 + docs/advanced-usage.md | 23 +++- src/build.rs | 1 + src/cli.rs | 32 +++--- src/config.rs | 244 ++++++++++++++++++++++++++++++++++++++--- src/prepare.rs | 51 ++++++++- 9 files changed, 369 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9d1f1d..496f310 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -232,6 +232,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "directories" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.61.2", +] + [[package]] name = "displaydoc" version = "0.2.7" @@ -635,6 +656,15 @@ version = "0.2.189" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +[[package]] +name = "libredox" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61ff90caf6077a803a240f62fdbe88645a890bbca49ef8174c3cb0404362171d" +dependencies = [ + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -714,6 +744,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "percent-encoding" version = "2.3.2" @@ -841,6 +877,16 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redox_users" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0" +dependencies = [ + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.13.1" @@ -930,6 +976,7 @@ name = "rust_realease" version = "0.1.0" dependencies = [ "clap", + "directories", "flate2", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index b08a912..d66722c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ publish = false [dependencies] clap = { version = "4.5", features = ["derive"] } +directories = "6.0" toml = "1.1" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/README.md b/README.md index a461a11..df69133 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,8 @@ For a remote repository, select the exact tag to check out: rust_realease https://gitea.example.org/owner/my-tool.git --tag v0.1.0 ``` +If Git uses an internal repository URL but releases must use a public proxy, add an exact repository fallback to a project `rust-release.toml` or to the user configuration. The public fallback is used for Cargo metadata, Gitea publication, download verification, and cargo-binstall while the configured Git origin remains valid for cloning and preparation pushes. See [Advanced usage](docs/advanced-usage.md#configuration) for the file locations and an example. + The token account needs repository read access and release write access. `--push-preparation` additionally needs code write access. The token is read from `GITEA_TOKEN` by default and is kept out of child build processes. Public releases are verified by anonymous downloads. Private releases are downloaded with Gitea authentication; use `--skip-binstall-test` for them because cargo-binstall cannot install from their private attachment URLs. An existing asset with the same name stops the upload; `--replace-assets` explicitly permits replacing matching assets. ## More options diff --git a/config.toml b/config.toml index ceb1efd..1e532b0 100644 --- a/config.toml +++ b/config.toml @@ -14,5 +14,8 @@ locked = true [package] format = "tgz" +[repository] +fallbacks = [] + [gitea] token-env = "GITEA_TOKEN" diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index f1422fd..3834410 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -21,7 +21,15 @@ Use `--package NAME` to select one workspace member by its exact Cargo package n ## Configuration -The bundled `config.toml` supplies defaults. Pass `--config PATH` to use another file; CLI values override its build and tag settings. +Configuration is layered in this order, with later values taking precedence: + +1. bundled defaults; +2. the user configuration at the platform-native config directory returned by the `directories` crate (`~/.config/rust_realease/config.toml` on Linux); +3. `rust-release.toml` in the acquired project root; +4. the file passed with `--config PATH`; +5. CLI options. + +Missing automatically discovered files are ignored. A path explicitly passed with `--config` must exist and be valid TOML. Repository fallback lists are combined across layers; a later entry for the same normalized origin replaces the earlier entry. ```toml [release] @@ -39,6 +47,9 @@ locked = true [package] format = "tgz" +[repository] +fallbacks = [] + [gitea] token-env = "GITEA_TOKEN" ``` @@ -47,6 +58,16 @@ Use `--tag-prefix ""` for an unprefixed tag such as `0.3.1`. `--tag TAG` must ma `package.format` accepts `"tgz"`, `"binary"`, or `"both"`. The default `"tgz"` produces cargo-binstall archives. `"binary"` produces only directly downloadable executables and does not require or verify cargo-binstall metadata. `"both"` produces both forms. The `--artifact-format` option overrides this setting for an individual run. +When a checkout uses an internal Git URL but its Gitea API and release downloads are exposed through another URL, configure an exact fallback pair. For example, either the project file or the user file can contain: + +```toml +[[repository.fallbacks]] +origin = "http://git.dandaedre.com/idle-org/john-slop" +fallback-url = "https://proxy.dandaedre.com/gitea/idle-org/john-slop" +``` + +URLs are normalized, including removal of a trailing `.git`; SCP-style and SSH URLs normalize to HTTPS. The scheme remains significant for HTTP(S), so add separate rules if the same origin is used through both HTTP and SSH. Each rule matches one repository, not an entire host. The fallback must use HTTPS because it becomes the Gitea API and public download base. It is also written to `package.repository` during metadata repair. Git continues to clone and push through `origin`; the mapping tells the safety checks that the internal and public repositories are equivalent. + ## Metadata and preparation The publisher checks the selected member's repository and, when archives are enabled, its cargo-binstall metadata. `--fix-metadata` repairs supported entries in that member's `Cargo.toml` while preserving comments where practical. It does not commit, tag, or push. To preview the full preparation diff, use `--prepare-repo --dry-run`. diff --git a/src/build.rs b/src/build.rs index 627423b..512af33 100644 --- a/src/build.rs +++ b/src/build.rs @@ -247,6 +247,7 @@ mod tests { tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), package_format: crate::config::PackageFormat::Tgz, + repository_fallbacks: Vec::new(), }; let args = cross_arguments( &project, diff --git a/src/cli.rs b/src/cli.rs index 81564d4..debd578 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -85,8 +85,20 @@ pub fn run(args: Cli) -> Result<(), Error> { "--prepare-repo is a separate mode; omit --stop-after.".into(), )); } + let acquisition_runner = Runner::new("GITEA_TOKEN").with_verbose(args.verbose); + let checkout = project::acquire_project( + &args.source, + if args.prepare_repo { + None + } else { + args.tag.as_deref() + }, + args.keep_workdir || args.prepare_repo, + &acquisition_runner, + )?; let config = config::load( args.config.as_deref(), + &checkout.root, Overrides { targets: args.target, profile: args.profile, @@ -96,16 +108,6 @@ pub fn run(args: Cli) -> Result<(), Error> { }, )?; let runner = Runner::new(&config.token_env).with_verbose(args.verbose); - let checkout = project::acquire_project( - &args.source, - if args.prepare_repo { - None - } else { - args.tag.as_deref() - }, - args.keep_workdir || args.prepare_repo, - &runner, - )?; let root = checkout .root .to_str() @@ -159,12 +161,16 @@ pub fn run(args: Cli) -> Result<(), Error> { .into(), ) })?; - let repository = project::normalize_repository(&candidate)?; + let normalized_candidate = project::normalize_repository(&candidate)?; + let repository = config.resolve_repository(&candidate)?; + if repository != normalized_candidate { + println!("Repository fallback: {normalized_candidate} -> {repository}"); + } if publishing { if let Some(origin) = project::origin_url(&checkout.root, &runner) { - if project::normalize_repository(&origin)? != repository { + if !config.repositories_match(&origin, &repository)? { return Err(Error( - "Git origin differs from the release repository.".into(), + "Git origin differs from the release repository and no configured fallback maps them together.".into(), )); } } diff --git a/src/config.rs b/src/config.rs index fbea4df..557526c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,36 +1,63 @@ -use crate::error::Error; +use crate::{error::Error, project}; use clap::ValueEnum; +use directories::ProjectDirs; use serde::Deserialize; use std::{fs, path::Path}; -#[derive(Debug, Deserialize, Default)] +pub const PROJECT_CONFIG_FILE: &str = "rust-release.toml"; + +#[derive(Debug, Clone, Deserialize, Default)] struct FileConfig { build: Option, release: Option, package: Option, + repository: Option, gitea: Option, } -#[derive(Debug, Deserialize, Default)] + +#[derive(Debug, Clone, Deserialize, Default)] struct Build { targets: Option>, profile: Option, locked: Option, } -#[derive(Debug, Deserialize, Default)] + +#[derive(Debug, Clone, Deserialize, Default)] struct Release { #[serde(rename = "tag-prefix")] tag_prefix: Option, } -#[derive(Debug, Deserialize, Default)] + +#[derive(Debug, Clone, Deserialize, Default)] struct Package { format: Option, } -#[derive(Debug, Deserialize, Default)] + +#[derive(Debug, Clone, Deserialize, Default)] +struct Repository { + #[serde(default)] + fallbacks: Vec, +} + +#[derive(Debug, Clone, Deserialize)] +struct FileRepositoryFallback { + origin: String, + #[serde(rename = "fallback-url")] + fallback_url: String, +} + +#[derive(Debug, Clone, Deserialize, Default)] struct Gitea { #[serde(rename = "token-env")] token_env: Option, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RepositoryFallback { + pub origin: String, + pub fallback_url: String, +} + #[derive(Debug, Clone)] pub struct Config { pub targets: Vec, @@ -39,21 +66,42 @@ pub struct Config { pub tag_prefix: String, pub token_env: String, pub package_format: PackageFormat, + pub repository_fallbacks: Vec, } + +impl Config { + pub fn resolve_repository(&self, input: &str) -> Result { + let normalized = project::normalize_repository(input)?; + Ok(self + .repository_fallbacks + .iter() + .rev() + .find(|fallback| fallback.origin == normalized) + .map_or(normalized, |fallback| fallback.fallback_url.clone())) + } + + pub fn repositories_match(&self, left: &str, right: &str) -> Result { + Ok(self.resolve_repository(left)? == self.resolve_repository(right)?) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] pub enum PackageFormat { Tgz, Binary, Both, } + impl PackageFormat { pub fn archives(self) -> bool { matches!(self, Self::Tgz | Self::Both) } + pub fn binaries(self) -> bool { matches!(self, Self::Binary | Self::Both) } } + #[derive(Default)] pub struct Overrides { pub targets: Vec, @@ -62,21 +110,113 @@ pub struct Overrides { pub tag_prefix: Option, pub package_format: Option, } -pub fn load(path: Option<&Path>, overrides: Overrides) -> Result { - if let Some(path) = path { - let source = fs::read_to_string(path) - .map_err(|e| Error(format!("cannot read config {}: {e}", path.display())))?; - parse(&source, overrides) - } else { - parse(include_str!("../config.toml"), overrides) + +pub fn load( + explicit_path: Option<&Path>, + project_root: &Path, + overrides: Overrides, +) -> Result { + let mut sources = vec![( + "bundled defaults".to_owned(), + include_str!("../config.toml").to_owned(), + )]; + let mut discovered = Vec::new(); + if let Some(project_dirs) = ProjectDirs::from("", "", "rust_realease") { + discovered.push(project_dirs.config_dir().join("config.toml")); + } + discovered.push(project_root.join(PROJECT_CONFIG_FILE)); + for path in discovered { + if path.is_file() && explicit_path != Some(path.as_path()) { + sources.push((path.display().to_string(), read_config(&path)?)); + } + } + if let Some(path) = explicit_path { + sources.push((path.display().to_string(), read_config(path)?)); + } + parse_sources(sources, overrides) +} + +fn read_config(path: &Path) -> Result { + fs::read_to_string(path) + .map_err(|e| Error(format!("cannot read config {}: {e}", path.display()))) +} + +fn parse_sources(sources: Vec<(String, String)>, overrides: Overrides) -> Result { + let mut merged = FileConfig::default(); + for (label, source) in sources { + let file: FileConfig = + toml::from_str(&source).map_err(|e| Error(format!("invalid config {label}: {e}")))?; + merged.merge(file); + } + finish(merged, overrides) +} + +impl FileConfig { + fn merge(&mut self, later: Self) { + merge_build(&mut self.build, later.build); + merge_release(&mut self.release, later.release); + merge_package(&mut self.package, later.package); + merge_repository(&mut self.repository, later.repository); + merge_gitea(&mut self.gitea, later.gitea); } } -fn parse(source: &str, overrides: Overrides) -> Result { - let file: FileConfig = - toml::from_str(source).map_err(|e| Error(format!("invalid config: {e}")))?; + +fn merge_build(current: &mut Option, later: Option) { + if let Some(later) = later { + let current = current.get_or_insert_with(Build::default); + if later.targets.is_some() { + current.targets = later.targets; + } + if later.profile.is_some() { + current.profile = later.profile; + } + if later.locked.is_some() { + current.locked = later.locked; + } + } +} + +fn merge_release(current: &mut Option, later: Option) { + if let Some(later) = later { + let current = current.get_or_insert_with(Release::default); + if later.tag_prefix.is_some() { + current.tag_prefix = later.tag_prefix; + } + } +} + +fn merge_package(current: &mut Option, later: Option) { + if let Some(later) = later { + let current = current.get_or_insert_with(Package::default); + if later.format.is_some() { + current.format = later.format; + } + } +} + +fn merge_repository(current: &mut Option, later: Option) { + if let Some(later) = later { + current + .get_or_insert_with(Repository::default) + .fallbacks + .extend(later.fallbacks); + } +} + +fn merge_gitea(current: &mut Option, later: Option) { + if let Some(later) = later { + let current = current.get_or_insert_with(Gitea::default); + if later.token_env.is_some() { + current.token_env = later.token_env; + } + } +} + +fn finish(file: FileConfig, overrides: Overrides) -> Result { let build = file.build.unwrap_or_default(); let release = file.release.unwrap_or_default(); let package = file.package.unwrap_or_default(); + let repository = file.repository.unwrap_or_default(); let gitea = file.gitea.unwrap_or_default(); let package_format = match package.format.as_deref().unwrap_or("tgz") { "tgz" => PackageFormat::Tgz, @@ -88,6 +228,21 @@ fn parse(source: &str, overrides: Overrides) -> Result { )) } }; + let mut repository_fallbacks = Vec::new(); + for fallback in repository.fallbacks { + let origin = project::normalize_repository(&fallback.origin) + .map_err(|e| Error(format!("invalid repository fallback origin: {e}")))?; + let fallback_url = project::normalize_repository(&fallback.fallback_url) + .map_err(|e| Error(format!("invalid repository fallback-url: {e}")))?; + if !fallback_url.starts_with("https://") { + return Err(Error("repository fallback-url must use HTTPS.".into())); + } + repository_fallbacks.retain(|existing: &RepositoryFallback| existing.origin != origin); + repository_fallbacks.push(RepositoryFallback { + origin, + fallback_url, + }); + } let mut config = Config { targets: if overrides.targets.is_empty() { build.targets.unwrap_or_else(|| { @@ -117,6 +272,7 @@ fn parse(source: &str, overrides: Overrides) -> Result { .unwrap_or_else(|| "v".into()), token_env: gitea.token_env.unwrap_or_else(|| "GITEA_TOKEN".into()), package_format: overrides.package_format.unwrap_or(package_format), + repository_fallbacks, }; if config.targets.is_empty() || config.targets.iter().any(|target| !valid_target(target)) { return Err(Error( @@ -150,26 +306,35 @@ fn parse(source: &str, overrides: Overrides) -> Result { } Ok(config) } + fn valid_target(s: &str) -> bool { s.split('-').count() >= 3 && s.split('-').all(|part| { !part.is_empty() && part.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') }) } + fn valid_profile(s: &str) -> bool { s.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) && s.bytes() .all(|b| b.is_ascii_alphanumeric() || b"_-".contains(&b)) } + fn valid_env(s: &str) -> bool { s.as_bytes() .first() .is_some_and(|b| b.is_ascii_alphabetic() || *b == b'_') && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') } + #[cfg(test)] mod tests { use super::*; + + fn parse(source: &str, overrides: Overrides) -> Result { + parse_sources(vec![("test".into(), source.into())], overrides) + } + #[test] fn overrides_and_validation() { let config = parse("[build]\ntargets=['x86_64-unknown-linux-musl']\nlocked=true\n[release]\ntag-prefix='v'", Overrides { targets: vec!["aarch64-unknown-linux-musl".into()], unlocked: true, tag_prefix: Some(String::new()), ..Overrides::default() }).unwrap(); @@ -184,6 +349,51 @@ mod tests { .package_format, PackageFormat::Both ); - assert_eq!(load(None, Overrides::default()).unwrap().targets.len(), 3); + } + + #[test] + fn layered_config_and_repository_fallbacks() { + let config = parse_sources( + vec![ + ("default".into(), "[build]\ntargets=['x86_64-unknown-linux-musl']\nprofile='release'\n[repository]\nfallbacks=[]".into()), + ("user".into(), "[build]\nprofile='small'\n[[repository.fallbacks]]\norigin='http://git.dandaedre.com/idle-org/john-slop.git'\nfallback-url='https://old.example/gitea/idle-org/john-slop'".into()), + ("project".into(), "[[repository.fallbacks]]\norigin='http://git.dandaedre.com/idle-org/john-slop'\nfallback-url='https://proxy.dandaedre.com/gitea/idle-org/john-slop.git'".into()), + ], + Overrides::default(), + ) + .unwrap(); + assert_eq!(config.profile, "small"); + assert_eq!(config.repository_fallbacks.len(), 1); + assert_eq!( + config + .resolve_repository("http://git.dandaedre.com/idle-org/john-slop.git") + .unwrap(), + "https://proxy.dandaedre.com/gitea/idle-org/john-slop" + ); + assert!(config + .repositories_match( + "http://git.dandaedre.com/idle-org/john-slop.git", + "https://proxy.dandaedre.com/gitea/idle-org/john-slop" + ) + .unwrap()); + } + + #[test] + fn fallback_requires_https() { + let result = parse("[[repository.fallbacks]]\norigin='https://git.example/owner/repo'\nfallback-url='http://proxy.example/owner/repo'", Overrides::default()); + assert!(result.unwrap_err().to_string().contains("must use HTTPS")); + } + + #[test] + fn project_config_is_loaded_after_bundled_config() { + let project = tempfile::tempdir().unwrap(); + fs::write( + project.path().join(PROJECT_CONFIG_FILE), + "[build]\nprofile='project-profile'\n", + ) + .unwrap(); + let config = load(None, project.path(), Overrides::default()).unwrap(); + assert_eq!(config.profile, "project-profile"); + assert_eq!(config.targets.len(), 3); } } diff --git a/src/prepare.rs b/src/prepare.rs index d39ac77..0a82f25 100644 --- a/src/prepare.rs +++ b/src/prepare.rs @@ -248,8 +248,8 @@ pub fn run( ); let destination = runner.text("git", &["remote", "get-url", "--push", "origin"], root)?; - if project::normalize_repository(&destination)? != repository { - return Err(Error("origin push URL differs from the release repository. Fix origin explicitly before pushing.".into())); + if !config.repositories_match(&destination, repository)? { + return Err(Error("origin push URL differs from the release repository and no configured fallback maps them together.".into())); } } } @@ -369,8 +369,8 @@ fn push_preparation( runner: &Runner, ) -> Result<(), Error> { let origin = runner.text("git", &["remote", "get-url", "--push", "origin"], root)?; - if project::normalize_repository(&origin)? != repository { - return Err(Error("origin push URL differs from the release repository. Fix origin explicitly before pushing.".into())); + if !config.repositories_match(&origin, repository)? { + return Err(Error("origin push URL differs from the release repository and no configured fallback maps them together.".into())); } let branch_ref = format!("HEAD:refs/heads/{branch}"); let tag_ref = format!("refs/tags/{tag}"); @@ -466,6 +466,7 @@ mod tests { tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), package_format: crate::config::PackageFormat::Tgz, + repository_fallbacks: Vec::new(), }; let plan = plan( @@ -526,6 +527,7 @@ mod tests { tag_prefix: "v".into(), token_env: "GITEA_TOKEN".into(), package_format: crate::config::PackageFormat::Tgz, + repository_fallbacks: Vec::new(), }; (temp, project, config, runner) } @@ -721,6 +723,47 @@ mod tests { assert!(!root.join("Cross.toml").exists()); } + #[test] + fn configured_fallback_allows_internal_push_destination() { + let (temp, project, mut config, runner) = fixture(); + let root = temp.path(); + runner + .output( + "git", + &[ + "remote", + "add", + "origin", + "http://git.example.org/team/demo.git", + ], + root, + ) + .unwrap(); + config + .repository_fallbacks + .push(crate::config::RepositoryFallback { + origin: "http://git.example.org/team/demo".into(), + fallback_url: "https://example.org/team/demo".into(), + }); + run( + root, + &project, + &config, + "https://example.org/team/demo", + Options { + tag: "v0.1.0", + image_tag: None, + dry_run: true, + commit: true, + create_tag: true, + push: true, + }, + &runner, + ) + .unwrap(); + assert!(!root.join("Cross.toml").exists()); + } + #[cfg(unix)] #[test] fn authorized_preparation_pushes_branch_and_tag_atomically() {