Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: implement jj root #2801

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `jj resolve` now displays the file being resolved.

* `jj workspace root` was aliased to `jj root`, for ease of discoverability

### Fixed bugs

* Fixed snapshots of symlinks in `gitignore`-d directory.
Expand Down
3 changes: 3 additions & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod prev;
mod rebase;
mod resolve;
mod restore;
mod root;
mod run;
mod show;
mod sparse;
Expand Down Expand Up @@ -119,6 +120,7 @@ enum Command {
help_template = "Not a real subcommand; consider `jj backout` or `jj restore`"
)]
Revert(DummyCommandArgs),
Root(root::RootArgs),
#[command(hide = true)]
// TODO: Flesh out.
Run(run::RunArgs),
Expand Down Expand Up @@ -182,6 +184,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co
Command::Unsquash(sub_args) => unsquash::cmd_unsquash(ui, command_helper, sub_args),
Command::Restore(sub_args) => restore::cmd_restore(ui, command_helper, sub_args),
Command::Revert(_args) => revert(),
Command::Root(sub_args) => root::cmd_root(ui, command_helper, sub_args),
Command::Run(sub_args) => run::cmd_run(ui, command_helper, sub_args),
Command::Diffedit(sub_args) => diffedit::cmd_diffedit(ui, command_helper, sub_args),
Command::Split(sub_args) => split::cmd_split(ui, command_helper, sub_args),
Expand Down
36 changes: 36 additions & 0 deletions cli/src/commands/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 tracing::instrument;

use crate::cli_util::{CommandError, CommandHelper};
use crate::commands::workspace;
use crate::ui::Ui;

/// Show the current workspace root directory
#[derive(clap::Args, Clone, Debug)]
pub(crate) struct RootArgs {}
v-gb marked this conversation as resolved.
Show resolved Hide resolved

#[instrument(skip_all)]
pub(crate) fn cmd_root(
ui: &mut Ui,
command: &CommandHelper,
RootArgs {}: &RootArgs,
) -> Result<(), CommandError> {
workspace::cmd_workspace(
ui,
command,
&workspace::WorkspaceCommand::Root(workspace::WorkspaceRootArgs {}),
)
}
44 changes: 44 additions & 0 deletions cli/tests/test_root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 std::path::Path;

use test_case::test_case;
use testutils::{TestRepoBackend, TestWorkspace};

use crate::common::TestEnvironment;

pub mod common;

#[test_case(TestRepoBackend::Local ; "local backend")]
#[test_case(TestRepoBackend::Git ; "git backend")]
fn test_root(backend: TestRepoBackend) {
let test_env = TestEnvironment::default();
let settings = testutils::user_settings();
let test_workspace = TestWorkspace::init_with_backend(&settings, backend);
let root = test_workspace.workspace.workspace_root();
let subdir = root.join("subdir");
std::fs::create_dir(&subdir).unwrap();
let stdout = test_env.jj_cmd_success(&subdir, &["root"]);
assert_eq!(&stdout, &[root.to_str().unwrap(), "\n"].concat());
}

#[test]
fn test_root_outside_a_repo() {
let test_env = TestEnvironment::default();
let stdout = test_env.jj_cmd_failure(Path::new("/"), &["root"]);
insta::assert_snapshot!(stdout, @r###"
Error: There is no jj repo in "."
"###);
}
Loading