Skip to content

Commit

Permalink
[rooch-networkgh-2242] change to long_version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Jul 23, 2024
1 parent 7e5eab2 commit 4d2ce75
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
45 changes: 45 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/rooch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
63 changes: 62 additions & 1 deletion crates/rooch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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;
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)
Expand Down Expand Up @@ -50,6 +51,66 @@ pub enum Command {
Genesis(Genesis),
}

struct TagData<'a> {
latest_tag_name: Option<String>,
commit_hash_str: Option<String>,
_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<TagData<'static>, 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<String> {
match opt.cmd {
Command::Account(account) => account.execute().await,
Expand Down

0 comments on commit 4d2ce75

Please sign in to comment.