Skip to content

Commit

Permalink
cli: error out on "init --at-op/--ignore-working-copy" or "clone --at…
Browse files Browse the repository at this point in the history
…-op"

--at-op should be invalid on repo initialization. "init --ignore-working-copy"
could be supported by using working_copy.reset(), but I don't think it's
worth the effort. If the working directory is empty, --ignore-working-copy
is meaningless, and if the directory is not empty, the user would probably
want to do snapshot at some point.
  • Loading branch information
yuja committed Jul 24, 2024
1 parent c705afa commit b76947c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 76 deletions.
5 changes: 4 additions & 1 deletion cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use jj_lib::str_util::StringPattern;
use jj_lib::workspace::Workspace;

use crate::cli_util::{CommandHelper, WorkspaceCommandHelper};
use crate::command_error::{user_error, user_error_with_message, CommandError};
use crate::command_error::{cli_error, user_error, user_error_with_message, CommandError};
use crate::commands::git::{map_git_error, maybe_add_gitignore};
use crate::config::{write_config_value_to_file, ConfigNamePathBuf};
use crate::git_util::{get_git_repo, print_git_import_stats, with_remote_git_callbacks};
Expand Down Expand Up @@ -85,6 +85,9 @@ pub fn cmd_git_clone(
command: &CommandHelper,
args: &GitCloneArgs,
) -> Result<(), CommandError> {
if command.global_args().at_operation != "@" {
return Err(cli_error("--at-op is not respected"));
}
let remote_name = "origin";
let source = absolute_git_source(command.cwd(), &args.source);
let wc_path_str = args
Expand Down
10 changes: 9 additions & 1 deletion cli/src/commands/git/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use jj_lib::workspace::Workspace;
use jj_lib::{file_util, git};

use crate::cli_util::{print_trackable_remote_branches, start_repo_transaction, CommandHelper};
use crate::command_error::{user_error_with_hint, user_error_with_message, CommandError};
use crate::command_error::{
cli_error, user_error_with_hint, user_error_with_message, CommandError,
};
use crate::commands::git::maybe_add_gitignore;
use crate::config::{write_config_value_to_file, ConfigNamePathBuf};
use crate::git_util::{
Expand Down Expand Up @@ -71,6 +73,12 @@ pub fn cmd_git_init(
command: &CommandHelper,
args: &GitInitArgs,
) -> Result<(), CommandError> {
if command.global_args().ignore_working_copy {
return Err(cli_error("--ignore-working-copy is not respected"));
}
if command.global_args().at_operation != "@" {
return Err(cli_error("--at-op is not respected"));
}
let cwd = command.cwd();
let wc_path = cwd.join(&args.destination);
let wc_path = file_util::create_or_reuse_dir(&wc_path)
Expand Down
10 changes: 9 additions & 1 deletion cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use tracing::instrument;

use super::git;
use crate::cli_util::CommandHelper;
use crate::command_error::{user_error_with_hint, user_error_with_message, CommandError};
use crate::command_error::{
cli_error, user_error_with_hint, user_error_with_message, CommandError,
};
use crate::ui::Ui;

/// Create a new repo in the given directory
Expand Down Expand Up @@ -50,6 +52,12 @@ pub(crate) fn cmd_init(
command: &CommandHelper,
args: &InitArgs,
) -> Result<(), CommandError> {
if command.global_args().ignore_working_copy {
return Err(cli_error("--ignore-working-copy is not respected"));
}
if command.global_args().at_operation != "@" {
return Err(cli_error("--at-op is not respected"));
}
let cwd = command.cwd();
let wc_path = cwd.join(&args.destination);
let wc_path = file_util::create_or_reuse_dir(&wc_path)
Expand Down
7 changes: 2 additions & 5 deletions cli/tests/test_git_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,12 @@ fn test_git_clone_at_operation() {
let git_repo = git2::Repository::init(git_repo_path).unwrap();
set_up_non_empty_git_repo(&git_repo);

// TODO: just error out? no operation should exist
let (_stdout, stderr) = test_env.jj_cmd_ok(
let stderr = test_env.jj_cmd_cli_error(
test_env.env_root(),
&["git", "clone", "--at-op=@-", "source", "clone"],
);
insta::assert_snapshot!(stderr, @r###"
Fetching into new repo in "$TEST_ENV/clone"
branch: main@origin [new] untracked
Setting the revset alias "trunk()" to "main@origin"
Error: --at-op is not respected
"###);
}

Expand Down
81 changes: 13 additions & 68 deletions cli/tests/test_git_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,11 @@ fn test_git_init_internal_ignore_working_copy() {
std::fs::create_dir(&workspace_root).unwrap();
std::fs::write(workspace_root.join("file1"), "").unwrap();

// No snapshot should be taken
let (_stdout, stderr) =
test_env.jj_cmd_ok(&workspace_root, &["git", "init", "--ignore-working-copy"]);
let stderr =
test_env.jj_cmd_cli_error(&workspace_root, &["git", "init", "--ignore-working-copy"]);
insta::assert_snapshot!(stderr, @r###"
Initialized repo in "."
"###);

let (stdout, stderr) =
test_env.jj_cmd_ok(&workspace_root, &["status", "--ignore-working-copy"]);
insta::assert_snapshot!(stdout, @r###"
The working copy is clean
Working copy : qpvuntsm 230dd059 (empty) (no description set)
Parent commit: zzzzzzzz 00000000 (empty) (no description set)
Error: --ignore-working-copy is not respected
"###);
insta::assert_snapshot!(stderr, @"");
}

#[test]
Expand All @@ -124,10 +114,9 @@ fn test_git_init_internal_at_operation() {
let workspace_root = test_env.env_root().join("repo");
std::fs::create_dir(&workspace_root).unwrap();

// TODO: just error out? no operation should exist
let (_stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["git", "init", "--at-op=@-"]);
let stderr = test_env.jj_cmd_cli_error(&workspace_root, &["git", "init", "--at-op=@-"]);
insta::assert_snapshot!(stderr, @r###"
Initialized repo in "."
Error: --at-op is not respected
"###);
}

Expand Down Expand Up @@ -254,7 +243,7 @@ fn test_git_init_external_ignore_working_copy() {
std::fs::write(workspace_root.join("file1"), "").unwrap();

// No snapshot should be taken
let (_stdout, stderr) = test_env.jj_cmd_ok(
let stderr = test_env.jj_cmd_cli_error(
&workspace_root,
&[
"git",
Expand All @@ -265,25 +254,7 @@ fn test_git_init_external_ignore_working_copy() {
],
);
insta::assert_snapshot!(stderr, @r###"
Done importing changes from the underlying Git repo.
Initialized repo in "."
"###);

let (stdout, stderr) =
test_env.jj_cmd_ok(&workspace_root, &["status", "--ignore-working-copy"]);
insta::assert_snapshot!(stdout, @r###"
The working copy is clean
Working copy : sqpuoqvx f6950fc1 (empty) (no description set)
Parent commit: mwrttmos 8d698d4a my-branch | My commit message
"###);
insta::assert_snapshot!(stderr, @"");

// TODO: Correct, but might be better to check out the root commit?
let stderr = test_env.jj_cmd_failure(&workspace_root, &["status"]);
insta::assert_snapshot!(stderr, @r###"
Error: The working copy is stale (not updated since operation b51416386f26).
Hint: Run `jj workspace update-stale` to update it.
See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-working-copy for more information.
Error: --ignore-working-copy is not respected
"###);
}

Expand All @@ -295,8 +266,7 @@ fn test_git_init_external_at_operation() {
let workspace_root = test_env.env_root().join("repo");
std::fs::create_dir(&workspace_root).unwrap();

// TODO: just error out? no operation should exist
let (_stdout, stderr) = test_env.jj_cmd_ok(
let stderr = test_env.jj_cmd_cli_error(
&workspace_root,
&[
"git",
Expand All @@ -307,8 +277,7 @@ fn test_git_init_external_at_operation() {
],
);
insta::assert_snapshot!(stderr, @r###"
Done importing changes from the underlying Git repo.
Initialized repo in "."
Error: --at-op is not respected
"###);
}

Expand Down Expand Up @@ -676,35 +645,13 @@ fn test_git_init_colocated_ignore_working_copy() {
init_git_repo(&workspace_root, false);
std::fs::write(workspace_root.join("file1"), "").unwrap();

// No snapshot should be taken
let (_stdout, stderr) = test_env.jj_cmd_ok(
let stderr = test_env.jj_cmd_cli_error(
&workspace_root,
&["git", "init", "--ignore-working-copy", "--colocate"],
);
insta::assert_snapshot!(stderr, @r###"
Done importing changes from the underlying Git repo.
Initialized repo in "."
Error: --ignore-working-copy is not respected
"###);

// FIXME: wrong HEAD, but fixing this would result in stale working copy
let (stdout, stderr) =
test_env.jj_cmd_ok(&workspace_root, &["status", "--ignore-working-copy"]);
insta::assert_snapshot!(stdout, @r###"
The working copy is clean
Working copy : qpvuntsm 230dd059 (empty) (no description set)
Parent commit: zzzzzzzz 00000000 (empty) (no description set)
"###);
insta::assert_snapshot!(stderr, @"");

let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["status"]);
insta::assert_snapshot!(stdout, @r###"
Working copy changes:
A file1
D some-file
Working copy : kkmpptxz 7864ea63 (no description set)
Parent commit: mwrttmos 8d698d4a my-branch | My commit message
"###);
insta::assert_snapshot!(stderr, @"");
}

#[test]
Expand All @@ -713,14 +660,12 @@ fn test_git_init_colocated_at_operation() {
let workspace_root = test_env.env_root().join("repo");
init_git_repo(&workspace_root, false);

// TODO: just error out? no operation should exist
let (_stdout, stderr) = test_env.jj_cmd_ok(
let stderr = test_env.jj_cmd_cli_error(
&workspace_root,
&["git", "init", "--at-op=@-", "--colocate"],
);
insta::assert_snapshot!(stderr, @r###"
Done importing changes from the underlying Git repo.
Initialized repo in "."
Error: --at-op is not respected
"###);
}

Expand Down
13 changes: 13 additions & 0 deletions cli/tests/test_init_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,17 @@ fn test_init_local() {
assert!(store_path.join("files").is_dir());
assert!(store_path.join("symlinks").is_dir());
assert!(store_path.join("conflicts").is_dir());

let stderr = test_env.jj_cmd_cli_error(
test_env.env_root(),
&["init", "--ignore-working-copy", "repo2"],
);
insta::assert_snapshot!(stderr, @r###"
Error: --ignore-working-copy is not respected
"###);

let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["init", "--at-op=@-", "repo3"]);
insta::assert_snapshot!(stderr, @r###"
Error: --at-op is not respected
"###);
}

0 comments on commit b76947c

Please sign in to comment.