@@ -12,10 +12,10 @@ use crate::core::shell::Verbosity::Verbose;
12
12
use crate :: core:: Shell ;
13
13
use anyhow:: Error ;
14
14
use log:: debug;
15
- use std:: fmt;
16
15
17
16
pub use crate :: util:: errors:: { InternalError , VerboseError } ;
18
17
pub use crate :: util:: { indented_lines, CargoResult , CliError , CliResult , Config } ;
18
+ pub use crate :: version:: version;
19
19
20
20
pub const CARGO_ENV : & str = "CARGO" ;
21
21
@@ -26,49 +26,7 @@ pub mod core;
26
26
pub mod ops;
27
27
pub mod sources;
28
28
pub mod util;
29
-
30
- pub struct CommitInfo {
31
- pub short_commit_hash : String ,
32
- pub commit_hash : String ,
33
- pub commit_date : String ,
34
- }
35
-
36
- pub struct CfgInfo {
37
- // Information about the Git repository we may have been built from.
38
- pub commit_info : Option < CommitInfo > ,
39
- // The release channel we were built for.
40
- pub release_channel : String ,
41
- }
42
-
43
- pub struct VersionInfo {
44
- pub major : u8 ,
45
- pub minor : u8 ,
46
- pub patch : u8 ,
47
- pub pre_release : Option < String > ,
48
- // Information that's only available when we were built with
49
- // configure/make, rather than Cargo itself.
50
- pub cfg_info : Option < CfgInfo > ,
51
- }
52
-
53
- impl fmt:: Display for VersionInfo {
54
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
55
- write ! ( f, "{}.{}.{}" , self . major, self . minor, self . patch) ?;
56
- if let Some ( channel) = self . cfg_info . as_ref ( ) . map ( |ci| & ci. release_channel ) {
57
- if channel != "stable" {
58
- write ! ( f, "-{}" , channel) ?;
59
- let empty = String :: new ( ) ;
60
- write ! ( f, "{}" , self . pre_release. as_ref( ) . unwrap_or( & empty) ) ?;
61
- }
62
- } ;
63
-
64
- if let Some ( ref cfg) = self . cfg_info {
65
- if let Some ( ref ci) = cfg. commit_info {
66
- write ! ( f, " ({} {})" , ci. short_commit_hash, ci. commit_date) ?;
67
- }
68
- } ;
69
- Ok ( ( ) )
70
- }
71
- }
29
+ mod version;
72
30
73
31
pub fn exit_with_error ( err : CliError , shell : & mut Shell ) -> ! {
74
32
debug ! ( "exit_with_error; err={:?}" , err) ;
@@ -143,58 +101,3 @@ fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
143
101
}
144
102
false
145
103
}
146
-
147
- pub fn version ( ) -> VersionInfo {
148
- macro_rules! option_env_str {
149
- ( $name: expr) => {
150
- option_env!( $name) . map( |s| s. to_string( ) )
151
- } ;
152
- }
153
-
154
- // So this is pretty horrible...
155
- // There are two versions at play here:
156
- // - version of cargo-the-binary, which you see when you type `cargo --version`
157
- // - version of cargo-the-library, which you download from crates.io for use
158
- // in your packages.
159
- //
160
- // We want to make the `binary` version the same as the corresponding Rust/rustc release.
161
- // At the same time, we want to keep the library version at `0.x`, because Cargo as
162
- // a library is (and probably will always be) unstable.
163
- //
164
- // Historically, Cargo used the same version number for both the binary and the library.
165
- // Specifically, rustc 1.x.z was paired with cargo 0.x+1.w.
166
- // We continue to use this scheme for the library, but transform it to 1.x.w for the purposes
167
- // of `cargo --version`.
168
- let major = 1 ;
169
- let minor = env ! ( "CARGO_PKG_VERSION_MINOR" ) . parse :: < u8 > ( ) . unwrap ( ) - 1 ;
170
- let patch = env ! ( "CARGO_PKG_VERSION_PATCH" ) . parse :: < u8 > ( ) . unwrap ( ) ;
171
-
172
- match option_env ! ( "CFG_RELEASE_CHANNEL" ) {
173
- // We have environment variables set up from configure/make.
174
- Some ( _) => {
175
- let commit_info = option_env ! ( "CFG_COMMIT_HASH" ) . map ( |s| CommitInfo {
176
- commit_hash : s. to_string ( ) ,
177
- short_commit_hash : option_env_str ! ( "CFG_SHORT_COMMIT_HASH" ) . unwrap ( ) ,
178
- commit_date : option_env_str ! ( "CFG_COMMIT_DATE" ) . unwrap ( ) ,
179
- } ) ;
180
- VersionInfo {
181
- major,
182
- minor,
183
- patch,
184
- pre_release : option_env_str ! ( "CARGO_PKG_VERSION_PRE" ) ,
185
- cfg_info : Some ( CfgInfo {
186
- release_channel : option_env_str ! ( "CFG_RELEASE_CHANNEL" ) . unwrap ( ) ,
187
- commit_info,
188
- } ) ,
189
- }
190
- }
191
- // We are being compiled by Cargo itself.
192
- None => VersionInfo {
193
- major,
194
- minor,
195
- patch,
196
- pre_release : option_env_str ! ( "CARGO_PKG_VERSION_PRE" ) ,
197
- cfg_info : None ,
198
- } ,
199
- }
200
- }
0 commit comments