mirror of
https://github.com/go-gitea/gitea.git
synced 2026-02-07 09:49:41 +09:00
@@ -9,24 +9,38 @@ package git
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Blob represents a Git object.
|
// Blob represents a Git object.
|
||||||
type Blob struct {
|
type Blob struct {
|
||||||
ID ObjectID
|
ID ObjectID
|
||||||
|
repo *Repository
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
gogitEncodedObj plumbing.EncodedObject
|
func (b *Blob) gogitEncodedObj() (plumbing.EncodedObject, error) {
|
||||||
name string
|
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.
|
// 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.
|
// Calling the Close function on the result will discard all unread output.
|
||||||
func (b *Blob) DataAsync() (io.ReadCloser, error) {
|
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
|
// Size returns the uncompressed size of the blob
|
||||||
func (b *Blob) Size() int64 {
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,11 @@ func (repo *Repository) GetBlob(idStr string) (*Blob, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return repo.getBlob(id)
|
if id.IsZero() {
|
||||||
|
return nil, ErrNotExist{id.String(), ""}
|
||||||
|
}
|
||||||
|
return &Blob{
|
||||||
|
ID: id,
|
||||||
|
repo: repo,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -53,14 +53,9 @@ func (te *TreeEntry) Size() int64 {
|
|||||||
|
|
||||||
// Blob returns the blob object the entry
|
// Blob returns the blob object the entry
|
||||||
func (te *TreeEntry) Blob() *Blob {
|
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{
|
return &Blob{
|
||||||
ID: te.ID,
|
ID: te.ID,
|
||||||
gogitEncodedObj: encodedObj,
|
repo: te.ptree.repo,
|
||||||
name: te.Name(),
|
name: te.Name(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user