Skip to content

Commit

Permalink
git: prohibit creation of remote named "git"
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Aug 29, 2023
1 parent 0e26ef7 commit 35a596f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj git push` will now push all branches in the range `remote_branches()..@`
instead of only branches pointing to `@` or `@-`.

* It's no longer allowed to create a Git remote named "git". Use `jj git remote
rename` to rename the existing remote.
[#1690](https://github.com/martinvonz/jj/issues/1690)

### New features

* Default template for `jj log` now does not show irrelevant information
Expand Down
32 changes: 32 additions & 0 deletions cli/tests/test_git_remotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ fn test_git_remote_add() {
insta::assert_snapshot!(stderr, @r###"
Error: Git remote named 'foo' already exists
"###);
let stderr = test_env.jj_cmd_failure(
&repo_path,
&["git", "remote", "add", "git", "http://example.com/repo/git"],
);
insta::assert_snapshot!(stderr, @r###"
Error: Git remote named 'git' is reserved for local Git repository
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "remote", "list"]);
insta::assert_snapshot!(stdout, @r###"
foo http://example.com/repo/foo
Expand Down Expand Up @@ -102,6 +109,10 @@ fn test_git_remote_rename() {
insta::assert_snapshot!(stderr, @r###"
Error: Git remote named 'baz' already exists
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["git", "remote", "rename", "foo", "git"]);
insta::assert_snapshot!(stderr, @r###"
Error: Git remote named 'git' is reserved for local Git repository
"###);
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"]);
Expand All @@ -110,3 +121,24 @@ fn test_git_remote_rename() {
baz http://example.com/repo/baz
"###);
}

#[test]
fn test_git_remote_named_git() {
let test_env = TestEnvironment::default();

// Existing remote named 'git' shouldn't block the repo initialization.
let repo_path = test_env.env_root().join("repo");
let git_repo = git2::Repository::init(&repo_path).unwrap();
git_repo
.remote("git", "http://example.com/repo/repo")
.unwrap();
test_env.jj_cmd_success(&repo_path, &["init", "--git-repo=."]);

// The remote can be renamed.
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "remote", "rename", "git", "bar"]);
insta::assert_snapshot!(stdout, @"");
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "remote", "list"]);
insta::assert_snapshot!(stdout, @r###"
bar http://example.com/repo/repo
"###);
}
11 changes: 11 additions & 0 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ pub enum GitRemoteManagementError {
NoSuchRemote(String),
#[error("Git remote named '{0}' already exists")]
RemoteAlreadyExists(String),
#[error(
"Git remote named '{name}' is reserved for local Git repository",
name = REMOTE_NAME_FOR_LOCAL_GIT_REPO
)]
RemoteReservedForLocalGitRepo,
#[error(transparent)]
InternalGitError(git2::Error),
}
Expand All @@ -587,6 +592,9 @@ pub fn add_remote(
remote_name: &str,
url: &str,
) -> Result<(), GitRemoteManagementError> {
if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
return Err(GitRemoteManagementError::RemoteReservedForLocalGitRepo);
}
git_repo.remote(remote_name, url).map_err(|err| {
if is_remote_exists_err(&err) {
GitRemoteManagementError::RemoteAlreadyExists(remote_name.to_owned())
Expand Down Expand Up @@ -638,6 +646,9 @@ pub fn rename_remote(
old_remote_name: &str,
new_remote_name: &str,
) -> Result<(), GitRemoteManagementError> {
if new_remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
return Err(GitRemoteManagementError::RemoteReservedForLocalGitRepo);
}
git_repo
.remote_rename(old_remote_name, new_remote_name)
.map_err(|err| {
Expand Down

0 comments on commit 35a596f

Please sign in to comment.