From 6d38521498695be22e9d09cb1bffe5f7e8af802c Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 1 Dec 2026 16:09:37 +0800 Subject: [PATCH] fix(vcs): read objects in a repository another user owns with more than 32 packs (#7191) Repo::discover kept the handle from ThreadSafeRepository::discover, which picks gitoxide's options by the folder owner's trust; for a repository another user owns (reduced trust) they cap the object database at 32 pack indices, so with more packs no object could be read and HEAD failed to resolve in the Git log. The discovered repository is now opened the way gix() reopens it, with slots sized by the pack directory. Fixes #7138 --- crates/ide-vcs/src/repo.rs | 77 +++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/crates/ide-vcs/src/repo.rs b/crates/ide-vcs/src/repo.rs index 041d22864a..e1ccfc9d9f 100644 --- a/crates/ide-vcs/src/repo.rs +++ b/crates/ide-vcs/src/repo.rs @@ -105,16 +105,26 @@ fn pack_stamp(pack_dir: &Path) -> Option { pub(crate) fn discover(path: &Path) -> IdeResult> { let start = if path.is_file() { path.parent().unwrap_or(path) } else { path }; - if let Err(e) = gix::discover::upwards(start) { - use gix::discover::upwards::Error as E; - return match e.downcast_any_ref::() { - Some( - E::NoGitRepository { .. } | E::NoGitRepositoryWithinCeiling { .. } | E::NoGitRepositoryWithinFs { .. }, - ) => Ok(None), - _ => Err(gx(e)), - }; - } - let ts = gix::ThreadSafeRepository::discover(start).map_err(gx)?; + let found = match gix::discover::upwards(start) { + Ok((found, _trust)) => found, + Err(e) => { + use gix::discover::upwards::Error as E; + return match e.downcast_any_ref::() { + Some( + E::NoGitRepository { .. } + | E::NoGitRepositoryWithinCeiling { .. } + | E::NoGitRepositoryWithinFs { .. }, + ) => Ok(None), + _ => Err(gx(e)), + }; + } + }; + // Opened like `gix()` reopens it, not with `ThreadSafeRepository::discover`: that picks the + // options for the folder owner's trust, and for a repository another user owns (reduced trust) + // they cap the object database at 32 pack indices. A repository with more packs then fails to + // read any object, so `HEAD` doesn't resolve (#7138). Trust is still decided per repository. + let (git_dir, worktree) = found.into_repository_and_work_tree_directories(); + let ts = gix::ThreadSafeRepository::open(worktree.unwrap_or(git_dir)).map_err(gx)?; let repo = ts.to_thread_local(); let Some(workdir) = repo.workdir() else { // Bare repositories have no worktree to show changes for. @@ -658,6 +668,53 @@ mod tests { assert!(cmd.status().unwrap().success(), "{cmd:?}"); } + /// More pack indices than the 32 slots `gix` gives a repository another user owns (#7138). The + /// owner can't be changed in a test, so this pins that discovery opens with slots sized by the + /// pack directory: objects in every pack stay readable through the cached handle. + #[test] + fn a_repository_with_many_packs_reads_objects_from_all_of_them() { + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path().join("repo"); + run(Command::new("git").args(["init", "-q", "-b", "main"]).arg(&root)); + let git = |args: &[&str]| { + let out = Command::new("git").current_dir(&root).args(args).output().unwrap(); + assert!(out.status.success(), "{args:?}"); + String::from_utf8(out.stdout).unwrap().trim().to_owned() + }; + git(&["config", "gc.auto", "0"]); + git(&["config", "maintenance.auto", "false"]); + let mut oids = Vec::new(); + for i in 0..40 { + std::fs::write(root.join(format!("f{i}.txt")), format!("{i}\n")).unwrap(); + git(&["add", "."]); + git(&["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", &format!("c{i}")]); + oids.push(git(&["rev-parse", "HEAD"])); + let mut pack = Command::new("git") + .current_dir(&root) + .args(["pack-objects", "-q", "--revs", ".git/objects/pack/pack"]) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::null()) + .spawn() + .unwrap(); + use std::io::Write; + let range = if i == 0 { "HEAD\n" } else { "HEAD\n^HEAD~1\n" }; + pack.stdin.take().unwrap().write_all(range.as_bytes()).unwrap(); + assert!(pack.wait().unwrap().success()); + git(&["prune-packed"]); + } + let packs = std::fs::read_dir(root.join(".git/objects/pack")) + .unwrap() + .filter(|e| e.as_ref().unwrap().path().extension().is_some_and(|x| x == "idx")) + .count(); + assert!(packs > 32, "{packs} pack indices"); + let repo = discover(&root).unwrap().unwrap(); + let gix = repo.gix().unwrap(); + assert_eq!(gix.head_id().unwrap().to_string(), *oids.last().unwrap()); + for oid in &oids { + gix.find_commit(gix::ObjectId::from_hex(oid.as_bytes()).unwrap()).unwrap(); + } + } + /// What a remounted volume looks like: every path and modification time is the same, but /// the files that the old handle mapped are gone. `tests/volume_loss.rs` does it for real. #[test]