From c0450b178d53e2a0f9bcea47487edb693fb7d96f Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Wed, 9 Feb 2022 19:35:17 -0600 Subject: [PATCH] Add command to remove remotes --- .../grgit/operation/RemoteRemoveOp.groovy | 34 +++++++++++++++++++ .../grgit/service/RemoteService.groovy | 4 ++- .../grgit/operation/RemoveRemoveOpSpec.groovy | 14 ++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/RemoteRemoveOp.groovy create mode 100644 grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/RemoveRemoveOpSpec.groovy diff --git a/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/RemoteRemoveOp.groovy b/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/RemoteRemoveOp.groovy new file mode 100644 index 00000000..571e9fd4 --- /dev/null +++ b/grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/RemoteRemoveOp.groovy @@ -0,0 +1,34 @@ +package org.ajoberstar.grgit.operation + +import java.util.concurrent.Callable + +import org.ajoberstar.grgit.Repository +import org.ajoberstar.grgit.internal.Operation +import org.eclipse.jgit.lib.Config + +/** + * Removes a remote from the repository. + * @see grgit-remote + * @see git-remote Manual Page + */ +@Operation('remove') +class RemoteRemoveOp implements Callable { + + private final Repository repository + + /** + * Name of the remote. + */ + String name + + RemoteRemoveOp(Repository repo) { + this.repository = repo + } + + @Override + Void call() { + Config config = repository.jgit.repository.config + config.unsetSection("remote", name) + config.save(); + } +} diff --git a/grgit-core/src/main/groovy/org/ajoberstar/grgit/service/RemoteService.groovy b/grgit-core/src/main/groovy/org/ajoberstar/grgit/service/RemoteService.groovy index 9eb19d61..299a08c6 100644 --- a/grgit-core/src/main/groovy/org/ajoberstar/grgit/service/RemoteService.groovy +++ b/grgit-core/src/main/groovy/org/ajoberstar/grgit/service/RemoteService.groovy @@ -4,6 +4,7 @@ import org.ajoberstar.grgit.Repository import org.ajoberstar.grgit.internal.WithOperations import org.ajoberstar.grgit.operation.RemoteAddOp import org.ajoberstar.grgit.operation.RemoteListOp +import org.ajoberstar.grgit.operation.RemoteRemoveOp /** * Provides support for remote-related operations on a Git repository. @@ -17,9 +18,10 @@ import org.ajoberstar.grgit.operation.RemoteListOp * */ -@WithOperations(instanceOperations=[RemoteListOp, RemoteAddOp]) +@WithOperations(instanceOperations=[RemoteListOp, RemoteAddOp, RemoteRemoveOp]) class RemoteService { private final Repository repository diff --git a/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/RemoveRemoveOpSpec.groovy b/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/RemoveRemoveOpSpec.groovy new file mode 100644 index 00000000..9ecdc4ab --- /dev/null +++ b/grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/RemoveRemoveOpSpec.groovy @@ -0,0 +1,14 @@ +package org.ajoberstar.grgit.operation + +import org.ajoberstar.grgit.fixtures.SimpleGitOpSpec + +class RemoveRemoveOpSpec extends SimpleGitOpSpec { + def 'remote with given name is removed'() { + given: + grgit.remote.add(name: 'newRemote', url: 'http://fetch.url/') + when: + grgit.remote.remove(name: 'newRemote') + then: + [] == grgit.remote.list() + } +}