From dcf75788e0eb858a6572414d069332eea03d73e6 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 28 Mar 2024 21:19:24 +0900 Subject: [PATCH] sparse: extract "set --reset" to subcommand Since --reset conflicts with the other flags, "set --reset" seems odd. --- CHANGELOG.md | 3 ++- cli/src/commands/sparse.rs | 39 ++++++++++++++++++++------------ cli/tests/cli-reference@.md.snap | 10 ++++++-- cli/tests/test_sparse_command.rs | 2 +- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d38ecc2f5..a45ad9c24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The `git_head` template keyword now returns an optional value instead of a list of 0 or 1 element. -* The `jj sparse set --edit` was split up into `jj sparse edit`. +* The `jj sparse set --edit`/`--reset` flags were split up into `jj sparse + edit`/`reset` subcommands respectively. * The `jj sparse` subcommands now parse and print patterns as workspace-relative paths. diff --git a/cli/src/commands/sparse.rs b/cli/src/commands/sparse.rs index 01a4433da2..9b4b79b186 100644 --- a/cli/src/commands/sparse.rs +++ b/cli/src/commands/sparse.rs @@ -37,6 +37,7 @@ use crate::ui::Ui; pub(crate) enum SparseArgs { List(SparseListArgs), Set(SparseSetArgs), + Reset(SparseResetArgs), Edit(SparseEditArgs), } @@ -73,11 +74,12 @@ pub(crate) struct SparseSetArgs { /// Include no files in the working copy (combine with --add) #[arg(long)] clear: bool, - /// Include all files in the working copy - #[arg(long, conflicts_with_all = &["add", "remove", "clear"])] - reset: bool, } +/// Reset the patterns to include all files in the working copy +#[derive(clap::Args, Clone, Debug)] +pub(crate) struct SparseResetArgs {} + /// Start an editor to update the patterns that are present in the working copy #[derive(clap::Args, Clone, Debug)] pub(crate) struct SparseEditArgs {} @@ -91,6 +93,7 @@ pub(crate) fn cmd_sparse( match args { SparseArgs::List(sub_args) => cmd_sparse_list(ui, command, sub_args), SparseArgs::Set(sub_args) => cmd_sparse_set(ui, command, sub_args), + SparseArgs::Reset(sub_args) => cmd_sparse_reset(ui, command, sub_args), SparseArgs::Edit(sub_args) => cmd_sparse_edit(ui, command, sub_args), } } @@ -117,23 +120,31 @@ fn cmd_sparse_set( let mut workspace_command = command.workspace_helper(ui)?; update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| { let mut new_patterns = HashSet::new(); - if args.reset { - new_patterns.insert(RepoPathBuf::root()); - } else { - if !args.clear { - new_patterns.extend(old_patterns.iter().cloned()); - for path in &args.remove { - new_patterns.remove(path); - } - } - for path in &args.add { - new_patterns.insert(path.to_owned()); + if !args.clear { + new_patterns.extend(old_patterns.iter().cloned()); + for path in &args.remove { + new_patterns.remove(path); } } + for path in &args.add { + new_patterns.insert(path.to_owned()); + } Ok(new_patterns.into_iter().sorted_unstable().collect()) }) } +#[instrument(skip_all)] +fn cmd_sparse_reset( + ui: &mut Ui, + command: &CommandHelper, + _args: &SparseResetArgs, +) -> Result<(), CommandError> { + let mut workspace_command = command.workspace_helper(ui)?; + update_sparse_patterns_with(ui, &mut workspace_command, |_ui, _old_patterns| { + Ok(vec![RepoPathBuf::root()]) + }) +} + #[instrument(skip_all)] fn cmd_sparse_edit( ui: &mut Ui, diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 78ce17806b..dc7d797e9f 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -70,6 +70,7 @@ This document contains the help content for the `jj` command-line program. * [`jj sparse`↴](#jj-sparse) * [`jj sparse list`↴](#jj-sparse-list) * [`jj sparse set`↴](#jj-sparse-set) +* [`jj sparse reset`↴](#jj-sparse-reset) * [`jj sparse edit`↴](#jj-sparse-edit) * [`jj split`↴](#jj-split) * [`jj squash`↴](#jj-squash) @@ -1615,6 +1616,7 @@ Manage which paths from the working-copy commit are present in the working copy * `list` — List the patterns that are currently present in the working copy * `set` — Update the patterns that are present in the working copy +* `reset` — Reset the patterns to include all files in the working copy * `edit` — Start an editor to update the patterns that are present in the working copy @@ -1645,11 +1647,15 @@ For example, if all you need is the `README.md` and the `lib/` directory, use `j Possible values: `true`, `false` -* `--reset` — Include all files in the working copy - Possible values: `true`, `false` +## `jj sparse reset` + +Reset the patterns to include all files in the working copy + +**Usage:** `jj sparse reset` + ## `jj sparse edit` diff --git a/cli/tests/test_sparse_command.rs b/cli/tests/test_sparse_command.rs index 6ad47880bc..7c2a816f7b 100644 --- a/cli/tests/test_sparse_command.rs +++ b/cli/tests/test_sparse_command.rs @@ -122,7 +122,7 @@ fn test_sparse_manage_patterns() { assert!(!repo_path.join("file3").exists()); // Can reset back to all files - let (stdout, stderr) = test_env.jj_cmd_ok(&sub_dir, &["sparse", "set", "--reset"]); + let (stdout, stderr) = test_env.jj_cmd_ok(&sub_dir, &["sparse", "reset"]); insta::assert_snapshot!(stdout, @""); insta::assert_snapshot!(stderr, @r###" Added 2 files, modified 0 files, removed 0 files