Small CommitSyncRepos refactoring

Summary:
Small refactoring to simplify the logic in `CommitSyncRepos::new` and also reuse code later.
Also simplified an if-else block in commit_syncer

Reviewed By: mitrandir77

Differential Revision: D56826691

fbshipit-source-id: 5c24538d5c1bc5079d6cd0e88113afb11077e600
This commit is contained in:
Gustavo Galvao Avena 2024-05-03 14:51:13 -07:00 committed by Facebook GitHub Bot
parent 909c27f37a
commit b17c1dd182
3 changed files with 69 additions and 45 deletions

View File

@ -719,7 +719,7 @@ where
.commit_graph()
.changeset_parents(ctx, ancestor)
.await?;
if parents.is_empty() {
let expected_version = if parents.is_empty() {
let version = self
.get_version_for_syncing_commit_with_no_parent(
ctx,
@ -731,24 +731,19 @@ where
format_err!("failed to sync ancestor {} of {}", ancestor, source_cs_id)
})?;
self.unsafe_sync_commit_impl(
ctx,
ancestor,
ancestor_selection_hint.clone(),
commit_sync_context,
Some(version),
)
.await?;
Some(version)
} else {
None
};
self.unsafe_sync_commit_impl(
ctx,
ancestor,
ancestor_selection_hint.clone(),
commit_sync_context,
None,
expected_version,
)
.await?;
}
Ok(())
};
let xrepo_disable_commit_sync_lease =

View File

@ -584,38 +584,21 @@ impl<R: Repo> CommitSyncRepos<R> {
submodule_deps: SubmoduleDeps<R>,
common_commit_sync_config: &CommonCommitSyncConfig,
) -> Result<Self, Error> {
let small_repo_id = if common_commit_sync_config.large_repo_id
== source_repo.repo_identity().id()
&& common_commit_sync_config
.small_repos
.contains_key(&target_repo.repo_identity().id())
{
target_repo.repo_identity().id()
} else if common_commit_sync_config.large_repo_id == target_repo.repo_identity().id()
&& common_commit_sync_config
.small_repos
.contains_key(&source_repo.repo_identity().id())
{
source_repo.repo_identity().id()
} else {
return Err(format_err!(
"CommitSyncMapping incompatible with source repo {:?} and target repo {:?}",
source_repo.repo_identity().id(),
target_repo.repo_identity().id()
));
};
if source_repo.repo_identity().id() == small_repo_id {
Ok(CommitSyncRepos::SmallToLarge {
let sync_direction = commit_sync_direction_from_config(
&source_repo,
&target_repo,
common_commit_sync_config,
)?;
match sync_direction {
CommitSyncDirection::SmallToLarge => Ok(CommitSyncRepos::SmallToLarge {
large_repo: target_repo,
small_repo: source_repo,
submodule_deps,
})
} else {
Ok(CommitSyncRepos::LargeToSmall {
}),
CommitSyncDirection::LargeToSmall => Ok(CommitSyncRepos::LargeToSmall {
large_repo: source_repo,
small_repo: target_repo,
})
}),
}
}
@ -673,6 +656,50 @@ impl<R: Repo> CommitSyncRepos<R> {
}
}
/// Get the direction of the sync based on the common commit sync config.
/// Forward sync -> SmallToLarge
/// Backsync -> LargeToSmall
pub fn commit_sync_direction_from_config<R: Repo>(
source_repo: &R,
target_repo: &R,
common_commit_sync_config: &CommonCommitSyncConfig,
) -> Result<CommitSyncDirection> {
let is_small_repo = |repo: &R| {
common_commit_sync_config
.small_repos
.contains_key(&repo.repo_identity().id())
};
if common_commit_sync_config.large_repo_id == source_repo.repo_identity().id()
&& is_small_repo(target_repo)
{
Ok(CommitSyncDirection::LargeToSmall)
} else if common_commit_sync_config.large_repo_id == target_repo.repo_identity().id()
&& is_small_repo(source_repo)
{
Ok(CommitSyncDirection::SmallToLarge)
} else {
Err(format_err!(
"CommitSyncMapping incompatible with source repo {:?} and target repo {:?}",
source_repo.repo_identity().id(),
target_repo.repo_identity().id()
))
}
}
pub fn get_small_and_large_repos<'a, R: Repo>(
source_repo: &'a R,
target_repo: &'a R,
common_commit_sync_config: &'a CommonCommitSyncConfig,
) -> Result<(&'a R, &'a R)> {
let sync_direction =
commit_sync_direction_from_config(source_repo, target_repo, common_commit_sync_config)?;
match sync_direction {
CommitSyncDirection::SmallToLarge => Ok((source_repo, target_repo)),
CommitSyncDirection::LargeToSmall => Ok((target_repo, source_repo)),
}
}
pub fn create_commit_syncer_lease(
fb: FacebookInit,
caching: Caching,

View File

@ -35,10 +35,12 @@ pub use commit_sync_outcome::CommitSyncOutcome;
pub use commit_sync_outcome::CommitSyncOutcome::*;
pub use commit_sync_outcome::PluralCommitSyncOutcome;
pub use commit_syncer::CommitSyncer;
pub use commit_syncers_lib::commit_sync_direction_from_config;
pub use commit_syncers_lib::create_commit_syncer_lease;
pub use commit_syncers_lib::create_commit_syncers;
pub use commit_syncers_lib::find_toposorted_unsynced_ancestors;
pub use commit_syncers_lib::find_toposorted_unsynced_ancestors_with_commit_graph;
pub use commit_syncers_lib::get_small_and_large_repos;
pub use commit_syncers_lib::get_version_and_parent_map_for_sync_via_pushrebase;
pub use commit_syncers_lib::get_x_repo_submodule_metadata_file_prefx_from_config;
pub use commit_syncers_lib::rewrite_commit;