bootstrap/utils/
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
8use std::fs;
9use std::path::Path;
10
11use super::helpers;
12use crate::Build;
13use crate::utils::helpers::{start_process, t};
14
15#[derive(Clone, Default)]
16pub enum GitInfo {
17    /// This is not a git repository.
18    #[default]
19    Absent,
20    /// This is a git repository.
21    /// If the info should be used (`omit_git_hash` is false), this will be
22    /// `Some`, otherwise it will be `None`.
23    Present(Option<Info>),
24    /// This is not a git repository, but the info can be fetched from the
25    /// `git-commit-info` file.
26    RecordedForTarball(Info),
27}
28
29#[derive(Clone)]
30pub struct Info {
31    pub commit_date: String,
32    pub sha: String,
33    pub short_sha: String,
34}
35
36impl GitInfo {
37    pub fn new(omit_git_hash: bool, dir: &Path) -> GitInfo {
38        // See if this even begins to look like a git dir
39        if !dir.join(".git").exists() {
40            match read_commit_info_file(dir) {
41                Some(info) => return GitInfo::RecordedForTarball(info),
42                None => return GitInfo::Absent,
43            }
44        }
45
46        // Make sure git commands work
47        match helpers::git(Some(dir)).arg("rev-parse").as_command_mut().output() {
48            Ok(ref out) if out.status.success() => {}
49            _ => return GitInfo::Absent,
50        }
51
52        // If we're ignoring the git info, we don't actually need to collect it, just make sure this
53        // was a git repo in the first place.
54        if omit_git_hash {
55            return GitInfo::Present(None);
56        }
57
58        // Ok, let's scrape some info
59        let ver_date = start_process(
60            helpers::git(Some(dir))
61                .arg("log")
62                .arg("-1")
63                .arg("--date=short")
64                .arg("--pretty=format:%cd")
65                .as_command_mut(),
66        );
67        let ver_hash =
68            start_process(helpers::git(Some(dir)).arg("rev-parse").arg("HEAD").as_command_mut());
69        let short_ver_hash = start_process(
70            helpers::git(Some(dir)).arg("rev-parse").arg("--short=9").arg("HEAD").as_command_mut(),
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
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
87    pub fn sha(&self) -> Option<&str> {
88        self.info().map(|s| &s.sha[..])
89    }
90
91    pub fn sha_short(&self) -> Option<&str> {
92        self.info().map(|s| &s.short_sha[..])
93    }
94
95    pub fn commit_date(&self) -> Option<&str> {
96        self.info().map(|s| &s.commit_date[..])
97    }
98
99    pub fn version(&self, build: &Build, num: &str) -> String {
100        let mut version = build.release(num);
101        if let Some(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.
112    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.
120    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.
130pub 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.
152pub 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.
158pub fn write_commit_hash_file(root: &Path, sha: &str) {
159    t!(fs::write(root.join("git-commit-hash"), sha));
160}