diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..28bbb74 --- /dev/null +++ b/build.rs @@ -0,0 +1,42 @@ +use std::process::Command; + +fn main() { + // Only re-run if .git state changes + println!("cargo:rerun-if-changed=.git/HEAD"); + println!("cargo:rerun-if-changed=.git/refs/tags"); + + let output = Command::new("git") + .args(["describe", "--tags", "--exact-match"]) + .output(); + + let version = if let Ok(output) = output { + if output.status.success() { + // Exact tag match + String::from_utf8(output.stdout).unwrap().trim().to_string() + } else { + // Not an exact match, construct version string + let tag_output = Command::new("git") + .args(["describe", "--tags", "--abbrev=0"]) + .output() + .expect("Failed to execute git describe"); + + let sha_output = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output() + .expect("Failed to execute git rev-parse"); + + if tag_output.status.success() && sha_output.status.success() { + let tag = String::from_utf8(tag_output.stdout).unwrap().trim().to_string(); + let sha = String::from_utf8(sha_output.stdout).unwrap().trim().to_string(); + format!("{}-g{}", tag, sha) + } else { + // Fallback if git fails or no tags + "unknown".to_string() + } + } + } else { + "unknown".to_string() + }; + + println!("cargo:rustc-env=GIT_VERSION={}", version); +} diff --git a/src/main.rs b/src/main.rs index 99c6263..0112a4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use tracing::{Level, error, info, instrument, warn}; #[derive(Parser, Debug)] #[command(name = "gitea-mirror")] +#[command(version = env!("GIT_VERSION"))] #[command(about = "Syncs Git repositories to Gitea based on a TOML config.")] struct Args { /// Path to the TOML configuration file. diff --git a/vibe_coding_log/session_2026_01_10_version_flag.md b/vibe_coding_log/session_2026_01_10_version_flag.md new file mode 100644 index 0000000..df72ccf --- /dev/null +++ b/vibe_coding_log/session_2026_01_10_version_flag.md @@ -0,0 +1,38 @@ +# Session Log: Add Version Flag + +**Date**: Saturday, January 10, 2026 +**Model**: Gemini (CLI Agent) +**Goal**: Add a `--version` flag that displays the git tag and short SHA. +**Outcome**: Implemented dynamic versioning using `build.rs` and `clap`. + +--- + +## Transcript Summary + +### 1. Version Flag Implementation + +**User**: Requested a `--version` flag that output the latest git tag. If the current commit is not exactly on a tag, it should append the short SHA (e.g., `v0.6-gd8a9fc`). + +**Agent Action**: +1. Analyzed `Cargo.toml` and `src/main.rs`. +2. Created a `build.rs` script to handle the logic: + * Tries `git describe --tags --exact-match`. + * If that fails, tries `git describe --tags --abbrev=0` (latest tag) combined with `git rev-parse --short HEAD` (current SHA). + * Sets the `GIT_VERSION` environment variable. +3. Modified `src/main.rs`: + * Updated the `clap` `Args` struct to use `#[command(version = env!("GIT_VERSION"))]`. +4. Verified the output using `cargo run -- --version`. + +### 2. Verification + +**User**: Implicitly relied on agent verification. + +**Agent Action**: +1. Ran `cargo run -- --version` -> Output: `gitea-mirror v0.6-gf13906d` (correct format). +2. Created a temporary tag `v0.7.0-test`, verified output was `gitea-mirror v0.7.0-test`, then deleted the tag. +3. Ran `cargo clippy` (clean). +4. Committed changes including the new `build.rs` and updated `src/main.rs`. + +## Final State + +The application now supports `gitea-mirror --version` which dynamically reports the git version at build time.