Skip to content

Commit 56201f3

Browse files
committed
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 <[email protected]>
1 parent d9473d0 commit 56201f3

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

pkg/git/libgit2/checkout.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
7676
if err != nil {
7777
return nil, "", fmt.Errorf("git resolve HEAD error: %w", err)
7878
}
79+
defer head.Free()
7980
commit, err := repo.LookupCommit(head.Target())
8081
if err != nil {
8182
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
168169
// Due to this, first attempt to resolve it as a simple tag (commit), but fallback to attempting to
169170
// resolve it as an annotated tag in case this results in an error.
170171
if c, err := repo.LookupCommit(id); err == nil {
172+
defer c.Free()
171173
// Use the commit metadata as the decisive timestamp.
172174
tagTimestamps[cleanName] = c.Committer().When
173175
tags[cleanName] = name
@@ -177,14 +179,17 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
177179
if err != nil {
178180
return fmt.Errorf("could not lookup '%s' as simple or annotated tag: %w", cleanName, err)
179181
}
182+
defer t.Free()
180183
commit, err := t.Peel(git2go.ObjectCommit)
181184
if err != nil {
182185
return fmt.Errorf("could not get commit for tag '%s': %w", t.Name(), err)
183186
}
187+
defer commit.Free()
184188
c, err := commit.AsCommit()
185189
if err != nil {
186190
return fmt.Errorf("could not get commit object for tag '%s': %w", t.Name(), err)
187191
}
192+
defer c.Free()
188193
tagTimestamps[t.Name()] = c.Committer().When
189194
tags[t.Name()] = name
190195
return nil

0 commit comments

Comments
 (0)