Skip to content

Commit

Permalink
Compute git hash at buildtime
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Mar 29, 2024
1 parent 5df8a91 commit 100a225
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 108 deletions.
86 changes: 0 additions & 86 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "interclip-server"
version = "0.1.0"
edition = "2021"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -15,7 +16,6 @@ tokio = { version = "1", features = ["full"] }
async-lock = "2.4"
log = "0.4"
fern = "0.5"
git2 = "0.16.1"
dotenv = "0.15.0"
diesel = { version = "2.1.4", features = ["postgres", "chrono"] }
regex = "1.10"
Expand Down
25 changes: 25 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;

fn main() {
// Get the directory where the output will be placed.
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("git_commit.rs");
let mut f = File::create(&dest_path).unwrap();

// Use git to get the current commit hash.
let git_output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.expect("Failed to execute git command");

let git_hash = String::from_utf8_lossy(&git_output.stdout).trim().to_string();

// Write the git hash to a file as a constant.
writeln!(f, "pub const GIT_COMMIT: &str = \"{}\";", git_hash).unwrap();
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
}
25 changes: 4 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ use utils::db;
use crate::utils::rate_limit::RateLimiter;
use crate::utils::structs::{APIResponse, APIStatus};

use git2::Repository;

use aws_sdk_s3::Client;

extern crate rand;
Expand All @@ -40,6 +38,7 @@ extern crate rocket;
extern crate fern;
extern crate log;

include!(concat!(env!("OUT_DIR"), "/git_commit.rs"));
#[derive(rocket::FromForm, serde::Deserialize)]
#[serde(crate = "rocket::serde")]
struct UploadQuery {
Expand Down Expand Up @@ -340,25 +339,9 @@ struct Version {

#[get("/version")]
fn version(_rate_limiter: RateLimiter) -> Json<Version> {
let repo = Repository::discover(".");
let commit = match repo {
Ok(r) => {
let head = r.head();
match head {
Ok(reference) => {
let peeling = reference.peel_to_commit();
match peeling {
Ok(commit) => Some(format!("{}", commit.id())),
Err(_) => None,
}
}
Err(_) => None,
}
}
Err(_) => None,
};

Json(Version { commit })
Json(Version {
commit: Some(GIT_COMMIT.to_string()),
})
}

#[launch]
Expand Down

0 comments on commit 100a225

Please sign in to comment.