Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions app/buck2_common/src/legacy_configs/cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

use std::collections::HashSet;
use std::str::FromStr;
use std::sync::Arc;

use allocative::Allocative;
Expand All @@ -20,6 +21,7 @@ use buck2_core::cells::cell_root_path::CellRootPath;
use buck2_core::cells::cell_root_path::CellRootPathBuf;
use buck2_core::cells::external::ExternalCellOrigin;
use buck2_core::cells::external::GitCellSetup;
use buck2_core::cells::external::GitObjectFormat;
use buck2_core::cells::name::CellName;
use buck2_core::fs::paths::RelativePath;
use buck2_core::fs::paths::abs_path::AbsPath;
Expand All @@ -30,7 +32,6 @@ use buck2_error::BuckErrorContext;
use dice::DiceComputations;
use dupe::Dupe;

use crate::cas_digest::RawDigest;
use crate::dice::cells::HasCellResolver;
use crate::dice::data::HasIoProvider;
use crate::external_cells::EXTERNAL_CELLS_IMPL;
Expand Down Expand Up @@ -515,12 +516,23 @@ impl BuckConfigBasedCells {
} else if value == "git" {
let section = &format!("external_cell_{}", cell.as_str());
let commit: Arc<str> = get_config(section, "commit_hash")?.into();
// No use in storing the commit hash as a byte array, but let's reuse existing code to
// check for validity
let _ = RawDigest::parse_sha1(commit.as_bytes())?;
let object_format = match get_config(section, "object_format") {
Ok(s) => {
let object_format = GitObjectFormat::from_str(s)?;
let _ = object_format.check(commit.as_ref());
Option::Some(GitObjectFormat::from_str(s)?)
}
Err(_) => {
// We pretend that the object format is SHA1 for this check only;
// We do not use it when interacting with Git.
let _ = GitObjectFormat::Sha1.check(commit.as_ref());
Option::None
}
};
Ok(ExternalCellOrigin::Git(GitCellSetup {
git_origin: get_config(section, "git_origin")?.into(),
commit,
object_format,
}))
} else {
Err(ExternalCellOriginParseError::Unknown(value.to_owned()).into())
Expand Down
62 changes: 60 additions & 2 deletions app/buck2_core/src/cells/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
*/

use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

use allocative::Allocative;
use buck2_error::buck2_error;
use derive_more::Display;
use dupe::Dupe;

use crate::cells::name::CellName;

#[derive(Debug, Clone, Dupe, allocative::Allocative, PartialEq, Eq)]
#[derive(Debug, Clone, Dupe, Allocative, PartialEq, Eq)]
pub enum ExternalCellOrigin {
Bundled(CellName),
Git(GitCellSetup),
Expand All @@ -34,8 +38,9 @@ pub enum ExternalCellOrigin {
#[display("git({}, {})", git_origin, commit)]
pub struct GitCellSetup {
pub git_origin: Arc<str>,
// Guaranteed to be a valid sha1 commit hash
// Guaranteed to be a valid commit hash
pub commit: Arc<str>,
pub object_format: Option<GitObjectFormat>,
}

impl fmt::Display for ExternalCellOrigin {
Expand All @@ -46,3 +51,56 @@ impl fmt::Display for ExternalCellOrigin {
}
}
}

#[derive(Debug, Display, Eq, PartialEq, Clone, Dupe, Hash, Allocative)]
pub enum GitObjectFormat {
#[display("sha1")]
Sha1,
#[display("sha256")]
Sha256,
}

impl FromStr for GitObjectFormat {
type Err = buck2_error::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"sha1" => Ok(GitObjectFormat::Sha1),
"sha256" => Ok(GitObjectFormat::Sha256),
_ => Err(buck2_error!(
buck2_error::ErrorTag::Input,
"object_format must be one of `sha1` or `sha256` (got: {})",
&s,
)),
}
}
}

impl GitObjectFormat {
pub fn check(&self, s: &str) -> Result<(), buck2_error::Error> {
match self {
Self::Sha1 => {
if s.len() == 40 && s.chars().all(|c| c.is_ascii_hexdigit()) {
Ok(())
} else {
Err(buck2_error!(
buck2_error::ErrorTag::Input,
"not a valid SHA1 digest (got: {})",
&s,
))
}
}
Self::Sha256 => {
if s.len() == 64 && s.chars().all(|c| c.is_ascii_hexdigit()) {
Ok(())
} else {
Err(buck2_error!(
buck2_error::ErrorTag::Input,
"not a valid SHA256 digest (got: {})",
&s,
))
}
}
}
}
}
8 changes: 7 additions & 1 deletion app/buck2_external_cells/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ impl IoRequest for GitFetchIoRequest {
}

run_git(&path, |c| {
c.arg("init");
match &self.setup.object_format {
None => c.arg("init"),
Some(object_format) => c
.arg("init")
.arg("--object-format")
.arg(object_format.to_string()),
};
})?;

run_git(&path, |c| {
Expand Down