Skip to content

Commit

Permalink
Extract rerun-if logic to separate fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed May 3, 2024
1 parent 224ee77 commit e781890
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions mullvad-version/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option<String> {
Target::Desktop => product_version.to_owned(),
};

rerun_if_git_ref_changed(&release_tag)?;

// Get the git commit hashes for the latest release and current HEAD
// Return `None` if unable to find the hash for HEAD.
let head_commit_hash = git_rev_parse_commit_hash("HEAD")?;
let product_version_commit_hash = git_rev_parse_commit_hash(&release_tag);

// If we are currently building the release tag, there is no dev suffix
if Some(&head_commit_hash) == product_version_commit_hash.as_ref() {
return None;
}
Some(format!(
"-dev-{}",
&head_commit_hash[..GIT_HASH_DEV_SUFFIX_LEN]
))
}

/// 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: &String) -> Option<()> {
let git_dir = Path::new("..").join(".git");

// If we build our output on information about HEAD we need to re-run if HEAD moves
Expand All @@ -83,14 +104,14 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option<String> {
println!("cargo:rerun-if-changed={}", head_path.display());
}

// If we build our output on information about the ref of the current branch, we need to re-run
// on changes to it
let output = Command::new("git")
.arg("branch")
.arg("--show-current")
.output()
.ok()?;
let current_branch = String::from_utf8(output.stdout).unwrap();
// If we build our output on information about a git reference, we need to re-run
// if it moves. Instead of trying to be smart, just re-run if any git reference moves.
let git_current_branch_ref = git_dir
.join("refs")
.join("heads")
Expand All @@ -101,27 +122,14 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option<String> {
git_current_branch_ref.display()
);
}
let git_current_branch_ref = git_dir.join("refs").join("tags").join(&release_tag);
if git_current_branch_ref.exists() {
println!(
"cargo:rerun-if-changed={}",
git_current_branch_ref.display()
);
}

// Get the git commit hashes for the latest release and current HEAD
// Return `None` if unable to find the hash for HEAD.
let head_commit_hash = git_rev_parse_commit_hash("HEAD")?;
let product_version_commit_hash = git_rev_parse_commit_hash(&release_tag);

// If we are currently building the release tag, there is no dev suffix
if Some(&head_commit_hash) == product_version_commit_hash.as_ref() {
return None;
}
Some(format!(
"-dev-{}",
&head_commit_hash[..GIT_HASH_DEV_SUFFIX_LEN]
))
// To rebuild in the case where the release tag moves to the current commit, we need to track
// changes to it
let git_release_tag_ref = git_dir.join("refs").join("tags").join(release_tag);
if git_release_tag_ref.exists() {
println!("cargo:rerun-if-changed={}", git_release_tag_ref.display());
};
Some(())
}

/// Returns the commit hash for the commit that `git_ref` is pointing to.
Expand Down

0 comments on commit e781890

Please sign in to comment.