git: on rename_remote(), check conflicts of new remote name

This commit is contained in:
Yuya Nishihara 2023-08-19 13:37:07 +09:00
parent 46dd6dd9c6
commit 61172b1c1e
2 changed files with 23 additions and 1 deletions

View file

@ -61,12 +61,23 @@ fn test_git_remote_rename() {
&repo_path,
&["git", "remote", "add", "foo", "http://example.com/repo/foo"],
);
test_env.jj_cmd_success(
&repo_path,
&["git", "remote", "add", "baz", "http://example.com/repo/baz"],
);
let stderr = test_env.jj_cmd_failure(&repo_path, &["git", "remote", "rename", "bar", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: No git remote named 'bar'
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["git", "remote", "rename", "foo", "baz"]);
insta::assert_snapshot!(stderr, @r###"
Error: Git remote named 'baz' already exists
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "remote", "rename", "foo", "bar"]);
insta::assert_snapshot!(stdout, @"");
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "remote", "list"]);
insta::assert_snapshot!(stdout, @"bar http://example.com/repo/foo");
insta::assert_snapshot!(stdout, @r###"
bar http://example.com/repo/foo
baz http://example.com/repo/baz
"###);
}

View file

@ -552,6 +552,8 @@ pub fn export_some_refs(
pub enum GitRemoteManagementError {
#[error("No git remote named '{0}'")]
NoSuchRemote(String),
#[error("Git remote named '{0}' already exists")]
RemoteAlreadyExists(String),
#[error(transparent)]
InternalGitError(git2::Error),
}
@ -566,6 +568,13 @@ fn is_remote_not_found_err(err: &git2::Error) -> bool {
)
}
fn is_remote_exists_err(err: &git2::Error) -> bool {
matches!(
(err.class(), err.code()),
(git2::ErrorClass::Config, git2::ErrorCode::Exists)
)
}
pub fn remove_remote(
mut_repo: &mut MutableRepo,
git_repo: &git2::Repository,
@ -612,6 +621,8 @@ pub fn rename_remote(
.map_err(|err| {
if is_remote_not_found_err(&err) {
GitRemoteManagementError::NoSuchRemote(old_remote_name.to_owned())
} else if is_remote_exists_err(&err) {
GitRemoteManagementError::RemoteAlreadyExists(new_remote_name.to_owned())
} else {
GitRemoteManagementError::InternalGitError(err)
}