Skip to content

Commit

Permalink
libgit2: overwrite remote url if it exists
Browse files Browse the repository at this point in the history
Update initRepoWithRemote() so that it overwrites the remote url with
the provided url if the remote already exists, instead of erroring out.

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
  • Loading branch information
aryan9600 committed Jul 9, 2022
1 parent ed70f73 commit a56b85d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
30 changes: 22 additions & 8 deletions pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,17 @@ func buildSignature(s *git2go.Signature) git.Signature {
}

// initializeRepoWithRemote initializes or opens a repository at the given path
// and configures it with the given remote "origin" URL. If a remote already
// exists with a different URL, it returns an error.
// and configures it with the given transport opts URL (as a placeholder for the
// actual target url). If a remote already exists with a different URL, it overwrites
// it with the provided transport opts URL.
func initializeRepoWithRemote(ctx context.Context, path, url string, opts *git.AuthOptions) (*git2go.Repository, *git2go.Remote, error) {
repo, err := git2go.InitRepository(path, false)
if err != nil {
return nil, nil, fmt.Errorf("unable to init repository for '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
return nil, nil, fmt.Errorf("unable to init repository for '%s': %w", url, gitutil.LibGit2Error(err))
}

remote, err := repo.Remotes.Create(defaultRemoteName, url)
transportOptsURL := opts.TransportOptionsURL
remote, err := repo.Remotes.Create(defaultRemoteName, transportOptsURL)
if err != nil {
// If the remote already exists, lookup the remote.
if git2go.IsErrorCode(err, git2go.ErrorCodeExists) {
Expand All @@ -510,13 +512,25 @@ func initializeRepoWithRemote(ctx context.Context, path, url string, opts *git.A
repo.Free()
return nil, nil, fmt.Errorf("unable to create or lookup remote '%s'", defaultRemoteName)
}
if remote.Url() != url {
repo.Free()
return nil, nil, fmt.Errorf("remote '%s' with different address '%s' already exists", defaultRemoteName, remote.Url())

if remote.Url() != transportOptsURL {
err = repo.Remotes.SetUrl("origin", transportOptsURL)
if err != nil {
repo.Free()
remote.Free()
return nil, nil, fmt.Errorf("unable to configure remote %s origin with url %s", defaultRemoteName, url)
}

// refresh the remote
remote, err = repo.Remotes.Lookup(defaultRemoteName)
if err != nil {
repo.Free()
return nil, nil, fmt.Errorf("unable to create or lookup remote '%s'", defaultRemoteName)
}
}
} else {
repo.Free()
return nil, nil, fmt.Errorf("unable to create remote for '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
return nil, nil, fmt.Errorf("unable to create remote for '%s': %w", url, gitutil.LibGit2Error(err))
}
}
return repo, remote, nil
Expand Down
19 changes: 0 additions & 19 deletions pkg/git/libgit2/managed/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,3 @@ func getTransportOptions(transportOptsURL string) (*TransportOptions, bool) {
}
return nil, false
}

// EffectiveURL returns the effective URL for requests.
//
// Given that TransportOptions can allow for the target URL to be overriden
// this returns the same input if Managed Transport is disabled or if no TargetURL
// is set on TransportOptions.
func EffectiveURL(transporOptsURL string) string {
if !Enabled() {
return transporOptsURL
}

if opts, found := getTransportOptions(transporOptsURL); found {
if opts.TargetURL != "" {
return opts.TargetURL
}
}

return transporOptsURL
}

0 comments on commit a56b85d

Please sign in to comment.