Skip to content

Commit

Permalink
Fix unnecessary rebuilds of mullvad-version
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed May 7, 2024
1 parent 76b10b3 commit 45712ac
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions mullvad-version/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option<String> {
Target::Desktop => product_version.to_owned(),
};

let git_dir = Path::new("..").join(".git");

// If we build our output on information about HEAD we need to re-run if HEAD moves
let head_path = git_dir.join("HEAD");
if head_path.exists() {
println!("cargo:rerun-if-changed={}", head_path.display());
}

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")
.join(current_branch.trim());
if git_current_branch_ref.exists() {
println!(
"cargo:rerun-if-changed={}",
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")?;
Expand All @@ -94,21 +128,6 @@ fn get_dev_suffix(target: Target, product_version: &str) -> Option<String> {
///
/// Returns `None` if executing the `git rev-parse` command fails for some reason.
fn git_rev_parse_commit_hash(git_ref: &str) -> Option<String> {
let git_dir = Path::new("..").join(".git");
// If we build our output on information about HEAD we need to re-run if HEAD moves
if git_ref == "HEAD" {
let head_path = git_dir.join("HEAD");
if head_path.exists() {
println!("cargo:rerun-if-changed={}", head_path.display());
}
}
// 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_refs_dir = git_dir.join("refs");
if git_refs_dir.exists() {
println!("cargo:rerun-if-changed={}", git_refs_dir.display());
}

let output = Command::new("git")
.arg("rev-parse")
.arg(format!("{git_ref}^{{commit}}"))
Expand Down

0 comments on commit 45712ac

Please sign in to comment.