Skip to content

Commit

Permalink
document the new git logic in more detail
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Sep 11, 2024
1 parent 0a7f9e2 commit 5f32717
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/tools/build_helper/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ pub fn updated_master_branch(
Err("Cannot find any suitable upstream master branch".to_owned())
}

fn get_git_merge_base(config: &GitConfig<'_>, git_dir: Option<&Path>) -> Result<String, String> {
/// 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 {
Expand All @@ -105,9 +112,10 @@ fn get_git_merge_base(config: &GitConfig<'_>, git_dir: Option<&Path>) -> Result<
Ok(output_result(git.arg("merge-base").arg(&updated_master).arg("HEAD"))?.trim().to_owned())
}

/// Resolves the closest merge commit by the given `author` and `target_paths`.
/// Searches for the nearest merge commit in the repository that also exists upstream.
///
/// If it fails to find the commit from upstream using `git merge-base`, fallbacks to HEAD.
/// 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.
pub fn get_closest_merge_commit(
git_dir: Option<&Path>,
config: &GitConfig<'_>,
Expand All @@ -119,7 +127,7 @@ pub fn get_closest_merge_commit(
git.current_dir(git_dir);
}

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

git.args([
"rev-list",
Expand Down
7 changes: 6 additions & 1 deletion src/tools/compiletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ pub fn parse_config(args: Vec<String>) -> Config {
.optopt("", "edition", "default Rust edition", "EDITION")
.reqopt("", "git-repository", "name of the git repository", "ORG/REPO")
.reqopt("", "nightly-branch", "name of the git branch for nightly", "BRANCH")
.reqopt("", "git-merge-commit-email", "email address used for the merge commits", "EMAIL");
.reqopt(
"",
"git-merge-commit-email",
"email address used for finding merge commits",
"EMAIL",
);

let (argv0, args_) = args.split_first().unwrap();
if args.len() == 1 || args[1] == "-h" || args[1] == "--help" {
Expand Down

0 comments on commit 5f32717

Please sign in to comment.