#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use super::*;
use crate::Options;
static COUNTER: AtomicU64 = AtomicU64::new(0);
fn hex_of(bytes: &[u8]) -> String {
use std::fmt::Write as _;
bytes.iter().fold(String::new(), |mut out, b| {
let _ = write!(out, "{b:02x}");
out
})
}
fn git_available() -> bool {
Command::new("git")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
struct TempRepo {
dir: PathBuf,
}
impl Drop for TempRepo {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}
impl TempRepo {
fn new() -> Self {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"brygge-git-test-{}-{nanos}-{n}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let r = Self { dir };
r.git(&["init", "-q", "--initial-branch=main"]);
r
}
fn path(&self) -> &Path {
&self.dir
}
fn git(&self, args: &[&str]) -> String {
let out = Command::new("git")
.current_dir(&self.dir)
.args(args)
.env("GIT_AUTHOR_NAME", "A U Thor")
.env("GIT_AUTHOR_EMAIL", "author@example.com")
.env("GIT_AUTHOR_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_COMMITTER_NAME", "C O Mitter")
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
.env("GIT_COMMITTER_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.output()
.expect("run git");
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn write(&self, rel: &str, contents: &str) {
let p = self.dir.join(rel);
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(p, contents).unwrap();
}
fn commit_all(&self, msg: &str) {
self.git(&["add", "-A"]);
self.git(&["-c", "commit.gpgsign=false", "commit", "-q", "-m", msg]);
}
fn commit_index(&self, msg: &str) {
self.git(&["-c", "commit.gpgsign=false", "commit", "-q", "-m", msg]);
}
fn set_cacheinfo(&self, mode: &str, oid: &str, path: &str) {
self.git(&[
"update-index",
"--add",
"--cacheinfo",
&format!("{mode},{oid},{path}"),
]);
}
fn force_remove(&self, path: &str) {
self.git(&["update-index", "--force-remove", path]);
}
fn hash_blob(&self, contents: &str) -> String {
let scratch = self.dir.join(format!(
".blob-scratch-{}",
COUNTER.fetch_add(1, Ordering::Relaxed)
));
std::fs::write(&scratch, contents).unwrap();
let oid = self.git(&["hash-object", "-w", &scratch.to_string_lossy()]);
let _ = std::fs::remove_file(&scratch);
oid
}
}
const GHOST_COMMIT: &str = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
#[test]
fn decodes_an_added_submodule_never_reading_its_pinned_commit() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("readme.txt", "hi\n");
r.commit_all("initial");
r.set_cacheinfo("160000", GHOST_COMMIT, "ghost-sub");
r.commit_index("add a gitlink to a commit this repo never has");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = ir.atoms.last().unwrap();
let op = atom
.ops
.iter()
.find(|op| op.path() == "ghost-sub")
.expect("the gitlink's own op");
match op.content() {
Some(Content::Submodule { commit }) => assert_eq!(hex_of(commit), GHOST_COMMIT),
other => panic!("expected an Add of Content::Submodule, got {other:?}"),
}
assert!(matches!(op, brygge_ir::PathOp::Add { .. }));
}
#[test]
fn decodes_a_moved_submodule_pointer_as_modify() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("readme.txt", "hi\n");
r.commit_all("c1");
let head1 = r.git(&["rev-parse", "HEAD"]);
r.write("other.txt", "x\n");
r.commit_all("c2");
let head2 = r.git(&["rev-parse", "HEAD"]);
r.set_cacheinfo("160000", &head1, "sub");
r.commit_index("add gitlink pinned at c1");
r.set_cacheinfo("160000", &head2, "sub");
r.commit_index("move gitlink to c2");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = ir.atoms.last().unwrap();
assert_eq!(
atom.ops.len(),
1,
"only the moved pointer should be an op: {:?}",
atom.ops
);
match &atom.ops[0] {
brygge_ir::PathOp::Modify {
path,
content: Content::Submodule { commit },
..
} => {
assert_eq!(path, "sub");
assert_eq!(hex_of(commit), head2);
}
other => panic!("expected a Modify of Content::Submodule, got {other:?}"),
}
}
#[test]
fn decodes_a_file_replaced_by_a_submodule_as_replace() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("x.txt", "a file\n");
r.commit_all("c1");
let pin = r.git(&["rev-parse", "HEAD"]);
r.set_cacheinfo("160000", &pin, "x.txt");
r.commit_index("replace x.txt with a submodule");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = ir.atoms.last().unwrap();
match atom.ops.iter().find(|op| op.path() == "x.txt") {
Some(brygge_ir::PathOp::Replace {
content: Content::Submodule { commit },
..
}) => assert_eq!(hex_of(commit), pin),
other => panic!("expected a Replace of Content::Submodule, got {other:?}"),
}
}
#[test]
fn decodes_a_submodule_replaced_by_a_file_as_replace() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("readme.txt", "hi\n");
r.commit_all("c1");
let pin = r.git(&["rev-parse", "HEAD"]);
r.set_cacheinfo("160000", &pin, "y");
r.commit_index("add gitlink y");
let blob = r.hash_blob("a replacement file\n");
r.set_cacheinfo("100644", &blob, "y");
r.commit_index("replace submodule y with a file");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = ir.atoms.last().unwrap();
match atom.ops.iter().find(|op| op.path() == "y") {
Some(brygge_ir::PathOp::Replace {
content: Content::Blob { mode, .. },
..
}) => assert_eq!(*mode, 0o100_644),
other => panic!("expected a Replace of Content::Blob, got {other:?}"),
}
}
#[test]
fn decodes_a_removed_submodule_as_delete() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("readme.txt", "hi\n");
r.commit_all("c1");
let pin = r.git(&["rev-parse", "HEAD"]);
r.set_cacheinfo("160000", &pin, "z");
r.commit_index("add gitlink z");
r.force_remove("z");
r.commit_index("remove gitlink z");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = ir.atoms.last().unwrap();
assert!(
atom.ops
.iter()
.any(|op| matches!(op, brygge_ir::PathOp::Delete { path, .. } if path == "z"))
);
}
#[test]
fn a_repository_with_a_submodule_no_longer_hits_the_floor() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("readme.txt", "hi\n");
r.commit_all("c1");
let pin = r.git(&["rev-parse", "HEAD"]);
r.set_cacheinfo("160000", &pin, "sub");
r.commit_index("add gitlink");
decode(r.path(), &Options::default()).expect("a submodule is carried, never refused");
}
fn build_rich_repo() -> TempRepo {
let r = TempRepo::new();
r.write("readme.txt", "hello\n");
r.write("keep.txt", "keep me\n");
r.commit_all("initial");
r.write("readme.txt", "hello world\n"); r.commit_all("expand readme");
r.git(&["checkout", "-q", "-b", "feature"]);
r.write("feature.txt", "a feature\n");
r.commit_all("add feature");
r.git(&["checkout", "-q", "main"]);
std::fs::remove_file(r.path().join("keep.txt")).unwrap(); r.commit_all("drop keep");
r.git(&[
"-c",
"commit.gpgsign=false",
"merge",
"-q",
"--no-ff",
"-m",
"merge feature",
"feature",
]);
r.git(&["tag", "-a", "v1", "-m", "release one"]);
r.write("readme.txt", "hello world, locally dirty\n");
r.git(&["stash", "push", "-q", "-m", "wip"]);
r
}
#[test]
fn decodes_a_rich_history_all_stated() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = build_rich_repo();
let ir = decode(r.path(), &Options::default()).unwrap();
assert_eq!(ir.atoms.len(), 5, "expected five commits");
assert!(ir.refs.iter().any(|rf| rf.name == "v1"));
assert!(ir.refs.iter().any(|rf| rf.name == "main"));
let report = brygge_ir::honesty::summary(&ir);
assert!(
report.derived.is_empty(),
"a plain import must be all Stated"
);
assert!(report.blobs >= 3);
assert!(
ir.atoms.iter().any(|a| a.parents.len() == 2),
"merge has two parents"
);
assert!(
ir.atoms.iter().any(|a| a.parents.is_empty()),
"root has no parents"
);
let any = &ir.atoms[0];
assert_eq!(
any.metadata
.author
.as_ref()
.unwrap()
.email
.as_ref()
.and_then(brygge_ir::Text::as_utf8),
Some("author@example.com")
);
assert!(any.metadata.commit_time.is_some());
let has_delete = ir
.atoms
.iter()
.flat_map(|a| &a.ops)
.any(|op| matches!(op, brygge_ir::PathOp::Delete { path, .. } if path == "keep.txt"));
let has_modify = ir
.atoms
.iter()
.flat_map(|a| &a.ops)
.any(|op| matches!(op, brygge_ir::PathOp::Modify { path, .. } if path == "readme.txt"));
assert!(has_delete, "the delete of keep.txt is a literal Delete op");
assert!(has_modify, "the readme modification is a literal Modify op");
}
#[test]
fn decode_is_byte_deterministic_and_pack_independent() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = build_rich_repo();
let a = brygge_ir::to_bytes(&decode(r.path(), &Options::default()).unwrap());
let b = brygge_ir::to_bytes(&decode(r.path(), &Options::default()).unwrap());
assert_eq!(
a, b,
"two decodes of the same repo are byte-identical (VF-1)"
);
r.git(&["repack", "-a", "-d", "-q"]);
r.git(&["gc", "-q", "--aggressive"]);
let c = brygge_ir::to_bytes(&decode(r.path(), &Options::default()).unwrap());
assert_eq!(
a, c,
"physical packing must not change the output (RFC 004 D-6)"
);
}
#[test]
fn renames_off_by_default_on_marks_derived() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("old/name.txt", "identical content\n");
r.commit_all("add file");
std::fs::remove_file(r.path().join("old/name.txt")).unwrap();
r.write("new/name.txt", "identical content\n");
r.commit_all("move file");
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(ir.atoms.iter().all(|a| a.copies.is_empty()));
assert!(brygge_ir::honesty::summary(&ir).derived.is_empty());
let opts = Options {
infer_renames: true,
rename_threshold: 100,
carry_extra_namespaces: Vec::new(),
};
let ir = decode(r.path(), &opts).unwrap();
let move_atom = ir
.atoms
.iter()
.find(|a| !a.copies.is_empty())
.expect("the move commit carries a copy record");
let copy = &move_atom.copies[0];
assert_eq!(copy.from, "old/name.txt");
assert_eq!(copy.to, "new/name.txt");
assert_eq!(
copy.from_atom, move_atom.parents[0],
"from_atom is the first parent"
);
assert!(
move_atom.is_move(copy),
"a delete+add move is reported as a move"
);
assert!(copy.status.is_derived(), "an inferred rename is Derived");
assert!(
move_atom.ops.iter().any(
|op| matches!(op, brygge_ir::PathOp::Delete { path, .. } if path == "old/name.txt")
)
);
assert!(
move_atom
.ops
.iter()
.any(|op| matches!(op, brygge_ir::PathOp::Add { path, .. } if path == "new/name.txt"))
);
assert_eq!(
brygge_ir::honesty::summary(&ir)
.derived
.get("inferred-rename"),
Some(&1)
);
}
#[test]
fn loss_boundary_records_representation_drops() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = build_rich_repo();
let ir = decode(r.path(), &Options::default()).unwrap();
let whats: Vec<&str> = ir.loss.dropped.iter().map(|d| d.what.as_str()).collect();
assert!(whats.iter().any(|w| w.contains("packfile")));
assert!(whats.iter().any(|w| w.contains("reflogs")));
assert!(
ir.loss
.dropped
.iter()
.all(|d| matches!(d.class, brygge_ir::LossClass::Representation)),
"an annotated tag's tagger/time/message is now carried in `annotation`, not dropped \
(batch-2 handoff §1.6): {:?}",
ir.loss.dropped
);
assert!(
!whats.iter().any(|w| w.contains("annotated tag")),
"the old annotated-tag drop record is gone: {whats:?}"
);
}
#[test]
fn refuses_shallow_and_grafts_and_replace() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
std::fs::write(
r.path().join(".git/shallow"),
r.git(&["rev-parse", "HEAD"]) + "\n",
)
.unwrap();
assert!(matches!(
decode(r.path(), &Options::default()),
Err(crate::Error::FloorRefusal { ref feature, .. }) if feature == "shallow-clone"
));
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
std::fs::create_dir_all(r.path().join(".git/info")).unwrap();
std::fs::write(
r.path().join(".git/info/grafts"),
r.git(&["rev-parse", "HEAD"]) + "\n",
)
.unwrap();
assert!(matches!(
decode(r.path(), &Options::default()),
Err(crate::Error::FloorRefusal { ref feature, .. }) if feature == "grafts"
));
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let c1 = r.git(&["rev-parse", "HEAD"]);
r.write("a.txt", "b\n");
r.commit_all("c2");
let c2 = r.git(&["rev-parse", "HEAD"]);
r.git(&["replace", &c2, &c1]);
assert!(matches!(
decode(r.path(), &Options::default()),
Err(crate::Error::FloorRefusal { ref feature, .. }) if feature == "replace-ref"
));
}
#[test]
fn symbolic_ref_in_a_dropped_namespace_decodes_without_panic() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let head = r.git(&["rev-parse", "HEAD"]);
r.git(&["update-ref", "refs/remotes/origin/main", &head]);
r.git(&[
"symbolic-ref",
"refs/remotes/origin/HEAD",
"refs/remotes/origin/main",
]);
let ir = decode(r.path(), &Options::default()).unwrap();
let whats: Vec<&str> = ir.loss.dropped.iter().map(|d| d.what.as_str()).collect();
assert!(
whats.contains(&"symbolic refs (1)"),
"the symbolic ref is recorded as its own drop: {whats:?}"
);
assert!(
whats.contains(&"remote-tracking refs (1)"),
"the target ref is still handled on its own: {whats:?}"
);
}
#[test]
fn symbolic_ref_in_a_carried_namespace_is_dropped_not_its_target() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&["symbolic-ref", "refs/heads/alias", "refs/heads/main"]);
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(
ir.refs.iter().any(|rf| rf.name == "main"),
"the real branch main is still carried"
);
assert!(
ir.refs.iter().all(|rf| rf.name != "alias"),
"a symbolic ref is never carried, even in a normally-carried namespace (RFC 004 OQ-B)"
);
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "symbolic refs (1)")
);
}
#[test]
fn dangling_symbolic_ref_decodes_and_is_dropped() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&[
"symbolic-ref",
"refs/heads/nowhere",
"refs/heads/does-not-exist",
]);
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(ir.refs.iter().all(|rf| rf.name != "nowhere"));
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "symbolic refs (1)"),
"a dangling symbolic ref is recorded exactly like a resolving one"
);
}
fn commit_with_raw_time(r: &TempRepo, time_field: &str) -> String {
use std::io::Write as _;
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> {time_field}\n\
committer A U Thor <author@example.com> {time_field}\n\
\n\
malformed time\n"
);
let mut child = Command::new("git")
.current_dir(r.path())
.args([
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
])
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("spawn git hash-object");
child
.stdin
.take()
.unwrap()
.write_all(body.as_bytes())
.unwrap();
let out = child.wait_with_output().unwrap();
assert!(
out.status.success(),
"git hash-object: {}",
String::from_utf8_lossy(&out.stderr)
);
let sha = String::from_utf8_lossy(&out.stdout).trim().to_string();
r.git(&["update-ref", "refs/heads/main", &sha]);
sha
}
#[test]
fn overflowing_numeric_time_becomes_an_absent_claim_not_a_fabricated_one() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
commit_with_raw_time(&r, "99999999999999999999 +0000");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
assert!(
atom.metadata.author_time.is_none(),
"an overflowing time is absent, never fabricated as 0 (NG-5)"
);
assert!(atom.metadata.commit_time.is_none(), "same for commit_time");
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "unparseable author/committer/tagger times (2)"),
"both the author and committer time failed to parse"
);
}
#[test]
fn a_time_token_that_is_not_purely_digits_becomes_an_absent_claim() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
commit_with_raw_time(&r, "16954-56000 +0000");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
assert!(
atom.metadata.author_time.is_none(),
"a non-digit-shaped time is absent, never salvaged to its leading digits"
);
assert!(atom.metadata.commit_time.is_none(), "same for commit_time");
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "unparseable author/committer/tagger times (2)")
);
}
#[test]
fn strict_seconds_accepts_well_formed_tokens_and_rejects_malformed_ones() {
assert_eq!(strict_seconds("1695456000 +0000"), Some(1_695_456_000));
assert_eq!(strict_seconds("-1695456000 +0000"), Some(-1_695_456_000));
assert_eq!(strict_seconds("0 +0000"), Some(0));
assert_eq!(
strict_seconds("+1695456000 +0000"),
None,
"a leading + is not -?[0-9]+"
);
assert_eq!(strict_seconds("1695456000abc +0000"), None);
assert_eq!(strict_seconds("16954-56000 +0000"), None);
assert_eq!(
strict_seconds("99999999999999999999 +0000"),
None,
"overflows i64"
);
assert_eq!(strict_seconds(""), None);
assert_eq!(strict_seconds(" "), None);
}
#[test]
fn strict_offset_minutes_accepts_well_formed_tokens_and_rejects_malformed_ones() {
assert_eq!(strict_offset_minutes("1112911993 +0900"), Some(540));
assert_eq!(strict_offset_minutes("1112911993 -0130"), Some(-90));
assert_eq!(strict_offset_minutes("1112911993 +0000"), Some(0));
assert_eq!(
strict_offset_minutes("1112911993 +09"),
None,
"not exactly four digits"
);
assert_eq!(
strict_offset_minutes("1112911993 +0960"),
None,
"MM must be < 60"
);
assert_eq!(
strict_offset_minutes("1112911993 0900"),
None,
"no sign is not +HHMM/-HHMM"
);
assert_eq!(
strict_offset_minutes("1112911993 +09a0"),
None,
"non-digit in the offset"
);
assert_eq!(
strict_offset_minutes("1112911993"),
None,
"missing offset token entirely"
);
}
#[test]
fn an_empty_repository_decodes_to_an_empty_ir() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(ir.atoms.is_empty());
assert!(ir.refs.is_empty());
}
#[test]
fn annotated_tag_preserves_identity_and_records_loss() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let commit = r.git(&["rev-parse", "HEAD"]);
r.git(&["tag", "-a", "rel-1", "-m", "the first release"]);
let tag_sha = r.git(&["rev-parse", "rel-1"]); assert_ne!(tag_sha, commit, "an annotated tag has its own object id");
let ir = decode(r.path(), &Options::default()).unwrap();
let tag = ir
.refs
.iter()
.find(|rf| rf.name == "rel-1")
.expect("tag carried");
assert!(matches!(tag.kind, brygge_ir::RefKind::Tag));
let src = tag
.source
.as_ref()
.expect("an annotated tag preserves its opaque source identity (PR-4)");
let hex = hex_of(&src.atom_id);
assert_eq!(
hex, tag_sha,
"the preserved id is the tag object's sha, not the commit's"
);
assert!(
!ir.loss
.dropped
.iter()
.any(|d| d.what.contains("annotated tag")),
"the old annotated-tag drop record is gone"
);
let annotation = tag
.annotation
.as_ref()
.expect("an annotated tag's tagger/time/message is carried as an Annotation");
assert_eq!(
annotation.tagger.as_ref().and_then(|t| t.name.as_utf8()),
Some("C O Mitter")
);
assert_eq!(
annotation.message.as_ref().and_then(|m| m.as_utf8()),
Some("the first release\n")
);
assert!(annotation.time.is_some());
assert!(
ir.loss
.dropped
.iter()
.all(|d| matches!(d.class, brygge_ir::LossClass::Representation)),
"nothing but representation-class drops remain: {:?}",
ir.loss.dropped
);
}
#[test]
fn lightweight_tag_has_no_separate_identity_and_no_tag_loss() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&["tag", "light"]); let ir = decode(r.path(), &Options::default()).unwrap();
let tag = ir
.refs
.iter()
.find(|rf| rf.name == "light")
.expect("carried");
assert!(
tag.source.is_none(),
"a lightweight tag has no separate object identity"
);
assert!(
!ir.loss
.dropped
.iter()
.any(|d| d.what.contains("annotated tag"))
);
}
#[test]
fn ambiguous_identical_content_move_is_not_marked() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "same\n");
r.write("b.txt", "same\n"); r.commit_all("two identical files");
std::fs::remove_file(r.path().join("a.txt")).unwrap();
std::fs::remove_file(r.path().join("b.txt")).unwrap();
r.write("x.txt", "same\n");
r.write("y.txt", "same\n"); r.commit_all("shuffle identical content");
let opts = Options {
infer_renames: true,
rename_threshold: 100,
carry_extra_namespaces: Vec::new(),
};
let ir = decode(r.path(), &opts).unwrap();
assert!(
ir.atoms.iter().all(|a| a.copies.is_empty()),
"ambiguous identical-content moves are left unmarked (OQ-A)"
);
assert!(brygge_ir::honesty::summary(&ir).derived.is_empty());
let move_atom = ir
.atoms
.iter()
.find(|a| {
a.ops
.iter()
.any(|op| matches!(op, brygge_ir::PathOp::Add { path, .. } if path == "x.txt"))
})
.expect("the shuffle commit exists");
assert!(
move_atom
.ops
.iter()
.any(|op| matches!(op, brygge_ir::PathOp::Delete { path, .. } if path == "a.txt"))
);
}
#[test]
fn stash_notes_and_remote_tracking_are_excluded_and_counted() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let baseline = TempRepo::new();
baseline.write("a.txt", "a\n");
baseline.commit_all("c1");
let baseline_ir = decode(baseline.path(), &Options::default()).unwrap();
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let head = r.git(&["rev-parse", "HEAD"]);
r.write("a.txt", "b\n");
r.git(&["stash", "push", "-q", "-m", "wip"]);
r.git(&["notes", "add", "-m", "a note", &head]);
r.write("only-on-remote.txt", "x\n");
r.commit_all("remote-only commit");
let remote_head = r.git(&["rev-parse", "HEAD"]);
r.git(&["update-ref", "refs/remotes/origin/x", &remote_head]);
r.git(&["reset", "-q", "--hard", &head]);
let ir = decode(r.path(), &Options::default()).unwrap();
assert_eq!(
ir.atoms.len(),
1,
"only c1 is carried history: {:?}",
ir.atoms
);
assert!(
ir.atoms.iter().all(|a| a.ops.iter().all(
|op| !matches!(op, brygge_ir::PathOp::Add { path, .. } if path == "only-on-remote.txt")
)),
"the commit reachable only from refs/remotes/origin/x never became an atom"
);
let whats: Vec<&str> = ir.loss.dropped.iter().map(|d| d.what.as_str()).collect();
assert!(whats.contains(&"stash refs (1)"), "{whats:?}");
assert!(whats.contains(&"notes refs (1)"), "{whats:?}");
assert!(whats.contains(&"remote-tracking refs (1)"), "{whats:?}");
assert!(
whats
.iter()
.any(|w| w.starts_with("commits reachable only from dropped refs")),
"{whats:?}"
);
assert_eq!(
ir.provenance.source.repo_id, baseline_ir.provenance.source.repo_id,
"repo_id is computed over imported commits only, unaffected by dropped-namespace refs"
);
}
#[test]
fn alternates_are_refused_but_an_empty_file_is_not() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let origin = TempRepo::new();
origin.write("a.txt", "a\n");
origin.commit_all("c1");
let clone_dir = std::env::temp_dir().join(format!(
"brygge-git-clone-{}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos(),
COUNTER.fetch_add(1, Ordering::Relaxed)
));
let out = Command::new("git")
.args([
"clone",
"-q",
"--shared",
origin.path().to_str().unwrap(),
clone_dir.to_str().unwrap(),
])
.output()
.unwrap();
assert!(
out.status.success(),
"git clone --shared failed: {}",
String::from_utf8_lossy(&out.stderr)
);
match decode(&clone_dir, &Options::default()) {
Err(crate::Error::FloorRefusal { feature, .. }) => assert_eq!(feature, "object-alternates"),
other => panic!("expected an object-alternates refusal, got {other:?}"),
}
let _ = std::fs::remove_dir_all(&clone_dir);
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
std::fs::create_dir_all(r.path().join(".git/objects/info")).unwrap();
std::fs::write(r.path().join(".git/objects/info/alternates"), b"").unwrap();
assert!(decode(r.path(), &Options::default()).is_ok());
}
#[test]
fn redirected_git_directory_is_refused_but_main_repo_succeeds() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let worktree_dir = std::env::temp_dir().join(format!(
"brygge-git-worktree-{}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos(),
COUNTER.fetch_add(1, Ordering::Relaxed)
));
r.git(&[
"worktree",
"add",
"-q",
worktree_dir.to_str().unwrap(),
"-b",
"wt-branch",
]);
match decode(&worktree_dir, &Options::default()) {
Err(crate::Error::FloorRefusal { feature, .. }) => {
assert_eq!(feature, "redirected-git-directory");
}
other => panic!("expected a redirected-git-directory refusal, got {other:?}"),
}
assert!(decode(r.path(), &Options::default()).is_ok());
r.git(&["worktree", "remove", "-f", worktree_dir.to_str().unwrap()]);
let _ = std::fs::remove_dir_all(&worktree_dir);
}
fn unique_temp_dir(label: &str) -> PathBuf {
std::env::temp_dir().join(format!(
"brygge-git-{label}-{}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos(),
COUNTER.fetch_add(1, Ordering::Relaxed)
))
}
fn link_created(what: &str, result: std::io::Result<()>) -> bool {
match result {
Ok(()) => true,
Err(e) if std::env::var_os("CI").is_none() => {
eprintln!("SKIPPED: cannot create {what} here: {e}");
false
}
Err(e) => panic!("cannot create {what} on CI: {e}"),
}
}
#[cfg(unix)]
fn symlink_dir(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::unix::fs::symlink(target, link)
}
#[cfg(unix)]
fn symlink_file(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::unix::fs::symlink(target, link)
}
#[cfg(windows)]
fn symlink_dir(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::windows::fs::symlink_dir(target, link)
}
#[cfg(windows)]
fn symlink_file(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::windows::fs::symlink_file(target, link)
}
#[cfg(windows)]
fn junction(target: &Path, link: &Path) -> std::io::Result<()> {
let status = Command::new("cmd")
.args(["/C", "mklink", "/J"])
.arg(link)
.arg(target)
.stdout(std::process::Stdio::null())
.status()?;
if status.success() {
Ok(())
} else {
Err(std::io::Error::other(format!(
"mklink /J exited with {status}"
)))
}
}
fn assert_redirected(result: Result<brygge_ir::Ir, crate::Error>) {
match result {
Err(crate::Error::FloorRefusal { feature, .. }) => {
assert_eq!(feature, "redirected-git-directory");
}
other => panic!("expected a redirected-git-directory refusal, got {other:?}"),
}
}
#[derive(Clone, Copy, Debug)]
enum Redirected {
GitDir,
Objects,
ObjectsInfo,
ObjectsPack,
}
impl Redirected {
fn inside(self, repo: &Path) -> PathBuf {
let git = repo.join(".git");
match self {
Self::GitDir => git,
Self::Objects => git.join("objects"),
Self::ObjectsInfo => git.join("objects").join("info"),
Self::ObjectsPack => git.join("objects").join("pack"),
}
}
}
fn refuse_a_redirected_directory(
which: Redirected,
what: &str,
link: impl Fn(&Path, &Path) -> std::io::Result<()>,
) {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let target = TempRepo::new();
target.write("a.txt", "a\n");
target.commit_all("c1");
target.git(&["repack", "-a", "-d", "-q"]);
std::fs::create_dir_all(Redirected::ObjectsInfo.inside(target.path())).unwrap();
let r = TempRepo::new();
r.write("b.txt", "b\n");
r.commit_all("c1");
r.git(&["repack", "-a", "-d", "-q"]);
std::fs::create_dir_all(Redirected::ObjectsInfo.inside(r.path())).unwrap();
let (subject, replaced) = if matches!(which, Redirected::GitDir) {
let outer = unique_temp_dir("redirected-outer");
std::fs::create_dir_all(&outer).unwrap();
let replaced = which.inside(&outer);
(outer, replaced)
} else {
let replaced = which.inside(r.path());
std::fs::remove_dir_all(&replaced).unwrap();
(r.path().to_path_buf(), replaced)
};
let made = link_created(what, link(&which.inside(target.path()), &replaced));
let result = if made {
Some(decode(&subject, &Options::default()))
} else {
None
};
if matches!(which, Redirected::GitDir) {
let _ = std::fs::remove_dir_all(&subject);
}
if let Some(result) = result {
assert_redirected(result);
}
}
#[test]
fn a_symlinked_git_directory_is_refused() {
refuse_a_redirected_directory(Redirected::GitDir, "a directory symlink", symlink_dir);
}
#[test]
fn a_symlinked_objects_directory_is_refused() {
refuse_a_redirected_directory(Redirected::Objects, "a directory symlink", symlink_dir);
}
#[test]
fn a_symlinked_objects_info_directory_is_refused() {
refuse_a_redirected_directory(Redirected::ObjectsInfo, "a directory symlink", symlink_dir);
}
#[test]
fn a_symlinked_objects_pack_directory_is_refused() {
refuse_a_redirected_directory(Redirected::ObjectsPack, "a directory symlink", symlink_dir);
}
#[cfg(windows)]
mod junctions {
use super::*;
#[test]
fn a_junctioned_git_directory_is_refused() {
refuse_a_redirected_directory(Redirected::GitDir, "a directory junction", junction);
}
#[test]
fn a_junctioned_objects_directory_is_refused() {
refuse_a_redirected_directory(Redirected::Objects, "a directory junction", junction);
}
#[test]
fn a_junctioned_objects_info_directory_is_refused() {
refuse_a_redirected_directory(Redirected::ObjectsInfo, "a directory junction", junction);
}
#[test]
fn a_junctioned_objects_pack_directory_is_refused() {
refuse_a_redirected_directory(Redirected::ObjectsPack, "a directory junction", junction);
}
#[test]
fn std_reports_a_junction_as_a_symlink() {
let dir = unique_temp_dir("junction-std");
let real = dir.join("real");
std::fs::create_dir_all(&real).unwrap();
let link = dir.join("link");
assert!(link_created("a directory junction", junction(&real, &link)));
let ft = std::fs::symlink_metadata(&link).unwrap().file_type();
eprintln!(
"junction: is_symlink={} is_dir={} is_file={}",
ft.is_symlink(),
ft.is_dir(),
ft.is_file()
);
let _ = std::fs::remove_dir_all(&dir);
assert!(ft.is_symlink());
}
}
#[test]
fn a_symlinked_pack_file_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&["repack", "-a", "-d", "-q"]);
let pack_dir = r.path().join(".git").join("objects").join("pack");
let entries: Vec<PathBuf> = std::fs::read_dir(&pack_dir)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.path())
.collect();
let victim = entries.first().expect("repack produced pack files").clone();
let name = victim.file_name().unwrap().to_owned();
std::fs::remove_file(&victim).unwrap();
let made = link_created(
"a file symlink",
symlink_file(
&pack_dir.join("does-not-exist").with_file_name(name),
&victim,
),
);
if made {
assert_redirected(decode(r.path(), &Options::default()));
}
}
#[test]
fn an_unsymlinked_repository_decodes_normally() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
assert!(decode(r.path(), &Options::default()).is_ok());
}
#[test]
fn corrupt_dropped_only_history_does_not_fail_the_decode() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let head = r.git(&["rev-parse", "HEAD"]);
r.write("a.txt", "b\n");
r.git(&["stash", "push", "-q", "-m", "wip"]);
let stash_id = r.git(&["rev-parse", "refs/stash"]);
let index_parent = r.git(&["rev-parse", &format!("{stash_id}^2")]);
assert_ne!(
index_parent, head,
"the index-tree parent is a commit of its own, not HEAD"
);
let loose = r
.path()
.join(".git")
.join("objects")
.join(&index_parent[..2])
.join(&index_parent[2..]);
assert!(
loose.exists(),
"expected a loose object at {}",
loose.display()
);
std::fs::remove_file(&loose).unwrap();
let ir = decode(r.path(), &Options::default()).unwrap();
assert_eq!(ir.atoms.len(), 1, "carried history (c1) is intact");
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "commits reachable only from dropped refs (count unavailable)"),
"{:?}",
ir.loss.dropped
);
let ir2 = decode(r.path(), &Options::default()).unwrap();
assert_eq!(
brygge_ir::to_bytes(&ir),
brygge_ir::to_bytes(&ir2),
"decoding twice gives identical bytes, count-unavailable case included"
);
}
#[cfg(unix)]
#[test]
fn non_utf8_path_is_refused_with_escaped_bytes_and_commit_hex() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
use std::os::unix::ffi::OsStrExt;
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let blob = r.git(&["hash-object", "-w", "--", "a.txt"]);
let mut cacheinfo_bytes = format!("100644,{blob},bad-").into_bytes();
cacheinfo_bytes.push(0xFF);
cacheinfo_bytes.extend_from_slice(b".txt");
let cacheinfo = std::ffi::OsStr::from_bytes(&cacheinfo_bytes).to_os_string();
let out = Command::new("git")
.current_dir(r.path())
.arg("update-index")
.arg("--add")
.arg("--cacheinfo")
.arg(&cacheinfo)
.output()
.unwrap();
assert!(
out.status.success(),
"update-index failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let out = Command::new("git")
.current_dir(r.path())
.args([
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"-m",
"add non-utf8 path",
])
.env("GIT_AUTHOR_NAME", "A U Thor")
.env("GIT_AUTHOR_EMAIL", "author@example.com")
.env("GIT_AUTHOR_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_COMMITTER_NAME", "C O Mitter")
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
.env("GIT_COMMITTER_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.output()
.unwrap();
assert!(
out.status.success(),
"commit failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let commit_hex = r.git(&["rev-parse", "HEAD"]);
match decode(r.path(), &Options::default()) {
Err(crate::Error::FloorRefusal { feature, reason }) => {
assert_eq!(feature, "non-utf8-path");
assert!(
reason.contains("\\xFF"),
"reason should escape the invalid byte: {reason}"
);
assert!(
reason.contains(&commit_hex),
"reason should name the commit: {reason}"
);
}
other => panic!("expected a non-UTF-8-path refusal, got {other:?}"),
}
}
#[cfg(unix)]
#[test]
fn non_utf8_ref_name_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
use std::os::unix::ffi::OsStrExt;
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let head = r.git(&["rev-parse", "HEAD"]);
let mut name_bytes = b"refs/heads/bad-".to_vec();
name_bytes.push(0xFF);
let ref_name = std::ffi::OsStr::from_bytes(&name_bytes).to_os_string();
let out = Command::new("git")
.current_dir(r.path())
.arg("update-ref")
.arg(&ref_name)
.arg(&head)
.output()
.unwrap();
if !out.status.success() {
eprintln!(
"skipping: this git/platform rejects a non-UTF-8 ref name: {}",
String::from_utf8_lossy(&out.stderr)
);
return;
}
match decode(r.path(), &Options::default()) {
Err(crate::Error::FloorRefusal { feature, reason }) => {
assert_eq!(feature, "non-utf8-ref-name");
assert!(
reason.contains("\\xFF"),
"reason should escape the invalid byte: {reason}"
);
}
other => panic!("expected a non-UTF-8-ref-name refusal, got {other:?}"),
}
}
#[test]
fn escape_invalid_utf8_never_substitutes_u_fffd() {
assert_eq!(escape_invalid_utf8(b"bad-\xFF.txt"), "bad-\\xFF.txt");
assert_eq!(escape_invalid_utf8(b"plain"), "plain");
assert_eq!(escape_invalid_utf8(b"\xC0\xC1"), "\\xC0\\xC1");
assert!(!escape_invalid_utf8(b"\xFF").contains('\u{FFFD}'));
}
#[test]
fn tag_on_a_blob_is_not_a_ref_record_but_is_counted() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let blob = r.git(&["hash-object", "-w", "--", "a.txt"]);
r.git(&["tag", "blobtag", &blob]);
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(
ir.refs.iter().all(|rf| rf.name != "blobtag"),
"a tag on a blob is never a RefRecord (it has no atom to target)"
);
let whats: Vec<&str> = ir.loss.dropped.iter().map(|d| d.what.as_str()).collect();
assert!(
whats.contains(&"refs to non-commit objects (1)"),
"the drop is counted, not silently skipped: {whats:?}"
);
}
fn git_stdin(dir: &Path, args: &[&str], stdin_data: &[u8]) -> String {
use std::io::Write as _;
let mut child = Command::new("git")
.current_dir(dir)
.args(args)
.env("GIT_AUTHOR_NAME", "A U Thor")
.env("GIT_AUTHOR_EMAIL", "author@example.com")
.env("GIT_AUTHOR_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_COMMITTER_NAME", "C O Mitter")
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
.env("GIT_COMMITTER_DATE", "2005-04-07T22:13:13 +0000")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.stdin.take().unwrap().write_all(stdin_data).unwrap();
let out = child.wait_with_output().unwrap();
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn build_nested_tree(r: &TempRepo, depth: usize) -> String {
assert!(depth >= 1);
let blob = git_stdin(r.path(), &["hash-object", "-w", "--stdin"], b"leaf\n");
let mut tree = git_stdin(
r.path(),
&["mktree"],
format!("100644 blob {blob}\tleaf.txt\n").as_bytes(),
);
for _ in 1..depth {
tree = git_stdin(
r.path(),
&["mktree"],
format!("040000 tree {tree}\td\n").as_bytes(),
);
}
tree
}
fn commit_tree(r: &TempRepo, tree: &str, msg: &str) -> String {
let commit = git_stdin(r.path(), &["commit-tree", tree, "-m", msg], b"");
r.git(&["update-ref", "refs/heads/main", &commit]);
commit
}
fn build_flat_tree(r: &TempRepo, n: usize) -> String {
let blob = git_stdin(r.path(), &["hash-object", "-w", "--stdin"], b"x\n");
let mut listing = String::new();
for i in 0..n {
listing.push_str(&format!("100644 blob {blob}\tf{i}\n"));
}
git_stdin(r.path(), &["mktree"], listing.as_bytes())
}
fn build_shared_subtree_bomb(r: &TempRepo, width: usize, levels: usize) -> String {
let blob = git_stdin(r.path(), &["hash-object", "-w", "--stdin"], b"leaf\n");
let mut tree = git_stdin(
r.path(),
&["mktree"],
format!("100644 blob {blob}\tleaf.txt\n").as_bytes(),
);
for _ in 0..levels {
let mut listing = String::new();
for i in 0..width {
listing.push_str(&format!("040000 tree {tree}\td{i}\n"));
}
tree = git_stdin(r.path(), &["mktree"], listing.as_bytes());
}
tree
}
#[test]
fn a_flat_tree_at_the_entry_ceiling_decodes_but_one_more_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let limits = Limits {
max_tree_entries: 20,
..Limits::default()
};
let at_ceiling = TempRepo::new();
let tree = build_flat_tree(&at_ceiling, 20);
commit_tree(&at_ceiling, &tree, "20 entries");
decode_with(
at_ceiling.path(),
&Options::default(),
&limits,
&Storage::Memory,
)
.expect("20 entries is exactly the injected 20-entry ceiling");
let over_ceiling = TempRepo::new();
let tree = build_flat_tree(&over_ceiling, 21);
commit_tree(&over_ceiling, &tree, "21 entries");
match decode_with(
over_ceiling.path(),
&Options::default(),
&limits,
&Storage::Memory,
) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
}
#[test]
fn a_small_shared_subtree_bomb_is_refused_by_the_entry_ceiling() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
let tree = build_shared_subtree_bomb(&r, 20, 4);
commit_tree(&r, &tree, "shared-subtree bomb");
let limits = Limits {
max_tree_entries: 1_000,
..Limits::default()
};
let start = std::time::Instant::now();
match decode_with(r.path(), &Options::default(), &limits, &Storage::Memory) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
assert!(
start.elapsed() < std::time::Duration::from_secs(10),
"refused fast, bounded by the ceiling, not by walking all 160,000 paths first"
);
}
#[test]
fn a_blob_over_the_limit_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("big.bin", &"x".repeat(5_000_000));
r.commit_all("big blob");
let limits = Limits {
max_blob_bytes: 1024,
..Limits::default()
};
match decode_with(r.path(), &Options::default(), &limits, &Storage::Memory) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
}
#[test]
fn a_deep_tree_is_refused_by_depth_but_a_shallower_one_decodes() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let limits = Limits {
max_tree_depth: 50,
..Limits::default()
};
let too_deep = TempRepo::new();
let tree = build_nested_tree(&too_deep, 60);
commit_tree(&too_deep, &tree, "60 levels");
match decode_with(
too_deep.path(),
&Options::default(),
&limits,
&Storage::Memory,
) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
let shallow_enough = TempRepo::new();
let tree = build_nested_tree(&shallow_enough, 40);
commit_tree(&shallow_enough, &tree, "40 levels");
let ir = decode_with(
shallow_enough.path(),
&Options::default(),
&limits,
&Storage::Memory,
)
.expect("40 levels is within the injected 50-level ceiling");
assert_eq!(ir.atoms.len(), 1);
}
#[test]
fn a_path_over_the_limit_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
let blob = git_stdin(r.path(), &["hash-object", "-w", "--stdin"], b"x\n");
let long_name = "a".repeat(5000);
let tree = git_stdin(
r.path(),
&["mktree"],
format!("100644 blob {blob}\t{long_name}\n").as_bytes(),
);
commit_tree(&r, &tree, "long path");
let limits = Limits {
max_path_bytes: 100,
..Limits::default()
};
match decode_with(r.path(), &Options::default(), &limits, &Storage::Memory) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
}
#[test]
fn the_commit_cap_refuses_when_exceeded() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
for i in 0..5 {
r.write("a.txt", &format!("{i}\n"));
r.commit_all(&format!("c{i}"));
}
let limits = Limits {
max_commits: 2,
..Limits::default()
};
match decode_with(r.path(), &Options::default(), &limits, &Storage::Memory) {
Err(crate::Error::ResourceLimit { .. }) => {}
other => panic!("expected a resource-limit refusal, got {other:?}"),
}
}
#[test]
fn provenance_floor_param_equals_the_declared_list() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let ir = decode(r.path(), &Options::default()).unwrap();
let expected = crate::floor::joined();
assert_eq!(ir.provenance.params.get("floor"), Some(&expected));
}
#[test]
fn a_retargeted_loose_object_is_refused_naming_the_id() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "victim content\n");
r.commit_all("c1");
let victim_blob = r.git(&["hash-object", "--", "a.txt"]);
let attacker_blob = git_stdin(
r.path(),
&["hash-object", "-w", "--stdin"],
b"attacker content\n",
);
assert_ne!(victim_blob, attacker_blob);
let loose_path = |sha: &str| {
r.path()
.join(".git")
.join("objects")
.join(&sha[..2])
.join(&sha[2..])
};
let victim_path = loose_path(&victim_blob);
let attacker_path = loose_path(&attacker_blob);
assert!(victim_path.exists(), "victim blob must be loose");
assert!(attacker_path.exists(), "attacker blob must be loose");
let attacker_bytes = std::fs::read(&attacker_path).unwrap();
let mut perms = std::fs::metadata(&victim_path).unwrap().permissions();
#[allow(clippy::permissions_set_readonly_false)]
perms.set_readonly(false);
std::fs::set_permissions(&victim_path, perms).unwrap();
std::fs::write(&victim_path, attacker_bytes).unwrap();
match decode(r.path(), &Options::default()) {
Err(crate::Error::Read(msg)) => {
assert!(msg.contains(&victim_blob), "names the mismatched id: {msg}");
assert!(msg.contains("does not match its content"));
}
other => panic!("expected Error::Read naming the mismatched object, got {other:?}"),
}
}
#[test]
fn an_ordinary_repository_decodes_with_every_object_verified() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = build_rich_repo();
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(!ir.atoms.is_empty());
}
#[test]
fn a_sha256_object_format_repository_is_refused_with_the_named_floor_feature() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("brygge-git-sha256-{}-{n}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let out = Command::new("git")
.current_dir(&dir)
.args(["init", "-q", "--object-format=sha256"])
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_SYSTEM", "/dev/null")
.output()
.unwrap();
if !out.status.success() {
eprintln!("skipping: installed git does not support --object-format=sha256");
let _ = std::fs::remove_dir_all(&dir);
return;
}
let result = decode(&dir, &Options::default());
let _ = std::fs::remove_dir_all(&dir);
match result {
Err(crate::Error::FloorRefusal { feature, .. }) => {
assert_eq!(feature, "sha256-object-format");
}
other => panic!("expected FloorRefusal naming the SHA-256 feature, got {other:?}"),
}
}
#[test]
fn a_declared_message_encoding_is_carried_on_the_message_only() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let mut body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> 1112911993 +0000\n\
committer A U Thor <author@example.com> 1112911993 +0000\n\
encoding ISO-8859-1\n\
\n"
)
.into_bytes();
body.extend_from_slice(&[0xE9, b'\n']);
let sha = git_stdin(
r.path(),
&[
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
],
&body,
);
r.git(&["update-ref", "refs/heads/main", &sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
let msg = atom.metadata.message.as_ref().unwrap();
assert_eq!(msg.bytes, vec![0xE9, b'\n']);
assert_eq!(msg.encoding.as_deref(), Some("ISO-8859-1"));
let author_name = &atom.metadata.author.as_ref().unwrap().name;
assert_eq!(
author_name.encoding, None,
"names keep encoding: None — \"not stated\", never the message's encoding"
);
}
#[test]
fn a_malformed_timezone_offset_is_absent_and_counted() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
commit_with_raw_time(&r, "1112911993 +090");
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
let author_time = atom.metadata.author_time.expect("seconds still parsed");
assert_eq!(author_time.seconds, 1_112_911_993);
assert_eq!(
author_time.offset_minutes, None,
"a malformed offset is absent, never salvaged or defaulted"
);
assert!(
ir.loss.dropped.iter().any(|d| d
.what
.starts_with("unparseable author/committer/tagger timezone offsets")),
"{:?}",
ir.loss.dropped
);
}
#[test]
fn a_mergetag_header_is_carried_as_an_extra_with_unfolded_bytes() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> 1112911993 +0000\n\
committer A U Thor <author@example.com> 1112911993 +0000\n\
mergetag object 0000000000000000000000000000000000000000\n\
\x20type commit\n\
\x20tag faketag\n\
\x20tagger t <t@example.com> 0 +0000\n\
\x20\n\
\x20fake mergetag message\n\
\n\
c1\n"
);
let sha = git_stdin(
r.path(),
&[
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
],
body.as_bytes(),
);
r.git(&["update-ref", "refs/heads/main", &sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
let extra = atom
.source
.extras
.iter()
.find(|e| e.label == "mergetag")
.expect("mergetag carried as an Extra");
let expected = "object 0000000000000000000000000000000000000000\ntype commit\ntag faketag\n\
tagger t <t@example.com> 0 +0000\n\nfake mergetag message\n";
assert_eq!(String::from_utf8_lossy(&extra.bytes), expected);
}
#[test]
fn gpgsig_and_gpgsig_sha256_are_both_carried_as_signatures_in_header_order() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> 1112911993 +0000\n\
committer A U Thor <author@example.com> 1112911993 +0000\n\
gpgsig -----BEGIN PGP SIGNATURE-----\n\
\x20\n\
\x20fake-sha1-signature\n\
\x20-----END PGP SIGNATURE-----\n\
gpgsig-sha256 -----BEGIN PGP SIGNATURE-----\n\
\x20\n\
\x20fake-sha256-signature\n\
\x20-----END PGP SIGNATURE-----\n\
\n\
c1\n"
);
let sha = git_stdin(
r.path(),
&[
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
],
body.as_bytes(),
);
r.git(&["update-ref", "refs/heads/main", &sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
let labels: Vec<&str> = atom
.source
.signatures
.iter()
.map(|s| s.label.as_str())
.collect();
assert_eq!(
labels,
vec!["gpgsig", "gpgsig-sha256"],
"both signature labels are present, in header order"
);
assert!(atom.source.extras.is_empty(), "neither is also an Extra");
}
fn commit_graph_chunk(bytes: &[u8], id: &[u8; 4]) -> (usize, usize) {
assert_eq!(&bytes[..4], b"CGPH");
let chunks = usize::from(bytes[6]);
for i in 0..chunks {
let e = 8 + i * 12;
if &bytes[e..e + 4] == id {
let off = |at: usize| u64::from_be_bytes(bytes[at + 4..at + 12].try_into().unwrap());
let start = usize::try_from(off(e)).unwrap();
let end = usize::try_from(off(e + 12)).unwrap();
return (start, end - start);
}
}
panic!("chunk {id:?} not found");
}
#[test]
fn a_crafted_commit_graph_cannot_drop_a_parent_and_the_walk_never_reads_it() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
for (i, msg) in ["c1", "c2", "c3", "c4"].iter().enumerate() {
r.write("a.txt", &format!("{i}\n"));
r.commit_all(msg);
}
let baseline = decode(r.path(), &Options::default()).unwrap();
assert_eq!(baseline.atoms.len(), 4);
r.git(&["commit-graph", "write", "--reachable"]);
let graph_path = r.path().join(".git/objects/info/commit-graph");
let mut bytes = std::fs::read(&graph_path).unwrap();
let tip_hex = r.git(&["rev-parse", "HEAD"]);
let tip: Vec<u8> = (0..20)
.map(|i| u8::from_str_radix(&tip_hex[i * 2..i * 2 + 2], 16).unwrap())
.collect();
let (oidl, oidl_len) = commit_graph_chunk(&bytes, b"OIDL");
let idx = (0..oidl_len / 20)
.find(|i| bytes[oidl + i * 20..oidl + i * 20 + 20] == tip[..])
.expect("the tip is in the graph");
let (cdat, _) = commit_graph_chunk(&bytes, b"CDAT");
let parent1 = cdat + idx * 36 + 20;
bytes[parent1..parent1 + 4].copy_from_slice(&0x7000_0000u32.to_be_bytes()); let mut perms = std::fs::metadata(&graph_path).unwrap().permissions();
#[allow(clippy::permissions_set_readonly_false)]
perms.set_readonly(false);
std::fs::set_permissions(&graph_path, perms).unwrap();
std::fs::write(&graph_path, &bytes).unwrap();
let repo = gix::open(r.path()).unwrap();
let tip_id = repo.head_id().unwrap().detach();
let with_graph = repo.rev_walk([tip_id]).all().unwrap().count();
let without_graph = repo
.rev_walk([tip_id])
.use_commit_graph(false)
.all()
.unwrap()
.count();
assert_eq!(without_graph, 4);
assert!(
with_graph < without_graph,
"the crafted graph must actually truncate a default walk ({with_graph} vs {without_graph}), \
or this test proves nothing"
);
let ir = decode(r.path(), &Options::default())
.expect("the graph is ignored, so the decode succeeds exactly as without it");
assert_eq!(
ir, baseline,
"a present commit-graph must never change history"
);
}
#[test]
fn a_tag_of_a_tag_verifies_the_final_target_and_counts_the_nested_tag() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&["tag", "-a", "inner", "-m", "inner tag"]);
let inner_sha = r.git(&["rev-parse", "inner"]);
let outer_body = format!(
"object {inner_sha}\n\
type tag\n\
tag outer\n\
tagger A U Thor <author@example.com> 1112911993 +0000\n\
\n\
outer tag\n"
);
let outer_sha = git_stdin(
r.path(),
&["hash-object", "-t", "tag", "-w", "--literally", "--stdin"],
outer_body.as_bytes(),
);
r.git(&["update-ref", "refs/tags/outer", &outer_sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let outer_ref = ir
.refs
.iter()
.find(|rf| rf.name == "outer")
.expect("outer tag ref is carried");
assert_eq!(outer_ref.target, ir.atoms[0].id);
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "nested tag objects not carried (1)"),
"{:?}",
ir.loss.dropped
);
}
#[test]
fn a_branch_pointing_at_a_tag_object_counts_that_tag_as_not_carried() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
r.git(&["tag", "-a", "t", "-m", "a tag"]);
let tag_sha = r.git(&["rev-parse", "t"]);
r.git(&["tag", "-d", "t"]);
std::fs::write(r.path().join(".git/refs/heads/x"), format!("{tag_sha}\n")).unwrap();
let ir = decode(r.path(), &Options::default()).unwrap();
assert!(
ir.refs.iter().any(|rf| rf.name == "x"),
"the branch is carried, pointing at the commit"
);
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "nested tag objects not carried (1)"),
"{:?}",
ir.loss.dropped
);
}
#[cfg(unix)]
#[test]
fn a_non_utf8_replace_ref_name_is_refused_with_escaped_bytes() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
use std::os::unix::ffi::OsStrExt;
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let head = r.git(&["rev-parse", "HEAD"]);
let mut name_bytes = b"refs/replace/bad-".to_vec();
name_bytes.push(0xFF);
let ref_name = std::ffi::OsStr::from_bytes(&name_bytes).to_os_string();
let out = Command::new("git")
.current_dir(r.path())
.arg("update-ref")
.arg(&ref_name)
.arg(&head)
.output()
.unwrap();
if !out.status.success() {
eprintln!("skipping: this git/platform rejects a non-UTF-8 ref name");
return;
}
match decode(r.path(), &Options::default()) {
Err(crate::Error::FloorRefusal { feature, reason }) => {
assert_eq!(feature, "replace-ref");
assert!(reason.contains("\\xFF"), "escaped byte expected: {reason}");
assert!(
!reason.contains('\u{FFFD}'),
"no lossy substitution: {reason}"
);
}
other => panic!("expected a replace-ref refusal, got {other:?}"),
}
}
#[test]
fn an_unparseable_tagger_time_is_counted_like_author_and_committer_times() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.commit_all("c1");
let commit_sha = r.git(&["rev-parse", "HEAD"]);
let tag_body = format!(
"object {commit_sha}\n\
type commit\n\
tag broken\n\
tagger A U Thor <author@example.com> 1112911993 +090\n\
\n\
broken tagger offset\n"
);
let tag_sha = git_stdin(
r.path(),
&["hash-object", "-t", "tag", "-w", "--literally", "--stdin"],
tag_body.as_bytes(),
);
r.git(&["update-ref", "refs/tags/broken", &tag_sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let tagged = ir.refs.iter().find(|rf| rf.name == "broken").unwrap();
let annotation = tagged.annotation.as_ref().expect("annotation is carried");
let time = annotation.time.expect("seconds still parsed");
assert_eq!(time.seconds, 1_112_911_993);
assert_eq!(
time.offset_minutes, None,
"a malformed tagger offset is absent, never salvaged or defaulted"
);
assert!(
ir.loss.dropped.iter().any(|d| d
.what
.starts_with("unparseable author/committer/tagger timezone offsets")),
"{:?}",
ir.loss.dropped
);
}
#[test]
fn a_non_utf8_encoding_header_value_is_not_carried_and_is_counted() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let mut body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> 1112911993 +0000\n\
committer A U Thor <author@example.com> 1112911993 +0000\n\
encoding "
)
.into_bytes();
body.extend_from_slice(&[0xff, 0xfe]); body.extend_from_slice(b"\n\nc1\n");
let sha = git_stdin(
r.path(),
&[
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
],
&body,
);
r.git(&["update-ref", "refs/heads/main", &sha]);
let ir = decode(r.path(), &Options::default()).unwrap();
let atom = &ir.atoms[0];
assert_eq!(
atom.metadata.message.as_ref().unwrap().encoding,
None,
"an undecodable encoding value is not carried"
);
assert!(
ir.loss
.dropped
.iter()
.any(|d| d.what == "undecodable encoding headers (1)"),
"{:?}",
ir.loss.dropped
);
}
#[test]
fn a_non_utf8_commit_header_name_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("a.txt", "a\n");
r.git(&["add", "-A"]);
let tree = r.git(&["write-tree"]);
let mut body = format!(
"tree {tree}\n\
author A U Thor <author@example.com> 1112911993 +0000\n\
committer A U Thor <author@example.com> 1112911993 +0000\n"
)
.into_bytes();
body.extend_from_slice(&[0xff, 0xfe]); body.extend_from_slice(b" some-value\n\nc1\n");
let sha = git_stdin(
r.path(),
&[
"hash-object",
"-t",
"commit",
"-w",
"--literally",
"--stdin",
],
&body,
);
r.git(&["update-ref", "refs/heads/main", &sha]);
match decode(r.path(), &Options::default()) {
Err(crate::Error::FloorRefusal { feature, .. }) => {
assert_eq!(feature, "non-utf8-commit-header-name");
}
other => panic!("expected a FloorRefusal, got {other:?}"),
}
}
fn has_internal_reference(text: &str) -> Option<String> {
const PREFIXES: [&str; 13] = [
"RFC", "OQ", "CR", "PR", "INV", "NG", "SRC", "FS", "VF", "HO", "CL", "CT", "FA",
];
let bytes = text.as_bytes();
for prefix in PREFIXES {
let mut from = 0;
while let Some(off) = text[from..].find(prefix) {
let at = from + off;
from = at + prefix.len();
let before_ok = at == 0 || !bytes[at - 1].is_ascii_alphanumeric();
if !before_ok {
continue;
}
let mut i = at + prefix.len();
if i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'-') {
i += 1;
}
if i < bytes.len() && bytes[i].is_ascii_digit() {
return Some(text[at..(i + 1).min(text.len())].to_string());
}
}
}
None
}
#[test]
fn the_reference_matcher_finds_what_it_should() {
assert!(has_internal_reference("see RFC 004 D-5").is_some());
assert!(has_internal_reference("(CR-16)").is_some());
assert!(has_internal_reference("(NG-5, RFC 011 D-5)").is_some());
assert!(has_internal_reference("(RFC 011 D-4)").is_some());
assert!(has_internal_reference("OQ-2").is_some());
assert!(has_internal_reference("(FA-1)").is_some());
assert!(has_internal_reference("per OQ-B").is_none()); assert!(has_internal_reference("local state, not history").is_none());
assert!(has_internal_reference("a CRC32 check").is_none());
}
fn assert_no_internal_references(ir: &brygge_ir::Ir, what: &str) {
for d in &ir.loss.dropped {
for text in [&d.what, &d.reason] {
assert!(
has_internal_reference(text).is_none(),
"{what}: a drop's text carries an internal reference: {text:?}"
);
}
}
for f in &ir.flags {
for text in [&f.what, &f.reason] {
assert!(
has_internal_reference(text).is_none(),
"{what}: a flag's text carries an internal reference: {text:?}"
);
}
}
}
#[test]
fn no_drop_or_flag_text_carries_an_internal_reference() {
let mut namespaces = std::collections::BTreeMap::new();
namespaces.insert("refs/remotes", 2u64);
let counts = super::ParseCounts {
unparseable_times: 1,
unparseable_offsets: 1,
undecodable_encodings: 1,
};
for only in [
super::DroppedOnlyCommits::Counted(3),
super::DroppedOnlyCommits::Unavailable,
] {
let lb = super::loss_boundary(&namespaces, only, 1, 1, counts, 1);
assert!(
lb.dropped.len() >= 9,
"expected every record: {:?}",
lb.dropped
);
for d in &lb.dropped {
for text in [&d.what, &d.reason] {
assert!(
has_internal_reference(text).is_none(),
"a drop's text carries an internal reference: {text:?}"
);
}
}
}
if !git_available() {
return;
}
let r = build_rich_repo();
let ir = decode(r.path(), &crate::Options::default()).unwrap();
assert_no_internal_references(&ir, "the rich repository");
let empty = TempRepo::new();
let ir = decode(empty.path(), &crate::Options::default()).unwrap();
assert_no_internal_references(&ir, "an empty repository");
}
fn tree(n: u8) -> ObjectId {
let mut bytes = [0u8; 20];
bytes[0] = n;
ObjectId::from_bytes_or_panic(&bytes)
}
fn snap_with(path: &str) -> Snapshot {
let mut s = Snapshot::new();
s.insert(path.to_string(), (tree(200), 0o100_644));
s
}
#[test]
fn a_snapshot_is_dropped_after_its_last_use_and_not_before() {
let mut cache = SnapshotCache::new(HashMap::from([(tree(1), 2)]));
let mut builds = 0;
for _ in 0..2 {
cache
.get_or_build(tree(1), || {
builds += 1;
Ok(snap_with("a"))
})
.unwrap();
}
assert_eq!(builds, 1, "the second use is served from the cache");
assert_eq!(cache.len(), 1);
cache.release(tree(1));
assert_eq!(cache.len(), 1, "one use is still to come");
cache.release(tree(1));
assert_eq!(cache.len(), 0, "the last use is done");
}
#[test]
fn a_tree_that_a_later_commit_reverts_to_stays_cached_until_that_commit() {
let (a, b) = (tree(1), tree(2));
let mut cache = SnapshotCache::new(HashMap::from([(a, 4), (b, 2)]));
let mut builds = 0;
let mut get = |cache: &mut SnapshotCache, t: ObjectId| {
cache
.get_or_build(t, || {
builds += 1;
Ok(snap_with("a"))
})
.unwrap();
};
get(&mut cache, a);
cache.release(a);
get(&mut cache, b);
get(&mut cache, a);
cache.release(b);
cache.release(a);
assert_eq!(
cache.len(),
2,
"A is used again by commits 3 and 4, B by commit 3"
);
get(&mut cache, a);
get(&mut cache, b);
cache.release(a);
cache.release(b);
assert_eq!(
cache.len(),
1,
"B is finished, A is used once more (commit 4)"
);
get(&mut cache, a);
get(&mut cache, a);
cache.release(a);
cache.release(a);
assert_eq!(cache.len(), 0);
assert_eq!(
builds, 2,
"each tree was built exactly once: nothing was evicted early"
);
}
#[test]
fn a_linear_history_holds_at_most_two_snapshots_however_long() {
let n = 1_000u16;
let id = |i: u16| {
let mut bytes = [0u8; 20];
bytes[..2].copy_from_slice(&i.to_be_bytes());
ObjectId::from_bytes_or_panic(&bytes)
};
let mut uses = HashMap::new();
for i in 0..n {
*uses.entry(id(i)).or_insert(0) += 1;
if i > 0 {
*uses.entry(id(i - 1)).or_insert(0) += 1;
}
}
let mut cache = SnapshotCache::new(uses);
let mut most = 0;
for i in 0..n {
cache.get_or_build(id(i), || Ok(snap_with("a"))).unwrap();
if i > 0 {
cache
.get_or_build(id(i - 1), || Ok(snap_with("a")))
.unwrap();
}
most = most.max(cache.len());
cache.release(id(i));
if i > 0 {
cache.release(id(i - 1));
}
}
assert!(most <= 2, "held {most} snapshots at once over {n} commits");
assert_eq!(cache.len(), 0);
}
#[test]
fn a_tree_without_a_counted_use_is_never_evicted() {
let mut cache = SnapshotCache::new(HashMap::new());
cache.get_or_build(tree(1), || Ok(snap_with("a"))).unwrap();
cache.release(tree(1));
assert_eq!(cache.len(), 1);
}
#[test]
fn a_revert_back_to_an_older_tree_is_diffed_correctly() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("f.txt", "one\n");
r.commit_all("c1");
r.write("f.txt", "two\n");
r.commit_all("c2");
r.write("f.txt", "one\n"); r.commit_all("c3 revert");
r.write("g.txt", "later\n");
r.commit_all("c4");
r.git(&[
"-c",
"commit.gpgsign=false",
"commit",
"-q",
"--allow-empty",
"-m",
"c5 empty",
]);
let ir = decode(r.path(), &Options::default()).unwrap();
assert_eq!(ir.atoms.len(), 5);
let content = |op: &PathOp| match op {
PathOp::Add { content, .. } | PathOp::Modify { content, .. } => match content {
Content::Blob { blob, .. } => ir.content.get(blob).unwrap(),
Content::Submodule { .. } => None,
},
_ => None,
};
let ops: Vec<(String, Vec<u8>)> = ir
.atoms
.iter()
.flat_map(|a| a.ops.iter())
.map(|op| match op {
PathOp::Add { path, .. } | PathOp::Modify { path, .. } => {
(path.clone(), content(op).expect("content"))
}
other => panic!("unexpected op {other:?}"),
})
.collect();
assert_eq!(
ops,
vec![
("f.txt".to_string(), b"one\n".to_vec()),
("f.txt".to_string(), b"two\n".to_vec()),
("f.txt".to_string(), b"one\n".to_vec()), ("g.txt".to_string(), b"later\n".to_vec()),
],
"c1 add, c2 modify, c3 revert, c4 add; the empty c5 has no ops"
);
assert!(
ir.atoms[4].ops.is_empty(),
"an empty commit is an atom with no ops"
);
let again = decode(r.path(), &Options::default()).unwrap();
assert_eq!(brygge_ir::to_bytes(&ir), brygge_ir::to_bytes(&again));
}
#[test]
fn a_carry_extra_namespaces_prefix_without_a_trailing_slash_is_refused() {
if !git_available() {
eprintln!("skipping: git not on PATH");
return;
}
let r = TempRepo::new();
r.write("f.txt", "one\n");
r.commit_all("c1");
let opts = Options {
infer_renames: false,
rename_threshold: 100,
carry_extra_namespaces: vec!["refs/brygge/other".to_string()], };
match decode(r.path(), &opts) {
Err(Error::InvalidOptions(msg)) => {
assert!(msg.contains("refs/brygge/other"), "{msg}");
}
other => panic!("expected Error::InvalidOptions, got {other:?}"),
}
}