Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-git/tests/h1_git_bridge.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-git / tests / h1_git_bridge.rs` 377 lines ยท 13237 bytes Raw Blame History 1 //! ADR 0003 step H1 (issue #48), first cut: the native Git object layer. The system `git` is 2 //! used here only as an independent oracle (it builds fixture history, then checks what the 3 //! native reader read and what the native exporter wrote: `cat-file`, `for-each-ref`, 4 //! `verify-pack`, `fsck --full --strict`, `clone`). Nothing in `synapse-git` starts a Git 5 //! process for these paths. 6 7 mod common; 8 9 use common::*; 10 use std::collections::BTreeSet; 11 use std::fs; 12 use std::path::{Path, PathBuf}; 13 use std::process::Command; 14 use synapse_core::{GitOid, RefusalCode}; 15 use synapse_git::pack::{decode_idx, PackReader}; 16 use synapse_git::{export_repository, GitRepository, ObjectKind}; 17 18 #[test] 19 fn h1_native_reader_resolves_symbolic_ref_chains_and_loose_shadowing() { 20 let dir = temp_dir("symbolic-ref"); 21 build_fixture(&dir); 22 let packed_target = git_text(&dir, &["rev-parse", "refs/heads/main"]); 23 let loose_target = git_text(&dir, &["rev-parse", "refs/heads/feature"]); 24 git( 25 &dir, 26 &["update-ref", "refs/remotes/origin/main", &packed_target], 27 ); 28 git(&dir, &["pack-refs", "--all"]); 29 // The loose symbolic ref shadows the packed direct ref of the same name. 30 git( 31 &dir, 32 &[ 33 "symbolic-ref", 34 "refs/remotes/origin/main", 35 "refs/heads/feature", 36 ], 37 ); 38 git( 39 &dir, 40 &[ 41 "symbolic-ref", 42 "refs/remotes/origin/alias", 43 "refs/remotes/origin/main", 44 ], 45 ); 46 git( 47 &dir, 48 &[ 49 "symbolic-ref", 50 "refs/remotes/origin/HEAD", 51 "refs/remotes/origin/alias", 52 ], 53 ); 54 55 let (refs, _) = GitRepository::open(&dir.join(".git")) 56 .unwrap() 57 .refs() 58 .unwrap(); 59 let resolved = GitOid::from_hex(&loose_target).unwrap(); 60 for name in [ 61 "refs/remotes/origin/HEAD", 62 "refs/remotes/origin/alias", 63 "refs/remotes/origin/main", 64 ] { 65 assert_eq!(refs.get(name), Some(&resolved), "resolved {name}"); 66 } 67 fs::remove_dir_all(&dir).unwrap(); 68 } 69 70 #[test] 71 fn h1_native_reader_refuses_symbolic_ref_cycles_and_dangling_targets() { 72 for (label, aliases) in [ 73 ( 74 "cycle", 75 vec![ 76 ("refs/remotes/origin/one", "refs/remotes/origin/two"), 77 ("refs/remotes/origin/two", "refs/remotes/origin/one"), 78 ], 79 ), 80 ( 81 "dangling", 82 vec![("refs/remotes/origin/HEAD", "refs/remotes/origin/missing")], 83 ), 84 ( 85 "escaping", 86 vec![("refs/remotes/origin/HEAD", "../../outside")], 87 ), 88 ( 89 "malformed", 90 vec![("refs/remotes/origin/HEAD", "refs/remotes//broken")], 91 ), 92 ] { 93 let dir = temp_dir(label); 94 build_fixture(&dir); 95 for (name, target) in aliases { 96 write( 97 &dir.join(".git"), 98 name, 99 format!("ref: {target}\n").as_bytes(), 100 ); 101 } 102 let err = GitRepository::open(&dir.join(".git")) 103 .unwrap() 104 .refs() 105 .unwrap_err(); 106 assert_eq!(err.code, RefusalCode::GitObjectCorrupt, "{label}: {err}"); 107 fs::remove_dir_all(&dir).unwrap(); 108 } 109 } 110 111 #[test] 112 fn h1_native_reader_reads_every_loose_object_exactly_as_git() { 113 let dir = temp_dir("loose"); 114 build_fixture(&dir); 115 let packs = fs::read_dir(dir.join(".git/objects/pack")).unwrap().count(); 116 assert_eq!(packs, 0, "fixture must start with loose objects only"); 117 let history = GitRepository::open(&dir.join(".git")) 118 .unwrap() 119 .full_history() 120 .unwrap(); 121 assert_history_matches_git(&history, &dir); 122 assert_eq!(history.head.as_deref(), Some("refs/heads/main")); 123 let kinds: BTreeSet = history.objects.values().map(|o| o.kind).collect(); 124 assert_eq!(kinds.len(), 4, "commits, trees, blobs and tags present"); 125 fs::remove_dir_all(&dir).unwrap(); 126 } 127 128 #[test] 129 fn h1_native_reader_resolves_offset_deltas_in_git_packs_and_matches_git_idx() { 130 let dir = temp_dir("ofs"); 131 build_fixture(&dir); 132 git( 133 &dir, 134 &[ 135 "repack", 136 "-a", 137 "-d", 138 "-f", 139 "-q", 140 "--depth=50", 141 "--window=250", 142 ], 143 ); 144 git(&dir, &["pack-refs", "--all"]); 145 let pack_dir = dir.join(".git/objects/pack"); 146 let idx_path = fs::read_dir(&pack_dir) 147 .unwrap() 148 .map(|e| e.unwrap().path()) 149 .find(|p| p.extension().is_some_and(|x| x == "idx")) 150 .unwrap(); 151 let verify = git_text(&dir, &["verify-pack", "-v", idx_path.to_str().unwrap()]); 152 assert!( 153 verify.contains("chain length = "), 154 "fixture pack must hold deltas:\n{verify}" 155 ); 156 let history = GitRepository::open(&dir.join(".git")) 157 .unwrap() 158 .full_history() 159 .unwrap(); 160 assert_history_matches_git(&history, &dir); 161 // Native indexing of the same pack reproduces Git's own index: every name, offset, CRC. 162 let pack = fs::read(idx_path.with_extension("pack")).unwrap(); 163 let native = PackReader::index(pack.clone()).unwrap(); 164 assert!( 165 count_entry_type(&pack, native.entries(), 6) > 0, 166 "Git pack holds OBJ_OFS_DELTA entries" 167 ); 168 let (git_entries, _) = decode_idx(&fs::read(&idx_path).unwrap()).unwrap(); 169 assert_eq!(native.entries(), &git_entries[..]); 170 fs::remove_dir_all(&dir).unwrap(); 171 } 172 173 #[test] 174 fn h1_native_indexer_resolves_reference_deltas_from_git_pack_objects() { 175 let dir = temp_dir("ref"); 176 build_fixture(&dir); 177 let list = git(&dir, &["rev-list", "--objects", "--all"]); 178 // Without --delta-base-offset, pack-objects writes OBJ_REF_DELTA entries. 179 let out = git_raw( 180 &dir, 181 &[ 182 "pack-objects", 183 "--stdout", 184 "-q", 185 "--window=250", 186 "--depth=50", 187 ], 188 Some(&list), 189 ); 190 assert!(out.status.success()); 191 let mut reader = PackReader::index(out.stdout.clone()).unwrap(); 192 assert!( 193 count_entry_type(&out.stdout, reader.entries(), 7) > 0, 194 "pack-objects output holds OBJ_REF_DELTA entries" 195 ); 196 let oracle = git_all_objects(&dir); 197 assert_eq!(reader.entries().len(), oracle.len()); 198 for (oid, (kind, payload)) in &oracle { 199 let object = reader.read(*oid).unwrap().unwrap(); 200 assert_eq!(object.kind.name(), kind); 201 assert_eq!(&object.payload, payload); 202 } 203 fs::remove_dir_all(&dir).unwrap(); 204 } 205 206 #[test] 207 fn h1_full_history_exports_as_a_pack_with_identical_object_ids_and_fsck_strict_clean() { 208 let dir = temp_dir("export-src"); 209 build_fixture(&dir); 210 git(&dir, &["repack", "-a", "-d", "-q"]); 211 let history = GitRepository::open(&dir.join(".git")) 212 .unwrap() 213 .full_history() 214 .unwrap(); 215 let out = temp_dir("export-dst"); 216 let dest = out.join("exported.git"); 217 let receipt = export_repository( 218 &dest, 219 &objects_of(&history), 220 &history.refs, 221 history.head.as_deref(), 222 ) 223 .unwrap(); 224 assert_eq!(receipt.object_count, history.objects.len()); 225 // Only the pack and its index: no loose objects. 226 let loose: Vec<_> = fs::read_dir(dest.join("objects")) 227 .unwrap() 228 .map(|e| e.unwrap().file_name().into_string().unwrap()) 229 .filter(|n| n != "pack" && n != "info") 230 .collect(); 231 assert!(loose.is_empty(), "loose object directories {loose:?}"); 232 git(&dest, &["verify-pack", receipt.idx_path.to_str().unwrap()]); 233 assert_fsck_strict_clean(&dest); 234 // Identical Git object IDs and references, by Git's own reading of both repositories. 235 let src_objects = git_all_objects(&dir); 236 let dst_objects = git_all_objects(&dest); 237 assert_eq!(src_objects, dst_objects, "object databases differ"); 238 assert_eq!(git_refs(&dir), git_refs(&dest), "references differ"); 239 assert_eq!( 240 git_text(&dir, &["rev-parse", "HEAD"]), 241 git_text(&dest, &["rev-parse", "HEAD"]) 242 ); 243 // And the exported repository works as a Git remote. 244 let clone = out.join("clone"); 245 let status = Command::new("git") 246 .args(["clone", "-q"]) 247 .arg(&dest) 248 .arg(&clone) 249 .env("GIT_CONFIG_NOSYSTEM", "1") 250 .env("GIT_CONFIG_GLOBAL", "/dev/null") 251 .status() 252 .unwrap(); 253 assert!(status.success()); 254 assert_eq!( 255 fs::read(clone.join("docs/big.txt")).unwrap(), 256 fs::read(dir.join("docs/big.txt")).unwrap() 257 ); 258 fs::remove_dir_all(&dir).unwrap(); 259 fs::remove_dir_all(&out).unwrap(); 260 } 261 262 #[test] 263 fn h1_export_is_deterministic_and_never_overwrites_a_repository() { 264 let dir = temp_dir("det-src"); 265 build_fixture(&dir); 266 let history = GitRepository::open(&dir.join(".git")) 267 .unwrap() 268 .full_history() 269 .unwrap(); 270 let out = temp_dir("det-dst"); 271 let mut objects = objects_of(&history); 272 let a = export_repository(&out.join("a.git"), &objects, &history.refs, None).unwrap(); 273 objects.reverse(); 274 let b = export_repository(&out.join("b.git"), &objects, &history.refs, None).unwrap(); 275 assert_eq!(a.pack_checksum, b.pack_checksum); 276 assert_eq!( 277 fs::read(&a.pack_path).unwrap(), 278 fs::read(&b.pack_path).unwrap() 279 ); 280 assert_eq!( 281 fs::read(&a.idx_path).unwrap(), 282 fs::read(&b.idx_path).unwrap() 283 ); 284 let err = export_repository(&out.join("a.git"), &objects, &history.refs, None).unwrap_err(); 285 assert_eq!(err.code, RefusalCode::PathInvalid); 286 let err = export_repository(&dir.join(".git"), &objects, &history.refs, None).unwrap_err(); 287 assert_eq!( 288 err.code, 289 RefusalCode::PathInvalid, 290 "existing repository refused" 291 ); 292 let mut bad_refs = history.refs.clone(); 293 bad_refs.insert("refs/heads/missing".into(), GitOid([0xab; 20])); 294 let err = export_repository(&out.join("c.git"), &objects, &bad_refs, None).unwrap_err(); 295 assert_eq!(err.code, RefusalCode::GitObjectCorrupt); 296 fs::remove_dir_all(&dir).unwrap(); 297 fs::remove_dir_all(&out).unwrap(); 298 } 299 300 #[test] 301 fn h1_damaged_or_incomplete_repositories_are_refused_typed() { 302 let dir = temp_dir("damaged"); 303 build_fixture(&dir); 304 let readme = git_text(&dir, &["rev-parse", "HEAD:README.md"]); 305 let path = dir 306 .join(".git/objects") 307 .join(&readme[..2]) 308 .join(&readme[2..]); 309 let mut bytes = fs::read(&path).unwrap(); 310 fs::set_permissions(&path, std::os::unix::fs::PermissionsExt::from_mode(0o644)).unwrap(); 311 let last = bytes.len() - 6; 312 bytes[last] ^= 0x01; 313 fs::write(&path, &bytes).unwrap(); 314 let mut repo = GitRepository::open(&dir.join(".git")).unwrap(); 315 let err = repo 316 .read_object(GitOid::from_hex(&readme).unwrap()) 317 .unwrap_err(); 318 assert_eq!(err.code, RefusalCode::GitObjectCorrupt); 319 fs::remove_file(&path).unwrap(); 320 let err = repo.full_history().unwrap_err(); 321 assert_eq!(err.code, RefusalCode::GitObjectCorrupt); 322 assert!(err.message.contains("missing"), "{}", err.message); 323 fs::remove_dir_all(&dir).unwrap(); 324 } 325 326 /// Full history of a real repository (path in `SYNAPSE_H1_REAL_REPO`, a full non-shallow 327 /// clone): native read, native export as one pack, then Git's own check that the export holds 328 /// the same object IDs and references and passes `fsck --full --strict`. Ignored by default 329 /// because CI has no such clone; run locally with `--ignored`. 330 #[test] 331 #[ignore] 332 fn h1_real_repository_full_history_round_trips_with_identical_object_ids() { 333 let Ok(src) = std::env::var("SYNAPSE_H1_REAL_REPO") else { 334 panic!("set SYNAPSE_H1_REAL_REPO to a full clone's git directory"); 335 }; 336 let src = PathBuf::from(src); 337 let history = GitRepository::open(&src).unwrap().full_history().unwrap(); 338 let out = temp_dir("real"); 339 let dest = out.join("exported.git"); 340 let receipt = export_repository( 341 &dest, 342 &objects_of(&history), 343 &history.refs, 344 history.head.as_deref(), 345 ) 346 .unwrap(); 347 assert_fsck_strict_clean(&dest); 348 // Every reference resolves to the same object in both; the exported database holds 349 // exactly the objects reachable in the source (`rev-list --objects --all`). 350 assert_eq!(git_refs(&src), git_refs(&dest)); 351 let reachable = |d: &Path| -> BTreeSet { 352 git_text(d, &["rev-list", "--objects", "--all"]) 353 .lines() 354 .map(|l| l[..40].to_string()) 355 .collect() 356 }; 357 let src_reachable = reachable(&src); 358 assert_eq!(src_reachable, reachable(&dest)); 359 let dst_all: BTreeSet = git_all_objects(&dest).keys().map(|o| o.to_hex()).collect(); 360 assert_eq!( 361 src_reachable, dst_all, 362 "export holds only reachable objects" 363 ); 364 let commits = history 365 .objects 366 .values() 367 .filter(|o| o.kind == ObjectKind::Commit) 368 .count(); 369 println!( 370 "real repository {}: {} objects ({commits} commits), {} refs, pack {} bytes", 371 src.display(), 372 history.objects.len(), 373 history.refs.len(), 374 receipt.pack_bytes 375 ); 376 fs::remove_dir_all(&out).unwrap(); 377 }