44use std:: env;
55use std:: path:: { Path , PathBuf } ;
66
7+ mod build_git;
78mod build_version;
89
910const PROTO_REL : & str = "../../proto" ;
@@ -35,11 +36,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3536 // Docker/CI builds where .git is absent, this silently does nothing and
3637 // the binary falls back to CARGO_PKG_VERSION (which is already sed-patched
3738 // by the build pipeline).
38- for path in git_rerun_paths ( ) {
39- println ! ( "cargo:rerun-if-changed={}" , path. display( ) ) ;
40- }
41-
42- if let Some ( version) = git_version ( ) {
39+ let manifest_dir = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) ?) ;
40+ let source_root = manifest_dir. ancestors ( ) . nth ( 2 ) . unwrap_or ( & manifest_dir) ;
41+ build_git:: emit_rerun_if_changed ( source_root) ;
42+ if let Some ( version) = build_git:: version ( source_root) {
4343 println ! ( "cargo:rustc-env=OPENSHELL_GIT_VERSION={version}" ) ;
4444 }
4545
@@ -59,7 +59,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5959 None
6060 } ;
6161
62- let manifest_dir = PathBuf :: from ( env:: var ( "CARGO_MANIFEST_DIR" ) ?) ;
6362 let proto_root = manifest_dir. join ( PROTO_REL ) ;
6463 let mut proto_files = Vec :: new ( ) ;
6564 collect_proto_files ( & proto_root, & mut proto_files) ?;
@@ -104,74 +103,3 @@ fn collect_proto_files(dir: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()
104103 }
105104 Ok ( ( ) )
106105}
107-
108- /// Git metadata files whose changes affect [`git_version`].
109- ///
110- /// In a linked worktree `.git` is a file, so `HEAD` and `logs/HEAD` live in the
111- /// per-worktree git dir while branch and tag refs live in the common git dir. Cargo treats a
112- /// missing `rerun-if-changed` path as always stale, so only existing paths are
113- /// returned. Without git metadata nothing is returned.
114- fn git_rerun_paths ( ) -> Vec < PathBuf > {
115- let resolve = |args : & [ & str ] | {
116- let dir = PathBuf :: from ( git_output ( args) ?) ;
117- // Relative output is relative to the build script's working directory.
118- Some ( env:: current_dir ( ) . ok ( ) ?. join ( dir) )
119- } ;
120- let mut paths = Vec :: new ( ) ;
121- if let Some ( git_dir) = resolve ( & [ "rev-parse" , "--git-dir" ] ) {
122- paths. push ( git_dir. join ( "HEAD" ) ) ;
123- paths. push ( git_dir. join ( "logs/HEAD" ) ) ;
124- }
125- if let Some ( common_dir) = resolve ( & [ "rev-parse" , "--git-common-dir" ] ) {
126- paths. push ( common_dir. join ( "refs/tags" ) ) ;
127- paths. push ( common_dir. join ( "packed-refs" ) ) ;
128- // A new worktree has no `logs/HEAD` until its first ref update, so also
129- // watch the checked-out branch ref, which moves on every commit.
130- if let Some ( branch) = git_output ( & [ "symbolic-ref" , "-q" , "HEAD" ] ) {
131- paths. push ( common_dir. join ( branch) ) ;
132- }
133- }
134- paths. retain ( |path| path. exists ( ) ) ;
135- paths
136- }
137-
138- /// Derive the release or development version from git metadata.
139- ///
140- /// Implements the "guess-next-dev" convention used by the release pipeline
141- /// (`tasks/scripts/release.py`): exact stable and prerelease tags retain their
142- /// version. Otherwise, the latest merged stable release gets a patch bump and
143- /// `-dev.<N>+g<sha>` is appended.
144- ///
145- /// Examples:
146- /// on tag v0.1.0-pre.1 → "0.1.0-pre.1"
147- /// 3 commits past v0.0.3 → "0.0.4-dev.3+g2bf9969ab"
148- ///
149- /// Returns `None` when git metadata cannot be read.
150- fn git_version ( ) -> Option < String > {
151- let exact_tags = git_output ( & [ "tag" , "--points-at" , "HEAD" ] ) ?;
152- if let Some ( version) = build_version:: exact_release_version ( exact_tags. lines ( ) ) {
153- return Some ( version) ;
154- }
155-
156- let merged_tags = git_output ( & [ "tag" , "--merged" , "HEAD" , "--list" , "v*.*.*" ] ) ?;
157- let latest_tag = build_version:: latest_stable_tag ( merged_tags. lines ( ) ) ;
158- let revision_range = latest_tag
159- . as_deref ( )
160- . map_or_else ( || "HEAD" . to_string ( ) , |tag| format ! ( "{tag}..HEAD" ) ) ;
161- let distance = git_output ( & [ "rev-list" , "--count" , & revision_range] ) ?
162- . parse ( )
163- . ok ( ) ?;
164- let sha = git_output ( & [ "rev-parse" , "--short=9" , "HEAD" ] ) ?;
165-
166- build_version:: next_dev_version ( latest_tag. as_deref ( ) , distance, & sha)
167- }
168-
169- fn git_output ( args : & [ & str ] ) -> Option < String > {
170- let output = std:: process:: Command :: new ( "git" ) . args ( args) . output ( ) . ok ( ) ?;
171- if !output. status . success ( ) {
172- return None ;
173- }
174- String :: from_utf8 ( output. stdout )
175- . ok ( )
176- . map ( |output| output. trim ( ) . to_string ( ) )
177- }
0 commit comments