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;
17 /// This is not a git repository.
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),
29 pub commit_date: String,
31 pub short_sha: String,
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,
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,
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.
53 return GitInfo::Present(None);
56 // Ok, let's scrape some info
57 let ver_date = output(
63 .arg("--pretty=format:%cd"),
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"),
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(),
76 pub fn info(&self) -> Option<&Info> {
78 GitInfo::Absent => None,
79 GitInfo::Present(info) => info.as_ref(),
80 GitInfo::RecordedForTarball(info) => Some(info),
84 pub fn sha(&self) -> Option<&str> {
85 self.info().map(|s| &s.sha[..])
88 pub fn sha_short(&self) -> Option<&str> {
89 self.info().map(|s| &s.short_sha[..])
92 pub fn commit_date(&self) -> Option<&str> {
93 self.info().map(|s| &s.commit_date[..])
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);
102 version.push_str(&inner.commit_date);
108 /// Returns whether this directory has a `.git` directory which should be managed by bootstrap.
109 pub fn is_managed_git_subrepository(&self) -> bool {
111 GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
112 GitInfo::Present(_) => true,
116 /// Returns whether this is being built from a tarball.
117 pub fn is_from_tarball(&self) -> bool {
119 GitInfo::Absent | GitInfo::Present(_) => false,
120 GitInfo::RecordedForTarball(_) => true,
125 /// Read the commit information from the `git-commit-info` file given the
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(),
137 short_sha: short_sha.to_owned(),
139 _ => panic!("the `git-comit-info` file is malformed"),
147 /// Write the commit information to the `git-commit-info` file given the project
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));
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));