Skip to content

fix(remote): fall back to set-url when remote already exists#127

Merged
appleboy merged 5 commits intomasterfrom
fix/handle-existing-remote
Mar 10, 2026
Merged

fix(remote): fall back to set-url when remote already exists#127
appleboy merged 5 commits intomasterfrom
fix/handle-existing-remote

Conversation

@appleboy
Copy link
Owner

Summary

  • Fix "error: remote origin already exists" by falling back to git remote set-url when git remote add fails
  • Add RemoteSetURL function in repo/remote.go
  • Add unit test for the new function

Closes #107

Test plan

  • Existing tests pass (go test ./...)
  • Verify plugin works when remote_name matches an existing remote (e.g., origin)
  • Verify plugin still works when adding a new remote name

🤖 Generated with Claude Code

- Fall back to git remote set-url when git remote add fails
- Add RemoteSetURL function for updating existing remote URLs
- Add test for RemoteSetURL command arguments

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 10, 2026 13:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses failures when configuring a git remote that already exists (e.g., origin) by introducing a git remote set-url path and updating remote handling to avoid remote ... already exists errors (Issue #107).

Changes:

  • Add RemoteSetURL helper to build git remote set-url commands.
  • Update Plugin.HandleRemote to attempt updating the remote URL when adding the remote fails.
  • Add a unit test validating the RemoteSetURL command arguments.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
repo/remote.go Adds RemoteSetURL command builder for updating an existing remote’s URL.
repo/remote_test.go Adds a unit test to verify RemoteSetURL constructs the expected command args.
plugin.go Changes remote setup logic to fall back to set-url when remote add fails.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

plugin.go Outdated
Comment on lines +154 to +158
if err := execute(repo.RemoteAdd(ctx, p.Config.RemoteName, p.Config.Remote)); err != nil {
return err
// If remote already exists, update its URL instead.
if err := execute(repo.RemoteSetURL(ctx, p.Config.RemoteName, p.Config.Remote)); err != nil {
return err
}
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleRemote falls back to RemoteSetURL for any RemoteAdd error, not just the "remote already exists" case described in the PR. This can mask the original failure (e.g., not a git repo, git missing) because the returned error will come from set-url. Consider detecting whether the remote already exists first (e.g., via git config --get remote.<name>.url / a RemoteExists helper) and only calling set-url in that case; otherwise return the original RemoteAdd error.

Copilot uses AI. Check for mistakes.
Comment on lines 150 to 152
// HandleRemote adds the git remote if required.
// If the remote already exists, it updates the URL instead.
func (p Plugin) HandleRemote(ctx context.Context) error {
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no test covering the new fallback behavior in HandleRemote (remote already exists -> update URL). Adding a unit test that sets up a temporary git repo, adds the remote once, and then calls HandleRemote again would verify the fix for #107 and prevent regressions.

Copilot uses AI. Check for mistakes.
appleboy and others added 4 commits March 10, 2026 21:20
- Add RemoteExists helper to check if a named remote already exists
- Use RemoteExists in HandleRemote to choose between add and set-url
- Add integration test for HandleRemote with pre-existing remote

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Check os.Chdir error return in defer to satisfy errcheck
- Use exec.CommandContext instead of exec.Command to satisfy noctx
- Add nolint:gosec for test-only git commands with static args

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Break long lines to satisfy golines linter
- Remove unused nolint:gosec directives

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@appleboy appleboy merged commit c37e987 into master Mar 10, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error: remote origin already exists

2 participants