Skip to content

add fallback logic to git_upstream_merge_base #137994

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

Closed
Closed
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
72 changes: 47 additions & 25 deletions src/build_helper/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,37 @@ pub fn get_closest_merge_commit(
config: &GitConfig<'_>,
target_paths: &[PathBuf],
) -> Result<String, String> {
let mut git = Command::new("git");
// The need of `--first-parent` is inconsistent. It's sometimes required, sometimes not.
// See https://github.com/rust-lang/rust/issues/101907#issuecomment-2677860377 for more
// context.
//
// FIXME: This is so hacky, is there a more sane way to fix this problem?
let result = match get_closest_merge_commit_impl(git_dir, config, target_paths, false) {
// If no commit is found without `--first-parent` flag, fallback to using it.
Ok(commit) if commit.is_empty() => {
get_closest_merge_commit_impl(git_dir, config, target_paths, true)
}
result => result,
};

if let Some(git_dir) = git_dir {
git.current_dir(git_dir);
}
return result;

fn get_closest_merge_commit_impl(
git_dir: Option<&Path>,
config: &GitConfig<'_>,
target_paths: &[PathBuf],
follow_first_parent_only: bool,
) -> Result<String, String> {
let mut git = Command::new("git");

if let Some(git_dir) = git_dir {
git.current_dir(git_dir);
}

let channel = include_str!("../../ci/channel");
let channel = include_str!("../../ci/channel");

let merge_base = {
if CiEnv::is_ci() &&
let merge_base = {
if CiEnv::is_ci() &&
// FIXME: When running on rust-lang managed CI and it's not a nightly build,
// `git_upstream_merge_base` fails with an error message similar to this:
// ```
Expand All @@ -141,28 +162,29 @@ pub fn get_closest_merge_commit(
// ```
// Investigate and resolve this issue instead of skipping it like this.
(channel == "nightly" || !CiEnv::is_rust_lang_managed_ci_job())
{
git_upstream_merge_base(config, git_dir).unwrap()
} else {
// For non-CI environments, ignore rust-lang/rust upstream as it usually gets
// outdated very quickly.
"HEAD".to_string()
{
git_upstream_merge_base(config, git_dir).unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my comment (#101907 (comment), footnote 1) this never executes in practice in (our) CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? We are running that on every pipeline (unless it's beta/stable channel) as far as I know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Mark explained, channel is "nightly\n", so I don't think we are...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, what the hack... lol

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that is the reason why sometimes CI fails?

} else {
// For non-CI environments, ignore rust-lang/rust upstream as it usually gets
// outdated very quickly.
"HEAD".to_string()
}
};

git.args(["rev-list", &format!("--author={}", config.git_merge_commit_email), "-n1"]);

if follow_first_parent_only {
git.arg("--first-parent");
}
};

git.args([
"rev-list",
&format!("--author={}", config.git_merge_commit_email),
"-n1",
"--first-parent",
&merge_base,
]);
git.arg(merge_base);

if !target_paths.is_empty() {
git.arg("--").args(target_paths);
}
if !target_paths.is_empty() {
git.arg("--").args(target_paths);
}

Ok(output_result(&mut git)?.trim().to_owned())
Ok(output_result(&mut git)?.trim().to_owned())
}
}

/// Returns the files that have been modified in the current branch compared to the master branch.
Expand Down
Loading