SetRemote sets the remote by name Rill for the given repository to the provided remote URL.
(path string, config *Config)
| 307 | |
| 308 | // SetRemote sets the remote by name Rill for the given repository to the provided remote URL. |
| 309 | func SetRemote(path string, config *Config) error { |
| 310 | if config.Remote == "" { |
| 311 | return nil |
| 312 | } |
| 313 | repo, err := git.PlainOpen(path) |
| 314 | if err != nil { |
| 315 | return fmt.Errorf("failed to open git repository: %w", err) |
| 316 | } |
| 317 | |
| 318 | remote, err := repo.Remote(config.RemoteName()) |
| 319 | if err != nil && !errors.Is(err, git.ErrRemoteNotFound) { |
| 320 | return fmt.Errorf("failed to get remote: %w", err) |
| 321 | } |
| 322 | if remote != nil { |
| 323 | if remote.Config().URLs[0] == config.Remote || !config.ManagedRepo { |
| 324 | // remote already exists with the same URL, no need to create it again |
| 325 | // remote other than managed git exists, can't overwrite user's remote |
| 326 | return nil |
| 327 | } |
| 328 | // if the remote already exists with a different URL, delete it |
| 329 | err = repo.DeleteRemote(config.RemoteName()) |
| 330 | if err != nil { |
| 331 | return fmt.Errorf("failed to delete existing remote: %w", err) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | _, err = repo.CreateRemote(&gitConfig.RemoteConfig{ |
| 336 | Name: config.RemoteName(), |
| 337 | URLs: []string{config.Remote}, |
| 338 | }) |
| 339 | return err |
| 340 | } |
| 341 | |
| 342 | func IsGitRepo(path string) bool { |
| 343 | _, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{ |