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

Commit d945eda

Browse files
HoffmannPlafriks
authored andcommitted
Blob api (#132)
* Add repo_blob This adds a new Repository.GetBlob(id) method for use by gitea. * This is a follow-up for PR #121 to implement blob_api including full test coverage Signed-off-by: Berengar W. Lehr <[email protected]>
1 parent f3afe30 commit d945eda

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
"fmt"
9+
"io/ioutil"
10+
"path/filepath"
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestRepository_GetBlob_Found(t *testing.T) {
17+
repoPath := filepath.Join(testReposDir, "repo1_bare")
18+
r, err := OpenRepository(repoPath)
19+
assert.NoError(t, err)
20+
21+
testCases := []struct {
22+
OID string
23+
Data []byte
24+
}{
25+
{"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")},
26+
{"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")},
27+
}
28+
29+
for _, testCase := range testCases {
30+
blob, err := r.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+
}
41+
42+
func TestRepository_GetBlob_NotExist(t *testing.T) {
43+
repoPath := filepath.Join(testReposDir, "repo1_bare")
44+
r, err := OpenRepository(repoPath)
45+
assert.NoError(t, err)
46+
47+
testCase := "0000000000000000000000000000000000000000"
48+
testError := ErrNotExist{testCase, ""}
49+
50+
blob, err := r.GetBlob(testCase)
51+
assert.Nil(t, blob)
52+
assert.EqualError(t, err, testError.Error())
53+
}
54+
55+
func TestRepository_GetBlob_NoId(t *testing.T) {
56+
repoPath := filepath.Join(testReposDir, "repo1_bare")
57+
r, err := OpenRepository(repoPath)
58+
assert.NoError(t, err)
59+
60+
testCase := ""
61+
testError := fmt.Errorf("Length must be 40: %s", testCase)
62+
63+
blob, err := r.GetBlob(testCase)
64+
assert.Nil(t, blob)
65+
assert.EqualError(t, err, testError.Error())
66+
}

0 commit comments

Comments
 (0)