]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/channel.rs
New upstream version 1.61.0+dfsg1
[rustc.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::path::Path;
9 use std::process::Command;
10
11 use crate::util::output;
12 use crate::Build;
13
14 pub enum GitInfo {
15 /// This is not a git repository.
16 Absent,
17 /// This is a git repository.
18 /// If the info should be used (`ignore_git` is false), this will be
19 /// `Some`, otherwise it will be `None`.
20 Present(Option<Info>),
21 }
22
23 pub struct Info {
24 commit_date: String,
25 sha: String,
26 short_sha: String,
27 }
28
29 impl GitInfo {
30 pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
31 // See if this even begins to look like a git dir
32 if !dir.join(".git").exists() {
33 return GitInfo::Absent;
34 }
35
36 // Make sure git commands work
37 match Command::new("git").arg("rev-parse").current_dir(dir).output() {
38 Ok(ref out) if out.status.success() => {}
39 _ => return GitInfo::Absent,
40 }
41
42 // If we're ignoring the git info, we don't actually need to collect it, just make sure this
43 // was a git repo in the first place.
44 if ignore_git {
45 return GitInfo::Present(None);
46 }
47
48 // Ok, let's scrape some info
49 let ver_date = output(
50 Command::new("git")
51 .current_dir(dir)
52 .arg("log")
53 .arg("-1")
54 .arg("--date=short")
55 .arg("--pretty=format:%cd"),
56 );
57 let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
58 let short_ver_hash = output(
59 Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
60 );
61 GitInfo::Present(Some(Info {
62 commit_date: ver_date.trim().to_string(),
63 sha: ver_hash.trim().to_string(),
64 short_sha: short_ver_hash.trim().to_string(),
65 }))
66 }
67
68 fn info(&self) -> Option<&Info> {
69 match self {
70 GitInfo::Present(info) => info.as_ref(),
71 GitInfo::Absent => None,
72 }
73 }
74
75 pub fn sha(&self) -> Option<&str> {
76 self.info().map(|s| &s.sha[..])
77 }
78
79 pub fn sha_short(&self) -> Option<&str> {
80 self.info().map(|s| &s.short_sha[..])
81 }
82
83 pub fn commit_date(&self) -> Option<&str> {
84 self.info().map(|s| &s.commit_date[..])
85 }
86
87 pub fn version(&self, build: &Build, num: &str) -> String {
88 let mut version = build.release(num);
89 if let Some(ref inner) = self.info() {
90 version.push_str(" (");
91 version.push_str(&inner.short_sha);
92 version.push(' ');
93 version.push_str(&inner.commit_date);
94 version.push(')');
95 }
96 version
97 }
98
99 pub fn is_git(&self) -> bool {
100 match self {
101 GitInfo::Absent => false,
102 GitInfo::Present(_) => true,
103 }
104 }
105 }