@@ -26,6 +26,7 @@ extern crate toml;
2626extern crate url;
2727
2828use std:: env;
29+ use std:: fmt;
2930use std:: io;
3031use rustc_serialize:: { Decodable , Encodable } ;
3132use rustc_serialize:: json;
@@ -49,6 +50,62 @@ pub mod ops;
4950pub mod sources;
5051pub mod util;
5152
53+ pub struct CommitInfo {
54+ pub short_commit_hash : String ,
55+ pub commit_hash : String ,
56+ pub commit_date : String ,
57+ }
58+
59+ pub struct CfgInfo {
60+ // Information about the git repository we may have been built from.
61+ pub commit_info : Option < CommitInfo > ,
62+ // The date that the build was performed.
63+ pub build_date : String ,
64+ // The release channel we were built for.
65+ pub release_channel : String ,
66+ }
67+
68+ pub struct VersionInfo {
69+ pub major : String ,
70+ pub minor : String ,
71+ pub patch : String ,
72+ pub pre_release : Option < String > ,
73+ // Information that's only available when we were built with
74+ // configure/make, rather than cargo itself.
75+ pub cfg_info : Option < CfgInfo > ,
76+ }
77+
78+ impl fmt:: Display for VersionInfo {
79+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
80+ write ! ( f, "cargo-{}.{}.{}" ,
81+ self . major, self . minor, self . patch) ?;
82+ match self . cfg_info . as_ref ( ) . map ( |ci| & ci. release_channel ) {
83+ Some ( channel) => {
84+ if channel != "stable" {
85+ write ! ( f, "-{}" , channel) ?;
86+ let empty = String :: from ( "" ) ;
87+ write ! ( f, "{}" , self . pre_release. as_ref( ) . unwrap_or( & empty) ) ?;
88+ }
89+ } ,
90+ None => ( ) ,
91+ } ;
92+
93+ if let Some ( ref cfg) = self . cfg_info {
94+ match cfg. commit_info {
95+ Some ( ref ci) => {
96+ write ! ( f, " ({} {})" ,
97+ ci. short_commit_hash, ci. commit_date) ?;
98+ } ,
99+ None => {
100+ write ! ( f, " (built {})" ,
101+ cfg. build_date) ?;
102+ }
103+ }
104+ } ;
105+ Ok ( ( ) )
106+ }
107+ }
108+
52109pub fn execute_main_without_stdin < T , V > (
53110 exec : fn ( T , & Config ) -> CliResult < Option < V > > ,
54111 options_first : bool ,
@@ -201,15 +258,47 @@ fn handle_cause(mut cargo_err: &CargoError, shell: &mut MultiShell) -> bool {
201258 }
202259}
203260
204- pub fn version ( ) -> String {
205- format ! ( "cargo {}" , match option_env!( "CFG_VERSION" ) {
206- Some ( s) => s. to_string( ) ,
207- None => format!( "{}.{}.{}{}" ,
208- env!( "CARGO_PKG_VERSION_MAJOR" ) ,
209- env!( "CARGO_PKG_VERSION_MINOR" ) ,
210- env!( "CARGO_PKG_VERSION_PATCH" ) ,
211- option_env!( "CARGO_PKG_VERSION_PRE" ) . unwrap_or( "" ) )
212- } )
261+ pub fn version ( ) -> VersionInfo {
262+ macro_rules! env_str {
263+ ( $name: expr) => { env!( $name) . to_string( ) }
264+ }
265+ macro_rules! option_env_str {
266+ ( $name: expr) => { option_env!( $name) . map( |s| s. to_string( ) ) }
267+ }
268+ match option_env ! ( "CFG_RELEASE_CHANNEL" ) {
269+ // We have environment variables set up from configure/make.
270+ Some ( _) => {
271+ let commit_info =
272+ option_env ! ( "CFG_COMMIT_HASH" ) . map ( |s| {
273+ CommitInfo {
274+ commit_hash : s. to_string ( ) ,
275+ short_commit_hash : option_env_str ! ( "CFG_SHORT_COMMIT_HASH" ) . unwrap ( ) ,
276+ commit_date : option_env_str ! ( "CFG_COMMIT_DATE" ) . unwrap ( ) ,
277+ }
278+ } ) ;
279+ VersionInfo {
280+ major : option_env_str ! ( "CFG_VERSION_MAJOR" ) . unwrap ( ) ,
281+ minor : option_env_str ! ( "CFG_VERSION_MINOR" ) . unwrap ( ) ,
282+ patch : option_env_str ! ( "CFG_VERSION_PATCH" ) . unwrap ( ) ,
283+ pre_release : option_env_str ! ( "CFG_PRERELEASE_VERSION" ) ,
284+ cfg_info : Some ( CfgInfo {
285+ build_date : option_env_str ! ( "CFG_BUILD_DATE" ) . unwrap ( ) ,
286+ release_channel : option_env_str ! ( "CFG_RELEASE_CHANNEL" ) . unwrap ( ) ,
287+ commit_info : commit_info,
288+ } ) ,
289+ }
290+ } ,
291+ // We are being compiled by Cargo itself.
292+ None => {
293+ VersionInfo {
294+ major : env_str ! ( "CARGO_PKG_VERSION_MAJOR" ) ,
295+ minor : env_str ! ( "CARGO_PKG_VERSION_MINOR" ) ,
296+ patch : env_str ! ( "CARGO_PKG_VERSION_PATCH" ) ,
297+ pre_release : option_env_str ! ( "CARGO_PKG_VERSION_PRE" ) ,
298+ cfg_info : None ,
299+ }
300+ }
301+ }
213302}
214303
215304fn flags_from_args < T > ( usage : & str , args : & [ String ] , options_first : bool ) -> CliResult < T >
0 commit comments