Skip to content

Commit

Permalink
cli tag: create a tag forget command
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyagr committed Feb 20, 2024
1 parent 84685a4 commit a95ebdd
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `jj git fetch` now automatically prints new remote branches and tags by default.

* New `jj tag forget` command.

* `--verbose/-v` is now `--debug` (no short option since it's not intended to be used often)

* `jj move --from/--to` can now be abbreviated to `jj move -f/-t`
Expand Down
61 changes: 60 additions & 1 deletion cli/src/commands/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Write;

use itertools::Itertools;
use jj_lib::op_store::RefTarget;
use jj_lib::str_util::StringPattern;

use crate::cli_util::{parse_string_pattern, CommandError, CommandHelper};
use crate::cli_util::{parse_string_pattern, user_error, CommandError, CommandHelper};
use crate::ui::Ui;

/// Manage tags.
#[derive(clap::Subcommand, Clone, Debug)]
pub enum TagCommand {
#[command(visible_alias("l"))]
List(TagListArgs),
#[command(visible_alias("f"))]
Forget(TagForgetArgs),
}

/// List tags.
Expand All @@ -36,13 +42,26 @@ pub struct TagListArgs {
pub names: Vec<StringPattern>,
}

/// Forget tags. They may be recreated on `fetch`.
#[derive(clap::Args, Clone, Debug)]
pub struct TagForgetArgs {
/// Forget tags whose local name matches
///
/// By default, the specified name matches exactly. Use `glob:` prefix to
/// select tags by wildcard pattern. For details, see
/// https://github.com/martinvonz/jj/blob/main/docs/revsets.md#string-patterns.
#[arg(required = true, value_parser = parse_string_pattern)]
pub names: Vec<StringPattern>,
}

pub fn cmd_tag(
ui: &mut Ui,
command: &CommandHelper,
subcommand: &TagCommand,
) -> Result<(), CommandError> {
match subcommand {
TagCommand::List(sub_args) => cmd_tag_list(ui, command, sub_args),
TagCommand::Forget(sub_args) => cmd_tag_forget(ui, command, sub_args),
}
}

Expand All @@ -69,3 +88,43 @@ fn cmd_tag_list(

Ok(())
}

fn cmd_tag_forget(
ui: &mut Ui,
command: &CommandHelper,
args: &TagForgetArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let mut tx = workspace_command.start_transaction();
let view = tx.repo().base_repo().view();

let mut to_forget = vec![];
let mut unmatched_patterns = vec![];
for pattern in args.names.iter() {
let mut matched = false;
for tagname in view.tags().keys().filter(|name| pattern.matches(name)) {
matched = true;
to_forget.push(tagname.clone());
}
if !matched {
unmatched_patterns.push(pattern.to_string())
}
}
if !unmatched_patterns.is_empty() {
return Err(user_error(format!(
"No matching tags for patterns: {}",
unmatched_patterns.into_iter().join(", ")
)));
}
for tagname in to_forget.iter() {
// Forgetting a tag is the same as deleting it at the moment, as we never push
// tags.
tx.mut_repo().set_tag_target(tagname, RefTarget::absent());
}
tx.finish(ui, format!("forgot {} tags", to_forget.len()))?;

if to_forget.len() > 1 {
writeln!(ui.stderr(), "Forgot {} branches.", to_forget.len())?;
};
Ok(())
}
14 changes: 14 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This document contains the help content for the `jj` command-line program.
* [`jj status`↴](#jj-status)
* [`jj tag`↴](#jj-tag)
* [`jj tag list`↴](#jj-tag-list)
* [`jj tag forget`↴](#jj-tag-forget)
* [`jj util`↴](#jj-util)
* [`jj util completion`↴](#jj-util-completion)
* [`jj util gc`↴](#jj-util-gc)
Expand Down Expand Up @@ -1703,6 +1704,7 @@ Manage tags
###### **Subcommands:**
* `list` — List tags
* `forget` — Forget tags. They may be recreated on `fetch`
Expand All @@ -1718,6 +1720,18 @@ List tags
## `jj tag forget`
Forget tags. They may be recreated on `fetch`
**Usage:** `jj tag forget <NAMES>...`
###### **Arguments:**
* `<NAMES>` — Forget tags whose local name matches
## `jj util`
Infrequently used commands such as for generating shell completions
Expand Down
69 changes: 69 additions & 0 deletions cli/tests/test_tag_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use itertools::Itertools;

use crate::common::TestEnvironment;

fn set_up_tagged_git_repo(git_repo: &git2::Repository) {
Expand Down Expand Up @@ -77,3 +79,70 @@ fn test_tag_list() {
test_tag2
"###);
}

#[test]
fn test_tag_forget() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-branch = true");
let git_repo_path = test_env.env_root().join("source");
let git_repo = git2::Repository::init(git_repo_path).unwrap();

set_up_tagged_git_repo(&git_repo);

test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "source", "tagged"]);

let local_path = test_env.env_root().join("tagged");
insta::assert_snapshot!(
test_env.jj_cmd_success(&local_path, &["tag", "list"]),
@r###"
test_tag
test_tag2
"###);

insta::assert_snapshot!(
test_env.jj_cmd_success(&local_path, &["tag", "forget", "test_tag2"]),
@"");
insta::assert_snapshot!(
test_env.jj_cmd_success(&local_path, &["tag", "list"]),
@r###"
test_tag
"###);

// Pushing does not delete the tag on the remote
let (_stdout, stderr) = test_env.jj_cmd_ok(&local_path, &["git", "push", "--all"]);
insta::assert_snapshot!(stderr,
@r###"
Nothing changed.
"###);
insta::assert_debug_snapshot!(
git_repo.tag_names(None).unwrap().into_iter().collect_vec(),
@r###"
[
Some(
"test_tag",
),
Some(
"test_tag2",
),
]
"###);

// Fetching recreates the tag
let (_stdout, stderr) = test_env.jj_cmd_ok(&local_path, &["git", "fetch"]);
insta::assert_snapshot!(stderr,
@r###"
tag: test_tag2 [new]
"###);
insta::assert_snapshot!(
test_env.jj_cmd_success(&local_path, &["tag", "list"]),
@r###"
test_tag
test_tag2
"###);

insta::assert_snapshot!(
test_env.jj_cmd_failure(&local_path, &["tag", "forget", "test_tag2", "thistagdoesnotexist"]),
@r###"
Error: No matching tags for patterns: thistagdoesnotexist
"###);
}

0 comments on commit a95ebdd

Please sign in to comment.