1 //! Build configuration for Rust's release channels.
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.
10 use std::process::Command;
12 use crate::util::output;
16 #[derive(Clone, Default)]
18 /// This is not a git repository.
21 /// This is a git repository.
22 /// If the info should be used (`ignore_git` is false), this will be
23 /// `Some`, otherwise it will be `None`.
24 Present(Option<Info>),
25 /// This is not a git repostory, but the info can be fetched from the
26 /// `git-commit-info` file.
27 RecordedForTarball(Info),
32 pub commit_date: String,
34 pub short_sha: String,
38 pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
39 // See if this even begins to look like a git dir
40 if !dir.join(".git").exists() {
41 match read_commit_info_file(dir) {
42 Some(info) => return GitInfo::RecordedForTarball(info),
43 None => return GitInfo::Absent,
47 // Make sure git commands work
48 match Command::new("git").arg("rev-parse").current_dir(dir).output() {
49 Ok(ref out) if out.status.success() => {}
50 _ => return GitInfo::Absent,
53 // If we're ignoring the git info, we don't actually need to collect it, just make sure this
54 // was a git repo in the first place.
56 return GitInfo::Present(None);
59 // Ok, let's scrape some info
60 let ver_date = output(
66 .arg("--pretty=format:%cd"),
68 let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
69 let short_ver_hash = output(
70 Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
72 GitInfo::Present(Some(Info {
73 commit_date: ver_date.trim().to_string(),
74 sha: ver_hash.trim().to_string(),
75 short_sha: short_ver_hash.trim().to_string(),
79 pub fn info(&self) -> Option<&Info> {
81 GitInfo::Absent => None,
82 GitInfo::Present(info) => info.as_ref(),
83 GitInfo::RecordedForTarball(info) => Some(info),
87 pub fn sha(&self) -> Option<&str> {
88 self.info().map(|s| &s.sha[..])
91 pub fn sha_short(&self) -> Option<&str> {
92 self.info().map(|s| &s.short_sha[..])
95 pub fn commit_date(&self) -> Option<&str> {
96 self.info().map(|s| &s.commit_date[..])
99 pub fn version(&self, build: &Build, num: &str) -> String {
100 let mut version = build.release(num);
101 if let Some(ref inner) = self.info() {
102 version.push_str(" (");
103 version.push_str(&inner.short_sha);
105 version.push_str(&inner.commit_date);
111 /// Returns whether this directory has a `.git` directory which should be managed by bootstrap.
112 pub fn is_managed_git_subrepository(&self) -> bool {
114 GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
115 GitInfo::Present(_) => true,
119 /// Returns whether this is being built from a tarball.
120 pub fn is_from_tarball(&self) -> bool {
122 GitInfo::Absent | GitInfo::Present(_) => false,
123 GitInfo::RecordedForTarball(_) => true,
128 /// Read the commit information from the `git-commit-info` file given the
130 pub fn read_commit_info_file(root: &Path) -> Option<Info> {
131 if let Ok(contents) = fs::read_to_string(root.join("git-commit-info")) {
132 let mut lines = contents.lines();
133 let sha = lines.next();
134 let short_sha = lines.next();
135 let commit_date = lines.next();
136 let info = match (commit_date, sha, short_sha) {
137 (Some(commit_date), Some(sha), Some(short_sha)) => Info {
138 commit_date: commit_date.to_owned(),
140 short_sha: short_sha.to_owned(),
142 _ => panic!("the `git-comit-info` file is malformed"),
150 /// Write the commit information to the `git-commit-info` file given the project
152 pub fn write_commit_info_file(root: &Path, info: &Info) {
153 let commit_info = format!("{}\n{}\n{}\n", info.sha, info.short_sha, info.commit_date);
154 t!(fs::write(root.join("git-commit-info"), &commit_info));
157 /// Write the commit hash to the `git-commit-hash` file given the project root.
158 pub fn write_commit_hash_file(root: &Path, sha: &str) {
159 t!(fs::write(root.join("git-commit-hash"), sha));