diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-01-03 21:05:18 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-01-03 21:05:18 +0800 |
| commit | 2e16a556e277035225d02d9a62c2da699235de36 (patch) | |
| tree | 6e81b9251c71d31620f60381b13af53888d216a0 | |
| parent | 7f3e51cf19e23292ab2087f521408c70c92f12fe (diff) | |
Add Git branch and commit info to compile info
- Include branch and commit hash in compile info display
- Add build script functions to extract git metadata
- Update export scripts to force rebuilds when needed
- Extend share command with placeholder implementations
| -rw-r--r-- | build.rs | 48 | ||||
| -rw-r--r-- | export.ps1 | 1 | ||||
| -rwxr-xr-x | export.sh | 2 | ||||
| -rw-r--r-- | locales/help_docs/en.yml | 12 | ||||
| -rw-r--r-- | locales/help_docs/zh-CN.yml | 12 | ||||
| -rw-r--r-- | src/bin/jv.rs | 164 | ||||
| -rw-r--r-- | src/bin/jvv.rs | 10 | ||||
| -rw-r--r-- | src/data/compile_info.rs.template | 4 |
8 files changed, 240 insertions, 13 deletions
@@ -1,10 +1,13 @@ use std::env; use std::path::PathBuf; +use std::process::Command; const COMPILE_INFO_RS: &str = "./src/data/compile_info.rs"; const COMPILE_INFO_RS_TEMPLATE: &str = "./src/data/compile_info.rs.template"; fn main() { + println!("cargo:rerun-if-env-changed=FORCE_BUILD"); + let repo_root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); // Only generate installer script on Windows @@ -87,13 +90,17 @@ fn generate_compile_info(repo_root: &PathBuf) -> Result<(), Box<dyn std::error:: let platform = get_platform(&target); let toolchain = get_toolchain(); let version = get_version(); + let branch = get_git_branch().unwrap_or_else(|_| "unknown".to_string()); + let commit = get_git_commit().unwrap_or_else(|_| "unknown".to_string()); let generated_code = template_code .replace("{date}", &date) .replace("{target}", &target) .replace("{platform}", &platform) .replace("{toolchain}", &toolchain) - .replace("{version}", &version); + .replace("{version}", &version) + .replace("{branch}", &branch) + .replace("{commit}", &commit); // Write the generated code let compile_info_path = repo_root.join(COMPILE_INFO_RS); @@ -163,3 +170,42 @@ fn get_version() -> String { "unknown".to_string() } + +/// Get current git branch +fn get_git_branch() -> Result<String, Box<dyn std::error::Error>> { + let output = Command::new("git") + .args(["branch", "--show-current"]) + .output()?; + + if output.status.success() { + let branch = String::from_utf8(output.stdout)?.trim().to_string(); + + if branch.is_empty() { + // Try to get HEAD reference if no branch (detached HEAD) + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output()?; + + if output.status.success() { + let head_ref = String::from_utf8(output.stdout)?.trim().to_string(); + return Ok(head_ref); + } + } else { + return Ok(branch); + } + } + + Err("Failed to get git branch".into()) +} + +/// Get current git commit hash +fn get_git_commit() -> Result<String, Box<dyn std::error::Error>> { + let output = Command::new("git").args(["rev-parse", "HEAD"]).output()?; + + if output.status.success() { + let commit = String::from_utf8(output.stdout)?.trim().to_string(); + return Ok(commit); + } + + Err("Failed to get git commit".into()) +} @@ -1,6 +1,7 @@ # Require : Cargo (Rust), ISCC (Inno Setup) # Build +$env:CARGO_BUILD_RERUN_IF_ENV_CHANGED="FORCE_BUILD=$(Get-Date -Format 'yyyyMMdd')" cargo build --workspace --release if ($LASTEXITCODE -ne 0) { # Build failed @@ -3,6 +3,8 @@ # Require : Cargo (Rust) # Build +export FORCE_BUILD=$(date +%Y-%m-%d) + if cargo build --workspace --release; then # Export if cargo run --manifest-path crates/build_helper/Cargo.toml --bin exporter; then diff --git a/locales/help_docs/en.yml b/locales/help_docs/en.yml index 7b2d200..caae1da 100644 --- a/locales/help_docs/en.yml +++ b/locales/help_docs/en.yml @@ -40,9 +40,12 @@ jvv: compile_info: | Compile Info - Compile Date - %{build_time} - Target - %{build_target} - Platform - %{build_platform} - %{build_toolchain} + Compile Date - [[cyan]]%{build_time}[[/]] + Target - [[cyan]]%{build_target}[[/]] + Platform - [[cyan]]%{build_platform} - %{build_toolchain}[[/]] + + Core - [[cyan]]%{core_build_branch}[[/]] (Commit: %{core_build_commit}) + CLI - [[cyan]]%{cli_build_branch}[[/]] (Commit: %{cli_build_commit}) help: | **JustEnoughVCS Upstream Vault Commands** @@ -202,6 +205,9 @@ jv: Target - [[cyan]]%{build_target}[[/]] Platform - [[cyan]]%{build_platform} - %{build_toolchain}[[/]] + Core - [[cyan]]%{core_build_branch}[[/]] (Commit: %{core_build_commit}) + CLI - [[cyan]]%{cli_build_branch}[[/]] (Commit: %{cli_build_commit}) + tip: not_workspace: | Not in workspace directory. diff --git a/locales/help_docs/zh-CN.yml b/locales/help_docs/zh-CN.yml index 7d93620..1ec2ffc 100644 --- a/locales/help_docs/zh-CN.yml +++ b/locales/help_docs/zh-CN.yml @@ -40,9 +40,12 @@ jvv: compile_info: | 编译信息: - 时间:%{build_time} - 目标:%{build_target} - 平台:%{build_platform} - %{build_toolchain} + 时间:[[cyan]]%{build_time}[[/]] + 目标:[[cyan]]%{build_target}[[/]] + 平台:[[cyan]]%{build_platform} - %{build_toolchain}[[/]] + + 核心库:[[cyan]]%{core_build_branch}[[/]] (Commit: %{core_build_commit}) + 命令行:[[cyan]]%{cli_build_branch}[[/]] (Commit: %{cli_build_commit}) help: | **JustEnoughVCS 上游库命令** @@ -197,6 +200,9 @@ jv: 目标:[[cyan]]%{build_target}[[/]] 平台:[[cyan]]%{build_platform} - %{build_toolchain}[[/]] + 核心库:[[cyan]]%{core_build_branch}[[/]] (Commit: %{core_build_commit}) + 命令行:[[cyan]]%{cli_build_branch}[[/]] (Commit: %{cli_build_commit}) + tip: not_workspace: | 当前不在工作区目录,您可使用 `jv create <名称>` 或 `jv init` 创建工作区 diff --git a/src/bin/jv.rs b/src/bin/jv.rs index 9ac6bbf..09e6ece 100644 --- a/src/bin/jv.rs +++ b/src/bin/jv.rs @@ -638,6 +638,20 @@ struct ShareFileArgs { /// Show help information #[arg(short, long)] help: bool, + + /// Arguments 1 + args1: Option<String>, + + /// Arguments 2 + args2: Option<String>, + + /// Arguments 3 + args3: Option<String>, + // 规则 + // 当无参数时,视作 Help + // 当只有 1 时, 视作 导入,1 = 导入的分享 ID + // 当有 1 和 2 时, 视作 拉取,1 = 来自的 Sheet 2 = 导入的 pattern(基于 1 的 Sheet) + // 当有 1 和 2 和 3 时, 视作 分享,1 = 文件 2 = 发送到的 Sheet 3 = 描述 } #[derive(Parser, Debug)] @@ -939,7 +953,13 @@ async fn main() { build_time = compile_info.date, build_target = compile_info.target, build_platform = compile_info.platform, - build_toolchain = compile_info.toolchain + build_toolchain = compile_info.toolchain, + cli_build_branch = compile_info.build_branch, + cli_build_commit = + &compile_info.build_commit[..7.min(compile_info.build_commit.len())], + core_build_branch = core_compile_info.build_branch, + core_build_commit = &core_compile_info.build_commit + [..7.min(core_compile_info.build_commit.len())] )) ); } @@ -2038,7 +2058,107 @@ async fn jv_status(_args: StatusArgs) { } } -async fn jv_info(args: InfoArgs) {} +async fn jv_info(args: InfoArgs) { + let local_dir = match current_local_path() { + Some(dir) => dir, + None => { + eprintln!("{}", t!("jv.fail.workspace_not_found").trim()); + return; + } + }; + + let query_file_paths = if let Some(pattern) = args.file_pattern.clone() { + let files = glob(pattern, &local_dir).await; + files + .iter() + .filter_map(|f| PathBuf::from_str(f.0).ok()) + .collect::<Vec<_>>() + } else { + println!("{}", md(t!("jv.info"))); + return; + }; + + let _ = correct_current_dir(); + + let Ok(local_cfg) = LocalConfig::read().await else { + eprintln!("{}", md(t!("jv.fail.read_cfg"))); + return; + }; + + let Ok(latest_info) = LatestInfo::read_from(LatestInfo::latest_info_path( + &local_dir, + &local_cfg.current_account(), + )) + .await + else { + eprintln!("{}", md(t!("jv.fail.read_cfg"))); + return; + }; + + let account = local_cfg.current_account(); + + let Some(sheet_name) = local_cfg.sheet_in_use().clone() else { + eprintln!("{}", md(t!("jv.fail.status.no_sheet_in_use")).trim()); + return; + }; + + let Some(local_workspace) = LocalWorkspace::init_current_dir(local_cfg.clone()) else { + eprintln!("{}", md(t!("jv.fail.workspace_not_found")).trim()); + return; + }; + + let Ok(local_sheet) = local_workspace.local_sheet(&account, &sheet_name).await else { + eprintln!("{}", md(t!("jv.fail.read_cfg"))); + return; + }; + + if query_file_paths.len() < 1 { + return; + } + + // 需要查询的文件 + let query_file_path = query_file_paths[0].to_path_buf(); + let Ok(mapping) = local_sheet.mapping_data(&query_file_path) else { + return; + }; + let vfid = mapping.mapping_vfid(); + + // 渲染初始位置 + { + println!("{}", query_file_path.display().to_string()); + } + + // 渲染参考表位置,没有则以 ID 代替 + { + let path_in_ref = if let Some(path) = latest_info.ref_sheet_vfs_mapping.get(vfid) { + path.display().to_string() + } else { + vfid.clone() + }; + + // 偏移字符串 + let query_file_path_string = query_file_path.display().to_string(); + let offset_string = " ".repeat(display_width( + if let Some(last_slash) = query_file_path_string.rfind('/') { + &query_file_path_string[..last_slash] + } else { + "" + }, + )); + + println!( + "{}{}{}", + offset_string, + "\\_ ".truecolor(128, 128, 128), + path_in_ref.cyan() + ); + } + + // 空行,渲染完整的文件历史 + println!(); + + // TODO :: +} async fn jv_sheet_list(args: SheetListArgs) { let _ = correct_current_dir(); @@ -3878,8 +3998,44 @@ async fn proc_mapping_edit( } } -async fn jv_share(_args: ShareFileArgs) { - todo!() +async fn jv_share(args: ShareFileArgs) { + // 导入分享模式 + if let (Some(import_id), None, None) = (&args.args1, &args.args2, &args.args3) { + share_accept(import_id).await; + return; + } + + // 拉取模式 + if let (Some(from_sheet), Some(import_pattern), None) = (&args.args1, &args.args2, &args.args3) + { + share_in(from_sheet, import_pattern).await; + return; + } + + // 分享模式 + if let (Some(share_pattern), Some(to_sheet), Some(description)) = + (&args.args1, &args.args2, &args.args3) + { + share_out(share_pattern, to_sheet, description).await; + return; + } + + println!("{}", md(t!("jv.share"))); +} + +async fn share_accept(_import_id: &str) { + // TODO: 实现导入分享逻辑 + eprintln!("share_accept not implemented yet"); +} + +async fn share_in(_from_sheet: &str, _import_pattern: &str) { + // TODO: 实现拉取模式逻辑 + eprintln!("share_in not implemented yet"); +} + +async fn share_out(_share_pattern: &str, _to_sheet: &str, _description: &str) { + // TODO: 实现分享模式逻辑 + eprintln!("share_out not implemented yet"); } async fn jv_account_add(user_dir: UserDirectory, args: AccountAddArgs) { diff --git a/src/bin/jvv.rs b/src/bin/jvv.rs index ee8cae8..54979d5 100644 --- a/src/bin/jvv.rs +++ b/src/bin/jvv.rs @@ -217,11 +217,17 @@ async fn main() { println!( "\n{}", md(t!( - "jvv.version.compile_info", + "jv.version.compile_info", build_time = compile_info.date, build_target = compile_info.target, build_platform = compile_info.platform, - build_toolchain = compile_info.toolchain + build_toolchain = compile_info.toolchain, + cli_build_branch = compile_info.build_branch, + cli_build_commit = + &compile_info.build_commit[..7.min(compile_info.build_commit.len())], + core_build_branch = core_compile_info.build_branch, + core_build_commit = &core_compile_info.build_commit + [..7.min(core_compile_info.build_commit.len())] )) ); } diff --git a/src/data/compile_info.rs.template b/src/data/compile_info.rs.template index 7720d2b..b65055b 100644 --- a/src/data/compile_info.rs.template +++ b/src/data/compile_info.rs.template @@ -5,6 +5,8 @@ pub struct CompileInfo { pub toolchain: String, pub cli_version: String, + pub build_branch: String, + pub build_commit: String, } impl Default for CompileInfo { @@ -15,6 +17,8 @@ impl Default for CompileInfo { platform: "{platform}".to_string(), toolchain: "{toolchain}".to_string(), cli_version: "{version}".to_string(), + build_branch: "{branch}".to_string(), + build_commit: "{commit}".to_string(), } } } |
