]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/channel.rs
remove pat2021
[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
15 pub struct GitInfo {
16     inner: Option<Info>,
17 }
18
19 struct Info {
20     commit_date: String,
21     sha: String,
22     short_sha: String,
23 }
24
25 impl GitInfo {
26     pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
27         // See if this even begins to look like a git dir
28         if ignore_git || !dir.join(".git").exists() {
29             return GitInfo { inner: None };
30         }
31
32         // Make sure git commands work
33         match Command::new("git").arg("rev-parse").current_dir(dir).output() {
34             Ok(ref out) if out.status.success() => {}
35             _ => return GitInfo { inner: None },
36         }
37
38         // Ok, let's scrape some info
39         let ver_date = output(
40             Command::new("git")
41                 .current_dir(dir)
42                 .arg("log")
43                 .arg("-1")
44                 .arg("--date=short")
45                 .arg("--pretty=format:%cd"),
46         );
47         let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
48         let short_ver_hash = output(
49             Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
50         );
51         GitInfo {
52             inner: Some(Info {
53                 commit_date: ver_date.trim().to_string(),
54                 sha: ver_hash.trim().to_string(),
55                 short_sha: short_ver_hash.trim().to_string(),
56             }),
57         }
58     }
59
60     pub fn sha(&self) -> Option<&str> {
61         self.inner.as_ref().map(|s| &s.sha[..])
62     }
63
64     pub fn sha_short(&self) -> Option<&str> {
65         self.inner.as_ref().map(|s| &s.short_sha[..])
66     }
67
68     pub fn commit_date(&self) -> Option<&str> {
69         self.inner.as_ref().map(|s| &s.commit_date[..])
70     }
71
72     pub fn version(&self, build: &Build, num: &str) -> String {
73         let mut version = build.release(num);
74         if let Some(ref inner) = self.inner {
75             version.push_str(" (");
76             version.push_str(&inner.short_sha);
77             version.push(' ');
78             version.push_str(&inner.commit_date);
79             version.push(')');
80         }
81         version
82     }
83
84     pub fn is_git(&self) -> bool {
85         self.inner.is_some()
86     }
87 }