-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild.rs
More file actions
75 lines (64 loc) · 2.35 KB
/
Copy pathbuild.rs
File metadata and controls
75 lines (64 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: GPL-3.0-only
use std::process::Command;
fn main() {
// Re-run build script if git HEAD changes
println!("cargo::rerun-if-changed=.git/HEAD");
println!("cargo::rerun-if-changed=.git/refs/tags");
// Check if version is already set (e.g., in flatpak builds)
let version = if let Ok(v) = std::env::var("CAMERA_VERSION") {
v
} else {
get_git_version()
};
println!("cargo::rustc-env=GIT_VERSION={}", version);
}
fn get_git_version() -> String {
// Try to get version from git describe
// This will return:
// - "v0.1.0" if HEAD is exactly at a tag
// - "v0.1.0-5-gabcdef1" if HEAD is 5 commits after v0.1.0
let output = Command::new("git")
.args(["describe", "--tags", "--always", "--match", "v*"])
.output();
let version = match output {
Ok(output) if output.status.success() => {
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
_ => {
// Fallback: try to get just the commit hash
get_commit_hash().unwrap_or_else(|| "unknown".to_string())
}
};
// Strip 'v' prefix if present
let version = version.strip_prefix('v').unwrap_or(&version);
// Get the current commit hash for all builds
let commit_hash = get_commit_hash().unwrap_or_else(|| "unknown".to_string());
// Transform git describe output to our format:
// "0.1.0" (exact tag) becomes "0.1.0-abcdef1"
// "0.1.0-5-gabcdef1" (commits after tag) becomes "0.1.0-dirty-abcdef1"
if version.contains('-') {
// Parse: version-commits-ghash (commits after a tag)
let parts: Vec<&str> = version.rsplitn(3, '-').collect();
if parts.len() >= 3 {
let hash = parts[0].strip_prefix('g').unwrap_or(parts[0]);
let base_version = parts[2];
format!("{}-dirty-{}", base_version, hash)
} else {
version.to_string()
}
} else {
// Exact tag - still append commit hash for traceability
format!("{}-{}", version, commit_hash)
}
}
fn get_commit_hash() -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()?;
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
None
}
}