completion: teach forget about workspaces
Some checks are pending
binaries / Build binary artifacts (push) Waiting to run
nix / flake check (push) Waiting to run
build / build (, macos-13) (push) Waiting to run
build / build (, macos-14) (push) Waiting to run
build / build (, ubuntu-latest) (push) Waiting to run
build / build (, windows-latest) (push) Waiting to run
build / build (--all-features, ubuntu-latest) (push) Waiting to run
build / Build jj-lib without Git support (push) Waiting to run
build / Check protos (push) Waiting to run
build / Check formatting (push) Waiting to run
build / Check that MkDocs can build the docs (push) Waiting to run
build / Check that MkDocs can build the docs with Poetry 1.8 (push) Waiting to run
build / cargo-deny (advisories) (push) Waiting to run
build / cargo-deny (bans licenses sources) (push) Waiting to run
build / Clippy check (push) Waiting to run
Codespell / Codespell (push) Waiting to run
website / prerelease-docs-build-deploy (ubuntu-latest) (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run

This commit is contained in:
Remo Senekowitsch 2024-11-15 10:18:08 +01:00
parent 200581164e
commit 1a121eae99
3 changed files with 50 additions and 0 deletions

View file

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use clap_complete::ArgValueCandidates;
use itertools::Itertools;
use jj_lib::op_store::WorkspaceId;
use tracing::instrument;
@ -19,6 +20,7 @@ use tracing::instrument;
use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
use crate::command_error::CommandError;
use crate::complete;
use crate::ui::Ui;
/// Stop tracking a workspace's working-copy commit in the repo
@ -29,6 +31,7 @@ use crate::ui::Ui;
pub struct WorkspaceForgetArgs {
/// Names of the workspaces to forget. By default, forgets only the current
/// workspace.
#[arg(add = ArgValueCandidates::new(complete::workspaces))]
workspaces: Vec<String>,
}

View file

@ -271,6 +271,27 @@ pub fn operations() -> Vec<CompletionCandidate> {
})
}
pub fn workspaces() -> Vec<CompletionCandidate> {
with_jj(|mut jj, _| {
let output = jj
.arg("--config-toml")
.arg(r#"templates.commit_summary = 'if(description, description.first_line(), "(no description set)")'"#)
.arg("workspace")
.arg("list")
.output()
.map_err(user_error)?;
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(stdout
.lines()
.map(|line| {
let (name, desc) = line.split_once(": ").unwrap_or((line, ""));
CompletionCandidate::new(name).help(Some(desc.to_string().into()))
})
.collect())
})
}
/// Shell out to jj during dynamic completion generation
///
/// In case of errors, print them and early return an empty vector.

View file

@ -446,3 +446,29 @@ fn test_operations() {
let stdout = test_env.jj_cmd_success(&repo_path, &["--", "jj", "op", "undo", "5b"]);
insta::assert_snapshot!(stdout, @"5bbb4ca536a8 (2001-02-03 08:05:12) describe commit 968261075dddabf4b0e333c1cc9a49ce26a3f710");
}
#[test]
fn test_workspaces() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "main"]);
let main_path = test_env.env_root().join("main");
std::fs::write(main_path.join("file"), "contents").unwrap();
test_env.jj_cmd_ok(&main_path, &["describe", "-m", "initial"]);
test_env.jj_cmd_ok(
&main_path,
// same prefix as "default" workspace
&["workspace", "add", "--name", "def-second", "../secondary"],
);
let mut test_env = test_env;
test_env.add_env_var("COMPLETE", "fish");
let test_env = test_env;
let stdout = test_env.jj_cmd_success(&main_path, &["--", "jj", "workspace", "forget", "def"]);
insta::assert_snapshot!(stdout, @r"
def-second (no description set)
default initial
");
}