fn sync_aws_sdk_with_smithy_rs()

in tools/smithy-rs-sync/src/main.rs [87:175]


fn sync_aws_sdk_with_smithy_rs(
    smithy_rs: &Path,
    aws_sdk: &Path,
    sdk_examples: &Path,
    branch: &str,
    max_commits_to_sync: usize,
) -> Result<()> {
    let aws_sdk = resolve_git_repo("aws-sdk-rust", aws_sdk)?;
    let smithy_rs = resolve_git_repo("smithy-rs", smithy_rs)?;
    let sdk_examples = resolve_git_repo("aws-doc-sdk-examples", sdk_examples)?;

    // Rebase aws-sdk-rust's target branch on top of main
    rebase_on_main(&aws_sdk, branch).context(here!())?;

    // Open the repositories we'll be working with
    let smithy_rs_repo = Repository::open(&smithy_rs).context("couldn't open smithy-rs repo")?;

    // Check repo that we're going to be moving the code into to see what commit it was last synced with
    let last_synced_commit =
        get_last_synced_commit(&aws_sdk).context("couldn't get last synced commit")?;
    let commit_revs = commits_to_be_applied(&smithy_rs_repo, &last_synced_commit)
        .context("couldn't build list of commits that need to be synced")?;

    if commit_revs.is_empty() {
        eprintln!("There are no new commits to be applied, have a nice day.");
        return Ok(());
    }

    let total_number_of_commits = commit_revs.len();
    let number_of_commits_to_sync = max_commits_to_sync.min(total_number_of_commits);
    eprintln!(
        "Syncing {} of {} un-synced commit(s)...",
        number_of_commits_to_sync, total_number_of_commits
    );

    // Run through all the new commits, syncing them one by one
    for (i, rev) in commit_revs.iter().enumerate().take(max_commits_to_sync) {
        let commit = smithy_rs_repo
            .find_commit(*rev)
            .with_context(|| format!("couldn't find commit {} in smithy-rs", rev))?;

        eprintln!(
            "[{}/{}]\tsyncing {}...",
            i + 1,
            number_of_commits_to_sync,
            rev
        );
        checkout_commit_to_sync_from(&smithy_rs_repo, &commit).with_context(|| {
            format!(
                "couldn't checkout commit {} from smithy-rs that we needed for syncing",
                rev,
            )
        })?;

        let build_artifacts = build_sdk(&sdk_examples, &smithy_rs).context("couldn't build SDK")?;
        clean_out_existing_sdk(&aws_sdk)
            .context("couldn't clean out existing SDK from aws-sdk-rust")?;

        // Check that we aren't generating any files that we've marked as "handwritten"
        let handwritten_files_in_generated_sdk_folder =
            find_handwritten_files_and_folders(&aws_sdk, &build_artifacts)?;
        if !handwritten_files_in_generated_sdk_folder.is_empty() {
            bail!(
                "found one or more 'handwritten' files/folders in generated code: {:#?}\nhint: if this file is newly generated, remove it from .handwritten",
                handwritten_files_in_generated_sdk_folder
            );
        }

        copy_sdk(&build_artifacts, &aws_sdk)?;
        create_mirror_commit(&aws_sdk, &commit)
            .context("couldn't commit SDK changes to aws-sdk-rust")?;
    }

    eprintln!(
        "Successfully synced {} mirror commit(s) to aws-sdk-rust/{}. Don't forget to push them",
        number_of_commits_to_sync, branch
    );

    let number_of_un_synced_commits =
        total_number_of_commits.saturating_sub(number_of_commits_to_sync);
    if number_of_un_synced_commits != 0 {
        eprintln!(
            "At least {} commits still need to be synced, please run the tool again",
            number_of_un_synced_commits
        );
    }

    Ok(())
}