ok/jj
1
0
Fork 0
forked from mirrors/jj

git: prohibit push to remote named "git"

Since I'm going to make git::push_branches() update the repo view internally,
it should fail fast if the remote name is reserved. Before, the problem was
detected on git::import_refs().
This commit is contained in:
Yuya Nishihara 2023-10-15 05:11:19 +09:00
parent 58897d79c7
commit c8a848d260
2 changed files with 28 additions and 0 deletions

View file

@ -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
"###);
}

View file

@ -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())