Skip to content

Commit

Permalink
Merge pull request #868 from Michael-F-Bryan/git-version
Browse files Browse the repository at this point in the history
Embed the git commit in fj-app's version number
  • Loading branch information
hannobraun authored Jul 28, 2022
2 parents 00a79d1 + d80dbd5 commit 016d5d2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Continuous Deployment

on:
push:
branches: [ main ]
branches: [main]

env:
CARGO_TERM_COLOR: always
Expand All @@ -11,6 +11,10 @@ env:
# used to rename and upload the binaries
PROJ_NAME: fj-app

# This lets our app know it's an "official" release. Otherwise we would get
# a version number like "fj-app 0.8.0 (8cb928bb, unreleased)"
FJ_OFFICIAL_RELEASE: 1

defaults:
run:
shell: bash
Expand Down
81 changes: 81 additions & 0 deletions crates/fj-app/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::{
path::PathBuf,
process::{Command, Output, Stdio},
};

fn main() {
println!("cargo:rustc-env=FJ_VERSION_STRING={}", version_string());
}

fn version_string() -> String {
let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap();
let commit = git_description();

let official_release =
std::env::var("FJ_OFFICIAL_RELEASE").as_deref() == Ok("1");
println!("cargo:rerun-if-env-changed=FJ_OFFICIAL_RELEASE");

match (commit, official_release) {
(Some(commit), true) => format!("{pkg_version} ({commit})"),
(Some(commit), false) => {
format!("{pkg_version} ({commit}, unreleased)")
}
(None, true) => pkg_version,
(None, false) => format!("{pkg_version} (unreleased)"),
}
}

/// Try to get the current git commit.
///
/// This may fail if `git` isn't installed (unlikely) or if the `.git/` folder
/// isn't accessible (more likely than you think). This typically happens when
/// we're building just the `fj-app` crate in a Docker container or when
/// someone is installing from crates.io via `cargo install`.
fn git_description() -> Option<String> {
let crate_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let mut cmd = Command::new("git");
cmd.args(["describe", "--always", "--dirty=-modified"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.current_dir(&crate_dir);

let Output {
status,
stdout,
stderr,
} = cmd.output().ok()?;

let stdout = String::from_utf8_lossy(&stdout);

if !status.success() {
// Not sure if anyone will ever see this, but it could be helpful for
// troubleshooting.
eprintln!("Command failed: {cmd:?}");
let stderr = String::from_utf8_lossy(&stderr);
eprintln!("---- Stdout ----");
eprintln!("{stdout}");
eprintln!("---- Stderr ----");
eprintln!("{stderr}");
return None;
}

// Make sure we re-run whenever the current commit changes
let project_root = crate_dir.ancestors().nth(2).unwrap();
let head_file = project_root.join(".git").join("HEAD");
println!("cargo:rerun-if-changed={}", head_file.display());

if let Ok(contents) = std::fs::read_to_string(&head_file) {
// Most of the time the HEAD file will be `ref: refs/heads/$branch`, but
// when it's a detached head we'll only get the commit hash and can skip
// the rerun-if-changed logic.

if let Some((_, branch)) = contents.split_once(' ') {
let commit_hash_file =
project_root.join(".git").join(branch.trim());
println!("cargo:rerun-if-changed={}", commit_hash_file.display());
}
}

Some(stdout.trim().to_string())
}
1 change: 1 addition & 0 deletions crates/fj-app/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use fj_math::Scalar;

/// Fornjot - Experimental CAD System
#[derive(clap::Parser)]
#[clap(version = env!("FJ_VERSION_STRING"))]
pub struct Args {
/// The model to open
#[clap(short, long)]
Expand Down

0 comments on commit 016d5d2

Please sign in to comment.