-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Handle non-existent upstream master branches in x fmt
#106415
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "build_helper" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::process::Command; | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub enum CiEnv { | ||
/// Not a CI environment. | ||
None, | ||
/// The Azure Pipelines environment, for Linux (including Docker), Windows, and macOS builds. | ||
AzurePipelines, | ||
/// The GitHub Actions environment, for Linux (including Docker), Windows and macOS builds. | ||
GitHubActions, | ||
} | ||
|
||
impl CiEnv { | ||
/// Obtains the current CI environment. | ||
pub fn current() -> CiEnv { | ||
if std::env::var("TF_BUILD").map_or(false, |e| e == "True") { | ||
CiEnv::AzurePipelines | ||
} else if std::env::var("GITHUB_ACTIONS").map_or(false, |e| e == "true") { | ||
CiEnv::GitHubActions | ||
} else { | ||
CiEnv::None | ||
} | ||
} | ||
|
||
pub fn is_ci() -> bool { | ||
Self::current() != CiEnv::None | ||
} | ||
|
||
/// If in a CI environment, forces the command to run with colors. | ||
pub fn force_coloring_in_ci(self, cmd: &mut Command) { | ||
if self != CiEnv::None { | ||
// Due to use of stamp/docker, the output stream of rustbuild is not | ||
// a TTY in CI, so coloring is by-default turned off. | ||
// The explicit `TERM=xterm` environment is needed for | ||
// `--color always` to actually work. This env var was lost when | ||
// compiling through the Makefile. Very strange. | ||
cmd.env("TERM", "xterm").args(&["--color", "always"]); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::{path::Path, process::Command}; | ||
|
||
/// Finds the remote for rust-lang/rust. | ||
/// For example for these remotes it will return `upstream`. | ||
/// ```text | ||
/// origin https://github.com/Nilstrieb/rust.git (fetch) | ||
/// origin https://github.com/Nilstrieb/rust.git (push) | ||
/// upstream https://github.com/rust-lang/rust (fetch) | ||
/// upstream https://github.com/rust-lang/rust (push) | ||
/// ``` | ||
pub fn get_rust_lang_rust_remote(git_dir: Option<&Path>) -> Result<String, String> { | ||
let mut git = Command::new("git"); | ||
if let Some(git_dir) = git_dir { | ||
git.current_dir(git_dir); | ||
} | ||
git.args(["config", "--local", "--get-regex", "remote\\..*\\.url"]); | ||
|
||
let output = git.output().map_err(|err| format!("{err:?}"))?; | ||
if !output.status.success() { | ||
return Err("failed to execute git config command".to_owned()); | ||
} | ||
|
||
let stdout = String::from_utf8(output.stdout).map_err(|err| format!("{err:?}"))?; | ||
|
||
let rust_lang_remote = stdout | ||
.lines() | ||
.find(|remote| remote.contains("rust-lang")) | ||
.ok_or_else(|| "rust-lang/rust remote not found".to_owned())?; | ||
|
||
let remote_name = | ||
rust_lang_remote.split('.').nth(1).ok_or_else(|| "remote name not found".to_owned())?; | ||
Ok(remote_name.into()) | ||
} | ||
|
||
pub fn rev_exists(rev: &str, git_dir: Option<&Path>) -> Result<bool, String> { | ||
let mut git = Command::new("git"); | ||
if let Some(git_dir) = git_dir { | ||
git.current_dir(git_dir); | ||
} | ||
git.args(["rev-parse", rev]); | ||
let output = git.output().map_err(|err| format!("{err:?}"))?; | ||
|
||
match output.status.code() { | ||
Some(0) => Ok(true), | ||
Some(128) => Ok(false), | ||
None => { | ||
return Err(format!( | ||
"git didn't exit properly: {}", | ||
String::from_utf8(output.stderr).map_err(|err| format!("{err:?}"))? | ||
)); | ||
} | ||
Some(code) => { | ||
return Err(format!( | ||
"git command exited with status code: {code}: {}", | ||
String::from_utf8(output.stderr).map_err(|err| format!("{err:?}"))? | ||
)); | ||
} | ||
} | ||
} | ||
|
||
/// Returns the master branch from which we can take diffs to see changes. | ||
/// This will usually be rust-lang/rust master, but sometimes this might not exist. | ||
/// This could be because the user is updating their forked master branch using the GitHub UI | ||
/// and therefore doesn't need an upstream master branch checked out. | ||
/// We will then fall back to origin/master in the hope that at least this exists. | ||
pub fn updated_master_branch(git_dir: Option<&Path>) -> Result<String, String> { | ||
let upstream_remote = get_rust_lang_rust_remote(git_dir)?; | ||
let upstream_master = format!("{upstream_remote}/master"); | ||
if rev_exists(&upstream_master, git_dir)? { | ||
return Ok(upstream_master); | ||
} | ||
|
||
// We could implement smarter logic here in the future. | ||
Ok("origin/master".into()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod ci; | ||
pub mod git; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've stared at this for quite a while now, but I don't think I get it... why does this push
.
to the path list? We have other error cases below ("could not find usable git"), and they don't do that. Why is this different? I think for./x.py fmt
it is unnecessary since we're anyway searching everything, and for./x.py fmt compiler/
this s actively buggy since it formats not just the files in the compiler dir. Am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're probably right. i don't remember the details of this change though, but this does look sus