]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/channel.rs
Rollup merge of #103715 - tshepang:consistency, r=Dylan-DPC
[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::fs;
9 use std::path::Path;
10 use std::process::Command;
11
12 use crate::util::output;
13 use crate::util::t;
14 use crate::Build;
15
16 pub enum GitInfo {
17     /// This is not a git repository.
18     Absent,
19     /// This is a git repository.
20     /// If the info should be used (`ignore_git` is false), this will be
21     /// `Some`, otherwise it will be `None`.
22     Present(Option<Info>),
23     /// This is not a git repostory, but the info can be fetched from the
24     /// `git-commit-info` file.
25     RecordedForTarball(Info),
26 }
27
28 pub struct Info {
29     pub commit_date: String,
30     pub sha: String,
31     pub short_sha: String,
32 }
33
34 impl GitInfo {
35     pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
36         // See if this even begins to look like a git dir
37         if !dir.join(".git").exists() {
38             match read_commit_info_file(dir) {
39                 Some(info) => return GitInfo::RecordedForTarball(info),
40                 None => return GitInfo::Absent,
41             }
42         }
43
44         // Make sure git commands work
45         match Command::new("git").arg("rev-parse").current_dir(dir).output() {
46             Ok(ref out) if out.status.success() => {}
47             _ => return GitInfo::Absent,
48         }
49
50         // If we're ignoring the git info, we don't actually need to collect it, just make sure this
51         // was a git repo in the first place.
52         if ignore_git {
53             return GitInfo::Present(None);
54         }
55
56         // Ok, let's scrape some info
57         let ver_date = output(
58             Command::new("git")
59                 .current_dir(dir)
60                 .arg("log")
61                 .arg("-1")
62                 .arg("--date=short")
63                 .arg("--pretty=format:%cd"),
64         );
65         let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
66         let short_ver_hash = output(
67             Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
68         );
69         GitInfo::Present(Some(Info {
70             commit_date: ver_date.trim().to_string(),
71             sha: ver_hash.trim().to_string(),
72             short_sha: short_ver_hash.trim().to_string(),
73         }))
74     }
75
76     pub fn info(&self) -> Option<&Info> {
77         match self {
78             GitInfo::Absent => None,
79             GitInfo::Present(info) => info.as_ref(),
80             GitInfo::RecordedForTarball(info) => Some(info),
81         }
82     }
83
84     pub fn sha(&self) -> Option<&str> {
85         self.info().map(|s| &s.sha[..])
86     }
87
88     pub fn sha_short(&self) -> Option<&str> {
89         self.info().map(|s| &s.short_sha[..])
90     }
91
92     pub fn commit_date(&self) -> Option<&str> {
93         self.info().map(|s| &s.commit_date[..])
94     }
95
96     pub fn version(&self, build: &Build, num: &str) -> String {
97         let mut version = build.release(num);
98         if let Some(ref inner) = self.info() {
99             version.push_str(" (");
100             version.push_str(&inner.short_sha);
101             version.push(' ');
102             version.push_str(&inner.commit_date);
103             version.push(')');
104         }
105         version
106     }
107
108     /// Returns whether this directory has a `.git` directory which should be managed by bootstrap.
109     pub fn is_managed_git_subrepository(&self) -> bool {
110         match self {
111             GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
112             GitInfo::Present(_) => true,
113         }
114     }
115
116     /// Returns whether this is being built from a tarball.
117     pub fn is_from_tarball(&self) -> bool {
118         match self {
119             GitInfo::Absent | GitInfo::Present(_) => false,
120             GitInfo::RecordedForTarball(_) => true,
121         }
122     }
123 }
124
125 /// Read the commit information from the `git-commit-info` file given the
126 /// project root.
127 pub fn read_commit_info_file(root: &Path) -> Option<Info> {
128     if let Ok(contents) = fs::read_to_string(root.join("git-commit-info")) {
129         let mut lines = contents.lines();
130         let sha = lines.next();
131         let short_sha = lines.next();
132         let commit_date = lines.next();
133         let info = match (commit_date, sha, short_sha) {
134             (Some(commit_date), Some(sha), Some(short_sha)) => Info {
135                 commit_date: commit_date.to_owned(),
136                 sha: sha.to_owned(),
137                 short_sha: short_sha.to_owned(),
138             },
139             _ => panic!("the `git-comit-info` file is malformed"),
140         };
141         Some(info)
142     } else {
143         None
144     }
145 }
146
147 /// Write the commit information to the `git-commit-info` file given the project
148 /// root.
149 pub fn write_commit_info_file(root: &Path, info: &Info) {
150     let commit_info = format!("{}\n{}\n{}\n", info.sha, info.short_sha, info.commit_date);
151     t!(fs::write(root.join("git-commit-info"), &commit_info));
152 }
153
154 /// Write the commit hash to the `git-commit-hash` file given the project root.
155 pub fn write_commit_hash_file(root: &Path, sha: &str) {
156     t!(fs::write(root.join("git-commit-hash"), sha));
157 }