sapling/eden/mononoke/cmds/admin/rebase.rs
Thomas Orozco c2c904f933 mononoke: initialize loggers, config, caching, tunables & runtime in MononokeMatches
Summary:
Basically every single Mononoke binary starts with the same preamble:

- Init mononoke
- Init caching
- Init logging
- Init tunables

Some of them forget to do it, some don't, etc. This is a mess.

To make things messier, our initialization consists of a bunch of lazy statics
interacting with each other (init logging & init configerator are kinda
intertwined due to the fact that configerator wants a logger but dynamic
observability wants a logger), and methods you must only call once.

This diff attempts to clean this up by moving all this initialization into the
construction of MononokeMatches. I didn't change all the accessor methods
(though I did update those that would otherwise return things instantiated at
startup).

I'm planning to do a bit more on top of this, as my actual goal here is to make
it easier to thread arguments from MononokeMatches to RepoFactory, and to do so
I'd like to just pass my MononokeEnvironment as an input to RepoFactory.

Reviewed By: HarveyHunt

Differential Revision: D27767698

fbshipit-source-id: 00d66b07b8c69f072b92d3d3919393300dd7a392
2021-04-16 10:27:43 -07:00

115 lines
3.3 KiB
Rust

/*
* 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 blobstore::Loadable;
use clap::{App, Arg, ArgMatches, SubCommand};
use fbinit::FacebookInit;
use futures::{compat::Future01CompatExt, future::try_join};
use blobrepo::save_bonsai_changesets;
use cmdlib::{
args::{self, MononokeMatches},
helpers,
};
use context::CoreContext;
use mononoke_types::{BonsaiChangesetMut, ChangesetId};
use slog::Logger;
use crate::error::SubcommandError;
pub const ARG_DEST: &str = "dest";
pub const ARG_CSID: &str = "csid";
pub const REBASE: &str = "rebase";
const ARG_I_KNOW: &str = "i-know-what-i-am-doing";
pub fn build_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name(REBASE)
.about(
"produce a bonsai changeset clone with p1 changed to a given value. \
DOES NOT RUN ANY SAFETY CHECKS, DOES NOT CHECK FOR CONFLICTS!",
)
.arg(
Arg::with_name(ARG_CSID)
.long(ARG_CSID)
.takes_value(true)
.required(true)
.help("{hg|bonsai} changeset id or bookmark name"),
)
.arg(
Arg::with_name(ARG_DEST)
.long(ARG_DEST)
.takes_value(true)
.required(true)
.help("desired value of the p1"),
)
.arg(
Arg::with_name(ARG_I_KNOW)
.long(ARG_I_KNOW)
.takes_value(false)
.help("Acknowledges that you understnad that this is an unsafe command"),
)
}
fn copyfrom_fixup(bcs: &mut BonsaiChangesetMut, new_parent: ChangesetId) {
for maybe_file_change in bcs.file_changes.values_mut() {
if let Some((_, ref mut cs_id)) = maybe_file_change.as_mut().and_then(|c| c.copy_from_mut())
{
*cs_id = new_parent;
}
}
}
pub async fn subcommand_rebase<'a>(
fb: FacebookInit,
logger: Logger,
matches: &'a MononokeMatches<'_>,
sub_matches: &'a ArgMatches<'_>,
) -> Result<(), SubcommandError> {
if !sub_matches.is_present(ARG_I_KNOW) {
return Err(anyhow!("{} is required", ARG_I_KNOW).into());
}
let ctx = CoreContext::new_with_logger(fb, logger.clone());
let repo = args::open_repo(fb, &logger, &matches).await?;
let cs_id = sub_matches
.value_of(ARG_CSID)
.ok_or_else(|| anyhow!("{} arg is not specified", ARG_CSID))?;
let dest = sub_matches
.value_of(ARG_DEST)
.ok_or_else(|| anyhow!("{} arg is not specified", ARG_DEST))?;
let (cs_id, dest) = try_join(
helpers::csid_resolve(ctx.clone(), repo.clone(), cs_id).compat(),
helpers::csid_resolve(ctx.clone(), repo.clone(), dest).compat(),
)
.await?;
let bcs = cs_id
.load(&ctx, repo.blobstore())
.await
.map_err(Error::from)?;
let mut rebased = bcs.into_mut();
if rebased.parents.is_empty() {
rebased.parents.push(dest);
} else {
rebased.parents[0] = dest;
}
copyfrom_fixup(&mut rebased, dest);
let rebased = rebased.freeze()?;
let rebased_cs_id = rebased.get_changeset_id();
save_bonsai_changesets(vec![rebased], ctx.clone(), repo).await?;
println!("{}", rebased_cs_id);
Ok(())
}