diff --git a/modules/git/blob_gogit.go b/modules/git/blob_gogit.go index 8c79c067c1..7cf622d180 100644 --- a/modules/git/blob_gogit.go +++ b/modules/git/blob_gogit.go @@ -9,24 +9,38 @@ package git import ( "io" + "code.gitea.io/gitea/modules/log" + "github.com/go-git/go-git/v5/plumbing" ) // Blob represents a Git object. type Blob struct { - ID ObjectID + ID ObjectID + repo *Repository + name string +} - gogitEncodedObj plumbing.EncodedObject - name string +func (b *Blob) gogitEncodedObj() (plumbing.EncodedObject, error) { + return b.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, plumbing.Hash(b.ID.RawValue())) } // DataAsync gets a ReadCloser for the contents of a blob without reading it all. // Calling the Close function on the result will discard all unread output. func (b *Blob) DataAsync() (io.ReadCloser, error) { - return b.gogitEncodedObj.Reader() + obj, err := b.gogitEncodedObj() + if err != nil { + return nil, err + } + return obj.Reader() } // Size returns the uncompressed size of the blob func (b *Blob) Size() int64 { - return b.gogitEncodedObj.Size() + obj, err := b.gogitEncodedObj() + if err != nil { + log.Error("Error getting gogit encoded object for blob %s(%s): %v", b.name, b.ID.String(), err) + return 0 + } + return obj.Size() } diff --git a/modules/git/repo_blob.go b/modules/git/repo_blob.go index 698b6c7074..ff930a3432 100644 --- a/modules/git/repo_blob.go +++ b/modules/git/repo_blob.go @@ -9,5 +9,11 @@ func (repo *Repository) GetBlob(idStr string) (*Blob, error) { if err != nil { return nil, err } - return repo.getBlob(id) + if id.IsZero() { + return nil, ErrNotExist{id.String(), ""} + } + return &Blob{ + ID: id, + repo: repo, + }, nil } diff --git a/modules/git/repo_blob_gogit.go b/modules/git/repo_blob_gogit.go deleted file mode 100644 index 66c8c2775c..0000000000 --- a/modules/git/repo_blob_gogit.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2018 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -//go:build gogit - -package git - -import ( - "github.com/go-git/go-git/v5/plumbing" -) - -func (repo *Repository) getBlob(id ObjectID) (*Blob, error) { - encodedObj, err := repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, plumbing.Hash(id.RawValue())) - if err != nil { - return nil, ErrNotExist{id.String(), ""} - } - - return &Blob{ - ID: id, - gogitEncodedObj: encodedObj, - }, nil -} diff --git a/modules/git/repo_blob_nogogit.go b/modules/git/repo_blob_nogogit.go deleted file mode 100644 index 04b0fb00ff..0000000000 --- a/modules/git/repo_blob_nogogit.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -//go:build !gogit - -package git - -func (repo *Repository) getBlob(id ObjectID) (*Blob, error) { - if id.IsZero() { - return nil, ErrNotExist{id.String(), ""} - } - return &Blob{ - ID: id, - repo: repo, - }, nil -} diff --git a/modules/git/tree_entry_gogit.go b/modules/git/tree_entry_gogit.go index 27877a2e28..f092e70636 100644 --- a/modules/git/tree_entry_gogit.go +++ b/modules/git/tree_entry_gogit.go @@ -53,14 +53,9 @@ func (te *TreeEntry) Size() int64 { // Blob returns the blob object the entry func (te *TreeEntry) Blob() *Blob { - encodedObj, err := te.ptree.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, te.toGogitTreeEntry().Hash) - if err != nil { - return nil - } - return &Blob{ - ID: te.ID, - gogitEncodedObj: encodedObj, - name: te.Name(), + ID: te.ID, + repo: te.ptree.repo, + name: te.Name(), } }