Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 26fb1c7

Browse files
committed
Add repo_blob
This adds a new Repository.GetBlob(id) method for use by gitea.
1 parent 31f4b8e commit 26fb1c7

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

repo_blob.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
func (repo *Repository) getBlob(id SHA1) (*Blob, error) {
8+
if _, err := NewCommand("cat-file", "-p", id.String()).RunInDir(repo.Path); err != nil {
9+
return nil, ErrNotExist{id.String(), ""}
10+
}
11+
12+
return &Blob{
13+
repo: repo,
14+
TreeEntry: &TreeEntry{
15+
ID: id,
16+
ptree: &Tree{
17+
repo: repo,
18+
},
19+
},
20+
}, nil
21+
}
22+
23+
// GetBlob finds the blob object in the repository.
24+
func (repo *Repository) GetBlob(idStr string) (*Blob, error) {
25+
id, err := NewIDFromString(idStr)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return repo.getBlob(id)
30+
}

repo_blob_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package git
6+
7+
import (
8+
"io/ioutil"
9+
"path/filepath"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestRepository_GetBlob(t *testing.T) {
16+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
17+
bareRepo1, err := OpenRepository(bareRepo1Path)
18+
assert.NoError(t, err)
19+
20+
testCases := []struct {
21+
OID string
22+
Data []byte
23+
}{
24+
{"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")},
25+
{"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")},
26+
{"b1fc9917b618c924cf4aa421dae74e8bf9b556d3", []byte("Hi\n")},
27+
}
28+
29+
for _, testCase := range testCases {
30+
blob, err := bareRepo1.GetBlob(testCase.OID)
31+
assert.NoError(t, err)
32+
33+
dataReader, err := blob.Data()
34+
assert.NoError(t, err)
35+
36+
data, err := ioutil.ReadAll(dataReader)
37+
assert.NoError(t, err)
38+
assert.Equal(t, testCase.Data, data)
39+
}
40+
}

0 commit comments

Comments
 (0)