use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use anyhow::{Context, Result, bail};
use crate::commands::run::{host_git, host_git_bare, host_git_new};
use crate::runtime::bounded;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Checkout {
pub branch: Option<String>,
}
pub fn probe(folder: &Path) -> Result<Option<Checkout>> {
if folder.join(".git").symlink_metadata().is_err() {
return Ok(None);
}
let branch = optional(host_git(folder)?.args(["symbolic-ref", "-q", "--short", "HEAD"]))?;
Ok(Some(Checkout { branch }))
}
pub fn has_branch(repo: Repo, branch: &str) -> Result<bool> {
check_branch(branch)?;
Ok(commit_at(repo, &format!("refs/heads/{branch}"))?.is_some())
}
pub(super) struct TempRef<'a> {
checkout: &'a Path,
pub(super) name: String,
}
impl<'a> TempRef<'a> {
pub(super) fn new(checkout: &'a Path) -> Self {
TempRef { checkout, name: format!("refs/devsandbox/wip/send-{}", nonce()) }
}
pub(super) fn at(checkout: &'a Path, id: &str) -> Result<Self> {
check_oid(id)?;
let temp = TempRef::new(checkout);
run(host_git(checkout)?.args(["update-ref", &temp.name, id]), QUICK).with_context(|| format!("cannot point a ref at {id}"))?;
Ok(temp)
}
}
impl Drop for TempRef<'_> {
fn drop(&mut self) {
if let Ok(mut cmd) = host_git(self.checkout) {
let _ = run(cmd.args(["update-ref", "-d", &self.name]), QUICK);
}
}
}
pub fn check_branch(branch: &str) -> Result<()> {
let ok_char = |c: char| c.is_ascii_alphanumeric() || "._+/-".contains(c);
let ok_part = |p: &str| !p.is_empty() && !p.starts_with(['.', '-']) && !p.ends_with(".lock");
if branch.is_empty() || branch.len() > 200 || !branch.chars().all(ok_char) || branch.contains("..") || !branch.split('/').all(ok_part) {
bail!("`{branch}` isn't a branch name devsandbox moves");
}
Ok(())
}
pub fn check_id(id: &str) -> Result<()> {
if id.len() != 16 || !id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) {
bail!("`{id}` isn't a repo identity");
}
Ok(())
}
pub fn wip_ref(branch: &str) -> String {
format!("refs/devsandbox/wip/{branch}")
}
pub fn ensure_bare(cache: &Path) -> Result<()> {
if !cache.join("HEAD").exists() {
run(host_git_new(cache)?.args(["init", "-q", "--bare"]), QUICK)
.with_context(|| format!("cannot make the repo cache {}", cache.display()))?;
}
Ok(())
}
pub fn unbundle(cache: &Path, bundle: &Path) -> Result<()> {
unbundle_into(Repo::Bare(cache), bundle)
}
pub fn unbundle_into(repo: Repo, bundle: &Path) -> Result<()> {
run(repo.git()?.args(["bundle", "unbundle"]).arg(bundle), WHOLE).context("cannot take the bundle")?;
Ok(())
}
pub fn set_refs(cache: &Path, refs: &std::collections::BTreeMap<String, String>) -> Result<()> {
for (name, id) in refs {
check_ref(name)?;
check_oid(id)?;
if commit_at(Repo::Bare(cache), id)?.as_deref() != Some(id.as_str()) {
bail!("{name}'s commit {id} is neither in the bundle nor already here");
}
}
for (name, id) in refs {
run(host_git_bare(cache)?.args(["update-ref", name, id]), QUICK).with_context(|| format!("cannot set {name}"))?;
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Landed {
Cut,
FastForwarded,
Moved,
Kept,
}
pub fn land_from(cache: &Path, at: &Path, branch: &str, wip: Option<&str>, in_use: bool) -> Result<Landed> {
check_branch(branch)?;
if let Some(w) = wip {
check_oid(w)?;
}
let fresh = !at.join(".git").exists();
if fresh {
std::fs::create_dir_all(at).with_context(|| format!("cannot create {}", at.display()))?;
if std::fs::read_dir(at)?.next().is_some() {
bail!("{} exists and isn't a git checkout", at.display());
}
run(host_git_new(at)?.args(["init", "-q"]), QUICK)?;
}
let head = optional(host_git(at)?.args(["symbolic-ref", "-q", "HEAD"]))?;
let full = format!("refs/heads/{branch}");
let fetch = |refspecs: &[String]| -> Result<()> {
run(host_git(at)?.args(["fetch", "-q", "--no-tags", "--update-head-ok"]).arg(cache).args(refspecs), WHOLE)
.with_context(|| format!("cannot fetch {branch} from the repo cache"))?;
Ok(())
};
if fresh || !in_use {
run(host_git(at)?.args(["symbolic-ref", "HEAD"]).arg(&full), QUICK)?;
let wip_at = wip.map(|_| wip_ref(branch));
let mut refspecs = vec![format!("+{full}:{full}")];
refspecs.extend(wip_at.iter().map(|w| format!("+{w}:{w}")));
fetch(&refspecs)?;
run(host_git(at)?.args(["reset", "-q", "--hard"]), WHOLE)?;
run(host_git(at)?.args(["clean", "-q", "-f", "-d"]), WHOLE).with_context(|| format!("cannot clean {}", at.display()))?;
if let (Some(w), Some(wip_at)) = (wip, wip_at) {
super::wip::apply_wip(at, w)?;
run(host_git(at)?.args(["update-ref", "-d", &wip_at]), QUICK)?;
}
return Ok(Landed::Cut);
}
if head.as_deref() != Some(full.as_str()) {
fetch(&[format!("+{full}:{full}")])?;
return Ok(Landed::Moved);
}
fetch(std::slice::from_ref(&full))?;
let clean = run(host_git(at)?.args(["status", "--porcelain"]), WHOLE)?.trim().is_empty();
let descends = || -> Result<bool> {
let out = bounded_git(host_git(at)?.args(["merge-base", "--is-ancestor", "HEAD", "FETCH_HEAD"]), QUICK)?;
match out.status.code() {
Some(0) => Ok(true),
Some(1) => Ok(false),
_ => bail!("git failed: {}", String::from_utf8_lossy(&out.stderr).trim()),
}
};
if !clean || !descends()? {
return Ok(Landed::Kept);
}
run(host_git(at)?.args(["merge", "-q", "--ff-only", "FETCH_HEAD"]), WHOLE)
.with_context(|| format!("cannot fast-forward {branch} in {}", at.display()))?;
Ok(Landed::FastForwarded)
}
#[derive(Debug, Clone, Copy)]
pub enum Repo<'a> {
Checkout(&'a Path),
Bare(&'a Path),
}
impl Repo<'_> {
fn git(&self) -> Result<Command> {
match self {
Repo::Checkout(p) => host_git(p),
Repo::Bare(p) => host_git_bare(p),
}
}
pub(super) fn git_dir(&self) -> Result<PathBuf> {
match self {
Repo::Bare(p) => Ok(p.to_path_buf()),
Repo::Checkout(p) => {
let dir = run(host_git(p)?.args(["rev-parse", "--absolute-git-dir"]), QUICK)?;
Ok(PathBuf::from(dir.trim()))
}
}
}
}
pub(super) const IDENTITY: [(&str, &str); 4] = [
("GIT_AUTHOR_NAME", "devsandbox"),
("GIT_AUTHOR_EMAIL", "devsandbox@localhost"),
("GIT_COMMITTER_NAME", "devsandbox"),
("GIT_COMMITTER_EMAIL", "devsandbox@localhost"),
];
pub const ROOT_REF: &str = "refs/devsandbox/root";
pub const HAVES_CAP: usize = 256;
pub fn identity(repo: Repo, rev: &str) -> Result<String> {
check_rev(rev)?;
let out = run(repo.git()?.args(["rev-list", "--max-parents=0"]).arg(rev).arg("--"), WHOLE)
.with_context(|| format!("cannot list the root commits of {rev}"))?;
let mut roots: Vec<&str> = out.lines().collect();
if roots.is_empty() {
bail!("{rev} has no root commit");
}
roots.sort_unstable();
let text: String = roots.iter().map(|r| format!("{r}\n")).collect();
Ok(super::short_hash(text.as_bytes()))
}
pub fn ensure_root(checkout: &Path) -> Result<Option<String>> {
if head_commit(checkout)?.is_some() {
return Ok(None);
}
if let Some(root) = commit_at(Repo::Checkout(checkout), ROOT_REF)? {
return Ok(Some(root));
}
let tree = run(host_git(checkout)?.arg("mktree"), QUICK)?.trim().to_string();
let commit = run(
host_git(checkout)?
.args(["-c", "commit.gpgsign=false", "commit-tree"])
.arg(&tree)
.arg("-m")
.arg(format!("devsandbox root {}", nonce()))
.envs(IDENTITY),
QUICK,
)?
.trim()
.to_string();
check_oid(&commit)?;
let made = run(host_git(checkout)?.args(["update-ref", ROOT_REF, &commit, ""]), QUICK);
match (made, commit_at(Repo::Checkout(checkout), ROOT_REF)?) {
(_, Some(root)) => Ok(Some(root)),
(Err(e), None) => Err(e).with_context(|| format!("cannot make {ROOT_REF}")),
(Ok(_), None) => bail!("{ROOT_REF} vanished after it was made"),
}
}
pub(super) fn head_commit(checkout: &Path) -> Result<Option<String>> {
commit_at(Repo::Checkout(checkout), "HEAD")
}
pub(super) fn commit_at(repo: Repo, rev: &str) -> Result<Option<String>> {
optional(repo.git()?.args(["rev-parse", "-q", "--verify"]).arg(format!("{rev}^{{commit}}")))
}
pub fn bundle_refs(repo: Repo, refs: &[&str], haves: &[String], out: &Path) -> Result<bool> {
if refs.is_empty() {
bail!("nothing to bundle");
}
for r in refs {
check_ref(r)?;
}
let haves = known_commits(repo, &haves[..haves.len().min(HAVES_CAP)])?;
let count = run(repo.git()?.args(["rev-list", "--count"]).args(refs).arg("--not").args(&haves).arg("--"), WHOLE)
.context("cannot count what to bundle")?;
if count.trim() == "0" {
return Ok(false);
}
run(repo.git()?.args(["bundle", "create", "-q"]).arg(out).args(refs).arg("--not").args(&haves), WHOLE)
.with_context(|| format!("cannot bundle {}", refs.join(" ")))?;
Ok(true)
}
fn known_commits(repo: Repo, haves: &[String]) -> Result<Vec<String>> {
let mut seen = std::collections::BTreeSet::new();
let haves: Vec<&String> = haves.iter().filter(|h| seen.insert(h.as_str())).collect();
for h in &haves {
check_oid(h)?;
}
if haves.is_empty() {
return Ok(Vec::new());
}
let list = TempPath::new(&repo.git_dir()?, "haves");
let text: String = haves.iter().map(|h| format!("{h}\n")).collect();
std::fs::write(&list.0, text).with_context(|| format!("cannot write {}", list.0.display()))?;
let input = std::fs::File::open(&list.0).with_context(|| format!("cannot read {}", list.0.display()))?;
let out = run(
repo.git()?.args(["cat-file", "--batch-check=%(objectname) %(objecttype)"]).stdin(Stdio::from(input)),
QUICK,
)?;
let known: std::collections::BTreeSet<&str> =
out.lines().filter_map(|l| l.strip_suffix(" commit")).collect();
Ok(haves.into_iter().filter(|h| known.contains(h.as_str())).cloned().collect())
}
pub fn tips(repo: Repo) -> Result<Vec<String>> {
let out = run(
repo.git()?.args(["for-each-ref", "--sort=-committerdate", "--format=%(objecttype) %(objectname)"]),
QUICK,
)?;
let mut seen = std::collections::BTreeSet::new();
Ok(out
.lines()
.filter_map(|l| l.strip_prefix("commit "))
.filter(|id| seen.insert(*id))
.take(HAVES_CAP)
.map(str::to_string)
.collect())
}
pub fn check_oid(id: &str) -> Result<()> {
if !matches!(id.len(), 40 | 64) || !id.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) {
bail!("`{id}` isn't a commit id");
}
Ok(())
}
pub fn check_ref(name: &str) -> Result<()> {
match name.strip_prefix("refs/") {
Some(rest) if check_branch(rest).is_ok() => Ok(()),
_ => bail!("`{name}` isn't a ref devsandbox moves"),
}
}
pub fn check_rev(rev: &str) -> Result<()> {
if rev == "HEAD" || check_ref(rev).is_ok() || check_oid(rev).is_ok() {
return Ok(());
}
bail!("`{rev}` isn't a revision devsandbox names")
}
pub(super) fn nonce() -> String {
static NEXT: AtomicU64 = AtomicU64::new(0);
let ns = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
format!("{ns:x}-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))
}
pub(super) struct TempPath(pub PathBuf);
impl TempPath {
pub(super) fn new(dir: &Path, tag: &str) -> Self {
TempPath(dir.join(format!("devsandbox-{tag}-{}", nonce())))
}
}
impl Drop for TempPath {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
let mut lock = self.0.clone().into_os_string();
lock.push(".lock");
let _ = std::fs::remove_file(lock);
}
}
pub(super) const QUICK: Duration = Duration::from_secs(60);
pub(super) const WHOLE: Duration = Duration::from_secs(30 * 60);
const STDOUT_CAP: usize = 16 * 1024 * 1024;
fn bounded_git(cmd: &mut Command, timeout: Duration) -> Result<bounded::Output> {
use std::os::unix::process::CommandExt;
cmd.process_group(0);
let out = bounded::run(cmd, STDOUT_CAP, timeout).map_err(|e| match e {
bounded::Error::TimedOut(d) => anyhow::anyhow!("git timed out after {}", bounded::human(d)),
bounded::Error::Io(e) => anyhow::Error::new(e).context("cannot run git"),
})?;
if out.truncated {
bail!("git printed more than {} MiB", STDOUT_CAP >> 20);
}
Ok(out)
}
pub(super) fn run(cmd: &mut Command, timeout: Duration) -> Result<String> {
let out = bounded_git(cmd, timeout)?;
if !out.status.success() {
bail!("git failed: {}", String::from_utf8_lossy(&out.stderr).trim());
}
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}
pub(super) fn optional(cmd: &mut Command) -> Result<Option<String>> {
let out = bounded_git(cmd, QUICK)?;
let text = String::from_utf8_lossy(&out.stdout).trim().to_string();
match out.status.code() {
Some(0) if !text.is_empty() => Ok(Some(text)),
Some(0 | 1 | 2) if text.is_empty() => Ok(None),
_ => bail!("git failed: {}", String::from_utf8_lossy(&out.stderr).trim()),
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub fn scratch(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("devsandbox-code-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::canonicalize(&dir).unwrap()
}
pub fn git(dir: &Path, args: &[&str]) -> String {
let mut cmd = Command::new("git");
cmd.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", "-c", "init.defaultBranch=main"])
.arg("-C")
.arg(dir)
.args(args)
.stdin(Stdio::null());
let out = bounded_git(&mut cmd, QUICK).unwrap();
assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
pub fn repo_with_branches(dir: &Path, name: &str) -> PathBuf {
let at = dir.join(name);
std::fs::create_dir_all(&at).unwrap();
git(&at, &["init", "-q", "-b", "main"]);
std::fs::write(at.join("README"), "main\n").unwrap();
git(&at, &["add", "README"]);
git(&at, &["commit", "-q", "-m", "main"]);
git(&at, &["checkout", "-q", "-b", "feature"]);
std::fs::write(at.join("F"), "feature\n").unwrap();
git(&at, &["add", "F"]);
git(&at, &["commit", "-q", "-m", "feature"]);
git(&at, &["checkout", "-q", "main"]);
at
}
#[test]
fn a_branch_goes_from_a_checkout_through_the_cache_into_a_new_checkout() {
let dir = scratch("bundle");
let src = repo_with_branches(&dir, "web");
assert_eq!(probe(&src).unwrap(), Some(Checkout { branch: Some("main".into()) }));
assert_eq!(probe(&dir).unwrap(), None, "not a checkout");
assert!(has_branch(Repo::Checkout(&src), "feature").unwrap());
assert!(!has_branch(Repo::Checkout(&src), "nope").unwrap());
git(&src, &["checkout", "-q", "--detach"]);
assert_eq!(probe(&src).unwrap(), Some(Checkout { branch: None }), "detached");
git(&src, &["checkout", "-q", "main"]);
let worker = dir.join("worker/c.git");
std::fs::create_dir_all(&worker).unwrap();
ensure_bare(&worker).unwrap();
ensure_bare(&worker).unwrap();
let send = |branch: &str, name: &str| {
let full = format!("refs/heads/{branch}");
let file = dir.join(name);
if bundle_refs(Repo::Checkout(&src), &[&full], &tips(Repo::Bare(&worker)).unwrap(), &file).unwrap() {
unbundle(&worker, &file).unwrap();
}
let id = commit_at(Repo::Checkout(&src), &full).unwrap().unwrap();
set_refs(&worker, &[(full, id)].into()).unwrap();
};
send("feature", "f.bundle");
let at = dir.join("worker/web");
assert_eq!(land_from(&worker, &at, "feature", None, false).unwrap(), Landed::Cut);
assert_eq!(std::fs::read_to_string(at.join("F")).unwrap(), "feature\n");
assert_eq!(git(&at, &["symbolic-ref", "--short", "HEAD"]), "feature");
assert_eq!(git(&at, &["remote"]), "", "no remote on the worker");
assert_eq!(git(&at, &["status", "--porcelain"]), "");
git(&src, &["checkout", "-q", "feature"]);
std::fs::write(src.join("F"), "feature 2\n").unwrap();
git(&src, &["commit", "-q", "-am", "two"]);
send("feature", "f2.bundle");
assert_eq!(land_from(&worker, &at, "feature", None, true).unwrap(), Landed::FastForwarded);
assert_eq!(std::fs::read_to_string(at.join("F")).unwrap(), "feature 2\n");
send("main", "m.bundle");
assert!(!dir.join("m.bundle").exists());
assert_eq!(land_from(&worker, &at, "main", None, true).unwrap(), Landed::Moved);
assert_eq!(git(&at, &["symbolic-ref", "--short", "HEAD"]), "feature");
assert_eq!(git(&at, &["rev-parse", "main"]), git(&src, &["rev-parse", "main"]));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_existing_checkout_is_recut_when_free_and_kept_when_in_use_and_dirty() {
let dir = scratch("land-modes");
let src = repo_with_branches(&dir, "web");
std::fs::write(src.join(".gitignore"), "*.log\n").unwrap();
git(&src, &["add", ".gitignore"]);
git(&src, &["commit", "-q", "-m", "ignore"]);
let cache = dir.join("c.git");
std::fs::create_dir_all(&cache).unwrap();
ensure_bare(&cache).unwrap();
let send = |files: &[(&str, &str)]| -> String {
let base = git(&src, &["rev-parse", "HEAD"]);
git(&src, &["checkout", "-q", "--detach"]);
for (f, body) in files {
std::fs::write(src.join(f), body).unwrap();
git(&src, &["add", f]);
}
git(&src, &["commit", "-q", "--allow-empty", "-m", "wip"]);
let wip = git(&src, &["rev-parse", "HEAD"]);
git(&src, &["checkout", "-q", "main"]);
let refs = ["refs/heads/main".to_string(), wip_ref("main")];
git(&src, &["update-ref", &refs[1], &wip]);
let file = dir.join(format!("{}.bundle", nonce()));
if bundle_refs(Repo::Checkout(&src), &[refs[0].as_str(), refs[1].as_str()], &tips(Repo::Bare(&cache)).unwrap(), &file).unwrap() {
unbundle(&cache, &file).unwrap();
}
set_refs(&cache, &[(refs[0].clone(), base), (refs[1].clone(), wip.clone())].into()).unwrap();
wip
};
let at = dir.join("worker/web");
let wip1 = send(&[("README", "wip 1\n"), ("U", "u\n")]);
assert_eq!(land_from(&cache, &at, "main", Some(&wip1), false).unwrap(), Landed::Cut);
assert_eq!(git(&at, &["status", "--porcelain=v1", "-uall"]), "M README\n?? U");
std::fs::write(at.join("x.log"), "built\n").unwrap();
let first = git(&at, &["rev-parse", "HEAD"]);
std::fs::write(src.join("README"), "two\n").unwrap();
git(&src, &["commit", "-q", "-am", "two"]);
let wip2 = send(&[("README", "wip 2\n")]);
assert_eq!(land_from(&cache, &at, "main", Some(&wip2), true).unwrap(), Landed::Kept);
assert_eq!(git(&at, &["rev-parse", "HEAD"]), first);
assert_eq!(git(&at, &["status", "--porcelain=v1", "-uall"]), "M README\n?? U");
assert_eq!(land_from(&cache, &at, "main", Some(&wip2), false).unwrap(), Landed::Cut);
assert_eq!(git(&at, &["rev-parse", "HEAD"]), git(&src, &["rev-parse", "main"]));
assert_eq!(git(&at, &["status", "--porcelain=v1", "-uall"]), "M README");
assert_eq!(std::fs::read_to_string(at.join("README")).unwrap(), "wip 2\n");
assert_eq!(std::fs::read_to_string(at.join("x.log")).unwrap(), "built\n");
assert_eq!(git(&at, &["for-each-ref", "refs/devsandbox"]), "");
git(&at, &["checkout", "-q", "--", "README"]);
git(&src, &["reset", "-q", "--hard", "HEAD~1"]);
let wip3 = send(&[]);
assert_eq!(land_from(&cache, &at, "main", Some(&wip3), true).unwrap(), Landed::Kept);
assert_ne!(git(&at, &["rev-parse", "HEAD"]), git(&src, &["rev-parse", "main"]));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_temp_ref_is_gone_once_dropped() {
let dir = scratch("temp-ref");
let src = repo_with_branches(&dir, "web");
{
let r = TempRef::new(&src);
crate::serve::fleet::code::wip::check_wip_ref(&r.name).unwrap();
git(&src, &["update-ref", &r.name, "HEAD"]);
assert_eq!(git(&src, &["for-each-ref", "--format=%(refname)", "refs/devsandbox"]), r.name);
}
assert_eq!(git(&src, &["for-each-ref", "refs/devsandbox"]), "");
drop(TempRef::new(&src));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_cache_refuses_bad_bundles_and_missing_commits_and_land_a_dir_that_isnt_a_checkout() {
let dir = scratch("land-refuses");
let cache = dir.join("c.git");
std::fs::create_dir_all(&cache).unwrap();
ensure_bare(&cache).unwrap();
std::fs::write(dir.join("junk.bundle"), "not a bundle").unwrap();
let err = format!("{:#}", unbundle(&cache, &dir.join("junk.bundle")).unwrap_err());
assert!(err.contains("cannot take the bundle"), "{err}");
let a = one_commit(&dir, "a", "one\n");
let base = git(&a, &["rev-parse", "HEAD"]);
std::fs::write(a.join("f"), "two\n").unwrap();
git(&a, &["commit", "-q", "-am", "two"]);
let delta = dir.join("d.bundle");
assert!(bundle_refs(Repo::Checkout(&a), &["refs/heads/main"], &[base], &delta).unwrap());
assert!(unbundle(&cache, &delta).is_err());
let head = git(&a, &["rev-parse", "HEAD"]);
let err = format!("{:#}", set_refs(&cache, &[("refs/heads/main".to_string(), head)].into()).unwrap_err());
assert!(err.contains("is neither in the bundle nor already here"), "{err}");
assert_eq!(tips(Repo::Bare(&cache)).unwrap(), Vec::<String>::new());
std::fs::create_dir_all(dir.join("full")).unwrap();
std::fs::write(dir.join("full/x"), "x").unwrap();
let err = format!("{:#}", land_from(&cache, &dir.join("full"), "main", None, false).unwrap_err());
assert!(err.contains("isn't a git checkout"), "{err}");
let err = format!("{:#}", land_from(&cache, &dir.join("empty"), "main", None, false).unwrap_err());
assert!(err.contains("cannot fetch main from the repo cache"), "{err}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn ids_are_checked() {
check_id("0123456789abcdef").unwrap();
for bad in ["", "0123456789abcde", "0123456789ABCDEF", "0123456789abcdefa", "../../../../etc/x"] {
assert!(check_id(bad).is_err(), "{bad:?}");
}
}
#[test]
fn branch_names_are_checked() {
for ok in ["main", "feature/x-1", "v1.2", "a+b_c"] {
check_branch(ok).unwrap();
}
for bad in ["", "-x", "a/.b", "a..b", "a b", "x.lock", "a//b", "/a", "a/", "a@{1}", "a~1", "a:b"] {
assert!(check_branch(bad).is_err(), "{bad:?}");
}
}
fn one_commit(dir: &Path, name: &str, body: &str) -> PathBuf {
let at = dir.join(name);
std::fs::create_dir_all(&at).unwrap();
git(&at, &["init", "-q", "-b", "main"]);
std::fs::write(at.join("f"), body).unwrap();
git(&at, &["add", "f"]);
git(&at, &["commit", "-q", "-m", body]);
at
}
#[test]
fn identity_is_the_roots_across_clones() {
let dir = scratch("identity");
let a = one_commit(&dir, "a", "a\n");
std::fs::write(a.join("f"), "a2\n").unwrap();
git(&a, &["commit", "-q", "-am", "two"]);
let id = identity(Repo::Checkout(&a), "HEAD").unwrap();
assert_eq!(id.len(), 16);
let root = git(&a, &["rev-list", "--max-parents=0", "HEAD"]);
assert_eq!(id, crate::serve::fleet::code::short_hash(format!("{root}\n").as_bytes()));
assert_eq!(identity(Repo::Checkout(&a), "refs/heads/main").unwrap(), id);
assert_eq!(identity(Repo::Checkout(&a), &git(&a, &["rev-parse", "HEAD~1"])).unwrap(), id);
git(&dir, &["clone", "-q", a.to_str().unwrap(), "clone"]);
assert_eq!(identity(Repo::Checkout(&dir.join("clone")), "HEAD").unwrap(), id);
git(&dir, &["clone", "-q", "--bare", a.to_str().unwrap(), "bare.git"]);
assert_eq!(identity(Repo::Bare(&dir.join("bare.git")), "refs/heads/main").unwrap(), id);
let b = one_commit(&dir, "b", "b\n");
assert_ne!(identity(Repo::Checkout(&b), "HEAD").unwrap(), id);
git(&a, &["fetch", "-q", b.to_str().unwrap(), "main:b"]);
git(&a, &["merge", "-q", "--allow-unrelated-histories", "-X", "ours", "-m", "m", "b"]);
assert_ne!(identity(Repo::Checkout(&a), "HEAD").unwrap(), id);
assert!(identity(Repo::Checkout(&a), "--all").is_err());
assert!(identity(Repo::Checkout(&a), "main").is_err(), "full refs only");
let e = dir.join("empty");
std::fs::create_dir_all(&e).unwrap();
git(&e, &["init", "-q"]);
assert!(identity(Repo::Checkout(&e), "HEAD").is_err());
assert_eq!(ensure_root(&a).unwrap(), None, "a repo with commits needs no root");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn bundles_carry_only_what_the_haves_lack() {
let dir = scratch("bundle-haves");
let a = one_commit(&dir, "a", "one\n");
let first = dir.join("1.bundle");
assert!(bundle_refs(Repo::Checkout(&a), &["refs/heads/main"], &[], &first).unwrap());
let b = dir.join("b.git");
std::fs::create_dir_all(&b).unwrap();
git(&b, &["init", "-q", "--bare"]);
git(&b, &["fetch", "-q", first.to_str().unwrap(), "refs/heads/main:refs/heads/main"]);
std::fs::write(a.join("f"), "two\n").unwrap();
git(&a, &["commit", "-q", "-am", "two"]);
let mut haves = tips(Repo::Bare(&b)).unwrap();
assert_eq!(haves, [git(&a, &["rev-parse", "HEAD~1"])]);
haves.insert(0, "0".repeat(40));
haves.push(haves[1].clone());
let delta = dir.join("2.bundle");
assert!(bundle_refs(Repo::Checkout(&a), &["refs/heads/main"], &haves, &delta).unwrap());
git(&b, &["bundle", "verify", "-q", delta.to_str().unwrap()]);
let empty = dir.join("e.git");
std::fs::create_dir_all(&empty).unwrap();
git(&empty, &["init", "-q", "--bare"]);
let mut verify = host_git_bare(&empty).unwrap();
verify.args(["bundle", "verify", "-q"]).arg(&delta);
assert!(!bounded_git(&mut verify, QUICK).unwrap().status.success());
let bytes = std::fs::read(&delta).unwrap();
let header = String::from_utf8_lossy(&bytes[..bytes.windows(2).position(|w| w == b"\n\n").unwrap()]).into_owned();
assert!(header.lines().any(|l| l.starts_with(&format!("-{}", haves[1]))), "{header}");
assert!(git(&b, &["bundle", "list-heads", delta.to_str().unwrap()]).ends_with(" refs/heads/main"));
git(&b, &["fetch", "-q", delta.to_str().unwrap(), "refs/heads/main:refs/heads/main"]);
let none = dir.join("3.bundle");
assert!(!bundle_refs(Repo::Checkout(&a), &["refs/heads/main"], &tips(Repo::Bare(&b)).unwrap(), &none).unwrap());
assert!(!none.exists());
for bad in ["-x", "abc", &"A".repeat(40), &"0".repeat(41), "HEAD"] {
assert!(bundle_refs(Repo::Checkout(&a), &["refs/heads/main"], &[bad.to_string()], &none).is_err(), "{bad}");
}
for bad in ["main", "-x", "refs/../x", "refs/", "HEAD"] {
assert!(bundle_refs(Repo::Checkout(&a), &[bad], &[], &none).is_err(), "{bad}");
}
assert!(!none.exists());
assert!(leftovers(&a.join(".git")).is_empty(), "the haves list is removed");
let _ = std::fs::remove_dir_all(&dir);
}
fn leftovers(gitdir: &Path) -> Vec<String> {
std::fs::read_dir(gitdir)
.unwrap()
.filter_map(|e| e.unwrap().file_name().into_string().ok())
.filter(|n| n.starts_with("devsandbox-"))
.collect()
}
#[test]
fn tips_are_commits_capped() {
let dir = scratch("tips");
let r = dir.join("r.git");
std::fs::create_dir_all(&r).unwrap();
git(&r, &["init", "-q", "--bare"]);
let mut stream = String::new();
for i in 0..300 {
let msg = format!("c{i}");
stream.push_str(&format!("commit refs/heads/b{i}\ncommitter t <t@t> {} +0000\ndata {}\n{msg}\n", 1_700_000_000 + i, msg.len()));
}
stream.push_str("reset refs/heads/same\nfrom refs/heads/b0\n\n");
stream.push_str("tag v1\nfrom refs/heads/b0\ntagger t <t@t> 1700000000 +0000\ndata 1\nx\n");
let file = dir.join("stream");
std::fs::write(&file, stream).unwrap();
let mut cmd = host_git_bare(&r).unwrap();
cmd.args(["fast-import", "--quiet"]).stdin(Stdio::from(std::fs::File::open(&file).unwrap()));
let out = bounded_git(&mut cmd, QUICK).unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let got = tips(Repo::Bare(&r)).unwrap();
assert_eq!(got.len(), HAVES_CAP);
assert_eq!(got[0], git(&r, &["rev-parse", "refs/heads/b299"]), "newest first");
assert_eq!(got.iter().collect::<std::collections::BTreeSet<_>>().len(), got.len(), "deduped");
assert!(!got.contains(&git(&r, &["rev-parse", "refs/tags/v1"])), "tag objects left out");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn revs_and_oids_are_checked() {
for ok in ["HEAD", "refs/heads/main", "refs/devsandbox/root", &"a".repeat(40), &"0".repeat(64)] {
check_rev(ok).unwrap();
}
for bad in ["", "main", "-x", "--all", "HEAD~1", "refs/heads/a..b", &"a".repeat(39), &"g".repeat(40), "refs/heads/x y"] {
assert!(check_rev(bad).is_err(), "{bad:?}");
}
}
#[test]
fn a_stuck_git_child_is_killed() {
let start = std::time::Instant::now();
let mut cmd = Command::new("sh");
cmd.args(["-c", "exec sleep 30"]).stdin(Stdio::null());
let err = run(&mut cmd, Duration::from_millis(200)).unwrap_err().to_string();
assert!(err.contains("timed out"), "{err}");
assert!(start.elapsed() < Duration::from_secs(10));
}
#[test]
fn the_cache_is_refused_like_a_checkout() {
let dir = scratch("bare-check");
let cache = dir.join("c.git");
std::fs::create_dir_all(&cache).unwrap();
git(&cache, &["init", "-q", "--bare"]);
host_git_bare(&cache).unwrap();
std::fs::write(cache.join("config"), "[core]\n\tbare = true\n\tfsmonitor = /tmp/x\n").unwrap();
let err = format!("{:#}", host_git_bare(&cache).unwrap_err());
assert!(err.contains("sets core.fsmonitor"), "{err}");
assert!(host_git_new(&dir.join("missing")).is_err());
let _ = std::fs::remove_dir_all(&dir);
}
}