diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 929a722..4e63a3e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -44,7 +44,9 @@ jobs: echo "IN_IS_GIT=true" >> .env echo "IN_GIT_HASH=$(git rev-parse --short HEAD)" >> .env echo "IN_GIT_DIRTY=$([[ -n $(git status --porcelain) ]] && echo true || echo false)" >> .env + echo "IN_GIT_TAG=$(git describe --tags --exact-match)" >> .env echo "IN_GIT_TAGGED=$(git describe --tags --exact-match > /dev/null 2>&1; [ $? -eq 0 ] && echo "true" || echo "false")" >> .env + echo "IN_GIT_REMOTE_URL=$(git remote get-url origin)" >> .env - name: Build & Push uses: docker/build-push-action@v5 diff --git a/build.rs b/build.rs index 6a5a3d5..9b8991a 100644 --- a/build.rs +++ b/build.rs @@ -4,6 +4,36 @@ use std::env; use std::process::Command; fn main() { + set_version(); + set_repo_url(); +} + +fn set_repo_url() { + let cargo_repo_url = env!("CARGO_PKG_REPOSITORY"); + let mut repo_url = cargo_repo_url.to_string(); + + if is_git_repo() { + repo_url = get_remote_url(); + if repo_url.starts_with("https://github.com/") { + // The URL might already end with a '/', but GitHub seems to handle it fine if there's two. + // Tested in both Firefox and Chromium. + repo_url.push_str(&format!( + "/tree/{}", + if is_git_tagged() { + get_git_tag() + } else { + get_git_hash() + } + )); + } + } else { + println!("Not a Git repo! Skipping repo URL metadata"); + } + + println!("cargo:rustc-env=IN_REPO_URL={}", repo_url); +} + +fn set_version() { let cargo_version = env!("CARGO_PKG_VERSION"); let mut version = cargo_version.to_string(); @@ -43,7 +73,7 @@ fn get_git_hash() -> String { .output() .expect("Failed to execute git command"); - String::from_utf8(output.stdout).unwrap().trim().to_string() + String::from_utf8_lossy(&output.stdout).trim().to_string() } fn is_repo_dirty() -> bool { @@ -64,10 +94,40 @@ fn is_git_tagged() -> bool { return var == "true"; } + get_git_tag_info().0 +} + +fn get_git_tag() -> String { + if let Ok(var) = env::var("IN_GIT_TAG") { + return var; + } + + get_git_tag_info().1 +} + +fn get_git_tag_info() -> (bool, String) { let output = Command::new("git") .args(["describe", "--tags", "--exact-match"]) .output() .expect("Failed to execute git describe command"); - output.status.success() + let is_tagged = output.status.success(); + let tag = String::from_utf8_lossy(&output.stdout).trim().to_string(); + + (is_tagged, tag) +} + +fn get_remote_url() -> String { + if let Ok(var) = env::var("IN_GIT_REMOTE_URL") { + return var; + } + + let remote_url = Command::new("git") + .args(["remote", "get-url", "origin"]) + .output() + .expect("Failed to execute git command"); + + String::from_utf8_lossy(&remote_url.stdout) + .trim() + .to_string() } diff --git a/templates/base.html b/templates/base.html index f66a864..283bcd1 100644 --- a/templates/base.html +++ b/templates/base.html @@ -19,7 +19,7 @@