@@ -47,16 +47,15 @@ fn compress_man() {
47
47
encoder. finish ( ) . unwrap ( ) ;
48
48
}
49
49
50
- fn commit_info ( ) {
51
- if !Path :: new ( ".git" ) . exists ( ) {
52
- return ;
53
- }
50
+ struct CommitInfo {
51
+ hash : String ,
52
+ short_hash : String ,
53
+ date : String ,
54
+ }
54
55
55
- // Var set by bootstrap whenever omit-git-hash is enabled in rust-lang/rust's config.toml.
56
- println ! ( "cargo:rerun-if-env-changed=CFG_OMIT_GIT_HASH" ) ;
57
- #[ allow( clippy:: disallowed_methods) ]
58
- if std:: env:: var_os ( "CFG_OMIT_GIT_HASH" ) . is_some ( ) {
59
- return ;
56
+ fn commit_info_from_git ( ) -> Option < CommitInfo > {
57
+ if !Path :: new ( ".git" ) . exists ( ) {
58
+ return None ;
60
59
}
61
60
62
61
let output = match Command :: new ( "git" )
@@ -68,14 +67,35 @@ fn commit_info() {
68
67
. output ( )
69
68
{
70
69
Ok ( output) if output. status . success ( ) => output,
71
- _ => return ,
70
+ _ => return None ,
72
71
} ;
72
+
73
73
let stdout = String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
74
- let mut parts = stdout. split_whitespace ( ) ;
75
- let mut next = || parts. next ( ) . unwrap ( ) ;
76
- println ! ( "cargo:rustc-env=CARGO_COMMIT_HASH={}" , next( ) ) ;
77
- println ! ( "cargo:rustc-env=CARGO_COMMIT_SHORT_HASH={}" , next( ) ) ;
78
- println ! ( "cargo:rustc-env=CARGO_COMMIT_DATE={}" , next( ) )
74
+ let mut parts = stdout. split_whitespace ( ) . map ( |s| s. to_string ( ) ) ;
75
+
76
+ Some ( CommitInfo {
77
+ hash : parts. next ( ) ?,
78
+ short_hash : parts. next ( ) ?,
79
+ date : parts. next ( ) ?,
80
+ } )
81
+ }
82
+
83
+ fn commit_info ( ) {
84
+ // Var set by bootstrap whenever omit-git-hash is enabled in rust-lang/rust's config.toml.
85
+ println ! ( "cargo:rerun-if-env-changed=CFG_OMIT_GIT_HASH" ) ;
86
+ // ALLOWED: Accessing environment during build time shouldn't be prohibited.
87
+ #[ allow( clippy:: disallowed_methods) ]
88
+ if std:: env:: var_os ( "CFG_OMIT_GIT_HASH" ) . is_some ( ) {
89
+ return ;
90
+ }
91
+
92
+ let Some ( git) = commit_info_from_git ( ) else {
93
+ return ;
94
+ } ;
95
+
96
+ println ! ( "cargo:rustc-env=CARGO_COMMIT_HASH={}" , git. hash) ;
97
+ println ! ( "cargo:rustc-env=CARGO_COMMIT_SHORT_HASH={}" , git. short_hash) ;
98
+ println ! ( "cargo:rustc-env=CARGO_COMMIT_DATE={}" , git. date) ;
79
99
}
80
100
81
101
#[ allow( clippy:: disallowed_methods) ]
0 commit comments