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.
9 use std::process::Command;
11 use build_helper::output;
16 /// This is not a git repository.
18 /// This is a git repository.
19 /// If the info should be used (`ignore_git` is false), this will be
20 /// `Some`, otherwise it will be `None`.
21 Present(Option<Info>),
31 pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
32 // See if this even begins to look like a git dir
33 if !dir.join(".git").exists() {
34 return GitInfo::Absent;
37 // Make sure git commands work
38 match Command::new("git").arg("rev-parse").current_dir(dir).output() {
39 Ok(ref out) if out.status.success() => {}
40 _ => return GitInfo::Absent,
43 // If we're ignoring the git info, we don't actually need to collect it, just make sure this
44 // was a git repo in the first place.
46 return GitInfo::Present(None);
49 // Ok, let's scrape some info
50 let ver_date = output(
56 .arg("--pretty=format:%cd"),
58 let ver_hash = output(Command::new("git").current_dir(dir).arg("rev-parse").arg("HEAD"));
59 let short_ver_hash = output(
60 Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
62 GitInfo::Present(Some(Info {
63 commit_date: ver_date.trim().to_string(),
64 sha: ver_hash.trim().to_string(),
65 short_sha: short_ver_hash.trim().to_string(),
69 fn info(&self) -> Option<&Info> {
71 GitInfo::Present(info) => info.as_ref(),
72 GitInfo::Absent => None,
76 pub fn sha(&self) -> Option<&str> {
77 self.info().map(|s| &s.sha[..])
80 pub fn sha_short(&self) -> Option<&str> {
81 self.info().map(|s| &s.short_sha[..])
84 pub fn commit_date(&self) -> Option<&str> {
85 self.info().map(|s| &s.commit_date[..])
88 pub fn version(&self, build: &Build, num: &str) -> String {
89 let mut version = build.release(num);
90 if let Some(ref inner) = self.info() {
91 version.push_str(" (");
92 version.push_str(&inner.short_sha);
94 version.push_str(&inner.commit_date);
100 pub fn is_git(&self) -> bool {
102 GitInfo::Absent => false,
103 GitInfo::Present(_) => true,