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 8e2b51e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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
- name: Build & Push
Expand Down
60 changes: 58 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand All @@ -64,10 +94,36 @@ 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 {
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()
}
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 8e2b51e

Please sign in to comment.