Skip to content

Commit

Permalink
exclude upstream check from `build_helper::git::get_closest_merge_com…
Browse files Browse the repository at this point in the history
…mit`

When rust-lang/rust is configured as remote, some of the git logic (for tracking changed files)
that uses `get_closest_merge_commit` starts to produce annoying results as the upstream branch becomes
outdated quickly (since it isn't updated with git pull). We can rely on `HEAD` as we specifically treat
bors commits as merge commits, which also exist on upstream. Therefore, checking the upstream branch
unnecessarily complicates things and also causes incorrect results.

Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Oct 7, 2024
1 parent 11ee3a8 commit f3dc740
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/tools/build_helper/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,10 @@ pub fn updated_master_branch(
Err("Cannot find any suitable upstream master branch".to_owned())
}

/// Finds the nearest merge commit by comparing the local `HEAD` with the upstream branch's state.
/// To work correctly, the upstream remote must be properly configured using `git remote add <name> <url>`.
/// In most cases `get_closest_merge_commit` is the function you are looking for as it doesn't require remote
/// to be configured.
fn git_upstream_merge_base(
config: &GitConfig<'_>,
git_dir: Option<&Path>,
) -> Result<String, String> {
let updated_master = updated_master_branch(config, git_dir)?;
let mut git = Command::new("git");
if let Some(git_dir) = git_dir {
git.current_dir(git_dir);
}
Ok(output_result(git.arg("merge-base").arg(&updated_master).arg("HEAD"))?.trim().to_owned())
}

/// Searches for the nearest merge commit in the repository that also exists upstream.
///
/// If it fails to find the upstream remote, it then looks for the most recent commit made
/// by the merge bot by matching the author's email address with the merge bot's email.
/// It looks for the most recent commit made by the merge bot by matching the author's email
/// address with the merge bot's email.
pub fn get_closest_merge_commit(
git_dir: Option<&Path>,
config: &GitConfig<'_>,
Expand All @@ -127,14 +111,12 @@ pub fn get_closest_merge_commit(
git.current_dir(git_dir);
}

let merge_base = git_upstream_merge_base(config, git_dir).unwrap_or_else(|_| "HEAD".into());

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

if !target_paths.is_empty() {
Expand Down

0 comments on commit f3dc740

Please sign in to comment.