cli: use RevisionArg type in "resolve -r", "bench", and example command

This commit is contained in:
Yuya Nishihara 2024-04-01 16:30:48 +09:00
parent ae91adbaf4
commit 311bdbf58d
3 changed files with 20 additions and 15 deletions

View file

@ -14,7 +14,7 @@
use std::io::Write as _; use std::io::Write as _;
use jj_cli::cli_util::{CliRunner, CommandHelper}; use jj_cli::cli_util::{CliRunner, CommandHelper, RevisionArg};
use jj_cli::command_error::CommandError; use jj_cli::command_error::CommandError;
use jj_cli::ui::Ui; use jj_cli::ui::Ui;
@ -28,7 +28,7 @@ enum CustomCommand {
struct FrobnicateArgs { struct FrobnicateArgs {
/// The revision to frobnicate /// The revision to frobnicate
#[arg(default_value = "@")] #[arg(default_value = "@")]
revision: String, revision: RevisionArg,
} }
fn run_custom_command( fn run_custom_command(

View file

@ -25,7 +25,7 @@ use jj_lib::object_id::HexPrefix;
use jj_lib::repo::Repo; use jj_lib::repo::Repo;
use jj_lib::revset::{self, DefaultSymbolResolver, RevsetExpression}; use jj_lib::revset::{self, DefaultSymbolResolver, RevsetExpression};
use crate::cli_util::{CommandHelper, WorkspaceCommandHelper}; use crate::cli_util::{CommandHelper, RevisionArg, WorkspaceCommandHelper};
use crate::command_error::CommandError; use crate::command_error::CommandError;
use crate::ui::Ui; use crate::ui::Ui;
@ -46,8 +46,8 @@ pub enum BenchCommand {
/// Find the common ancestor(s) of a set of commits /// Find the common ancestor(s) of a set of commits
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub struct BenchCommonAncestorsArgs { pub struct BenchCommonAncestorsArgs {
revision1: String, revision1: RevisionArg,
revision2: String, revision2: RevisionArg,
#[command(flatten)] #[command(flatten)]
criterion: CriterionArgs, criterion: CriterionArgs,
} }
@ -55,8 +55,8 @@ pub struct BenchCommonAncestorsArgs {
/// Checks if the first commit is an ancestor of the second commit /// Checks if the first commit is an ancestor of the second commit
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub struct BenchIsAncestorArgs { pub struct BenchIsAncestorArgs {
ancestor: String, ancestor: RevisionArg,
descendant: String, descendant: RevisionArg,
#[command(flatten)] #[command(flatten)]
criterion: CriterionArgs, criterion: CriterionArgs,
} }
@ -66,7 +66,7 @@ pub struct BenchIsAncestorArgs {
#[command(group(clap::ArgGroup::new("revset_source").required(true)))] #[command(group(clap::ArgGroup::new("revset_source").required(true)))]
pub struct BenchRevsetArgs { pub struct BenchRevsetArgs {
#[arg(group = "revset_source")] #[arg(group = "revset_source")]
revisions: Vec<String>, revisions: Vec<RevisionArg>,
/// Read revsets from file /// Read revsets from file
#[arg(long, short = 'f', group = "revset_source", value_hint = clap::ValueHint::FilePath)] #[arg(long, short = 'f', group = "revset_source", value_hint = clap::ValueHint::FilePath)]
file: Option<String>, file: Option<String>,
@ -142,7 +142,7 @@ pub(crate) fn cmd_bench(
|| index.common_ancestors(&[commit1.id().clone()], &[commit2.id().clone()]); || index.common_ancestors(&[commit1.id().clone()], &[commit2.id().clone()]);
run_bench( run_bench(
ui, ui,
&format!("commonancestors-{}-{}", &args.revision1, &args.revision2), &format!("commonancestors-{}-{}", &*args.revision1, &*args.revision2),
&args.criterion, &args.criterion,
routine, routine,
)?; )?;
@ -155,7 +155,7 @@ pub(crate) fn cmd_bench(
let routine = || index.is_ancestor(ancestor_commit.id(), descendant_commit.id()); let routine = || index.is_ancestor(ancestor_commit.id(), descendant_commit.id());
run_bench( run_bench(
ui, ui,
&format!("isancestor-{}-{}", &args.ancestor, &args.descendant), &format!("isancestor-{}-{}", &*args.ancestor, &*args.descendant),
&args.criterion, &args.criterion,
routine, routine,
)?; )?;
@ -179,6 +179,7 @@ pub(crate) fn cmd_bench(
.lines() .lines()
.map(|line| line.trim().to_owned()) .map(|line| line.trim().to_owned())
.filter(|line| !line.is_empty() && !line.starts_with('#')) .filter(|line| !line.is_empty() && !line.starts_with('#'))
.map(RevisionArg::from)
.collect() .collect()
} else { } else {
args.revisions.clone() args.revisions.clone()
@ -201,9 +202,13 @@ fn bench_revset<M: Measurement>(
command: &CommandHelper, command: &CommandHelper,
workspace_command: &WorkspaceCommandHelper, workspace_command: &WorkspaceCommandHelper,
group: &mut BenchmarkGroup<M>, group: &mut BenchmarkGroup<M>,
revset: &str, revset: &RevisionArg,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
writeln!(ui.status(), "----------Testing revset: {revset}----------")?; writeln!(
ui.status(),
"----------Testing revset: {revset}----------",
revset = &**revset
)?;
let expression = revset::optimize(workspace_command.parse_revset(revset)?.expression().clone()); let expression = revset::optimize(workspace_command.parse_revset(revset)?.expression().clone());
// Time both evaluation and iteration. // Time both evaluation and iteration.
let routine = |workspace_command: &WorkspaceCommandHelper, expression: Rc<RevsetExpression>| { let routine = |workspace_command: &WorkspaceCommandHelper, expression: Rc<RevsetExpression>| {
@ -226,7 +231,7 @@ fn bench_revset<M: Measurement>(
)?; )?;
group.bench_with_input( group.bench_with_input(
BenchmarkId::from_parameter(revset), BenchmarkId::from_parameter(&**revset),
&expression, &expression,
|bencher, expression| { |bencher, expression| {
bencher.iter_batched( bencher.iter_batched(

View file

@ -22,7 +22,7 @@ use jj_lib::object_id::ObjectId;
use jj_lib::repo_path::RepoPathBuf; use jj_lib::repo_path::RepoPathBuf;
use tracing::instrument; use tracing::instrument;
use crate::cli_util::{CommandHelper, WorkspaceCommandHelper}; use crate::cli_util::{CommandHelper, RevisionArg, WorkspaceCommandHelper};
use crate::command_error::{cli_error, CommandError}; use crate::command_error::{cli_error, CommandError};
use crate::formatter::Formatter; use crate::formatter::Formatter;
use crate::ui::Ui; use crate::ui::Ui;
@ -45,7 +45,7 @@ use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
pub(crate) struct ResolveArgs { pub(crate) struct ResolveArgs {
#[arg(long, short, default_value = "@")] #[arg(long, short, default_value = "@")]
revision: String, revision: RevisionArg,
/// Instead of resolving one conflict, list all the conflicts /// Instead of resolving one conflict, list all the conflicts
// TODO: Also have a `--summary` option. `--list` currently acts like // TODO: Also have a `--summary` option. `--list` currently acts like
// `diff --summary`, but should be more verbose. // `diff --summary`, but should be more verbose.