AddGitRemote add a remote URL to the current git repository
(ctx context.Context, url string, name string)
| 84 | |
| 85 | // AddGitRemote add a remote URL to the current git repository |
| 86 | func AddGitRemote(ctx context.Context, url string, name string) error { |
| 87 | repo, err := git.PlainOpen(".") |
| 88 | if err != nil { |
| 89 | return errors.Wrapf(ctx, err, "fail to initialize the Git repository") |
| 90 | } |
| 91 | |
| 92 | _, err = repo.CreateRemote(&gitconfig.RemoteConfig{ |
| 93 | Name: name, |
| 94 | URLs: []string{url}, |
| 95 | }) |
| 96 | if err != nil { |
| 97 | return errors.Wrapf(ctx, err, "fail to add the Git remote") |
| 98 | } |
| 99 | |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | // MainBranchName returns the repository main branch name, preferring origin/HEAD, |
| 104 | // then common local branch names. In case of errors, return `main`. |