Skip to content

Commit

Permalink
Use grev crate for embedding git revision information
Browse files Browse the repository at this point in the history
So far we maintained a bunch of bespoke git revision retrieval code in
our build script, in order to display more detailed version information
to the user.
With this change we switch over to using the grev crate for this very
purpose instead.
  • Loading branch information
d-e-s-o committed Dec 18, 2022
1 parent 1c5fe0e commit 1403e88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 93 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "ext/otp_cache.rs"

[build-dependencies]
anyhow = "1.0"
grev = "0.1.1"

[profile.release]
opt-level = "z"
Expand Down
97 changes: 4 additions & 93 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,104 +1,15 @@
// Copyright (C) 2021-2022 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later

use std::process::Command;
use std::io::stdout;

use anyhow::bail;
use anyhow::Context;
use anyhow::Result;

const GIT: &str = "git";

/// Format a git command with the given list of arguments as a string.
fn git_command(args: &[&str]) -> String {
args.iter().fold(GIT.to_string(), |mut cmd, arg| {
cmd += " ";
cmd += arg;
cmd
})
}

/// Run git with the provided arguments and read the output it emits.
fn git_output(args: &[&str]) -> Result<String> {
let git = Command::new(GIT)
.args(args)
.output()
.with_context(|| format!("failed to run `{}`", git_command(args)))?;

if !git.status.success() {
let code = if let Some(code) = git.status.code() {
format!(" ({})", code)
} else {
String::new()
};

bail!(
"`{}` reported non-zero exit-status{}",
git_command(args),
code
);
}

let output = String::from_utf8(git.stdout).with_context(|| {
format!(
"failed to read `{}` output as UTF-8 string",
git_command(args)
)
})?;

Ok(output)
}

/// Run git with the provided arguments and report the status of the
/// command.
fn git_run(args: &[&str]) -> Result<bool> {
Command::new(GIT)
.args(args)
.status()
.with_context(|| format!("failed to run `{}`", git_command(args)))
.map(|status| status.success())
}

/// Retrieve a git revision identifier that either includes the tag we
/// are on or the shortened SHA-1. It also contains an indication
/// whether local changes were present.
fn git_revision() -> Result<Option<String>> {
// As a first step we check whether we are in a git repository and
// whether git is working to begin with. If not, we can't do much; yet
// we still want to allow the build to continue, so we merely print a
// warning and continue without a git revision. But once these checks
// are through, we treat subsequent failures as unexpected and fatal.
match git_run(&["rev-parse", "--git-dir"]) {
Ok(true) => (),
Ok(false) => {
println!("cargo:warning=Not in a git repository; unable to embed git revision");
return Ok(None);
}
Err(err) => {
println!(
"cargo:warning=Failed to invoke `git`; unable to embed git revision: {}",
err
);
return Ok(None);
}
}

let local_changes = git_output(&["status", "--porcelain", "--untracked-files=no"])?;
let modified = !local_changes.is_empty();

// If we are on a tag then just include the tag name. Otherwise use
// the shortened SHA-1.
let revision = if let Ok(tag) = git_output(&["describe", "--exact-match", "--tags", "HEAD"]) {
tag
} else {
git_output(&["rev-parse", "--short", "HEAD"])?
};
let revision = format!("{}{}", revision.trim(), if modified { "+" } else { "" });
Ok(Some(revision))
}
use grev::get_revision as get_git_revision;

fn main() -> Result<()> {
if let Some(git_revision) = git_revision()? {
let directory = env!("CARGO_MANIFEST_DIR");
if let Some(git_revision) = get_git_revision(directory, stdout().lock())? {
println!("cargo:rustc-env=NITROCLI_GIT_REVISION={}", git_revision);
}
// Make sure to run this script again if any of our sources files or
Expand Down

0 comments on commit 1403e88

Please sign in to comment.