Skip to content

Commit ee18c39

Browse files
committed
add fallback logic to git_upstream_merge_base
Signed-off-by: onur-ozkan <[email protected]>
1 parent fd17dea commit ee18c39

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

Diff for: src/build_helper/src/git.rs

+47-25
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,37 @@ pub fn get_closest_merge_commit(
123123
config: &GitConfig<'_>,
124124
target_paths: &[PathBuf],
125125
) -> Result<String, String> {
126-
let mut git = Command::new("git");
126+
// The need of `--first-parent` is inconsistent. It's sometimes required, sometimes not.
127+
// See https://github.com/rust-lang/rust/issues/101907#issuecomment-2677860377 for more
128+
// context.
129+
//
130+
// FIXME: This is so hacky, is there a more sane way to fix this problem?
131+
let result = match get_closest_merge_commit_impl(git_dir, config, target_paths, false) {
132+
// If no commit is found without `--first-parent` flag, fallback to using it.
133+
Ok(commit) if commit.is_empty() => {
134+
get_closest_merge_commit_impl(git_dir, config, target_paths, true)
135+
}
136+
result => result,
137+
};
127138

128-
if let Some(git_dir) = git_dir {
129-
git.current_dir(git_dir);
130-
}
139+
return result;
140+
141+
fn get_closest_merge_commit_impl(
142+
git_dir: Option<&Path>,
143+
config: &GitConfig<'_>,
144+
target_paths: &[PathBuf],
145+
follow_first_parent_only: bool,
146+
) -> Result<String, String> {
147+
let mut git = Command::new("git");
148+
149+
if let Some(git_dir) = git_dir {
150+
git.current_dir(git_dir);
151+
}
131152

132-
let channel = include_str!("../../ci/channel");
153+
let channel = include_str!("../../ci/channel");
133154

134-
let merge_base = {
135-
if CiEnv::is_ci() &&
155+
let merge_base = {
156+
if CiEnv::is_ci() &&
136157
// FIXME: When running on rust-lang managed CI and it's not a nightly build,
137158
// `git_upstream_merge_base` fails with an error message similar to this:
138159
// ```
@@ -141,28 +162,29 @@ pub fn get_closest_merge_commit(
141162
// ```
142163
// Investigate and resolve this issue instead of skipping it like this.
143164
(channel == "nightly" || !CiEnv::is_rust_lang_managed_ci_job())
144-
{
145-
git_upstream_merge_base(config, git_dir).unwrap()
146-
} else {
147-
// For non-CI environments, ignore rust-lang/rust upstream as it usually gets
148-
// outdated very quickly.
149-
"HEAD".to_string()
165+
{
166+
git_upstream_merge_base(config, git_dir).unwrap()
167+
} else {
168+
// For non-CI environments, ignore rust-lang/rust upstream as it usually gets
169+
// outdated very quickly.
170+
"HEAD".to_string()
171+
}
172+
};
173+
174+
git.args(["rev-list", &format!("--author={}", config.git_merge_commit_email), "-n1"]);
175+
176+
if follow_first_parent_only {
177+
git.arg("--first-parent");
150178
}
151-
};
152179

153-
git.args([
154-
"rev-list",
155-
&format!("--author={}", config.git_merge_commit_email),
156-
"-n1",
157-
"--first-parent",
158-
&merge_base,
159-
]);
180+
git.arg(merge_base);
160181

161-
if !target_paths.is_empty() {
162-
git.arg("--").args(target_paths);
163-
}
182+
if !target_paths.is_empty() {
183+
git.arg("--").args(target_paths);
184+
}
164185

165-
Ok(output_result(&mut git)?.trim().to_owned())
186+
Ok(output_result(&mut git)?.trim().to_owned())
187+
}
166188
}
167189

168190
/// Returns the files that have been modified in the current branch compared to the master branch.

0 commit comments

Comments
 (0)