feat: support repository fallback URLs
Rust CI Cargo tests passed.
Gitea/rust_realease/pipeline/head This commit looks good Details

This commit is contained in:
Dandaedre 2026-09-30 21:42:16 +02:00
parent 8b0f5ebf1e
commit cef29b1146
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
9 changed files with 369 additions and 35 deletions

47
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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

View File

@ -14,5 +14,8 @@ locked = true
[package]
format = "tgz"
[repository]
fallbacks = []
[gitea]
token-env = "GITEA_TOKEN"

View File

@ -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`.

View File

@ -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,

View File

@ -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(),
));
}
}

View File

@ -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<Build>,
release: Option<Release>,
package: Option<Package>,
repository: Option<Repository>,
gitea: Option<Gitea>,
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default)]
struct Build {
targets: Option<Vec<String>>,
profile: Option<String>,
locked: Option<bool>,
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default)]
struct Release {
#[serde(rename = "tag-prefix")]
tag_prefix: Option<String>,
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default)]
struct Package {
format: Option<String>,
}
#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default)]
struct Repository {
#[serde(default)]
fallbacks: Vec<FileRepositoryFallback>,
}
#[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<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepositoryFallback {
pub origin: String,
pub fallback_url: String,
}
#[derive(Debug, Clone)]
pub struct Config {
pub targets: Vec<String>,
@ -39,21 +66,42 @@ pub struct Config {
pub tag_prefix: String,
pub token_env: String,
pub package_format: PackageFormat,
pub repository_fallbacks: Vec<RepositoryFallback>,
}
impl Config {
pub fn resolve_repository(&self, input: &str) -> Result<String, Error> {
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<bool, Error> {
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<String>,
@ -62,21 +110,113 @@ pub struct Overrides {
pub tag_prefix: Option<String>,
pub package_format: Option<PackageFormat>,
}
pub fn load(path: Option<&Path>, overrides: Overrides) -> Result<Config, Error> {
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<Config, Error> {
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<String, Error> {
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<Config, Error> {
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<Config, Error> {
let file: FileConfig =
toml::from_str(source).map_err(|e| Error(format!("invalid config: {e}")))?;
fn merge_build(current: &mut Option<Build>, later: Option<Build>) {
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<Release>, later: Option<Release>) {
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<Package>, later: Option<Package>) {
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<Repository>, later: Option<Repository>) {
if let Some(later) = later {
current
.get_or_insert_with(Repository::default)
.fallbacks
.extend(later.fallbacks);
}
}
fn merge_gitea(current: &mut Option<Gitea>, later: Option<Gitea>) {
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<Config, Error> {
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<Config, Error> {
))
}
};
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<Config, Error> {
.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<Config, Error> {
}
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<Config, Error> {
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);
}
}

View File

@ -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() {