git: reject reserved remote name early in fetch() function

I'm going to make git::import_refs() not fail because of a real remote named
"git", but fetching from such remote should be an error.
This commit is contained in:
Yuya Nishihara 2023-10-18 07:37:39 +09:00
parent b2fd1a002e
commit dfb10cab9b
3 changed files with 9 additions and 9 deletions

View File

@ -540,7 +540,7 @@ jj currently does not support partial clones. To use jj with this repository, tr
} }
match err { match err {
GitFetchError::NoSuchRemote(_) => user_error(err), GitFetchError::NoSuchRemote(_) => user_error(err),
GitFetchError::RemoteWithSlash(_) => user_error_with_hint( GitFetchError::RemoteName(_) => user_error_with_hint(
err, err,
"Run `jj git remote rename` to give a different name.", "Run `jj git remote rename` to give a different name.",
), ),

View File

@ -531,14 +531,16 @@ fn test_git_fetch_from_remote_named_git(subprocess: bool) {
insta::allow_duplicates! { insta::allow_duplicates! {
insta::assert_snapshot!(output, @r" insta::assert_snapshot!(output, @r"
------- stderr ------- ------- stderr -------
Error: Failed to import refs from underlying Git repo Error: Git remote named 'git' is reserved for local Git repository
Caused by: Git remote named 'git' is reserved for local Git repository Hint: Run `jj git remote rename` to give a different name.
Hint: Run `jj git remote rename` to give different name.
[EOF] [EOF]
[exit status: 1] [exit status: 1]
"); ");
} }
// Fetch remote refs by using the git CLI.
git::fetch(&repo_path, "git");
// Implicit import shouldn't fail because of the remote ref. // Implicit import shouldn't fail because of the remote ref.
let output = test_env.run_jj_in(&repo_path, ["bookmark", "list", "--all-remotes"]); let output = test_env.run_jj_in(&repo_path, ["bookmark", "list", "--all-remotes"]);
insta::allow_duplicates! { insta::allow_duplicates! {

View File

@ -1499,8 +1499,8 @@ pub enum GitFetchError {
chars = INVALID_REFSPEC_CHARS.iter().join("`, `") chars = INVALID_REFSPEC_CHARS.iter().join("`, `")
)] )]
InvalidBranchPattern(StringPattern), InvalidBranchPattern(StringPattern),
#[error("Git remotes with slashes are incompatible with jj: {0}")] #[error(transparent)]
RemoteWithSlash(String), RemoteName(#[from] GitRemoteNameError),
// TODO: I'm sure there are other errors possible, such as transport-level errors. // TODO: I'm sure there are other errors possible, such as transport-level errors.
#[error("Unexpected git error when fetching")] #[error("Unexpected git error when fetching")]
InternalGitError(#[from] git2::Error), InternalGitError(#[from] git2::Error),
@ -1581,9 +1581,7 @@ impl<'a> GitFetch<'a> {
callbacks: RemoteCallbacks<'_>, callbacks: RemoteCallbacks<'_>,
depth: Option<NonZeroU32>, depth: Option<NonZeroU32>,
) -> Result<(), GitFetchError> { ) -> Result<(), GitFetchError> {
if remote_name.contains("/") { validate_remote_name(remote_name)?;
return Err(GitFetchError::RemoteWithSlash(remote_name.to_owned()));
}
self.fetch_impl self.fetch_impl
.fetch(remote_name, branch_names, callbacks, depth)?; .fetch(remote_name, branch_names, callbacks, depth)?;
self.fetched.push(FetchedBranches { self.fetched.push(FetchedBranches {