Skip to content

Commit

Permalink
Add link to exact Git repo at commit/tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Insprill committed Mar 26, 2024
1 parent 491a864 commit cf25403
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
53 changes: 52 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<nav>
<div class="inner-nav">
<div class="nav-item">
<a class="site-version" href="{{env!("CARGO_PKG_REPOSITORY")}}">v{{ env!("IN_VERSION") }}</a>
<a class="site-version" href="{{env!("IN_REPO_URL")}}">v{{ env!("IN_VERSION") }}</a>
</div>
<a href="/" class="site-name">Intellectual</a>
<div class="nav-item right">
Expand Down

0 comments on commit cf25403

Please sign in to comment.