Skip to content

Commit bfaa2bc

Browse files
10ne1claude
andcommitted
gix: reject implicit sha1 repos in sha256-only builds
A missing extensions.objectFormat means legacy Sha1. In sha256-only builds, Kind::default() is Sha256, so such repos were silently mislabeled as Sha256. Resolve the implicit case to Sha1 when supported, else error out to avoid any mislabeling. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
1 parent 62de1ec commit bfaa2bc

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

gix/src/config/cache/incubate.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,10 @@ impl StageOne {
4545
.map(|version| Core::REPOSITORY_FORMAT_VERSION.try_into_usize(version))
4646
.transpose()?
4747
.unwrap_or_default();
48-
let object_hash = (repo_format_version != 1)
49-
.then_some(Ok(gix_hash::Kind::default()))
50-
.or_else(|| {
51-
config
52-
.string(Extensions::OBJECT_FORMAT)
53-
.map(|format| Extensions::OBJECT_FORMAT.try_into_object_format(format))
54-
})
55-
.transpose()?
56-
.unwrap_or_else(gix_hash::Kind::default);
48+
let object_hash = match config.string(Extensions::OBJECT_FORMAT) {
49+
Some(format) if repo_format_version == 1 => Extensions::OBJECT_FORMAT.try_into_object_format(format)?,
50+
_ => legacy_object_hash()?,
51+
};
5752

5853
let extension_worktree = util::config_bool(
5954
&config,
@@ -104,6 +99,22 @@ impl StageOne {
10499
}
105100
}
106101

102+
/// Return the object hash for a repository that does not set `extensions.objectFormat`.
103+
///
104+
/// Git interprets a missing objectFormat as the original Sha1 layout, so we return
105+
/// gix_hash::Kind::Sha1 whenever this build can handle it.
106+
/// In Sha256-only builds we cannot open such a repository, so return an error instead.
107+
fn legacy_object_hash() -> Result<gix_hash::Kind, Error> {
108+
#[cfg(feature = "sha1")]
109+
{
110+
Ok(gix_hash::Kind::Sha1)
111+
}
112+
#[cfg(not(feature = "sha1"))]
113+
{
114+
Err(Error::UnsupportedObjectFormat { name: "sha1".into() })
115+
}
116+
}
117+
107118
fn load_config(
108119
config_path: std::path::PathBuf,
109120
buf: &mut Vec<u8>,

0 commit comments

Comments
 (0)