Skip to content
Merged
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
91 changes: 91 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bytes = { workspace = true }
cargo_metadata = { workspace = true }
cargo-component-core = { workspace = true }
cargo-config2 = { workspace = true }
git2 = { workspace = true }
clap = { workspace = true }
futures = { workspace = true }
heck = { workspace = true }
Expand Down Expand Up @@ -74,6 +75,7 @@ bytes = "1.6.0"
cargo_metadata = "0.19.1"
cargo-component-core = { path = "crates/core", version = "0.21.1" }
cargo-config2 = "0.1.24"
git2 = "0.20.1"
clap = { version = "4.5.4", features = ["derive", "env"] }
dirs = "5"
futures = "0.3.30"
Expand Down
51 changes: 51 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use anyhow::Result;
use cargo_metadata::Package;
use git2::{ErrorClass, ErrorCode, Repository};

#[derive(Debug)]
pub struct GitMetadata {
commit: String,
}

impl GitMetadata {
/// Creates a new Git metadata for the given cargo package.
pub fn from_package(package: &Package) -> Result<Option<Self>> {
log::debug!(
"searching for git metadata from manifest `{path}`",
path = package.manifest_path
);

let repository = match Repository::discover(package.manifest_path.clone()) {
Ok(repository) => Ok(repository),
Err(ref e)
if e.class() == ErrorClass::Repository && e.code() == ErrorCode::NotFound =>
{
return Ok(None)
}
Err(e) => Err(e),
}?;

let head = match repository.head() {
Ok(head) => Ok(head),
Err(ref e)
if e.class() == ErrorClass::Reference && e.code() == ErrorCode::UnbornBranch =>
{
return Ok(None)
}
Err(error) => Err(error),
}?;

let commit = head.peel_to_commit()?;
let commit_id = commit.id();

let metadata = Self {
commit: commit_id.to_string(),
};

Ok(Some(metadata))
}

pub fn commit(&self) -> &str {
&self.commit
}
}
25 changes: 16 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use wit_component::ComponentEncoder;
use crate::target::install_wasm32_wasip1;

use config::{CargoArguments, CargoPackageSpec, Config};
use git::GitMetadata;
use lock::{acquire_lock_file_ro, acquire_lock_file_rw};
use metadata::ComponentMetadata;
use registry::{PackageDependencyResolution, PackageResolutionMap};
Expand All @@ -44,6 +45,7 @@ mod bindings;
pub mod commands;
pub mod config;
mod generator;
mod git;
mod lock;
mod metadata;
mod registry;
Expand Down Expand Up @@ -971,12 +973,14 @@ fn componentize(
.validate(true);

let package = &cargo_metadata[&artifact.package_id];
let component = add_component_metadata(&package, &encoder.encode()?).with_context(|| {
format!(
"failed to add metadata to output component `{path}`",
path = path.display()
)
})?;
let git = GitMetadata::from_package(package)?;
let component = add_component_metadata(package, git.as_ref(), &encoder.encode()?)
.with_context(|| {
format!(
"failed to add metadata to output component `{path}`",
path = path.display()
)
})?;

// To make the write atomic, first write to a temp file and then rename the file
let temp_dir = cargo_metadata.target_directory.join("tmp");
Expand Down Expand Up @@ -1021,7 +1025,11 @@ pub struct PublishOptions<'a> {
}

/// Read metadata from `Cargo.toml` and add it to the component
fn add_component_metadata(package: &Package, wasm: &[u8]) -> Result<Vec<u8>> {
fn add_component_metadata(
package: &Package,
git: Option<&GitMetadata>,
wasm: &[u8],
) -> Result<Vec<u8>> {
let metadata = wasm_metadata::AddMetadata {
name: Some(package.name.clone()),
language: vec![("Rust".to_string(), "".to_string())],
Expand Down Expand Up @@ -1055,8 +1063,7 @@ fn add_component_metadata(package: &Package, wasm: &[u8]) -> Result<Vec<u8>> {
.as_ref()
.map(|s| wasm_metadata::Homepage::new(s.to_string().as_str()))
.transpose()?,
// TODO: get the git commit hash
revision: None,
revision: git.map(|git| wasm_metadata::Revision::new(git.commit().to_string())),
version: Some(wasm_metadata::Version::new(package.version.to_string())),
};
metadata.to_wasm(wasm)
Expand Down