Skip to content

Commit 6164458

Browse files
Add Git commit hash to the revision field of component metadata (#383)
* Add Git commit hash to the revision field of component metadata * Run `cargo fmt`
1 parent e0cc637 commit 6164458

File tree

4 files changed

+160
-9
lines changed

4 files changed

+160
-9
lines changed

Cargo.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ bytes = { workspace = true }
2323
cargo_metadata = { workspace = true }
2424
cargo-component-core = { workspace = true }
2525
cargo-config2 = { workspace = true }
26+
git2 = { workspace = true }
2627
clap = { workspace = true }
2728
futures = { workspace = true }
2829
heck = { workspace = true }
@@ -74,6 +75,7 @@ bytes = "1.6.0"
7475
cargo_metadata = "0.19.1"
7576
cargo-component-core = { path = "crates/core", version = "0.21.1" }
7677
cargo-config2 = "0.1.24"
78+
git2 = "0.20.1"
7779
clap = { version = "4.5.4", features = ["derive", "env"] }
7880
dirs = "5"
7981
futures = "0.3.30"

src/git.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use anyhow::Result;
2+
use cargo_metadata::Package;
3+
use git2::{ErrorClass, ErrorCode, Repository};
4+
5+
#[derive(Debug)]
6+
pub struct GitMetadata {
7+
commit: String,
8+
}
9+
10+
impl GitMetadata {
11+
/// Creates a new Git metadata for the given cargo package.
12+
pub fn from_package(package: &Package) -> Result<Option<Self>> {
13+
log::debug!(
14+
"searching for git metadata from manifest `{path}`",
15+
path = package.manifest_path
16+
);
17+
18+
let repository = match Repository::discover(package.manifest_path.clone()) {
19+
Ok(repository) => Ok(repository),
20+
Err(ref e)
21+
if e.class() == ErrorClass::Repository && e.code() == ErrorCode::NotFound =>
22+
{
23+
return Ok(None)
24+
}
25+
Err(e) => Err(e),
26+
}?;
27+
28+
let head = match repository.head() {
29+
Ok(head) => Ok(head),
30+
Err(ref e)
31+
if e.class() == ErrorClass::Reference && e.code() == ErrorCode::UnbornBranch =>
32+
{
33+
return Ok(None)
34+
}
35+
Err(error) => Err(error),
36+
}?;
37+
38+
let commit = head.peel_to_commit()?;
39+
let commit_id = commit.id();
40+
41+
let metadata = Self {
42+
commit: commit_id.to_string(),
43+
};
44+
45+
Ok(Some(metadata))
46+
}
47+
48+
pub fn commit(&self) -> &str {
49+
&self.commit
50+
}
51+
}

src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use wit_component::ComponentEncoder;
3636
use crate::target::install_wasm32_wasip1;
3737

3838
use config::{CargoArguments, CargoPackageSpec, Config};
39+
use git::GitMetadata;
3940
use lock::{acquire_lock_file_ro, acquire_lock_file_rw};
4041
use metadata::ComponentMetadata;
4142
use registry::{PackageDependencyResolution, PackageResolutionMap};
@@ -44,6 +45,7 @@ mod bindings;
4445
pub mod commands;
4546
pub mod config;
4647
mod generator;
48+
mod git;
4749
mod lock;
4850
mod metadata;
4951
mod registry;
@@ -971,12 +973,14 @@ fn componentize(
971973
.validate(true);
972974

973975
let package = &cargo_metadata[&artifact.package_id];
974-
let component = add_component_metadata(&package, &encoder.encode()?).with_context(|| {
975-
format!(
976-
"failed to add metadata to output component `{path}`",
977-
path = path.display()
978-
)
979-
})?;
976+
let git = GitMetadata::from_package(package)?;
977+
let component = add_component_metadata(package, git.as_ref(), &encoder.encode()?)
978+
.with_context(|| {
979+
format!(
980+
"failed to add metadata to output component `{path}`",
981+
path = path.display()
982+
)
983+
})?;
980984

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

10231027
/// Read metadata from `Cargo.toml` and add it to the component
1024-
fn add_component_metadata(package: &Package, wasm: &[u8]) -> Result<Vec<u8>> {
1028+
fn add_component_metadata(
1029+
package: &Package,
1030+
git: Option<&GitMetadata>,
1031+
wasm: &[u8],
1032+
) -> Result<Vec<u8>> {
10251033
let metadata = wasm_metadata::AddMetadata {
10261034
name: Some(package.name.clone()),
10271035
language: vec![("Rust".to_string(), "".to_string())],
@@ -1055,8 +1063,7 @@ fn add_component_metadata(package: &Package, wasm: &[u8]) -> Result<Vec<u8>> {
10551063
.as_ref()
10561064
.map(|s| wasm_metadata::Homepage::new(s.to_string().as_str()))
10571065
.transpose()?,
1058-
// TODO: get the git commit hash
1059-
revision: None,
1066+
revision: git.map(|git| wasm_metadata::Revision::new(git.commit().to_string())),
10601067
version: Some(wasm_metadata::Version::new(package.version.to_string())),
10611068
};
10621069
metadata.to_wasm(wasm)

0 commit comments

Comments
 (0)