From cf2540332fed458d374420d3452196797a45e09b Mon Sep 17 00:00:00 2001 From: Pierce Thompson Date: Mon, 25 Mar 2024 23:26:17 -0400 Subject: [PATCH] Add link to exact Git repo at commit/tag --- build.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++- templates/base.html | 2 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 6a5a3d5..a583417 100644 --- a/build.rs +++ b/build.rs @@ -4,6 +4,34 @@ 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.ends_with('/') { + repo_url.push('/'); + } + if repo_url.starts_with("https://github.com/") { + if is_git_tagged() { + repo_url.push_str(&format!("releases/tag/{}", get_git_tag())); + } else { + repo_url.push_str(&format!("commit/{}", 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(); @@ -64,10 +92,33 @@ fn is_git_tagged() -> bool { return var == "true"; } + get_git_tag_info().0 +} + +fn get_git_tag() -> String { + 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(output.stdout).unwrap().trim().to_string(); + + (is_tagged, tag) +} + +fn get_remote_url() -> String { + let remote_url = Command::new("git") + .args(["remote", "get-url", "origin"]) + .output() + .expect("Failed to execute git command"); + + String::from_utf8(remote_url.stdout) + .unwrap() + .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 @@