mod common;
#[path = "common/pull.rs"]
mod pull;
use std::collections::HashSet;
use std::os::fd::AsFd;
use std::path::{Path, PathBuf};
use common::TmpDir;
use ostrya::endpoint::EndpointOverrides;
use ostrya::{
Checksum, CommitModifier, CommitModifierFlags, CommitOptions, CreateOptions, DeltaOptions,
Ed25519Signer, Error, FileKind, FsckOptions, MutableTree, ObjectName, ObjectType, PullFlags,
PullOptions, PullVerify, Ref, Repo, RepoMode, SummaryOptions, TimestampCheck, Type, Value,
commit_operand,
};
use ostrya_rt::block_on;
const ID: &str = "org.example.Os";
const OTHER: &str = "org.example.Other";
const MAIN: &str = "test/main";
const SECOND: &str = "test/other";
const TS: u64 = 1_700_000_000;
const SECRET_B64: &str =
"o74ME/dmhvDeYf64dDJQY8kX2piK0M/nyIRWVi30i6DCOzRsHVcvgYToz6zOb5OvK/v8nH6KfLR3dfdsn6ZSyQ==";
const PUBLIC_B64: &str = "wjs0bB1XL4GE6M+szm+Tryv7/Jx+iny0d3X3bJ+mUsk=";
#[derive(Clone, Copy, Default)]
struct Bind<'a> {
collection: Option<&'a str>,
refs: Option<&'a [&'a str]>,
}
fn entry(key: &str, ty: &str, value: Value) -> Value {
Value::Tuple(vec![
Value::Str(key.to_owned()),
Value::Variant(Box::new((Type::parse(ty).unwrap(), value))),
])
}
async fn create(dir: &Path, name: &str, mode: RepoMode, own: Option<&str>) -> Repo {
let mut opts = CreateOptions::new(mode);
if let Some(id) = own {
opts = opts.collection_id(id);
}
Repo::create(&dir.join(name), opts).await.unwrap()
}
async fn commit(
repo: &Repo,
dir: &Path,
marker: &str,
r: Option<&Ref>,
timestamp: u64,
bind: Bind<'_>,
) -> Checksum {
let sub = format!("tree-{marker}");
let tree = dir.join(&sub);
std::fs::create_dir_all(tree.join("sub")).unwrap();
std::fs::write(tree.join("file"), marker.as_bytes()).unwrap();
std::fs::write(tree.join("sub/nested"), b"nested\n").unwrap();
let txn = repo.transaction().await.unwrap();
let mut mtree = MutableTree::new();
let mut modifier = CommitModifier::new(
CommitModifierFlags::SKIP_XATTRS | CommitModifierFlags::CANONICAL_PERMISSIONS,
);
let dfd = std::fs::File::open(dir).unwrap();
txn.write_dfd_to_mtree(
dfd.as_fd(),
Path::new(&sub),
&mut mtree,
Some(&mut modifier),
)
.await
.unwrap();
let root = txn.write_mtree(&mut mtree).await.unwrap();
let mut metadata = Vec::new();
if let Some(id) = bind.collection {
metadata.push(entry(
"ostree.collection-binding",
"s",
Value::Str(id.to_owned()),
));
}
if let Some(refs) = bind.refs {
let refs = refs.iter().map(|r| Value::Str((*r).to_owned())).collect();
metadata.push(entry("ostree.ref-binding", "as", Value::Array(refs)));
}
let checksum = txn
.write_commit(
CommitOptions {
subject: Some(marker.to_owned()),
timestamp: Some(timestamp),
metadata: Some(Value::Array(metadata)),
..CommitOptions::default()
},
&root,
)
.await
.unwrap();
if let Some(r) = r {
txn.set_ref(r, Some(&checksum));
}
txn.commit().await.unwrap();
checksum
}
async fn summarize(repo: &Repo) {
repo.regenerate_summary(&SummaryOptions {
last_modified: Some(TS),
..SummaryOptions::default()
})
.await
.unwrap();
}
fn file_url(dir: &Path, name: &str) -> String {
format!("file://{}", dir.join(name).display())
}
async fn dest_named(
dir: &Path,
name: &str,
mode: RepoMode,
own: Option<&str>,
extra: &str,
) -> Repo {
drop(create(dir, name, mode, own).await);
let path = dir.join(name);
let config = path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str(&format!(
"\n[remote \"loc\"]\nurl={}\ngpg-verify=false\n{extra}",
file_url(dir, "src")
));
std::fs::write(&config, text).unwrap();
Repo::open(&path).await.unwrap()
}
async fn dest(dir: &Path, extra: &str) -> Repo {
dest_named(dir, "dest", RepoMode::BareUser, None, extra).await
}
fn refs(names: &[&str]) -> PullOptions {
PullOptions {
refs: names.iter().map(|name| (*name).to_owned()).collect(),
..PullOptions::default()
}
}
async fn remote_ref(repo: &Repo, name: &str) -> Option<Checksum> {
repo.resolve_rev(&format!("loc:{name}"), true)
.await
.unwrap()
}
async fn tip(repo: &Repo, r: &Ref) -> Option<Checksum> {
repo.resolve_ref_tip(r).await.unwrap()
}
async fn assert_nothing_written(repo: &Repo) {
assert!(repo.list_refs(None).await.unwrap().is_empty());
assert!(repo.list_mirror_refs().await.unwrap().is_empty());
assert!(repo.list_remote_refs().await.unwrap().is_empty());
let objects = repo.list_objects().await.unwrap();
assert!(objects.is_empty(), "{objects:?}");
}
fn path(dir: &Path, name: &str) -> PathBuf {
dir.join(name)
}
#[test]
fn a_file_remote_writes_the_refs_of_an_http_pull() {
block_on(async {
let dir = TmpDir::new("pull-file-refs");
let src = create(dir.path(), "src", RepoMode::Archive, Some(ID)).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind {
refs: Some(&[MAIN]),
..Bind::default()
},
)
.await;
let bound = commit(
&src,
dir.path(),
"os",
Some(&Ref::local("os/x")),
TS,
Bind {
collection: Some(ID),
refs: Some(&["os/x"]),
},
)
.await;
summarize(&src).await;
let d = dest(dir.path(), "").await;
let report = d.pull_with_report("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(main));
assert_eq!(report.changed_refs.len(), 1);
assert_eq!(report.changed_refs[0].new, main);
assert_eq!(report.changed_refs[0].old, None);
assert!(report.stats.metadata_imported > 0);
assert_eq!(report.stats.metadata_fetched, 0);
assert!(d.resolve_rev(MAIN, true).await.unwrap().is_none());
assert!(d.fsck(&FsckOptions::default()).await.unwrap().is_ok());
let opts = PullOptions {
remote: Some("x".to_owned()),
..refs(&[MAIN])
};
d.pull("loc", opts).await.unwrap();
assert_eq!(
d.resolve_rev(&format!("x:{MAIN}"), true).await.unwrap(),
Some(main)
);
let at = format!("@{ID}:os/x");
d.pull("loc", refs(&[&at])).await.unwrap();
assert_eq!(tip(&d, &Ref::collection(ID, "os/x")).await, Some(bound));
let own = dest_named(dir.path(), "own", RepoMode::BareUser, Some(ID), "").await;
own.pull("loc", refs(&[&at])).await.unwrap();
assert_eq!(tip(&own, &Ref::local("os/x")).await, Some(bound));
let unbound = dest_named(dir.path(), "unbound", RepoMode::BareUser, None, "").await;
let err = unbound
.pull("loc", refs(&[&format!("@{ID}:{MAIN}")]))
.await
.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_nothing_written(&unbound).await;
let none = dest_named(dir.path(), "none", RepoMode::BareUser, None, "").await;
let opts = PullOptions {
no_ref_writes: true,
..refs(&[MAIN])
};
none.pull("loc", opts).await.unwrap();
assert!(none.list_remote_refs().await.unwrap().is_empty());
assert!(none.load_commit(&main).await.is_ok());
});
}
#[test]
fn a_file_remote_with_no_ref_takes_branches_and_pull_branches() {
block_on(async {
let dir = TmpDir::new("pull-file-branches");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
let second = commit(
&src,
dir.path(),
"second",
Some(&Ref::local(SECOND)),
TS,
Bind::default(),
)
.await;
summarize(&src).await;
for (name, keys, expected) in [
("branches", "branches=test/main;\n", vec![(MAIN, main)]),
(
"selected",
"pull-branches=test/other\n",
vec![(SECOND, second)],
),
(
"both",
"branches=test/main;\npull-branches=test/*\n",
vec![(MAIN, main), (SECOND, second)],
),
] {
let d = dest_named(dir.path(), name, RepoMode::BareUser, None, keys).await;
d.pull("loc", PullOptions::default()).await.unwrap();
let mut listed = d.list_remote_refs().await.unwrap();
listed.sort();
let expected: Vec<(String, Checksum)> = expected
.into_iter()
.map(|(name, commit)| (format!("loc:{name}"), commit))
.collect();
assert_eq!(listed, expected, "{name}");
}
let d = dest_named(dir.path(), "neither", RepoMode::BareUser, None, "").await;
let err = d.pull("loc", PullOptions::default()).await.unwrap_err();
assert!(
matches!(&err, Error::Pull(m) if m == "no configured branches for remote loc"),
"{err:?}"
);
assert_nothing_written(&d).await;
std::fs::remove_file(path(dir.path(), "src/summary")).unwrap();
let d = dest_named(
dir.path(),
"nosummary",
RepoMode::BareUser,
None,
"branches=test/main;\npull-branches=test/*\n",
)
.await;
let err = d.pull("loc", PullOptions::default()).await.unwrap_err();
assert!(
matches!(&err, Error::Pull(m) if m.contains("pull-branches selects refs of the summary")),
"{err:?}"
);
assert_nothing_written(&d).await;
});
}
#[test]
fn a_mirror_pull_from_a_file_remote() {
block_on(async {
let dir = TmpDir::new("pull-file-mirror");
let src = create(dir.path(), "src", RepoMode::Archive, Some(ID)).await;
let bind = Bind {
collection: Some(ID),
refs: None,
};
let main = commit(&src, dir.path(), "main", Some(&Ref::local(MAIN)), TS, bind).await;
let second = commit(
&src,
dir.path(),
"second",
Some(&Ref::local(SECOND)),
TS,
bind,
)
.await;
summarize(&src).await;
let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
src.sign_summary(&signer).await.unwrap();
let mirror = |refs: &[&str]| PullOptions {
flags: PullFlags::MIRROR,
remote: Some("ignored".to_owned()),
..self::refs(refs)
};
let read = |name: &str, file: &str| std::fs::read(dir.path().join(name).join(file)).ok();
let d = dest_named(dir.path(), "dest", RepoMode::Archive, None, "").await;
d.pull("loc", mirror(&[])).await.unwrap();
assert_eq!(tip(&d, &Ref::collection(ID, MAIN)).await, Some(main));
assert_eq!(tip(&d, &Ref::collection(ID, SECOND)).await, Some(second));
assert!(d.list_remote_refs().await.unwrap().is_empty());
let signature = read("src", "summary.sig");
assert!(signature.is_some());
assert_eq!(read("dest", "summary"), read("src", "summary"));
assert_eq!(read("dest", "summary.sig"), signature);
let own = dest_named(dir.path(), "own", RepoMode::Archive, Some(ID), "").await;
own.pull("loc", mirror(&[])).await.unwrap();
assert_eq!(tip(&own, &Ref::local(MAIN)).await, Some(main));
let newer = commit(
&src,
dir.path(),
"newer",
Some(&Ref::local(MAIN)),
TS + 1,
bind,
)
.await;
summarize(&src).await;
assert!(read("src", "summary.sig").is_none());
d.pull("loc", mirror(&[])).await.unwrap();
assert_eq!(tip(&d, &Ref::collection(ID, MAIN)).await, Some(newer));
assert_eq!(read("dest", "summary"), read("src", "summary"));
assert_eq!(read("dest", "summary.sig"), signature);
for (name, opts) in [
("named", mirror(&[MAIN])),
(
"no-writes",
PullOptions {
no_ref_writes: true,
..mirror(&[])
},
),
] {
let d = dest_named(dir.path(), name, RepoMode::Archive, None, "").await;
d.pull("loc", opts).await.unwrap();
assert!(read(name, "summary").is_none(), "{name}");
}
std::fs::remove_file(path(dir.path(), "src/summary")).unwrap();
let d = dest_named(dir.path(), "nosummary", RepoMode::Archive, None, "").await;
let err = d.pull("loc", mirror(&[])).await.unwrap_err();
assert!(
matches!(&err, Error::Pull(m) if m.contains("fetching all refs was requested in mirror mode")),
"{err:?}"
);
assert_nothing_written(&d).await;
});
}
#[test]
fn a_commit_operand_from_a_file_remote() {
block_on(async {
let dir = TmpDir::new("pull-file-commit");
let src = create(dir.path(), "src", RepoMode::Archive, Some(ID)).await;
let free = commit(&src, dir.path(), "free", None, TS, Bind::default()).await;
let bound = commit(
&src,
dir.path(),
"bound",
None,
TS,
Bind {
collection: Some(ID),
refs: None,
},
)
.await;
std::fs::write(path(dir.path(), "src/summary"), b"not a summary").unwrap();
let d = dest(dir.path(), "").await;
d.pull("loc", refs(&[&free.to_hex()])).await.unwrap();
assert!(d.load_commit(&free).await.is_ok());
assert!(d.list_remote_refs().await.unwrap().is_empty());
let at = commit_operand(&Ref::local(MAIN), &free);
d.pull("loc", refs(&[&at])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(free));
assert!(d.pull("loc", refs(&[MAIN])).await.is_err());
let keyed = dest_named(
dir.path(),
"keyed",
RepoMode::BareUser,
None,
&format!("collection-id={OTHER}\n"),
)
.await;
let err = keyed
.pull("loc", refs(&[&bound.to_hex()]))
.await
.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_nothing_written(&keyed).await;
let absent = Checksum::from_hex(&"ab".repeat(32)).unwrap();
let err = d.pull("loc", refs(&[&absent.to_hex()])).await.unwrap_err();
assert!(matches!(err, Error::ObjectNotFound { .. }), "{err:?}");
});
}
#[test]
fn the_timestamp_check_of_a_file_remote() {
block_on(async {
let dir = TmpDir::new("pull-file-timestamp");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let r = Ref::local(MAIN);
let newer = commit(
&src,
dir.path(),
"newer",
Some(&r),
TS + 10,
Bind::default(),
)
.await;
summarize(&src).await;
let d = dest(dir.path(), "").await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
let check = |check| PullOptions {
timestamp_check: check,
..refs(&[MAIN])
};
d.pull("loc", check(TimestampCheck::CurrentRef))
.await
.unwrap();
let older = commit(&src, dir.path(), "older", Some(&r), TS, Bind::default()).await;
summarize(&src).await;
for (name, opts) in [
("current", check(TimestampCheck::CurrentRef)),
("rev", check(TimestampCheck::Rev(newer))),
] {
let err = d.pull("loc", opts).await.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{name}: {err:?}");
assert_eq!(remote_ref(&d, MAIN).await, Some(newer), "{name}");
}
let absent = Checksum::from_hex(&"ab".repeat(32)).unwrap();
let err = d
.pull("loc", check(TimestampCheck::Rev(absent)))
.await
.unwrap_err();
assert!(matches!(err, Error::ObjectNotFound { .. }), "{err:?}");
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(older));
});
}
#[test]
fn a_file_remote_takes_the_trust_of_the_remote() {
block_on(async {
let dir = TmpDir::new("pull-file-trust");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let signed = commit(
&src,
dir.path(),
"signed",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
commit(
&src,
dir.path(),
"unsigned",
Some(&Ref::local(SECOND)),
TS,
Bind::default(),
)
.await;
let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
src.sign_commit(&signed, &signer).await.unwrap();
summarize(&src).await;
let keys = format!("verification-ed25519-key={PUBLIC_B64}\n");
let group = format!("[ex-ostrya trust \"t\"]\nsign-verify=ed25519\n{keys}");
for (name, extra, trust_group) in [
("section", format!("sign-verify=ed25519\n{keys}"), None),
("key", format!("trust=t\n{group}"), None),
("option", group.clone(), Some("t")),
] {
let d = dest_named(dir.path(), name, RepoMode::BareUser, None, &extra).await;
let opts = |name: &str| PullOptions {
trust_group: trust_group.map(str::to_owned),
..refs(&[name])
};
d.pull("loc", opts(MAIN)).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(signed), "{name}");
let err = d.pull("loc", opts(SECOND)).await.unwrap_err();
assert!(matches!(err, Error::Signature(_)), "{name}: {err:?}");
assert_eq!(remote_ref(&d, SECOND).await, None, "{name}");
}
let policy = format!("sign-verify-summary=ed25519\n{keys}");
let d = dest_named(dir.path(), "nosig", RepoMode::BareUser, None, &policy).await;
let err = d.pull("loc", refs(&[MAIN])).await.unwrap_err();
assert!(matches!(err, Error::Signature(_)), "{err:?}");
assert_nothing_written(&d).await;
src.sign_summary(&signer).await.unwrap();
let d = dest_named(dir.path(), "sig", RepoMode::BareUser, None, &policy).await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(signed));
});
}
async fn swap_content(dir: &Path, repo: &Repo, commit: &Checksum) -> Checksum {
let mut names: Vec<Checksum> = repo
.traverse_commit(commit, 0)
.await
.unwrap()
.into_iter()
.filter(|name| name.ty == ObjectType::File)
.map(|name| name.checksum)
.collect();
names.sort_by_key(Checksum::to_hex);
let mut regular = Vec::new();
for checksum in names {
let file = repo.load_file(&checksum).await.unwrap();
if matches!(file.kind, FileKind::Regular { .. }) {
regular.push(checksum);
}
}
let path = |checksum: Checksum| {
dir.join("src/objects")
.join(ObjectName::new(checksum, ObjectType::File).loose_path(RepoMode::Archive))
};
std::fs::remove_file(path(regular[0])).unwrap();
std::fs::copy(path(regular[1]), path(regular[0])).unwrap();
regular[0]
}
#[test]
fn a_signature_policy_hashes_each_object_of_a_file_remote() {
block_on(async {
let keys = format!("sign-verify=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
let summary_keys =
format!("sign-verify-summary=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
for copy in [PullFlags::empty(), PullFlags::FORCE_COPY] {
for case in ["keys", "summary keys", "no keys", "untrusted"] {
let dir = TmpDir::new("pull-file-hash");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
src.sign_commit(&main, &signer).await.unwrap();
summarize(&src).await;
src.sign_summary(&signer).await.unwrap();
let target = swap_content(dir.path(), &src, &main).await;
let extra = match case {
"keys" => keys.as_str(),
"summary keys" => summary_keys.as_str(),
_ => "",
};
let d = dest_named(dir.path(), "dest", RepoMode::Archive, None, extra).await;
let mut opts = refs(&[MAIN]);
opts.flags = copy;
if case == "untrusted" {
opts.flags |= PullFlags::UNTRUSTED;
}
let pulled = d.pull("loc", opts).await;
if case == "no keys" && copy == PullFlags::empty() {
pulled.unwrap();
let report = d.fsck(&FsckOptions::default()).await.unwrap();
let bad: Vec<Checksum> =
report.errors.iter().map(|e| e.object.checksum).collect();
assert_eq!(bad, [target], "{copy:?}");
} else {
let err = pulled.unwrap_err();
assert!(
matches!(err, Error::ChecksumMismatch { expected, .. } if expected == target),
"{copy:?} {case}: {err:?}"
);
assert_nothing_written(&d).await;
}
}
}
});
}
#[test]
fn a_file_remote_in_each_source_mode() {
block_on(async {
for source in [
RepoMode::Archive,
RepoMode::BareUser,
RepoMode::BareUserOnly,
] {
for target in [RepoMode::BareUser, RepoMode::Archive] {
let dir = TmpDir::new("pull-file-modes");
let src = create(dir.path(), "src", source, None).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
summarize(&src).await;
let d = dest_named(dir.path(), "dest", target, None, "").await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(main));
assert!(
d.fsck(&FsckOptions::default()).await.unwrap().is_ok(),
"{source:?} -> {target:?}"
);
if source != RepoMode::Archive {
let opts = PullOptions {
require_static_deltas: true,
..refs(&[MAIN])
};
let err = d.pull("loc", opts).await.unwrap_err();
assert!(matches!(err, Error::Unsupported(_)), "{err:?}");
}
}
}
});
}
#[test]
fn an_alias_ref_of_a_file_remote() {
block_on(async {
for summary in [true, false] {
for bound in [false, true] {
let dir = TmpDir::new("pull-file-alias");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let bind = Bind {
refs: bound.then_some(&[MAIN][..]),
..Bind::default()
};
let main =
commit(&src, dir.path(), "main", Some(&Ref::local(MAIN)), TS, bind).await;
std::os::unix::fs::symlink(
"../test/main",
dir.path().join("src/refs/heads/test/alias"),
)
.unwrap();
if summary {
summarize(&src).await;
}
let d = dest(dir.path(), "").await;
let pulled = d.pull("loc", refs(&["test/alias"])).await;
if bound {
let err = pulled.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
assert_nothing_written(&d).await;
} else {
pulled.unwrap();
assert_eq!(remote_ref(&d, "test/alias").await, Some(main));
let written = dir.path().join("dest/refs/remotes/loc/test/alias");
assert!(std::fs::symlink_metadata(written).unwrap().is_file());
}
}
}
let dir = TmpDir::new("pull-file-alias-bad");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
std::os::unix::fs::symlink("/etc/hostname", dir.path().join("src/refs/heads/test/bad"))
.unwrap();
let d = dest(dir.path(), "").await;
let err = d.pull("loc", refs(&["test/bad"])).await.unwrap_err();
assert!(matches!(err, Error::NotARef { .. }), "{err:?}");
assert_nothing_written(&d).await;
});
}
#[test]
fn a_file_remote_reads_the_summary_first() {
block_on(async {
let dir = TmpDir::new("pull-file-summary-first");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let listed = commit(
&src,
dir.path(),
"listed",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
summarize(&src).await;
let stale = commit(
&src,
dir.path(),
"stale",
Some(&Ref::local(MAIN)),
TS + 1,
Bind::default(),
)
.await;
let new = commit(
&src,
dir.path(),
"new",
Some(&Ref::local("test/new")),
TS,
Bind::default(),
)
.await;
let d = dest(dir.path(), "").await;
d.pull("loc", refs(&[MAIN, "test/new"])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(listed));
assert_eq!(remote_ref(&d, "test/new").await, Some(new));
let err = d.pull("loc", refs(&["test/absent"])).await.unwrap_err();
assert!(
matches!(&err, Error::RefNotFound(name) if name == "test/absent"),
"{err:?}"
);
std::fs::remove_file(path(dir.path(), "src/summary")).unwrap();
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(stale));
});
}
#[test]
fn a_checked_summary_of_a_file_remote_is_the_list_of_refs() {
block_on(async {
let dir = TmpDir::new("pull-file-summary-list");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let listed = commit(
&src,
dir.path(),
"listed",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
summarize(&src).await;
src.sign_summary(&Ed25519Signer::from_base64(SECRET_B64).unwrap())
.await
.unwrap();
commit(
&src,
dir.path(),
"stale",
Some(&Ref::local(MAIN)),
TS + 1,
Bind::default(),
)
.await;
commit(
&src,
dir.path(),
"new",
Some(&Ref::local("test/new")),
TS,
Bind::default(),
)
.await;
commit(
&src,
dir.path(),
"other",
Some(&Ref::collection(OTHER, "x")),
TS,
Bind {
collection: Some(OTHER),
refs: None,
},
)
.await;
std::fs::write(
path(dir.path(), "src/refs/heads/test/garbage"),
b"no checksum\n",
)
.unwrap();
let policy =
format!("sign-verify-summary=ed25519\nverification-ed25519-key={PUBLIC_B64}\n");
let collection = format!("@{OTHER}:x");
for (i, (opts, keys, missing)) in [
(refs(&["test/new"]), "", "test/new"),
(refs(&["test/garbage"]), "", "test/garbage"),
(refs(&[&collection]), "", collection.as_str()),
(PullOptions::default(), "branches=test/new;\n", "test/new"),
]
.into_iter()
.enumerate()
{
let d = dest_named(
dir.path(),
&format!("dest-{i}"),
RepoMode::BareUser,
None,
&format!("{policy}{keys}"),
)
.await;
let err = d.pull("loc", opts).await.unwrap_err();
assert!(
matches!(&err, Error::RefNotFound(name) if name == missing),
"{missing}: {err:?}"
);
assert_nothing_written(&d).await;
}
let d = dest(dir.path(), &policy).await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(remote_ref(&d, MAIN).await, Some(listed));
});
}
#[test]
fn a_file_remote_refuses_the_fields_of_another_transport() {
block_on(async {
let dir = TmpDir::new("pull-file-refusals");
let d = dest(dir.path(), "").await;
let cases: Vec<(&str, EndpointOverrides)> = vec![
(
"http_headers",
EndpointOverrides {
http_headers: vec![("A".to_owned(), "b".to_owned())],
..EndpointOverrides::default()
},
),
(
"max_retries",
EndpointOverrides {
max_retries: Some(1),
..EndpointOverrides::default()
},
),
(
"tls_ca_path",
EndpointOverrides {
tls_ca_path: Some(PathBuf::from("/ca.pem")),
..EndpointOverrides::default()
},
),
(
"ssh_command",
EndpointOverrides {
ssh_command: Some(vec!["ssh".to_owned()]),
..EndpointOverrides::default()
},
),
(
"s3_region",
EndpointOverrides {
s3_region: Some("eu-west-1".to_owned()),
..EndpointOverrides::default()
},
),
(
"push_user",
EndpointOverrides {
push_user: Some("u".to_owned()),
..EndpointOverrides::default()
},
),
];
for (name, overrides) in cases {
let opts = PullOptions {
overrides,
..refs(&[MAIN])
};
let err = d.pull("loc", opts).await.unwrap_err();
assert!(matches!(err, Error::InvalidInput(_)), "{name}: {err:?}");
}
let opts = PullOptions {
subpaths: vec!["sub".to_owned()],
..refs(&[MAIN])
};
let err = d.pull("loc", opts).await.unwrap_err();
assert!(matches!(err, Error::Pull(_)), "{err:?}");
let err = d.pull("loc", refs(&[MAIN])).await.unwrap_err();
assert!(matches!(err, Error::Io(_)), "{err:?}");
assert_nothing_written(&d).await;
});
}
#[test]
fn the_url_override_reads_a_file_address() {
block_on(async {
let dir = TmpDir::new("pull-file-url");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
summarize(&src).await;
let d = create(dir.path(), "dest", RepoMode::BareUser, None).await;
let opts = PullOptions {
overrides: EndpointOverrides {
url: Some(file_url(dir.path(), "src")),
..EndpointOverrides::default()
},
verify: PullVerify {
gpg: Some(false),
..PullVerify::default()
},
..refs(&[MAIN])
};
d.pull("nosection", opts).await.unwrap();
assert_eq!(
d.resolve_rev(&format!("nosection:{MAIN}"), true)
.await
.unwrap(),
Some(main)
);
});
}
#[test]
fn a_file_remote_takes_static_deltas_as_pull_local() {
block_on(async {
let dir = TmpDir::new("pull-file-deltas");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let main = commit(
&src,
dir.path(),
"main",
Some(&Ref::local(MAIN)),
TS,
Bind::default(),
)
.await;
let opts = PullOptions {
require_static_deltas: true,
..refs(&[MAIN])
};
summarize(&src).await;
let d = dest_named(dir.path(), "none", RepoMode::BareUser, None, "").await;
let err = d.pull("loc", opts.clone()).await.unwrap_err();
assert!(
matches!(&err, Error::Pull(m) if m.starts_with("Static deltas required, but none found for")),
"{err:?}"
);
assert_nothing_written(&d).await;
src.generate_static_delta(None, &main, &DeltaOptions::default())
.await
.unwrap();
summarize(&src).await;
let d = dest_named(dir.path(), "dest", RepoMode::BareUser, None, "").await;
let stats = d.pull("loc", opts).await.unwrap();
assert!(stats.delta_parts > 0, "{stats:?}");
assert_eq!(remote_ref(&d, MAIN).await, Some(main));
assert!(d.fsck(&FsckOptions::default()).await.unwrap().is_ok());
});
}
async fn subpath_source(dir: &Path) -> (Repo, Checksum, Checksum) {
use std::os::unix::fs::PermissionsExt;
let src = create(dir, "src", RepoMode::Archive, None).await;
let trees: [(&str, &[(&str, &str)]); 2] = [
(
"tree-c1",
&[("top", "top 1\n"), ("a/f1", "f1 1\n"), ("b/f2", "f2 1\n")],
),
(
"tree-c2",
&[
("top", "top\n"),
("a/f1", "f1\n"),
("a/g", "g\n"),
("b/f2", "f2\n"),
("b/n", "n\n"),
("sub/z", "z\n"),
("sub/x/y", "y\n"),
],
),
];
let mut parent = None;
for (name, files) in trees {
let tree = dir.join(name);
for (file, content) in files {
let file = tree.join(file);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(&file, content).unwrap();
}
for (sub, mode) in [
("", 0o755),
("a", 0o750),
("b", 0o711),
("sub", 0o701),
("sub/x", 0o705),
] {
let sub = tree.join(sub);
if sub.exists() {
std::fs::set_permissions(&sub, std::fs::Permissions::from_mode(mode)).unwrap();
}
}
parent = Some(
pull::commit_tree_with(
&src,
dir,
name,
MAIN,
parent,
TS,
CommitModifierFlags::SKIP_XATTRS,
)
.await,
);
}
let c2 = parent.unwrap();
let c1 = src.load_commit(&c2).await.unwrap().0.parent.unwrap();
let signer = Ed25519Signer::from_base64(SECRET_B64).unwrap();
src.sign_commit(&c2, &signer).await.unwrap();
summarize(&src).await;
(src, c1, c2)
}
fn subpath_opts(values: &[&str]) -> PullOptions {
PullOptions {
subpaths: values.iter().map(|v| (*v).to_owned()).collect(),
..refs(&[MAIN])
}
}
async fn subpath_objects(src: &Repo, commit: &Checksum, values: &[&str]) -> HashSet<ObjectName> {
let mut out = pull::subpath_base(src, commit).await;
let root = src.load_commit(commit).await.unwrap().0.root_dirtree;
for value in values {
let components: Vec<&str> = value.split('/').filter(|c| !c.is_empty()).collect();
let mut tree = root;
for (index, component) in components.iter().enumerate() {
let path = format!("/{}", components[..=index].join("/"));
let dirtree = src.load_dirtree(&tree).await.unwrap();
if dirtree.files.iter().any(|(name, _)| name == component) {
if index + 1 == components.len() {
out.extend(pull::entry_objects(src, commit, &path).await);
}
break;
}
let Some((_, subtree, _)) = dirtree.dirs.iter().find(|(name, _, _)| name == component)
else {
break;
};
tree = *subtree;
if index + 1 == components.len() {
out.extend(pull::whole_objects(src, commit, &path).await);
} else {
out.extend(pull::entry_objects(src, commit, &path).await);
}
}
}
out
}
fn commitmeta(commit: &Checksum) -> ObjectName {
ObjectName::new(*commit, ObjectType::CommitMeta)
}
async fn assert_partial(repo: &Repo, dir: &Path, name: &str, commit: &Checksum) {
assert_eq!(
repo.commit_state(commit).await.unwrap(),
ostrya::CommitState::Partial
);
let marker = path(dir, name)
.join("state")
.join(format!("{}.commitpartial", commit.to_hex()));
assert_eq!(std::fs::metadata(&marker).unwrap().len(), 0, "{marker:?}");
}
#[test]
fn a_subpath_pull_from_a_file_remote_imports_the_subpath_objects() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath");
let (src, _, c2) = subpath_source(dir.path()).await;
let cases: [&[&str]; 4] = [&["/a"], &["/a", "/sub/x"], &["/sub/x"], &["/nonexist"]];
for (index, values) in cases.into_iter().enumerate() {
let mut expected = subpath_objects(&src, &c2, values).await;
expected.insert(commitmeta(&c2));
for mode in [RepoMode::BareUser, RepoMode::Archive] {
let name = format!("dest-{index}-{}", mode.as_mode_str());
let d = dest_named(dir.path(), &name, mode, None, "").await;
let stats = d.pull("loc", subpath_opts(values)).await.unwrap();
let tag = format!("{values:?} {mode:?}");
assert_eq!(d.list_objects().await.unwrap(), expected, "{tag}");
assert_eq!(remote_ref(&d, MAIN).await, Some(c2), "{tag}");
assert_partial(&d, dir.path(), &name, &c2).await;
let files = expected.iter().filter(|o| o.ty == ObjectType::File).count();
let metadata = expected.len() - files - 1;
assert_eq!(stats.content_imported as usize, files, "{tag}");
assert_eq!(stats.metadata_imported as usize, metadata, "{tag}");
assert_eq!(stats.metadata_fetched, 0, "{tag}");
assert_eq!(stats.content_fetched, 0, "{tag}");
assert_eq!(stats.delta_parts, 0, "{tag}");
}
}
});
}
#[test]
fn a_second_pull_completes_a_subpath_pull_from_a_file_remote() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-second");
let (src, _, c2) = subpath_source(dir.path()).await;
let d = dest(dir.path(), "").await;
let mut expected = subpath_objects(&src, &c2, &["/a"]).await;
expected.insert(commitmeta(&c2));
for _ in 0..2 {
d.pull("loc", subpath_opts(&["/a"])).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "dest", &c2).await;
}
d.pull("loc", subpath_opts(&["/b"])).await.unwrap();
expected.extend(subpath_objects(&src, &c2, &["/b"]).await);
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "dest", &c2).await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
let mut whole = subpath_objects(&src, &c2, &["/"]).await;
whole.extend(pull::whole_objects(&src, &c2, "/").await);
whole.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), whole);
assert_eq!(
d.commit_state(&c2).await.unwrap(),
ostrya::CommitState::Normal
);
assert!(d.fsck(&FsckOptions::default()).await.unwrap().is_ok());
let stats = d.pull("loc", subpath_opts(&["/a"])).await.unwrap();
assert_eq!(stats.metadata_imported + stats.content_imported, 0);
assert_eq!(
d.commit_state(&c2).await.unwrap(),
ostrya::CommitState::Normal
);
assert!(!dir.path().join("dest/state").read_dir().unwrap().any(|e| {
e.unwrap()
.file_name()
.to_string_lossy()
.ends_with(".commitpartial")
}));
});
}
#[test]
fn a_subpath_that_names_no_entry_imports_the_root() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-noentry");
let (src, _, c2) = subpath_source(dir.path()).await;
let mut base = pull::subpath_base(&src, &c2).await;
base.insert(commitmeta(&c2));
let mut dir_a = base.clone();
dir_a.extend(pull::entry_objects(&src, &c2, "/a").await);
let cases = [
("/", &base),
("//sub", &base),
("/top/", &base),
("/nonexist", &base),
("/a/", &dir_a),
];
for (index, (value, expected)) in cases.into_iter().enumerate() {
let name = format!("dest-{index}");
let d = dest_named(dir.path(), &name, RepoMode::BareUser, None, "").await;
d.pull("loc", subpath_opts(&[value])).await.unwrap();
assert_eq!(&d.list_objects().await.unwrap(), expected, "{value}");
assert_eq!(remote_ref(&d, MAIN).await, Some(c2), "{value}");
assert_partial(&d, dir.path(), &name, &c2).await;
}
});
}
#[test]
fn a_dirtree_at_two_subpath_positions_of_a_file_remote_takes_the_union() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-union");
pull::build_subpath_tree(&dir.path().join("tree"));
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let tip = pull::commit_tree(&src, dir.path(), "tree", MAIN, None, TS).await;
assert_eq!(
pull::entry_objects(&src, &tip, "/sub/x").await,
pull::entry_objects(&src, &tip, "/other/x").await
);
let mut expected = pull::subpath_base(&src, &tip).await;
expected.extend(pull::entry_objects(&src, &tip, "/sub").await);
expected.extend(pull::entry_objects(&src, &tip, "/other").await);
expected.extend(pull::whole_objects(&src, &tip, "/sub/x").await);
let cases: [&[&str]; 4] = [
&["/sub/x/", "/other/x"],
&["/other/x", "/sub/x/"],
&["/other/x/", "/sub/x"],
&["/sub/x", "/other/x/"],
];
for (index, values) in cases.into_iter().enumerate() {
let name = format!("dest-{index}");
let d = dest_named(dir.path(), &name, RepoMode::BareUser, None, "").await;
d.pull("loc", subpath_opts(values)).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), expected, "{values:?}");
assert_partial(&d, dir.path(), &name, &tip).await;
}
});
}
#[test]
fn a_dirtree_at_two_subpath_depths_of_a_file_remote_takes_the_union() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-depths");
let tree = dir.path().join("tree");
std::fs::create_dir_all(tree.join("y/x")).unwrap();
std::fs::create_dir_all(tree.join("x")).unwrap();
std::fs::write(tree.join("top"), b"top\n").unwrap();
std::fs::write(tree.join("y/f"), b"f\n").unwrap();
std::fs::write(tree.join("x/s"), b"same\n").unwrap();
std::fs::write(tree.join("y/x/s"), b"same\n").unwrap();
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let tip = pull::commit_tree(&src, dir.path(), "tree", MAIN, None, TS).await;
assert_eq!(
pull::entry_objects(&src, &tip, "/x").await,
pull::entry_objects(&src, &tip, "/y/x").await
);
let mut expected = pull::subpath_base(&src, &tip).await;
expected.extend(pull::entry_objects(&src, &tip, "/y").await);
expected.extend(pull::whole_objects(&src, &tip, "/x").await);
let file = pull::entry_objects(&src, &tip, "/y/x/s").await;
assert!(expected.contains(&file[0]));
let cases: [&[&str]; 2] = [&["/x/", "/y/x"], &["/y/x", "/x/"]];
for (index, values) in cases.into_iter().enumerate() {
let name = format!("dest-{index}");
let d = dest_named(dir.path(), &name, RepoMode::BareUser, None, "").await;
d.pull("loc", subpath_opts(values)).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), expected, "{values:?}");
assert_eq!(remote_ref(&d, MAIN).await, Some(tip), "{values:?}");
assert_partial(&d, dir.path(), &name, &tip).await;
}
});
}
#[test]
fn a_dirtree_at_two_subpath_positions_across_a_chain_takes_the_union() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-chain");
for (tree, files) in [
("tree-c1", [("a/t", "t\n"), ("b/s", "same\n")]),
("tree-c2", [("a/s", "same\n"), ("b/u", "u\n")]),
] {
for (file, content) in files {
let file = dir.path().join(tree).join(file);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(&file, content).unwrap();
}
}
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
let c1 = pull::commit_tree(&src, dir.path(), "tree-c1", MAIN, None, TS).await;
let c2 = pull::commit_tree(&src, dir.path(), "tree-c2", MAIN, Some(c1), TS).await;
assert_eq!(
pull::entry_objects(&src, &c2, "/a").await,
pull::entry_objects(&src, &c1, "/b").await
);
let mut expected = HashSet::new();
for commit in [c1, c2] {
expected.extend(pull::subpath_base(&src, &commit).await);
expected.extend(pull::entry_objects(&src, &commit, "/a").await);
expected.extend(pull::whole_objects(&src, &commit, "/b").await);
}
assert!(expected.is_superset(&pull::whole_objects(&src, &c2, "/a").await));
let cases: [&[&str]; 2] = [&["/a/", "/b"], &["/b", "/a/"]];
for (index, values) in cases.into_iter().enumerate() {
let name = format!("dest-{index}");
let d = dest_named(dir.path(), &name, RepoMode::BareUser, None, "").await;
let opts = PullOptions {
depth: 1,
..subpath_opts(values)
};
d.pull("loc", opts).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), expected, "{values:?}");
assert_eq!(remote_ref(&d, MAIN).await, Some(c2), "{values:?}");
assert_partial(&d, dir.path(), &name, &c1).await;
assert_partial(&d, dir.path(), &name, &c2).await;
}
});
}
#[test]
fn subpaths_of_a_file_remote_combine_with_depth_commit_only_and_mirror() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-options");
let (src, c1, c2) = subpath_source(dir.path()).await;
let d = dest_named(dir.path(), "depth", RepoMode::BareUser, None, "").await;
let opts = PullOptions {
depth: 1,
..subpath_opts(&["/a"])
};
d.pull("loc", opts).await.unwrap();
let mut expected = subpath_objects(&src, &c2, &["/a"]).await;
expected.extend(subpath_objects(&src, &c1, &["/a"]).await);
expected.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "depth", &c1).await;
assert_partial(&d, dir.path(), "depth", &c2).await;
let d = dest_named(dir.path(), "commit-only", RepoMode::BareUser, None, "").await;
let commit_only = HashSet::from([ObjectName::new(c2, ObjectType::Commit), commitmeta(&c2)]);
let opts = PullOptions {
flags: PullFlags::COMMIT_ONLY,
..subpath_opts(&["/a"])
};
d.pull("loc", opts).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), commit_only);
let opts = PullOptions {
flags: PullFlags::COMMIT_ONLY,
..refs(&[MAIN])
};
d.pull("loc", opts).await.unwrap();
assert_eq!(d.list_objects().await.unwrap(), commit_only);
assert_partial(&d, dir.path(), "commit-only", &c2).await;
d.pull("loc", subpath_opts(&["/b"])).await.unwrap();
let mut expected = subpath_objects(&src, &c2, &["/b"]).await;
expected.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "commit-only", &c2).await;
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert_eq!(
d.commit_state(&c2).await.unwrap(),
ostrya::CommitState::Normal
);
assert!(d.fsck(&FsckOptions::default()).await.unwrap().is_ok());
let d = dest_named(dir.path(), "mirror", RepoMode::Archive, None, "").await;
let opts = PullOptions {
flags: PullFlags::MIRROR,
..subpath_opts(&["/a"])
};
d.pull("loc", opts).await.unwrap();
assert_eq!(tip(&d, &Ref::local(MAIN)).await, Some(c2));
assert!(d.list_remote_refs().await.unwrap().is_empty());
let mut expected = subpath_objects(&src, &c2, &["/a"]).await;
expected.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "mirror", &c2).await;
assert!(!path(dir.path(), "mirror").join("summary").exists());
let d = dest_named(dir.path(), "mirror-all", RepoMode::Archive, None, "").await;
let opts = PullOptions {
flags: PullFlags::MIRROR,
subpaths: vec!["/a".to_owned()],
..PullOptions::default()
};
d.pull("loc", opts).await.unwrap();
assert_eq!(tip(&d, &Ref::local(MAIN)).await, Some(c2));
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "mirror-all", &c2).await;
assert_eq!(
std::fs::read(path(dir.path(), "mirror-all").join("summary")).unwrap(),
std::fs::read(path(dir.path(), "src").join("summary")).unwrap()
);
});
}
#[test]
fn a_subpath_pull_from_a_file_remote_imports_no_attachment() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-attachment");
let src = create(dir.path(), "src", RepoMode::Archive, None).await;
pull::build_tree(&dir.path().join("att"), b"attachment\n");
let att = pull::commit_tree(&src, dir.path(), "att", "test/att", None, TS).await;
pull::build_subpath_tree(&dir.path().join("tree"));
let main =
pull::commit_tree_related(&src, dir.path(), "tree", MAIN, None, &[("att", att)]).await;
let d = dest(dir.path(), "").await;
d.pull("loc", subpath_opts(&["/sub"])).await.unwrap();
assert_eq!(
d.list_objects().await.unwrap(),
subpath_objects(&src, &main, &["/sub"]).await
);
assert!(!d.has_object(ObjectType::Commit, &att).await.unwrap());
d.pull("loc", refs(&[MAIN])).await.unwrap();
assert!(d.has_object(ObjectType::Commit, &att).await.unwrap());
assert!(d.fsck(&FsckOptions::default()).await.unwrap().is_ok());
});
}
#[test]
fn a_subpath_pull_from_a_file_remote_requires_deltas_as_pull_local() {
block_on(async {
let dir = TmpDir::new("pull-file-subpath-delta");
let (src, _, c2) = subpath_source(dir.path()).await;
src.generate_static_delta(None, &c2, &DeltaOptions::default())
.await
.unwrap();
summarize(&src).await;
let d = dest_named(dir.path(), "loose", RepoMode::BareUser, None, "").await;
let stats = d.pull("loc", subpath_opts(&["/a"])).await.unwrap();
assert_eq!(stats.delta_parts, 0, "{stats:?}");
let mut expected = subpath_objects(&src, &c2, &["/a"]).await;
expected.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), expected);
assert_partial(&d, dir.path(), "loose", &c2).await;
let d = dest_named(dir.path(), "delta", RepoMode::BareUser, None, "").await;
let opts = PullOptions {
require_static_deltas: true,
..subpath_opts(&["/a"])
};
let stats = d.pull("loc", opts).await.unwrap();
assert!(stats.delta_parts > 0, "{stats:?}");
let mut whole = pull::subpath_base(&src, &c2).await;
whole.extend(pull::whole_objects(&src, &c2, "/").await);
whole.insert(commitmeta(&c2));
assert_eq!(d.list_objects().await.unwrap(), whole);
assert_eq!(remote_ref(&d, MAIN).await, Some(c2));
assert_partial(&d, dir.path(), "delta", &c2).await;
});
}