Skip to content

Commit

Permalink
Use the Free() method on all git2go objects (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Jun 7, 2024
1 parent 7353907 commit 0188c5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions components/serverless/internal/git/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (g *git2goFetcher) git2goFetch(url, outputPath string, remoteCallbacks git2

remote, err := g.lookupCreateRemote(repo, url, outputPath)
if err != nil {
repo.Free()
return nil, errors.Wrap(err, "while creating/using remote")
}
defer remote.Free()
Expand All @@ -28,6 +29,7 @@ func (g *git2goFetcher) git2goFetch(url, outputPath string, remoteCallbacks git2
DownloadTags: git2go.DownloadTagsAll,
}, "")
if err != nil {
repo.Free()
return nil, errors.Wrap(err, "while fetching remote")
}
return repo, nil
Expand All @@ -40,7 +42,7 @@ func (g *git2goFetcher) openInitRepo(outputPath string) (*git2go.Repository, err
if err == nil {
return repo, nil
}
g.logger.Errorf("failed to open existing repo at [%s]: %v", outputPath, err)
g.logger.Warnf("failed to open existing repo at [%s]: %v", outputPath, err)
return git2go.InitRepository(outputPath, true)
}

Expand All @@ -49,6 +51,6 @@ func (g *git2goFetcher) lookupCreateRemote(repo *git2go.Repository, url, outputP
if err == nil {
return remote, nil
}
g.logger.Errorf("failed to use existing origin remote at [%s]: %v", outputPath, err)
g.logger.Warnf("failed to use existing origin remote at [%s]: %v", outputPath, err)
return repo.Remotes.Create("origin", url)
}
19 changes: 18 additions & 1 deletion components/serverless/internal/git/go2git.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,30 @@ func (g *git2GoClient) LastCommit(options Options) (string, error) {
return "", errors.Wrap(err, "while creating temporary directory")
}
defer os.RemoveAll(repoDir)

repo, err := g.fetchRepo(options, repoDir)
if err != nil {
return "", errors.Wrap(err, "while fetching the repository")
}
defer repo.Free()

//branch
ref, err := g.lookupBranch(repo, options.Reference)
if err == nil {
defer ref.Free()
return ref.Target().String(), nil
}
if !git2go.IsErrorCode(err, git2go.ErrorCodeNotFound) {
return "", errors.Wrap(err, "while lookup branch")
}

//tag
commit, err := g.lookupTag(repo, options.Reference)
if err != nil {
return "", errors.Wrap(err, "while lookup tag")
}
defer commit.Free()

return commit.Id().String(), nil
}

Expand All @@ -116,6 +121,7 @@ func (g *git2GoClient) Clone(path string, options Options) (string, error) {
if err != nil {
return "", errors.Wrap(err, "while lookup for commit")
}
defer commit.Free()

err = repo.ResetToCommit(commit, git2go.ResetHard, &git2go.CheckoutOptions{})
if err != nil {
Expand All @@ -126,6 +132,7 @@ func (g *git2GoClient) Clone(path string, options Options) (string, error) {
if err != nil {
return "", errors.Wrap(err, "while getting head")
}
defer ref.Free()

return ref.Target().String(), nil
}
Expand All @@ -135,6 +142,7 @@ func (g *git2GoClient) cloneRepo(opts Options, path string) (*git2go.Repository,
if err != nil {
return nil, errors.Wrap(err, "while getting authentication opts")
}

return g.git2goClone(opts.URL, path, authCallbacks)
}
func (g *git2GoClient) fetchRepo(opts Options, path string) (*git2go.Repository, error) {
Expand All @@ -150,6 +158,8 @@ func (g *git2GoClient) lookupBranch(repo *git2go.Repository, branchName string)
if err != nil {
return nil, errors.Wrap(err, "while creating reference iterator")
}
defer iter.Free()

for {
item, err := iter.Next()
if err != nil {
Expand All @@ -158,9 +168,12 @@ func (g *git2GoClient) lookupBranch(repo *git2go.Repository, branchName string)
}
return nil, errors.Wrap(err, "while listing reference")
}

if g.isBranch(item, branchName) {
return item, nil
}

defer item.Free()
}
}

Expand Down Expand Up @@ -190,6 +203,7 @@ func (g *git2GoClient) lookupTag(repo *git2go.Repository, tagName string) (*git2
}
return nil, errors.Wrap(err, "while creating dwim from tag name")
}
defer ref.Free()

if err = repo.SetHeadDetached(ref.Target()); err != nil {
return nil, errors.Wrapf(err, "while checkout to ref: %s", ref.Target().String())
Expand All @@ -198,10 +212,13 @@ func (g *git2GoClient) lookupTag(repo *git2go.Repository, tagName string) (*git2
if err != nil {
return nil, errors.Wrap(err, "while getting head")
}
defer head.Free()

commit, err := repo.LookupCommit(head.Target())
if err != nil {
return nil, errors.Wrap(err, "while getting commit from head")
}
defer commit.Free()

return commit, nil
}

0 comments on commit 0188c5b

Please sign in to comment.