Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't treat git repos as non-existent when ignore_git is set #87443

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions src/bootstrap/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ use build_helper::output;

use crate::Build;

pub struct GitInfo {
inner: Option<Info>,
pub enum GitInfo {
/// This is not a git repository.
Absent,
/// This is a git repository.
/// If the info should be used (`ignore_git` is false), this will be
/// `Some`, otherwise it will be `None`.
Present(Option<Info>),
}

struct Info {
pub struct Info {
commit_date: String,
sha: String,
short_sha: String,
Expand All @@ -25,14 +30,20 @@ struct Info {
impl GitInfo {
pub fn new(ignore_git: bool, dir: &Path) -> GitInfo {
// See if this even begins to look like a git dir
if ignore_git || !dir.join(".git").exists() {
return GitInfo { inner: None };
if !dir.join(".git").exists() {
return GitInfo::Absent;
}

// Make sure git commands work
match Command::new("git").arg("rev-parse").current_dir(dir).output() {
Ok(ref out) if out.status.success() => {}
_ => return GitInfo { inner: None },
_ => return GitInfo::Absent,
}

// If we're ignoring the git info, we don't actually need to collect it, just make sure this
// was a git repo in the first place.
if ignore_git {
return GitInfo::Present(None);
}

// Ok, let's scrape some info
Expand All @@ -48,30 +59,35 @@ impl GitInfo {
let short_ver_hash = output(
Command::new("git").current_dir(dir).arg("rev-parse").arg("--short=9").arg("HEAD"),
);
GitInfo {
inner: Some(Info {
commit_date: ver_date.trim().to_string(),
sha: ver_hash.trim().to_string(),
short_sha: short_ver_hash.trim().to_string(),
}),
GitInfo::Present(Some(Info {
commit_date: ver_date.trim().to_string(),
sha: ver_hash.trim().to_string(),
short_sha: short_ver_hash.trim().to_string(),
}))
}

fn info(&self) -> Option<&Info> {
match self {
GitInfo::Present(info) => info.as_ref(),
GitInfo::Absent => None,
}
}

pub fn sha(&self) -> Option<&str> {
self.inner.as_ref().map(|s| &s.sha[..])
self.info().map(|s| &s.sha[..])
}

pub fn sha_short(&self) -> Option<&str> {
self.inner.as_ref().map(|s| &s.short_sha[..])
self.info().map(|s| &s.short_sha[..])
}

pub fn commit_date(&self) -> Option<&str> {
self.inner.as_ref().map(|s| &s.commit_date[..])
self.info().map(|s| &s.commit_date[..])
}

pub fn version(&self, build: &Build, num: &str) -> String {
let mut version = build.release(num);
if let Some(ref inner) = self.inner {
if let Some(ref inner) = self.info() {
version.push_str(" (");
version.push_str(&inner.short_sha);
version.push(' ');
Expand All @@ -82,6 +98,9 @@ impl GitInfo {
}

pub fn is_git(&self) -> bool {
self.inner.is_some()
match self {
GitInfo::Absent => false,
GitInfo::Present(_) => true,
}
}
}
2 changes: 1 addition & 1 deletion src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ impl Build {
match &self.config.channel[..] {
"stable" => num.to_string(),
"beta" => {
if self.rust_info.is_git() {
if self.rust_info.is_git() && !self.config.ignore_git {
format!("{}-beta.{}", num, self.beta_prerelease_version())
} else {
format!("{}-beta", num)
Expand Down