Skip to content

Commit

Permalink
sparse: extract "set --reset" to subcommand
Browse files Browse the repository at this point in the history
Since --reset conflicts with the other flags, "set --reset" seems odd.
  • Loading branch information
yuja committed Mar 29, 2024
1 parent db94848 commit dcf7578
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 25 additions & 14 deletions cli/src/commands/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::ui::Ui;
pub(crate) enum SparseArgs {
List(SparseListArgs),
Set(SparseSetArgs),
Reset(SparseResetArgs),
Edit(SparseEditArgs),
}

Expand Down Expand Up @@ -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 {}
Expand All @@ -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),
}
}
Expand All @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_sparse_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dcf7578

Please sign in to comment.