Skip to content
Prev Previous commit
Next Next commit
test(gl): guard three-level nested visibility in sparse clone
Add a regression test for deny /secret, allow /secret/public, deny
/secret/public/admin and clarify the sparse-checkout comment. git does
not re-traverse an explicitly excluded directory, so emitting all
excludes before re-includes keeps the deepest deny in force; the broader
parent re-include does not resurrect it.
  • Loading branch information
beardthelion committed Jun 10, 2026
commit 95a223e3bbc520dcb74ef46df5e18f981b16a623
48 changes: 45 additions & 3 deletions crates/gl/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ pub fn setup_partial_clone(
dest_str,
])?;
git(dest, &["sparse-checkout", "init", "--no-cone"])?;
// Non-cone sparse-checkout, gitignore-style and order-sensitive: include
// everything, exclude the withheld globs, then re-include any allowed globs
// nested under an excluded one (later patterns win).
// Non-cone sparse-checkout, gitignore-style: include everything, exclude the
// withheld globs, then re-include any allowed globs nested under an excluded
// one. Emitting all excludes before the re-includes is safe even for deeper
// re-denials (deny /secret, allow /secret/public, deny /secret/public/admin):
// git does not re-traverse an explicitly excluded directory, so a broader
// parent re-include never resurrects a more specific excluded subtree.
let mut spec = String::from("/*\n");
for g in withheld_globs {
for pat in sparse_patterns(g) {
Expand Down Expand Up @@ -339,6 +342,45 @@ mod tests {
);
}

#[test]
fn three_level_alternating_nesting_respects_specificity() {
// deny /secret, allow /secret/public, deny /secret/public/admin.
// The deepest deny must win even though a shallower allow re-includes
// its parent: order patterns by depth, not all-excludes-then-includes.
let (td, url) = bare_remote(&[
("public/a.txt", b"pub\n"),
("secret/private/p.txt", b"PRIV\n"),
("secret/public/s.txt", b"SHARED\n"),
("secret/public/admin/k.txt", b"ADMIN\n"),
]);
let dest = td.path().join("dest");
setup_partial_clone(
&dest,
&url,
&[
"/secret/**".to_string(),
"/secret/public/admin/**".to_string(),
],
&["/secret/public/**".to_string()],
None,
)
.unwrap();

assert!(dest.join("public/a.txt").exists(), "public present");
assert!(
dest.join("secret/public/s.txt").exists(),
"allowed middle path must be re-included"
);
assert!(
!dest.join("secret/private/p.txt").exists(),
"denied sibling must stay excluded"
);
assert!(
!dest.join("secret/public/admin/k.txt").exists(),
"deepest denied path must stay excluded despite the shallower re-include"
);
}

#[test]
fn exact_path_glob_is_excluded() {
// A wildcard-free glob must exclude the exact file, not just a subtree.
Expand Down
Loading