diff --git a/mullvad-version/build.rs b/mullvad-version/build.rs index 008ec8d104e1..b120f5b6b1f0 100644 --- a/mullvad-version/build.rs +++ b/mullvad-version/build.rs @@ -75,6 +75,27 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option { 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 @@ -83,14 +104,14 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option { 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") @@ -101,27 +122,14 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option { 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.