diff --git a/Cargo.lock b/Cargo.lock index 2f05e7a5b0..6c395ef370 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4376,6 +4376,21 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" +[[package]] +name = "git2" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b903b73e45dc0c6c596f2d37eccece7c1c8bb6e4407b001096387c63d0d93724" +dependencies = [ + "bitflags 2.5.0", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + [[package]] name = "glob" version = "0.3.1" @@ -5790,6 +5805,20 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "libgit2-sys" +version = "0.17.0+1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10472326a8a6477c3c20a64547b0059e4b0d086869eee31e6d7da728a8eb7224" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + [[package]] name = "libloading" version = "0.8.3" @@ -5857,6 +5886,20 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libssh2-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + [[package]] name = "libtest-mimic" version = "0.5.2" @@ -5875,6 +5918,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" dependencies = [ "cc", + "libc", "pkg-config", "vcpkg", ] @@ -9215,6 +9259,7 @@ dependencies = [ "framework-builder", "framework-release", "framework-types", + "git2", "hex", "itertools 0.13.0", "jemallocator", diff --git a/Cargo.toml b/Cargo.toml index 5d33fb63ef..f5bd8c6fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -316,6 +316,7 @@ rocksdb = { version = "0.22.0", features = ["lz4"] } ripemd = { version = "0.1.3" } fastcrypto-zkp = { version = "0.1.3" } function_name = { version = "0.3.0" } +git2 = { version = "0.19.0" } # Note: the BEGIN and END comments below are required for external tooling. Do not remove. # BEGIN MOVE DEPENDENCIES diff --git a/crates/rooch/Cargo.toml b/crates/rooch/Cargo.toml index 4ca7eb8c2e..21cfc4c9d6 100644 --- a/crates/rooch/Cargo.toml +++ b/crates/rooch/Cargo.toml @@ -47,6 +47,7 @@ tempfile = { workspace = true } redb = { workspace = true } [target.'cfg(not(target_env = "msvc"))'.dependencies] jemallocator = { version = "0.5.4", features = ["unprefixed_malloc_on_supported_platforms", "profiling"] } +git2 = { workspace = true } move-bytecode-utils = { workspace = true } move-binary-format = { workspace = true } diff --git a/crates/rooch/src/lib.rs b/crates/rooch/src/lib.rs index afde5c416e..3cb6af69d7 100644 --- a/crates/rooch/src/lib.rs +++ b/crates/rooch/src/lib.rs @@ -11,6 +11,7 @@ use commands::{ object::ObjectCommand, resource::ResourceCommand, rpc::Rpc, server::Server, session_key::SessionKey, state::StateCommand, transaction::Transaction, }; +use git2::{Oid, Repository}; use rooch_types::error::RoochResult; pub mod cli_types; @@ -18,7 +19,7 @@ pub mod commands; pub mod utils; #[derive(clap::Parser)] -#[clap(author, version, about, long_about = None, +#[clap(author, long_version(get_latest_tag_and_commit_hash().unwrap().commit_hash_str().unwrap()), about, long_about = None, styles = Styles::styled() .header(AnsiColor::Green.on_default() | Effects::BOLD) .usage(AnsiColor::Green.on_default() | Effects::BOLD) @@ -50,6 +51,66 @@ pub enum Command { Genesis(Genesis), } +struct TagData<'a> { + latest_tag_name: Option, + commit_hash_str: Option, + _phantom: std::marker::PhantomData<&'a ()>, +} + +impl<'a> TagData<'a> { + fn latest_tag_name(&self) -> Option<&str> { + self.latest_tag_name.as_deref() + } + + fn commit_hash_str(&self) -> Option<&str> { + self.commit_hash_str.as_deref() + } +} + +fn get_latest_tag_and_commit_hash() -> Result, anyhow::Error> { + // Open the repository + let repo = Repository::open(".")?; + + // Get all tag names with a specific pattern + let tag_names = repo.tag_names(Some("v*.*.*"))?; + + // Collect tags with their commit OIDs + let mut tags_with_commits: Vec<(String, Oid)> = vec![]; + + for name in tag_names.iter().flatten() { + if let Ok(reference) = repo.find_reference(name) { + if let Ok(tag) = reference.peel_to_tag() { + let target_commit = tag + .target() + .map_err(|_| anyhow::anyhow!("Tag has no target commit"))?; + tags_with_commits.push((name.to_string(), target_commit.id())); + } + } + } + + // Sort tags by their commit OID in descending order + tags_with_commits.sort_by(|a, b| b.1.cmp(&a.1)); + + // Create a TagData struct to hold the results + let tag_data = + if let Some((latest_tag_name, commit_hash)) = tags_with_commits.into_iter().next() { + let commit_hash_str = commit_hash.to_string(); + TagData { + latest_tag_name: Some(latest_tag_name), + commit_hash_str: Some(commit_hash_str), + _phantom: std::marker::PhantomData, + } + } else { + TagData { + latest_tag_name: None, + commit_hash_str: None, + _phantom: std::marker::PhantomData, + } + }; + + Ok(tag_data) +} + pub async fn run_cli(opt: RoochCli) -> RoochResult { match opt.cmd { Command::Account(account) => account.execute().await,