use std::path::Path;
use anyhow::{Context, Result};
use super::git::{host_git, read_origin};
use crate::state::State;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Refs<'a> {
All { prune: bool },
Branch(&'a str),
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Fetched {
NoOrigin,
Done,
Untrusted,
Failed(String),
}
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Trust {
Fetch(String),
Pin(String),
Mismatch { pinned: String, current: String },
}
pub(crate) fn decide(pinned: Option<&str>, current: &str) -> Trust {
match pinned {
None => Trust::Pin(current.to_string()),
Some(p) if p == current => Trust::Fetch(p.to_string()),
Some(p) => Trust::Mismatch { pinned: p.to_string(), current: current.to_string() },
}
}
pub(crate) fn trust_key(common: &Path) -> String {
common.to_string_lossy().into_owned()
}
pub(crate) fn fetch_trusted_origin(state: &Path, repo: &Path, refs: Refs) -> Result<Fetched> {
let origin = read_origin(repo)?;
let Some(current) = origin.url else { return Ok(Fetched::NoOrigin) };
let key = trust_key(&origin.common);
let trust = State::update_at(state, |s| {
let trust = decide(s.trusted_origins.get(&key).map(String::as_str), ¤t);
if let Trust::Pin(url) = &trust {
s.trusted_origins.insert(key.clone(), url.clone());
}
Ok(trust)
})?;
let url = match trust {
Trust::Fetch(url) | Trust::Pin(url) => url,
Trust::Mismatch { pinned, current } => {
super::notes::noted!("{}", mismatch_warning(repo, &pinned, ¤t));
return Ok(Fetched::Untrusted);
}
};
let out = host_git(repo)?
.args(fetch_args(&url, refs))
.output()
.context("failed to run git (is it installed?)")?;
Ok(if out.status.success() {
Fetched::Done
} else {
Fetched::Failed(String::from_utf8_lossy(&out.stderr).trim().to_string())
})
}
pub(crate) fn mismatch_warning(repo: &Path, pinned: &str, current: &str) -> String {
format!(
"warning: not fetching origin in `{}`: its URL is now `{current}`, not the trusted `{pinned}` (a \
sandbox may have rewritten it); if you changed it, trust it with `devsandbox trust-origin {}`",
repo.display(),
repo.display()
)
}
fn fetch_args(url: &str, refs: Refs) -> Vec<String> {
let mut args = vec!["fetch".to_string(), "--quiet".to_string()];
let refspec = match refs {
Refs::All { prune } => {
if prune {
args.push("--prune".into());
}
"+refs/heads/*:refs/remotes/origin/*".to_string()
}
Refs::Branch(b) => {
args.push("--no-write-fetch-head".into());
format!("+refs/heads/{b}:refs/remotes/origin/{b}")
}
};
args.extend(["--end-of-options".to_string(), url.to_string(), refspec]);
args
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
use std::process::Command;
#[test]
fn decide_pins_fetches_or_refuses() {
assert_eq!(decide(None, "/a"), Trust::Pin("/a".into()));
assert_eq!(decide(Some("/a"), "/a"), Trust::Fetch("/a".into()));
assert_eq!(
decide(Some("/a"), "file:///b"),
Trust::Mismatch { pinned: "/a".into(), current: "file:///b".into() }
);
}
#[test]
fn fetch_args_name_the_url_and_own_refspec() {
assert_eq!(
fetch_args("git@h:o/r.git", Refs::All { prune: true }),
["fetch", "--quiet", "--prune", "--end-of-options", "git@h:o/r.git", "+refs/heads/*:refs/remotes/origin/*"]
);
assert_eq!(
fetch_args("/r", Refs::All { prune: false }),
["fetch", "--quiet", "--end-of-options", "/r", "+refs/heads/*:refs/remotes/origin/*"]
);
assert_eq!(
fetch_args("/r", Refs::Branch("pr/1")),
["fetch", "--quiet", "--no-write-fetch-head", "--end-of-options", "/r", "+refs/heads/pr/1:refs/remotes/origin/pr/1"]
);
}
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.arg("-C")
.arg(dir)
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
.args(args)
.output()
.unwrap();
assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn repos(tag: &str) -> (PathBuf, PathBuf, PathBuf) {
let root = std::env::temp_dir().join(format!("devsandbox-origin-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).unwrap();
for (bare, branch) in [("upstream.git", "main"), ("other.git", "secret")] {
let seed = root.join(format!("seed-{branch}"));
git(&root, &["init", "-q", "--bare", "-b", "main", bare]);
git(&root, &["init", "-q", "-b", branch, seed.to_str().unwrap()]);
git(&seed, &["commit", "-q", "--allow-empty", "-m", branch]);
git(&seed, &["push", "-q", root.join(bare).to_str().unwrap(), branch]);
}
git(&root, &["clone", "-q", root.join("upstream.git").to_str().unwrap(), "base"]);
(root.clone(), root.join("base"), root.join("state.toml"))
}
fn remote_refs(base: &Path) -> String {
git(base, &["for-each-ref", "--format=%(refname) %(objectname)", "refs/remotes"])
}
#[test]
fn pins_on_first_fetch_then_refuses_a_rewritten_url() {
let (root, base, state) = repos("pin");
let upstream = root.join("upstream.git");
let seed = root.join("seed-main");
git(&seed, &["commit", "-q", "--allow-empty", "-m", "two"]);
git(&seed, &["push", "-q", upstream.to_str().unwrap(), "main"]);
assert_eq!(fetch_trusted_origin(&state, &base, Refs::All { prune: false }).unwrap(), Fetched::Done);
let key = trust_key(&base.join(".git").canonicalize().unwrap());
let pins = State::load_at(&state).unwrap().trusted_origins;
assert_eq!(pins.get(&key).map(String::as_str), upstream.to_str());
assert_eq!(git(&base, &["rev-parse", "origin/main"]), git(&seed, &["rev-parse", "HEAD"]));
assert_eq!(fetch_trusted_origin(&state, &base, Refs::All { prune: true }).unwrap(), Fetched::Done);
assert_eq!(git(&base, &["symbolic-ref", "refs/remotes/origin/HEAD"]), "refs/remotes/origin/main");
let before = remote_refs(&base);
let other = root.join("other.git");
git(&base, &["remote", "set-url", "origin", &format!("file://{}", other.display())]);
for refs in [Refs::All { prune: true }, Refs::Branch("secret")] {
assert_eq!(fetch_trusted_origin(&state, &base, refs).unwrap(), Fetched::Untrusted);
}
assert_eq!(remote_refs(&base), before);
assert_eq!(State::load_at(&state).unwrap().trusted_origins, pins);
git(&base, &["remote", "remove", "origin"]);
assert_eq!(fetch_trusted_origin(&state, &base, Refs::All { prune: false }).unwrap(), Fetched::NoOrigin);
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn fetches_the_pinned_url_not_the_config_refspec() {
let (root, base, state) = repos("refspec");
fetch_trusted_origin(&state, &base, Refs::All { prune: false }).unwrap();
git(&base, &["config", "remote.origin.fetch", "+refs/heads/*:refs/heads/x/*"]);
git(&base, &["config", "--add", "remote.origin.url", "/elsewhere"]);
assert_eq!(fetch_trusted_origin(&state, &base, Refs::Branch("main")).unwrap(), Fetched::Done);
assert_eq!(git(&base, &["for-each-ref", "--format=%(refname)", "refs/heads/x"]), "");
assert!(matches!(
fetch_trusted_origin(&state, &base, Refs::Branch("missing")).unwrap(),
Fetched::Failed(_)
));
std::fs::remove_dir_all(&root).unwrap();
}
}