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 #[derive(Clone, Default)]
17 pub enum GitInfo {
18 /// This is not a git repository.
19 #[default]
20 Absent,
21 /// This is a git repository.
22 /// If the info should be used (`omit_git_hash` is false), this will be
23 /// `Some`, otherwise it will be `None`.
24 Present(Option<Info>),
25 /// This is not a git repository, but the info can be fetched from the
26 /// `git-commit-info` file.
27 RecordedForTarball(Info),
28 }
29
30 #[derive(Clone)]
31 pub struct Info {
32 pub commit_date: String,
33 pub sha: String,
34 pub short_sha: String,
35 }
36
37 impl GitInfo {
new(omit_git_hash: bool, dir: &Path) -> GitInfo38 pub fn new(omit_git_hash: 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,
44 }
45 }
46
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,
51 }
52
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.
55 if omit_git_hash {
56 return GitInfo::Present(None);
57 }
58
59 // Ok, let's scrape some info
60 let ver_date = output(
61 Command::new("git")
62 .current_dir(dir)
63 .arg("log")
64 .arg("-1")
65 .arg("--date=short")
66 .arg("--pretty=format:%cd"),
67 );
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"),
71 );
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(),
76 }))
77 }
78
info(&self) -> Option<&Info>79 pub fn info(&self) -> Option<&Info> {
80 match self {
81 GitInfo::Absent => None,
82 GitInfo::Present(info) => info.as_ref(),
83 GitInfo::RecordedForTarball(info) => Some(info),
84 }
85 }
86
sha(&self) -> Option<&str>87 pub fn sha(&self) -> Option<&str> {
88 self.info().map(|s| &s.sha[..])
89 }
90
sha_short(&self) -> Option<&str>91 pub fn sha_short(&self) -> Option<&str> {
92 self.info().map(|s| &s.short_sha[..])
93 }
94
commit_date(&self) -> Option<&str>95 pub fn commit_date(&self) -> Option<&str> {
96 self.info().map(|s| &s.commit_date[..])
97 }
98
version(&self, build: &Build, num: &str) -> String99 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);
104 version.push(' ');
105 version.push_str(&inner.commit_date);
106 version.push(')');
107 }
108 version
109 }
110
111 /// Returns whether this directory has a `.git` directory which should be managed by bootstrap.
is_managed_git_subrepository(&self) -> bool112 pub fn is_managed_git_subrepository(&self) -> bool {
113 match self {
114 GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
115 GitInfo::Present(_) => true,
116 }
117 }
118
119 /// Returns whether this is being built from a tarball.
is_from_tarball(&self) -> bool120 pub fn is_from_tarball(&self) -> bool {
121 match self {
122 GitInfo::Absent | GitInfo::Present(_) => false,
123 GitInfo::RecordedForTarball(_) => true,
124 }
125 }
126 }
127
128 /// Read the commit information from the `git-commit-info` file given the
129 /// project root.
read_commit_info_file(root: &Path) -> Option<Info>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(),
139 sha: sha.to_owned(),
140 short_sha: short_sha.to_owned(),
141 },
142 _ => panic!("the `git-commit-info` file is malformed"),
143 };
144 Some(info)
145 } else {
146 None
147 }
148 }
149
150 /// Write the commit information to the `git-commit-info` file given the project
151 /// root.
write_commit_info_file(root: &Path, info: &Info)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));
155 }
156
157 /// Write the commit hash to the `git-commit-hash` file given the project root.
write_commit_hash_file(root: &Path, sha: &str)158 pub fn write_commit_hash_file(root: &Path, sha: &str) {
159 t!(fs::write(root.join("git-commit-hash"), sha));
160 }
161