Skip to content

Commit

Permalink
Add check git binary and .git folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed May 13, 2024
1 parent fd8647f commit e452385
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions mullvad-version/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ fn compute_product_version(target: Target) -> String {

// Rerun this build script on changes to the git ref that affects the build version.
// NOTE: This must be kept up to date with the behavior of `git_rev_parse_commit_hash`.
rerun_if_git_ref_changed(&release_tag)
.expect("Failed to set 'cargo:rerun-if-changed' on git ref changes");
// If an error is returned, we are not in a repository and a proper release version cannot be
// built.
if let Err(()) = rerun_if_git_ref_changed(&release_tag) {
return format!("{release_tag}-dev-local");
};

// Get the git commit hashes for the latest release and current HEAD
let head_commit_hash = git_rev_parse_commit_hash("HEAD");
Expand All @@ -87,9 +90,21 @@ fn compute_product_version(target: Target) -> String {
/// Trigger rebuild of `mullvad-version` on changing branch (`.git/HEAD`), on changes to the ref of
/// the current branch (`.git/refs/heads/$current_branch`) and on changes to the ref of the current
/// release tag (`.git/refs/tags/$current_release_tag`).
fn rerun_if_git_ref_changed(release_tag: &str) -> std::io::Result<()> {
///
/// Returns an error if not in a git repository, or the git binary is not in `PATH`.
fn rerun_if_git_ref_changed(release_tag: &str) -> Result<(), ()> {
let git_dir = Path::new("..").join(".git");

if !git_dir.exists() {
return Err(());
}

// Check that `git` can be executed
match Command::new("git").arg("--version").status() {
Ok(status) if status.success() => {}
_ => return Err(()),
};

// The `.git/HEAD` file contains the position of the current head. If in 'detached HEAD' state,
// this will be the ref of the current commit. If on a branch it will just point to it, e.g.
// `ref: refs/heads/main`. Tracking changes to this file will tell us if we change branch, or
Expand All @@ -104,7 +119,8 @@ fn rerun_if_git_ref_changed(release_tag: &str) -> std::io::Result<()> {
let output = Command::new("git")
.arg("branch")
.arg("--show-current")
.output()?;
.output()
.expect("Failed to execute `git branch --show-current`");

let current_branch = String::from_utf8(output.stdout).unwrap();
let current_branch = current_branch.trim();
Expand Down

0 comments on commit e452385

Please sign in to comment.