-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mononoke: add CommitSyncDataProvider
Summary: As described in D23845720 (5de500b) we are doing a pretty significant change in the CommitSyncer. Previously it stored static Movers and BookmarkRenamers, but it needs to change and they would need to fetch config from LiveCommitSyncConfig. Unfortunately we already have a bunch of tests that create weird movers that would be very hard to model via LiveCommitSyncConfig. So we have 2 options: 1) delete or rewrite all these tests 2) Create a wrapper that would return a mover/bookmark renamers. Option #2 is preferable, and that's what this diff does. In production it would use LiveCommitSyncConfig, but it also let's tests specify whatever weird movers they need. Reviewed By: ikostia Differential Revision: D23909432 fbshipit-source-id: 83fb627812f625e07f7e40044e2f69274cd2d768
- Loading branch information
1 parent
5d6dfac
commit f2a7850
Showing
2 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
eden/mononoke/commit_rewriting/cross_repo_sync/src/commit_sync_data_provider.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This software may be used and distributed according to the terms of the | ||
* GNU General Public License version 2. | ||
*/ | ||
|
||
use anyhow::{anyhow, Error}; | ||
use bookmark_renaming::{get_bookmark_renamers, BookmarkRenamer, BookmarkRenamers}; | ||
use live_commit_sync_config::LiveCommitSyncConfig; | ||
use metaconfig_types::{CommitSyncConfig, CommitSyncConfigVersion, CommitSyncDirection}; | ||
use mononoke_types::RepositoryId; | ||
use movers::{get_movers, Mover, Movers}; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub enum CommitSyncDataProvider { | ||
Live(Arc<dyn LiveCommitSyncConfig>), | ||
Test(HashMap<CommitSyncConfigVersion, SyncData>), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct SyncData { | ||
pub mover: Mover, | ||
pub reverse_mover: Mover, | ||
pub bookmark_renamer: BookmarkRenamer, | ||
pub reverse_bookmark_renamer: BookmarkRenamer, | ||
} | ||
|
||
impl CommitSyncDataProvider { | ||
pub fn get_mover( | ||
&self, | ||
version: &CommitSyncConfigVersion, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<Mover, Error> { | ||
use CommitSyncDataProvider::*; | ||
|
||
match self { | ||
Live(live_commit_sync_config) => { | ||
let commit_sync_config = live_commit_sync_config | ||
.get_commit_sync_config_by_version(source_repo_id, version)?; | ||
|
||
let Movers { mover, .. } = | ||
get_movers_from_config(&commit_sync_config, source_repo_id, target_repo_id)?; | ||
Ok(mover) | ||
} | ||
Test(map) => { | ||
let sync_data = map | ||
.get(version) | ||
.ok_or_else(|| anyhow!("sync data not found for {}", version))?; | ||
Ok(sync_data.mover.clone()) | ||
} | ||
} | ||
} | ||
|
||
pub fn get_reverse_mover( | ||
&self, | ||
version: &CommitSyncConfigVersion, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<Mover, Error> { | ||
use CommitSyncDataProvider::*; | ||
|
||
match self { | ||
Live(live_commit_sync_config) => { | ||
let commit_sync_config = live_commit_sync_config | ||
.get_commit_sync_config_by_version(source_repo_id, version)?; | ||
|
||
let Movers { reverse_mover, .. } = | ||
get_movers_from_config(&commit_sync_config, source_repo_id, target_repo_id)?; | ||
Ok(reverse_mover) | ||
} | ||
Test(map) => { | ||
let sync_data = map | ||
.get(version) | ||
.ok_or_else(|| anyhow!("sync data not found for {}", version))?; | ||
Ok(sync_data.reverse_mover.clone()) | ||
} | ||
} | ||
} | ||
|
||
pub fn get_bookmark_renamer( | ||
&self, | ||
version: &CommitSyncConfigVersion, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<BookmarkRenamer, Error> { | ||
use CommitSyncDataProvider::*; | ||
|
||
match self { | ||
Live(live_commit_sync_config) => { | ||
let commit_sync_config = live_commit_sync_config | ||
.get_commit_sync_config_by_version(source_repo_id, version)?; | ||
|
||
let BookmarkRenamers { | ||
bookmark_renamer, .. | ||
} = get_bookmark_renamers_from_config( | ||
&commit_sync_config, | ||
source_repo_id, | ||
target_repo_id, | ||
)?; | ||
Ok(bookmark_renamer) | ||
} | ||
Test(map) => { | ||
let sync_data = map | ||
.get(version) | ||
.ok_or_else(|| anyhow!("sync data not found for {}", version))?; | ||
Ok(sync_data.bookmark_renamer.clone()) | ||
} | ||
} | ||
} | ||
|
||
pub fn get_reverse_bookmark_renamer( | ||
&self, | ||
version: &CommitSyncConfigVersion, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<BookmarkRenamer, Error> { | ||
use CommitSyncDataProvider::*; | ||
|
||
match self { | ||
Live(live_commit_sync_config) => { | ||
let commit_sync_config = live_commit_sync_config | ||
.get_commit_sync_config_by_version(source_repo_id, version)?; | ||
|
||
let BookmarkRenamers { | ||
reverse_bookmark_renamer, | ||
.. | ||
} = get_bookmark_renamers_from_config( | ||
&commit_sync_config, | ||
source_repo_id, | ||
target_repo_id, | ||
)?; | ||
Ok(reverse_bookmark_renamer) | ||
} | ||
Test(map) => { | ||
let sync_data = map | ||
.get(version) | ||
.ok_or_else(|| anyhow!("sync data not found for {}", version))?; | ||
Ok(sync_data.reverse_bookmark_renamer.clone()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn get_movers_from_config( | ||
commit_sync_config: &CommitSyncConfig, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<Movers, Error> { | ||
let (direction, small_repo_id) = | ||
get_direction_and_small_repo_id(commit_sync_config, source_repo_id, target_repo_id)?; | ||
get_movers(&commit_sync_config, small_repo_id, direction) | ||
} | ||
|
||
fn get_bookmark_renamers_from_config( | ||
commit_sync_config: &CommitSyncConfig, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<BookmarkRenamers, Error> { | ||
let (direction, small_repo_id) = | ||
get_direction_and_small_repo_id(commit_sync_config, source_repo_id, target_repo_id)?; | ||
get_bookmark_renamers(commit_sync_config, small_repo_id, direction) | ||
} | ||
|
||
fn get_direction_and_small_repo_id( | ||
commit_sync_config: &CommitSyncConfig, | ||
source_repo_id: RepositoryId, | ||
target_repo_id: RepositoryId, | ||
) -> Result<(CommitSyncDirection, RepositoryId), Error> { | ||
let small_repo_id = if commit_sync_config.large_repo_id == source_repo_id | ||
&& commit_sync_config.small_repos.contains_key(&target_repo_id) | ||
{ | ||
target_repo_id | ||
} else if commit_sync_config.large_repo_id == target_repo_id | ||
&& commit_sync_config.small_repos.contains_key(&source_repo_id) | ||
{ | ||
source_repo_id | ||
} else { | ||
return Err(anyhow!( | ||
"CommitSyncMapping incompatible with source repo {:?} and target repo {:?}", | ||
source_repo_id, | ||
target_repo_id, | ||
)); | ||
}; | ||
|
||
let direction = if source_repo_id == small_repo_id { | ||
CommitSyncDirection::SmallToLarge | ||
} else { | ||
CommitSyncDirection::LargeToSmall | ||
}; | ||
|
||
Ok((direction, small_repo_id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters