From 60a8cc8b071b9c5dc82f469f24d9b834c37d78cd Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Fri, 22 Oct 2021 11:52:10 +0200 Subject: [PATCH] libgit2: Free most objects This commit ensures most of the `git2go` objects `Free` themselves from the underlying C object. Ensuring all objects are freed is not possible yet, due to the way commits are wired in to facilitate verification later on. In a later follow up, we should change this and e.g. validate as part of the checkout process, and move the implementation specific authentication configuration from `git` into `libgit2`. Signed-off-by: Hidde Beydals --- pkg/git/libgit2/checkout.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/git/libgit2/checkout.go b/pkg/git/libgit2/checkout.go index cff1059ed..7fcfe4512 100644 --- a/pkg/git/libgit2/checkout.go +++ b/pkg/git/libgit2/checkout.go @@ -76,6 +76,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g if err != nil { return nil, "", fmt.Errorf("git resolve HEAD error: %w", err) } + defer head.Free() commit, err := repo.LookupCommit(head.Target()) if err != nil { return nil, "", fmt.Errorf("git commit '%s' not found: %w", head.Target(), err) @@ -168,6 +169,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g // Due to this, first attempt to resolve it as a simple tag (commit), but fallback to attempting to // resolve it as an annotated tag in case this results in an error. if c, err := repo.LookupCommit(id); err == nil { + defer c.Free() // Use the commit metadata as the decisive timestamp. tagTimestamps[cleanName] = c.Committer().When tags[cleanName] = name @@ -177,14 +179,17 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g if err != nil { return fmt.Errorf("could not lookup '%s' as simple or annotated tag: %w", cleanName, err) } + defer t.Free() commit, err := t.Peel(git2go.ObjectCommit) if err != nil { return fmt.Errorf("could not get commit for tag '%s': %w", t.Name(), err) } + defer commit.Free() c, err := commit.AsCommit() if err != nil { return fmt.Errorf("could not get commit object for tag '%s': %w", t.Name(), err) } + defer c.Free() tagTimestamps[t.Name()] = c.Committer().When tags[t.Name()] = name return nil