]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/channel.rs
aa683161b6547fe5a47f102c9b6eb1078998bda4
[rust.git] / src / bootstrap / channel.rs
1 //! Build configuration for Rust's release channels.
2 //!
3 //! Implements the stable/beta/nightly channel distinctions by setting various
4 //! flags like the `unstable_features`, calculating variables like `release` and
5 //! `package_vers`, and otherwise indicating to the compiler what it should
6 //! print out as part of its version information.
7
8 use std::path::Path;
9 use std::process::Command;
10
11 use build_helper::output;
12
13 use crate::Build;
14 use crate::config::Config;
15
16 // The version number
17 pub const CFG_RELEASE_NUM: &str = "1.35.0";
18
19 pub struct GitInfo {
20     inner: Option<Info>,
21 }
22
23 struct Info {
24     commit_date: String,
25     sha: String,
26     short_sha: String,
27 }
28
29 impl GitInfo {
30     pub fn new(config: &Config, dir: &Path) -> GitInfo {
31         // See if this even begins to look like a git dir
32         if config.ignore_git || !dir.join(".git").exists() {
33             return GitInfo { inner: None }
34         }
35
36         // Make sure git commands work
37         let out = Command::new("git")
38                           .arg("rev-parse")
39                           .current_dir(dir)
40                           .output()
41                           .expect("failed to spawn git");
42         if !out.status.success() {
43             return GitInfo { inner: None }
44         }
45
46         // Ok, let's scrape some info
47         let ver_date = output(Command::new("git").current_dir(dir)
48                                       .arg("log").arg("-1")
49                                       .arg("--date=short")
50                                       .arg("--pretty=format:%cd"));
51         let ver_hash = output(Command::new("git").current_dir(dir)
52                                       .arg("rev-parse").arg("HEAD"));
53         let short_ver_hash = output(Command::new("git")
54                                             .current_dir(dir)
55                                             .arg("rev-parse")
56                                             .arg("--short=9")
57                                             .arg("HEAD"));
58         GitInfo {
59             inner: Some(Info {
60                 commit_date: ver_date.trim().to_string(),
61                 sha: ver_hash.trim().to_string(),
62                 short_sha: short_ver_hash.trim().to_string(),
63             }),
64         }
65     }
66
67     pub fn sha(&self) -> Option<&str> {
68         self.inner.as_ref().map(|s| &s.sha[..])
69     }
70
71     pub fn sha_short(&self) -> Option<&str> {
72         self.inner.as_ref().map(|s| &s.short_sha[..])
73     }
74
75     pub fn commit_date(&self) -> Option<&str> {
76         self.inner.as_ref().map(|s| &s.commit_date[..])
77     }
78
79     pub fn version(&self, build: &Build, num: &str) -> String {
80         let mut version = build.release(num);
81         if let Some(ref inner) = self.inner {
82             version.push_str(" (");
83             version.push_str(&inner.short_sha);
84             version.push_str(" ");
85             version.push_str(&inner.commit_date);
86             version.push_str(")");
87         }
88         version
89     }
90
91     pub fn is_git(&self) -> bool {
92         self.inner.is_some()
93     }
94 }