Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 03796c06af fix(vcs): read objects in a repository another user owns with more than 32 packs
Repo::discover kept the handle from ThreadSafeRepository::discover, which picks gitoxide's
options for the folder owner's trust. For a repository owned by another user (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
2026-12-01 16:09:19 +08:00
+67 -10
Переглянути файл
@@ -105,16 +105,26 @@ fn pack_stamp(pack_dir: &Path) -> Option<SystemTime> {
pub(crate) fn discover(path: &Path) -> IdeResult<Option<Repo>> {
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::<E>() {
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::<E>() {
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]