Skip to content

build_helper: handle emails containing square brackets #140675

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env::VarError;
use std::{panic, thread};

use build_helper::ci::CiEnv;
use build_helper::git::GitConfig;
use build_helper::stage0_parser::parse_stage0_file;
use llvm::prebuilt_llvm_config;

Expand Down Expand Up @@ -267,6 +269,35 @@ fn ci_rustc_if_unchanged_invalidate_on_library_changes_in_ci() {
});
}

// this test is similar to the other two `ci_rustc_if_unchanged_` tests below but overrides
// `merge_bot_email`. it uses the lower level `build_helper::git` API because
// `parse_config_download_rustc_at` is hard-coded to use the `git_merge_commit_email` value in
// the git-tracked file `/src/stage0`
#[test]
fn ci_rustc_with_square_bracket_in_author_email() {
git_test(|ctx| {
let author_email = "bors[bot]@example.com";
ctx.merge_bot_email = format!("Merge bot <{}>", author_email);
ctx.write("src/ci/channel", "nightly");
ctx.commit();

let sha = ctx.create_upstream_merge(&["compiler/bar"]);

let git_config = GitConfig {
git_repository: &ctx.git_repo,
nightly_branch: &ctx.nightly_branch,
git_merge_commit_email: author_email,
};
let got = build_helper::git::get_closest_upstream_commit(
Some(ctx.get_path()),
&git_config,
CiEnv::None,
);

assert_eq!(got, Ok(Some(sha)));
});
}

#[test]
fn ci_rustc_if_unchanged_do_not_invalidate_on_library_changes_outside_ci() {
git_test(|ctx| {
Expand Down
20 changes: 12 additions & 8 deletions src/build_helper/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ pub struct GitConfig<'a> {
pub git_merge_commit_email: &'a str,
}

impl GitConfig<'_> {
// prepares `git_merge_commit_email` before it gets passed to `git rev-list`'s `--author` flag
//
// the `--author` flag takes a "pattern" (regular expression) not a substring so any
// square bracket in the original email needs to be escaped
fn author_email(&self) -> String {
self.git_merge_commit_email.replace('[', "\\[").replace(']', "\\]")
}
}

/// Runs a command and returns the output
pub fn output_result(cmd: &mut Command) -> Result<String, String> {
let output = match cmd.stderr(Stdio::inherit()).output() {
Expand Down Expand Up @@ -183,7 +193,7 @@ fn get_latest_upstream_commit_that_modified_files(
"-n1",
&upstream,
"--author",
git_config.git_merge_commit_email,
&git_config.author_email(),
]);

if !target_paths.is_empty() {
Expand Down Expand Up @@ -227,13 +237,7 @@ fn get_closest_upstream_commit(
// chronologically recent bors commit.
// Here we assume that none of our subtrees use bors anymore, and that all their old bors
// commits are way older than recent rustc bors commits!
git.args([
"rev-list",
"--author-date-order",
&format!("--author={}", config.git_merge_commit_email),
"-n1",
&base,
]);
git.args(["rev-list", "--author-date-order", "--author", &config.author_email(), "-n1", &base]);

let output = output_result(&mut git)?.trim().to_owned();
if output.is_empty() { Ok(None) } else { Ok(Some(output)) }
Expand Down
Loading