aboutsummaryrefslogtreecommitdiff
path: root/build.rs
blob: 349f946fe21d2d66ccd8a3cbe001923b1ef05bcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::{fs, path::Path, process::Command};

fn git_directory() -> Option<std::path::PathBuf> {
  let directory = Path::new(".git");

  if directory.is_dir() {
    return Some(directory.to_path_buf());
  }

  let content = fs::read_to_string(directory).ok()?;
  let path = Path::new(content.trim().strip_prefix("gitdir: ")?);

  if path.is_absolute() {
    Some(path.to_path_buf())
  } else {
    Some(directory.parent()?.join(path))
  }
}

fn git_sha() -> Option<String> {
  let output = Command::new("git").args(["rev-parse", "HEAD"]).output().ok()?;

  if !output.status.success() {
    return None;
  }

  let sha = String::from_utf8(output.stdout).ok()?.trim().to_string();

  if sha.is_empty() { None } else { Some(sha) }
}

fn main() {
  if let Some(git_dir) = git_directory() {
    let head_path = git_dir.join("HEAD");

    println!("cargo:rerun-if-changed={}", head_path.display());

    if let Ok(head_contents) = fs::read_to_string(&head_path) {
      if let Some(reference) = head_contents.trim().strip_prefix("ref: ") {
        println!(
          "cargo:rerun-if-changed={}",
          git_dir.join(reference).display()
        );
      }
    }
  }

  let sha = git_sha().unwrap_or_else(|| "UNKNOWN".to_string());

  println!("cargo:rustc-env=VERGEN_GIT_SHA={sha}");
}