diff --git a/CHANGELOG.md b/CHANGELOG.md index 70e8fe069..207516947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `jj config list` now accepts `--user` or `--repo` option to specify config origin. +* `jj tag list` command prints imported git tags. + ### Fixed bugs diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 31a443a68..69d8e1a2e 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -49,6 +49,7 @@ mod sparse; mod split; mod squash; mod status; +mod tag; mod unsquash; mod untrack; mod util; @@ -128,6 +129,8 @@ enum Command { Squash(squash::SquashArgs), Status(status::StatusArgs), #[command(subcommand)] + Tag(tag::TagCommand), + #[command(subcommand)] Util(util::UtilCommand), /// Undo an operation (shortcut for `jj op undo`) Undo(operation::OperationUndoArgs), @@ -191,6 +194,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Command::Operation(sub_args) => operation::cmd_operation(ui, command_helper, sub_args), Command::Workspace(sub_args) => workspace::cmd_workspace(ui, command_helper, sub_args), Command::Sparse(sub_args) => sparse::cmd_sparse(ui, command_helper, sub_args), + Command::Tag(sub_args) => tag::cmd_tag(ui, command_helper, sub_args), Command::Chmod(sub_args) => chmod::cmd_chmod(ui, command_helper, sub_args), Command::Git(sub_args) => git::cmd_git(ui, command_helper, sub_args), Command::Util(sub_args) => util::cmd_util(ui, command_helper, sub_args), diff --git a/cli/src/commands/tag.rs b/cli/src/commands/tag.rs new file mode 100644 index 000000000..1dd7afc6a --- /dev/null +++ b/cli/src/commands/tag.rs @@ -0,0 +1,71 @@ +// Copyright 2020-2024 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use jj_lib::str_util::StringPattern; + +use crate::cli_util::{parse_string_pattern, CommandError, CommandHelper}; +use crate::ui::Ui; + +/// Manage tags. +#[derive(clap::Subcommand, Clone, Debug)] +pub enum TagCommand { + #[command(visible_alias("l"))] + List(TagListArgs), +} + +/// List tags. +#[derive(clap::Args, Clone, Debug)] +pub struct TagListArgs { + /// Show 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(value_parser = parse_string_pattern)] + pub names: Vec, +} + +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), + } +} + +fn cmd_tag_list( + ui: &mut Ui, + command: &CommandHelper, + args: &TagListArgs, +) -> Result<(), CommandError> { + let workspace_command = command.workspace_helper(ui)?; + let repo = workspace_command.repo(); + let view = repo.view(); + + ui.request_pager(); + let mut formatter = ui.stdout_formatter(); + let formatter = formatter.as_mut(); + + for name in view.tags().keys() { + if !args.names.is_empty() && !args.names.iter().any(|pattern| pattern.matches(name)) { + continue; + } + + writeln!(formatter.labeled("tag"), "{name}")?; + } + + Ok(()) +} diff --git a/cli/tests/test_tag_command.rs b/cli/tests/test_tag_command.rs new file mode 100644 index 000000000..96d9f4e22 --- /dev/null +++ b/cli/tests/test_tag_command.rs @@ -0,0 +1,81 @@ +// Copyright 2024 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::common::TestEnvironment; + +pub mod common; + +fn set_up_tagged_git_repo(git_repo: &git2::Repository) { + let signature = + git2::Signature::new("Some One", "some.one@example.com", &git2::Time::new(0, 0)).unwrap(); + let mut tree_builder = git_repo.treebuilder(None).unwrap(); + let file_oid = git_repo.blob(b"content").unwrap(); + tree_builder + .insert("file", file_oid, git2::FileMode::Blob.into()) + .unwrap(); + let tree_oid = tree_builder.write().unwrap(); + let tree = git_repo.find_tree(tree_oid).unwrap(); + git_repo + .commit( + Some("refs/heads/main"), + &signature, + &signature, + "message", + &tree, + &[], + ) + .unwrap(); + git_repo.set_head("refs/heads/main").unwrap(); + + let obj = git_repo.revparse_single("HEAD").unwrap(); + git_repo + .tag("test_tag", &obj, &signature, "test tag message", false) + .unwrap(); + git_repo + .tag("test_tag2", &obj, &signature, "test tag message", false) + .unwrap(); +} + +#[test] +fn test_tag_list() { + 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 + "###); + + // Test pattern matching. + insta::assert_snapshot!( + test_env.jj_cmd_success(&local_path, &["tag", "list", "test_tag2"]), + @r###" + test_tag2 + "###); + + insta::assert_snapshot!( + test_env.jj_cmd_success(&local_path, &["tag", "list", "glob:test_tag?"]), + @r###" + test_tag2 + "###); +}