|
1 | 1 | // Copyright 2015 The Gogs Authors. All rights reserved. |
| 2 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
2 | 3 | // Use of this source code is governed by a MIT-style |
3 | 4 | // license that can be found in the LICENSE file. |
4 | 5 |
|
5 | 6 | package git |
6 | 7 |
|
7 | 8 | import ( |
8 | | - "bytes" |
9 | 9 | "encoding/base64" |
10 | | - "fmt" |
11 | 10 | "io" |
12 | 11 | "io/ioutil" |
13 | | - "os" |
14 | | - "os/exec" |
| 12 | + |
| 13 | + "gopkg.in/src-d/go-git.v4/plumbing" |
15 | 14 | ) |
16 | 15 |
|
17 | 16 | // Blob represents a Git object. |
18 | 17 | type Blob struct { |
19 | | - repo *Repository |
20 | | - *TreeEntry |
21 | | -} |
22 | | - |
23 | | -// Data gets content of blob all at once and wrap it as io.Reader. |
24 | | -// This can be very slow and memory consuming for huge content. |
25 | | -func (b *Blob) Data() (io.Reader, error) { |
26 | | - stdout := new(bytes.Buffer) |
27 | | - stderr := new(bytes.Buffer) |
28 | | - |
29 | | - // Preallocate memory to save ~50% memory usage on big files. |
30 | | - stdout.Grow(int(b.Size() + 2048)) |
31 | | - |
32 | | - if err := b.DataPipeline(stdout, stderr); err != nil { |
33 | | - return nil, concatenateError(err, stderr.String()) |
34 | | - } |
35 | | - return stdout, nil |
36 | | -} |
| 18 | + ID SHA1 |
37 | 19 |
|
38 | | -// DataPipeline gets content of blob and write the result or error to stdout or stderr |
39 | | -func (b *Blob) DataPipeline(stdout, stderr io.Writer) error { |
40 | | - return NewCommand("show", b.ID.String()).RunInDirPipeline(b.repo.Path, stdout, stderr) |
41 | | -} |
42 | | - |
43 | | -type cmdReadCloser struct { |
44 | | - cmd *exec.Cmd |
45 | | - stdout io.Reader |
46 | | -} |
47 | | - |
48 | | -func (c cmdReadCloser) Read(p []byte) (int, error) { |
49 | | - return c.stdout.Read(p) |
50 | | -} |
51 | | - |
52 | | -func (c cmdReadCloser) Close() error { |
53 | | - io.Copy(ioutil.Discard, c.stdout) |
54 | | - return c.cmd.Wait() |
| 20 | + gogitEncodedObj plumbing.EncodedObject |
| 21 | + name string |
55 | 22 | } |
56 | 23 |
|
57 | 24 | // DataAsync gets a ReadCloser for the contents of a blob without reading it all. |
58 | 25 | // Calling the Close function on the result will discard all unread output. |
59 | 26 | func (b *Blob) DataAsync() (io.ReadCloser, error) { |
60 | | - cmd := exec.Command("git", "show", b.ID.String()) |
61 | | - cmd.Dir = b.repo.Path |
62 | | - cmd.Stderr = os.Stderr |
63 | | - |
64 | | - stdout, err := cmd.StdoutPipe() |
65 | | - if err != nil { |
66 | | - return nil, fmt.Errorf("StdoutPipe: %v", err) |
67 | | - } |
| 27 | + return b.gogitEncodedObj.Reader() |
| 28 | +} |
68 | 29 |
|
69 | | - if err = cmd.Start(); err != nil { |
70 | | - return nil, fmt.Errorf("Start: %v", err) |
71 | | - } |
| 30 | +// Size returns the uncompressed size of the blob |
| 31 | +func (b *Blob) Size() int64 { |
| 32 | + return b.gogitEncodedObj.Size() |
| 33 | +} |
72 | 34 |
|
73 | | - return cmdReadCloser{stdout: stdout, cmd: cmd}, nil |
| 35 | +// Name returns name of the tree entry this blob object was created from (or empty string) |
| 36 | +func (b *Blob) Name() string { |
| 37 | + return b.name |
74 | 38 | } |
75 | 39 |
|
76 | 40 | // GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string |
|
0 commit comments