Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repo_blob): add CatFileBlob #79

Merged
merged 8 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions repo_blob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

// CatFileBlob returns the blob corresponding to the given revision of the repository.
func (repo *Repository) CatFileBlob(rev string) (*Blob, error) {
unknwon marked this conversation as resolved.
Show resolved Hide resolved
rev, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint
if err != nil {
return nil, err
}

typ, err := repo.CatFileType(rev)
unknwon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

if typ != ObjectBlob {
return nil, ErrNotBlob
}

return &Blob{
TreeEntry: &TreeEntry{
mode: EntryBlob,
typ: ObjectBlob,
id: MustIDFromString(rev),
jonatan5524 marked this conversation as resolved.
Show resolved Hide resolved
parent: &Tree{
repo: repo,
unknwon marked this conversation as resolved.
Show resolved Hide resolved
},
},
}, nil
}
31 changes: 31 additions & 0 deletions repo_blob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import (
"testing"

"github.com/stretchr/testify/assert"
unknwon marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/require"
)

func TestRepository_CatFileBlob(t *testing.T) {
t.Run("not a blob", func(t *testing.T) {
_, err := testrepo.CatFileBlob("007cb92318c7bd3b56908ea8c2e54370245562f8")
assert.Equal(t, ErrNotBlob, err)
})

t.Run("get a blob, no full rev hash", func(t *testing.T) {
b, err := testrepo.CatFileBlob("021a")
require.NoError(t, err)
assert.True(t, b.IsBlob())
})

t.Run("get a blob", func(t *testing.T) {
b, err := testrepo.CatFileBlob("021a721a61a1de65865542c405796d1eb985f784")
jonatan5524 marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
assert.True(t, b.IsBlob())
})
}