Skip to content

Commit e7274c8

Browse files
committed
Use go-git for reading blobs.
Signed-off-by: Filip Navara <[email protected]>
1 parent 0b6dbfb commit e7274c8

File tree

7 files changed

+16
-58
lines changed

7 files changed

+16
-58
lines changed

modules/context/repo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
142142
if treeEntry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
143143
return nil, git.ErrNotExist{ID: "", RelPath: ".editorconfig"}
144144
}
145-
reader, err := treeEntry.Blob().Data()
145+
reader, err := treeEntry.Blob().DataAsync()
146146
if err != nil {
147147
return nil, err
148148
}
149+
defer reader.Close()
149150
data, err := ioutil.ReadAll(reader)
150151
if err != nil {
151152
return nil, err

modules/git/blob.go

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
package git
66

77
import (
8-
"bytes"
9-
"fmt"
108
"io"
11-
"io/ioutil"
12-
"os"
13-
"os/exec"
149
)
1510

1611
// Blob represents a Git object.
@@ -19,55 +14,13 @@ type Blob struct {
1914
*TreeEntry
2015
}
2116

22-
// Data gets content of blob all at once and wrap it as io.Reader.
23-
// This can be very slow and memory consuming for huge content.
24-
func (b *Blob) Data() (io.Reader, error) {
25-
stdout := new(bytes.Buffer)
26-
stderr := new(bytes.Buffer)
27-
28-
// Preallocate memory to save ~50% memory usage on big files.
29-
stdout.Grow(int(b.Size() + 2048))
30-
31-
if err := b.DataPipeline(stdout, stderr); err != nil {
32-
return nil, concatenateError(err, stderr.String())
33-
}
34-
return stdout, nil
35-
}
36-
37-
// DataPipeline gets content of blob and write the result or error to stdout or stderr
38-
func (b *Blob) DataPipeline(stdout, stderr io.Writer) error {
39-
return NewCommand("show", b.ID.String()).RunInDirPipeline(b.repo.Path, stdout, stderr)
40-
}
41-
42-
type cmdReadCloser struct {
43-
cmd *exec.Cmd
44-
stdout io.Reader
45-
}
46-
47-
func (c cmdReadCloser) Read(p []byte) (int, error) {
48-
return c.stdout.Read(p)
49-
}
50-
51-
func (c cmdReadCloser) Close() error {
52-
io.Copy(ioutil.Discard, c.stdout)
53-
return c.cmd.Wait()
54-
}
55-
5617
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
5718
// Calling the Close function on the result will discard all unread output.
5819
func (b *Blob) DataAsync() (io.ReadCloser, error) {
59-
cmd := exec.Command("git", "show", b.ID.String())
60-
cmd.Dir = b.repo.Path
61-
cmd.Stderr = os.Stderr
62-
63-
stdout, err := cmd.StdoutPipe()
20+
gogitBlob, err := b.repo.gogitRepo.BlobObject(b.gogitTreeEntry.Hash)
6421
if err != nil {
65-
return nil, fmt.Errorf("StdoutPipe: %v", err)
66-
}
67-
68-
if err = cmd.Start(); err != nil {
69-
return nil, fmt.Errorf("Start: %v", err)
22+
return nil, err
7023
}
7124

72-
return cmdReadCloser{stdout: stdout, cmd: cmd}, nil
25+
return gogitBlob.Reader()
7326
}

modules/git/commit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,13 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
237237
}
238238
return nil, err
239239
}
240-
rd, err := entry.Blob().Data()
240+
241+
rd, err := entry.Blob().DataAsync()
241242
if err != nil {
242243
return nil, err
243244
}
244245

246+
defer rd.Close()
245247
scanner := bufio.NewScanner(rd)
246248
c.submoduleCache = newObjectCache()
247249
var ismodule bool

modules/git/tree_entry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ func (te *TreeEntry) FollowLink() (*TreeEntry, error) {
106106
}
107107

108108
// read the link
109-
r, err := te.Blob().Data()
109+
r, err := te.Blob().DataAsync()
110110
if err != nil {
111111
return nil, err
112112
}
113+
defer r.Close()
113114
buf := make([]byte, te.Size())
114115
_, err = io.ReadFull(r, buf)
115116
if err != nil {

routers/repo/editor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ func editFile(ctx *context.Context, isNewFile bool) {
9494
return
9595
}
9696

97-
dataRc, err := blob.Data()
97+
dataRc, err := blob.DataAsync()
9898
if err != nil {
9999
ctx.NotFound("blob.Data", err)
100100
return
101101
}
102+
defer dataRc.Close()
102103

103104
ctx.Data["FileSize"] = blob.Size()
104105
ctx.Data["FileName"] = blob.Name()

routers/repo/issue.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"bytes"
1010
"errors"
1111
"fmt"
12-
"io"
1312
"io/ioutil"
1413
"net/http"
1514
"strconv"
@@ -355,7 +354,6 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.
355354
}
356355

357356
func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) {
358-
var r io.Reader
359357
var bytes []byte
360358

361359
if ctx.Repo.Commit == nil {
@@ -373,10 +371,11 @@ func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (str
373371
if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
374372
return "", false
375373
}
376-
r, err = entry.Blob().Data()
374+
r, err := entry.Blob().DataAsync()
377375
if err != nil {
378376
return "", false
379377
}
378+
defer r.Close()
380379
bytes, err = ioutil.ReadAll(r)
381380
if err != nil {
382381
return "", false

routers/repo/wiki.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
8181
// wikiContentsByEntry returns the contents of the wiki page referenced by the
8282
// given tree entry. Writes to ctx if an error occurs.
8383
func wikiContentsByEntry(ctx *context.Context, entry *git.TreeEntry) []byte {
84-
reader, err := entry.Blob().Data()
84+
reader, err := entry.Blob().DataAsync()
8585
if err != nil {
8686
ctx.ServerError("Blob.Data", err)
8787
return nil
8888
}
89+
defer reader.Close()
8990
content, err := ioutil.ReadAll(reader)
9091
if err != nil {
9192
ctx.ServerError("ReadAll", err)

0 commit comments

Comments
 (0)