Skip to content

Commit

Permalink
Extract ResultingChangesetArgs into common module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gustavoavena authored and facebook-github-bot committed Jan 31, 2025
1 parent 01471d9 commit 5ef5d21
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
1 change: 1 addition & 0 deletions eden/mononoke/tools/admin/src/commands/megarepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* GNU General Public License version 2.
*/

pub(crate) mod common;
mod merge;
mod pushredirection;

Expand Down
53 changes: 53 additions & 0 deletions eden/mononoke/tools/admin/src/commands/megarepo/common.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

#[clap(
long,
help = "bookmark to point to resulting commits (no sanity checks, will move existing bookmark, be careful)"
)]
pub set_bookmark: Option<String>,

#[clap(long = "mark-public")]
pub mark_public: bool,
}

impl TryInto<MegarepoNewChangesetArgs> for ResultingChangesetArgs {
type Error = Error;

fn try_into(self) -> Result<MegarepoNewChangesetArgs> {
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)
}
}
50 changes: 8 additions & 42 deletions eden/mononoke/tools/admin/src/commands/megarepo/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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<String>,
#[clap(
long,
help = "bookmark to point to resulting commits (no sanity checks, will move existing bookmark, be careful)"
)]
pub set_bookmark: Option<String>,

#[clap(long = "mark-public")]
pub mark_public: bool,
}

impl TryInto<MegarepoNewChangesetArgs> for MergeArgs {
type Error = Error;

fn try_into(self) -> Result<MegarepoNewChangesetArgs> {
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(
Expand Down Expand Up @@ -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<HgChangesetId, Error> {
cloned!(ctx, repo);
let (first_hg_cs_id, second_hg_cs_id) = try_join!(
Expand All @@ -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
}
Expand All @@ -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(|_| ())
Expand Down

0 comments on commit 5ef5d21

Please sign in to comment.