use std::{
env,
path::Path,
process::{Command, ExitStatus},
};
fn checkout(curr_dir: &Path, branch: &str) -> ExitStatus {
let git_checkout = format!("git checkout {}", branch);
Command::new("git")
.arg("submodule")
.arg("foreach")
.arg(&git_checkout)
.current_dir(&curr_dir)
.status()
.unwrap_or_else(|_| panic!("Failed to execute `git submodule update {}`", git_checkout))
}
fn update_ref(curr_dir: &Path, namespace: &str, target: &str) -> ExitStatus {
let namespace = format!("git update-ref {} {}", namespace, target);
Command::new("git")
.arg("submodule")
.arg("foreach")
.arg(&namespace)
.current_dir(&curr_dir)
.status()
.unwrap_or_else(|_| panic!("Failed to execute `git submodule update {}`", namespace))
}
fn setup_fixtures() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR");
let curr_dir = Path::new(&manifest_dir);
let dev_status = checkout(curr_dir, "dev");
assert!(dev_status.success(), "failed to checkout dev");
let master_status = checkout(curr_dir, "master");
assert!(master_status.success(), "failed to checkout master");
for (new_rev, rev) in [
(
"refs/namespaces/golden/refs/heads/master",
"refs/heads/master",
),
("refs/namespaces/golden/refs/heads/banana", "refs/heads/dev"),
(
"refs/namespaces/golden/refs/tags/v0.1.0",
"refs/tags/v0.1.0",
),
(
"refs/namespaces/golden/refs/tags/v0.2.0",
"refs/tags/v0.2.0",
),
(
"refs/namespaces/golden/refs/remotes/kickflip/heads/heelflip",
"refs/heads/dev",
),
(
"refs/namespaces/golden/refs/namespaces/silver/refs/heads/master",
"refs/heads/dev",
),
(
"refs/remotes/banana/pineapple",
"refs/remotes/origin/master",
),
]
.iter()
{
let update_rev = update_ref(curr_dir, new_rev, rev);
assert!(
update_rev.success(),
"failed to set up '{} -> {}'",
new_rev,
rev
);
}
}
fn main() {
if env::var("GIT_FIXTURES").is_ok() {
setup_fixtures();
}
println!("cargo:rerun-if-env-changed=GIT_FIXTURES");
}