@@ -4,59 +4,48 @@ use std::{env, fs};
4
4
5
5
use toml:: Value ;
6
6
7
- fn current_git_hash ( ) -> Option < String > {
8
- if option_env ! ( "GIT_COMMIT" ) == None {
9
- let commit = Command :: new ( "git" )
10
- . arg ( "log" )
11
- . arg ( "-1" )
12
- . arg ( "--pretty=format:%h" ) // Abbreviated commit hash
13
- . current_dir ( env ! ( "CARGO_MANIFEST_DIR" ) )
14
- . output ( ) ;
15
-
16
- if let Ok ( commit) = commit {
17
- if let Ok ( commit) = String :: from_utf8 ( commit. stdout ) {
18
- return Some ( commit. trim ( ) . to_string ( ) ) ;
19
- }
20
- }
21
- } else {
22
- return option_env ! ( "GIT_COMMIT" ) . map ( String :: from) ;
23
- }
7
+ /// Given a [Command], run it and return the output as a string,
8
+ /// returning `None` if the command fails.
9
+ fn run_git_command ( command : & mut Command ) -> Option < String > {
10
+ command
11
+ . output ( )
12
+ . map ( |output| String :: from_utf8 ( output. stdout ) . ok ( ) )
13
+ . unwrap_or ( None )
14
+ . map ( |s| s. trim ( ) . to_string ( ) )
15
+ }
24
16
25
- None
17
+ fn current_git_hash ( ) -> Option < String > {
18
+ option_env ! ( "GIT_COMMIT" ) . map ( String :: from) . or_else ( || {
19
+ run_git_command (
20
+ Command :: new ( "git" )
21
+ . arg ( "log" )
22
+ . arg ( "-1" )
23
+ . arg ( "--pretty=format:%h" )
24
+ . current_dir ( env ! ( "CARGO_MANIFEST_DIR" ) ) ,
25
+ )
26
+ } )
26
27
}
27
28
28
29
fn current_git_branch ( ) -> Option < String > {
29
- if option_env ! ( "GIT_BRANCH" ) == None {
30
- let commit = Command :: new ( "git" )
31
- . arg ( "rev-parse" )
32
- . arg ( "--abbrev-ref" )
33
- . arg ( "HEAD" )
34
- . output ( ) ;
35
- if let Ok ( commit) = commit {
36
- if let Ok ( commit) = String :: from_utf8 ( commit. stdout ) {
37
- return Some ( commit. trim ( ) . to_string ( ) ) ;
38
- }
39
- }
40
- } else {
41
- return option_env ! ( "GIT_BRANCH" ) . map ( String :: from) ;
42
- }
43
-
44
- None
30
+ option_env ! ( "GIT_BRANCH" ) . map ( String :: from) . or_else ( || {
31
+ run_git_command (
32
+ Command :: new ( "git" )
33
+ . arg ( "rev-parse" )
34
+ . arg ( "--abbrev-ref" )
35
+ . arg ( "HEAD" ) ,
36
+ )
37
+ } )
45
38
}
46
39
47
40
fn is_working_tree_clean ( ) -> bool {
48
- let status = Command :: new ( "git" )
41
+ Command :: new ( "git" )
49
42
. arg ( "diff" )
50
43
. arg ( "--quiet" )
51
44
. arg ( "--exit-code" )
52
45
. current_dir ( env ! ( "CARGO_MANIFEST_DIR" ) )
53
- . status ( ) ;
54
-
55
- if let Ok ( status) = status {
56
- status. code ( ) == Some ( 0 )
57
- } else {
58
- true
59
- }
46
+ . status ( )
47
+ . map ( |status| status. code ( ) == Some ( 0 ) )
48
+ . unwrap_or ( true )
60
49
}
61
50
62
51
fn main ( ) {
@@ -90,40 +79,29 @@ fn main() {
90
79
}
91
80
}
92
81
93
- if let Some ( git) = current_git_hash ( ) {
94
- // println!("git commit: {}", git);
95
- rust_code. push_str ( & format ! (
96
- "pub const GIT_COMMIT: Option<&'static str> = Some(\" {}\" );\n " ,
97
- git
98
- ) ) ;
99
- println ! ( "cargo:rustc-env=GIT_COMMIT={}" , git) ;
100
- } else {
101
- rust_code. push_str ( & format ! (
102
- "pub const GIT_COMMIT: Option<&'static str> = None;\n "
103
- ) ) ;
104
- }
105
- if let Some ( git) = current_git_branch ( ) {
106
- rust_code. push_str ( & format ! (
107
- "pub const GIT_BRANCH: Option<&'static str> = Some(\" {}\" );\n " ,
108
- git
109
- ) ) ;
110
- println ! ( "cargo:rustc-env=GIT_BRANCH={}" , git) ;
111
- } else {
112
- rust_code. push_str ( & format ! (
113
- "pub const GIT_BRANCH: Option<&'static str> = None;\n "
114
- ) ) ;
82
+ let git_commit = current_git_hash ( ) ;
83
+ rust_code. push_str ( & format ! (
84
+ "pub const GIT_COMMIT: Option<&'static str> = {git_commit:?};\n " ,
85
+ ) ) ;
86
+ if let Some ( git_commit) = git_commit {
87
+ println ! ( "cargo:rustc-env=GIT_COMMIT={}" , git_commit) ;
115
88
}
116
- if !is_working_tree_clean ( ) {
117
- rust_code. push_str ( & format ! (
118
- "pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\" \" );\n "
119
- ) ) ;
120
- println ! ( "cargo:rustc-env=GIT_TREE_CLEAN=+" ) ;
121
- } else {
122
- rust_code. push_str ( & format ! (
123
- "pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\" +\" );\n "
124
- ) ) ;
89
+
90
+ let git_branch = current_git_branch ( ) ;
91
+ rust_code. push_str ( & format ! (
92
+ "pub const GIT_BRANCH: Option<&'static str> = {git_branch:?};\n " ,
93
+ ) ) ;
94
+ if let Some ( git_branch) = git_branch {
95
+ println ! ( "cargo:rustc-env=GIT_BRANCH={}" , git_branch) ;
125
96
}
126
97
98
+ let is_clean = if is_working_tree_clean ( ) { "" } else { "+" } ;
99
+ rust_code. push_str ( & format ! (
100
+ "pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\" {}\" );\n " ,
101
+ is_clean
102
+ ) ) ;
103
+ println ! ( "cargo:rustc-env=GIT_TREE_CLEAN={}" , is_clean) ;
104
+
127
105
let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
128
106
let dest_path = Path :: new ( & out_dir) . join ( "versions.rs" ) ;
129
107
fs:: write ( & dest_path, rust_code) . expect ( "Failed to write generated code" ) ;
0 commit comments