1- //! Just embed git -hash to `--version`
1+ //! Construct version in the `commit -hash date chanel` format
22
33use std:: { env, path:: PathBuf , process:: Command } ;
44
55fn main ( ) {
66 set_rerun ( ) ;
7-
8- let rev =
9- env:: var ( "RUST_ANALYZER_REV" ) . ok ( ) . or_else ( rev) . unwrap_or_else ( || "???????" . to_string ( ) ) ;
10- println ! ( "cargo:rustc-env=REV={}" , rev)
7+ println ! ( "cargo:rustc-env=REV={}" , rev( ) )
118}
129
1310fn set_rerun ( ) {
@@ -18,29 +15,52 @@ fn set_rerun() {
1815 ) ;
1916
2017 while manifest_dir. parent ( ) . is_some ( ) {
21- if manifest_dir. join ( ".git/HEAD" ) . exists ( ) {
22- let git_dir = manifest_dir. join ( ".git" ) ;
23-
24- println ! ( "cargo:rerun-if-changed={}" , git_dir. join( "HEAD" ) . display( ) ) ;
25- // current branch ref
26- if let Ok ( output) =
27- Command :: new ( "git" ) . args ( & [ "rev-parse" , "--symbolic-full-name" , "HEAD" ] ) . output ( )
28- {
29- if let Ok ( ref_link) = String :: from_utf8 ( output. stdout ) {
30- println ! ( "cargo:rerun-if-changed={}" , git_dir. join( ref_link) . display( ) ) ;
31- }
32- }
18+ let head_ref = manifest_dir. join ( ".git/HEAD" ) ;
19+ if head_ref. exists ( ) {
20+ println ! ( "cargo:rerun-if-changed={}" , head_ref. display( ) ) ;
3321 return ;
3422 }
3523
3624 manifest_dir. pop ( ) ;
3725 }
26+
3827 println ! ( "cargo:warning=Could not find `.git/HEAD` from manifest dir!" ) ;
3928}
4029
41- fn rev ( ) -> Option < String > {
42- let output =
43- Command :: new ( "git" ) . args ( & [ "describe" , "--tags" , "--exclude" , "nightly" ] ) . output ( ) . ok ( ) ?;
30+ fn rev ( ) -> String {
31+ if let Ok ( rev) = env:: var ( "RUST_ANALYZER_REV" ) {
32+ return rev;
33+ }
34+
35+ if let Some ( commit_hash) = commit_hash ( ) {
36+ let mut buf = commit_hash;
37+
38+ if let Some ( date) = build_date ( ) {
39+ buf. push ( ' ' ) ;
40+ buf. push_str ( & date) ;
41+ }
42+
43+ let channel = env:: var ( "RUST_ANALYZER_CHANNEL" ) . unwrap_or_else ( |_| "dev" . to_string ( ) ) ;
44+ buf. push ( ' ' ) ;
45+ buf. push_str ( & channel) ;
46+
47+ return buf;
48+ }
49+
50+ "???????" . to_string ( )
51+ }
52+
53+ fn commit_hash ( ) -> Option < String > {
54+ output_to_string ( "git rev-parse --short HEAD" )
55+ }
56+
57+ fn build_date ( ) -> Option < String > {
58+ output_to_string ( "date --iso --utc" )
59+ }
60+
61+ fn output_to_string ( command : & str ) -> Option < String > {
62+ let args = command. split_ascii_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
63+ let output = Command :: new ( args[ 0 ] ) . args ( & args[ 1 ..] ) . output ( ) . ok ( ) ?;
4464 let stdout = String :: from_utf8 ( output. stdout ) . ok ( ) ?;
45- Some ( stdout)
65+ Some ( stdout. trim ( ) . to_string ( ) )
4666}
0 commit comments