diff --git a/cli/tests/test_git_push.rs b/cli/tests/test_git_push.rs index b6446595c..23a84c544 100644 --- a/cli/tests/test_git_push.rs +++ b/cli/tests/test_git_push.rs @@ -663,3 +663,23 @@ fn test_git_push_conflicting_branches() { Move branch branch1 from fd1d63e031ea to 8263cf992d33 "###); } + +#[test] +fn test_git_push_to_remote_named_git() { + let (test_env, workspace_root) = set_up(); + let git_repo = { + let mut git_repo_path = workspace_root.clone(); + git_repo_path.extend([".jj", "repo", "store", "git"]); + git2::Repository::open(&git_repo_path).unwrap() + }; + git_repo.remote_rename("origin", "git").unwrap(); + + let stderr = + test_env.jj_cmd_failure(&workspace_root, &["git", "push", "--all", "--remote=git"]); + insta::assert_snapshot!(stderr, @r###" + Branch changes to push to git: + Add branch branch1 to 45a3aa29e907 + Add branch branch2 to 8476341eb395 + Error: Git remote named 'git' is reserved for local Git repository + "###); +} diff --git a/lib/src/git.rs b/lib/src/git.rs index 2cebba747..c87becc5b 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -1064,6 +1064,11 @@ pub fn fetch( pub enum GitPushError { #[error("No git remote named '{0}'")] NoSuchRemote(String), + #[error( + "Git remote named '{name}' is reserved for local Git repository", + name = REMOTE_NAME_FOR_LOCAL_GIT_REPO + )] + RemoteReservedForLocalGitRepo, #[error("Push is not fast-forwardable")] NotFastForward, #[error("Remote rejected the update of some refs (do you have permission to push to {0:?}?)")] @@ -1167,6 +1172,9 @@ fn push_refs( refspecs: &[String], callbacks: RemoteCallbacks<'_>, ) -> Result<(), GitPushError> { + if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO { + return Err(GitPushError::RemoteReservedForLocalGitRepo); + } let mut remote = git_repo.find_remote(remote_name).map_err(|err| { if is_remote_not_found_err(&err) { GitPushError::NoSuchRemote(remote_name.to_string())