From 5ef5d217f1ab210be96eecdca0de60731279699e Mon Sep 17 00:00:00 2001 From: Gustavo Galvao Avena Date: Fri, 31 Jan 2025 08:55:19 -0800 Subject: [PATCH] Extract ResultingChangesetArgs into common module Summary: ## This stack Migrate all commands from megarepo tool CLI to mononoke admin `megarepo` subcommand, so they use mononoke_app + clap 4, instad of old cmdlib + clap-2. I'll try to do one diff per command. ## This diff Many commands call a `add_resulting_commit_args` function (or variant) to add arguments for a changeset being created. In this diff I extract those into a common module, so they can be easily reused. Reviewed By: liubov-dmitrieva Differential Revision: D68951264 fbshipit-source-id: 361512dcf10bbd6d9f6f81fffc46861bd88605c7 --- .../tools/admin/src/commands/megarepo.rs | 1 + .../admin/src/commands/megarepo/common.rs | 53 +++++++++++++++++++ .../admin/src/commands/megarepo/merge.rs | 50 +++-------------- 3 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 eden/mononoke/tools/admin/src/commands/megarepo/common.rs diff --git a/eden/mononoke/tools/admin/src/commands/megarepo.rs b/eden/mononoke/tools/admin/src/commands/megarepo.rs index 69be8b7a5aecf..01d44bc6360cd 100644 --- a/eden/mononoke/tools/admin/src/commands/megarepo.rs +++ b/eden/mononoke/tools/admin/src/commands/megarepo.rs @@ -5,6 +5,7 @@ * GNU General Public License version 2. */ +pub(crate) mod common; mod merge; mod pushredirection; diff --git a/eden/mononoke/tools/admin/src/commands/megarepo/common.rs b/eden/mononoke/tools/admin/src/commands/megarepo/common.rs new file mode 100644 index 0000000000000..bd41d6c1313bd --- /dev/null +++ b/eden/mononoke/tools/admin/src/commands/megarepo/common.rs @@ -0,0 +1,53 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This software may be used and distributed according to the terms of the + * GNU General Public License version 2. + */ + +use anyhow::Error; +use anyhow::Result; +use bookmarks::BookmarkKey; +use megarepolib::common::ChangesetArgs as MegarepoNewChangesetArgs; +use mononoke_types::DateTime; + +#[derive(Debug, clap::Args, Clone)] +pub(crate) struct ResultingChangesetArgs { + #[clap(long, short = 'm')] + pub commit_message: String, + #[clap(long, short = 'a')] + pub commit_author: String, + + #[clap(long = "commit-date-rfc3339")] + pub datetime: Option, + + #[clap( + long, + help = "bookmark to point to resulting commits (no sanity checks, will move existing bookmark, be careful)" + )] + pub set_bookmark: Option, + + #[clap(long = "mark-public")] + pub mark_public: bool, +} + +impl TryInto for ResultingChangesetArgs { + type Error = Error; + + fn try_into(self) -> Result { + let mb_datetime = self + .datetime + .as_deref() + .map_or_else(|| Ok(DateTime::now()), DateTime::from_rfc3339)?; + + let mb_bookmark = self.set_bookmark.map(BookmarkKey::new).transpose()?; + let res = MegarepoNewChangesetArgs { + message: self.commit_message, + author: self.commit_author, + datetime: mb_datetime, + bookmark: mb_bookmark, + mark_public: self.mark_public, + }; + Ok(res) + } +} diff --git a/eden/mononoke/tools/admin/src/commands/megarepo/merge.rs b/eden/mononoke/tools/admin/src/commands/megarepo/merge.rs index d6123a39a5873..c54583aba0106 100644 --- a/eden/mononoke/tools/admin/src/commands/megarepo/merge.rs +++ b/eden/mononoke/tools/admin/src/commands/megarepo/merge.rs @@ -10,7 +10,6 @@ use anyhow::format_err; use anyhow::Error; use anyhow::Result; use bonsai_hg_mapping::BonsaiHgMappingRef; -use bookmarks::BookmarkKey; use cloned::cloned; use context::CoreContext; use futures::try_join; @@ -24,9 +23,10 @@ use mononoke_app::args::ChangesetArgs; use mononoke_app::args::RepoArgs; use mononoke_app::MononokeApp; use mononoke_types::ChangesetId; -use mononoke_types::DateTime; use slog::info; +use super::common::ResultingChangesetArgs; + /// Create a merge commit with given parents #[derive(Debug, clap::Args)] pub struct MergeArgs { @@ -35,42 +35,8 @@ pub struct MergeArgs { #[clap(flatten)] pub repo_args: RepoArgs, - #[clap(long, short = 'm')] - pub commit_message: String, - #[clap(long, short = 'a')] - pub commit_author: String, - - #[clap(long = "commit-date-rfc3339")] - pub datetime: Option, - #[clap( - long, - help = "bookmark to point to resulting commits (no sanity checks, will move existing bookmark, be careful)" - )] - pub set_bookmark: Option, - - #[clap(long = "mark-public")] - pub mark_public: bool, -} - -impl TryInto for MergeArgs { - type Error = Error; - - fn try_into(self) -> Result { - let mb_datetime = self - .datetime - .as_deref() - .map_or_else(|| Ok(DateTime::now()), DateTime::from_rfc3339)?; - - let mb_bookmark = self.set_bookmark.map(BookmarkKey::new).transpose()?; - let res = MegarepoNewChangesetArgs { - message: self.commit_message, - author: self.commit_author, - datetime: mb_datetime, - bookmark: mb_bookmark, - mark_public: self.mark_public, - }; - Ok(res) - } + #[command(flatten)] + pub res_cs_args: ResultingChangesetArgs, } async fn fail_on_path_conflicts( @@ -102,7 +68,7 @@ pub async fn perform_merge( repo: Repo, first_bcs_id: ChangesetId, second_bcs_id: ChangesetId, - resulting_changeset_args: MegarepoNewChangesetArgs, + res_cs_args: MegarepoNewChangesetArgs, ) -> Result { cloned!(ctx, repo); let (first_hg_cs_id, second_hg_cs_id) = try_join!( @@ -119,7 +85,7 @@ pub async fn perform_merge( &repo, vec![first_bcs_id, second_bcs_id], Default::default(), - resulting_changeset_args, + res_cs_args, ) .await } @@ -135,14 +101,14 @@ pub async fn run(ctx: &CoreContext, app: MononokeApp, args: MergeArgs) -> Result _ => bail!("Expected exactly two parent commits"), }; - let resulting_changeset_args = MegarepoNewChangesetArgs::from(args.try_into()?); + let res_cs_args = args.res_cs_args.try_into()?; perform_merge( ctx.clone(), repo.clone(), first_parent, second_parent, - resulting_changeset_args, + res_cs_args, ) .await .map(|_| ())