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

Fix path handling in storagegit #2282

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions private/pkg/storage/storagegit/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func newBucket(
}

func (b *bucket) Get(ctx context.Context, path string) (storage.ReadObjectCloser, error) {
path, err := storageutil.ValidatePath(path)
if err != nil {
return nil, err
}
node, err := b.root.Descendant(path, b.objectReader)
if err != nil {
if errors.Is(err, git.ErrTreeNodeNotFound) {
Expand Down Expand Up @@ -76,7 +80,7 @@ func (b *bucket) Get(ctx context.Context, path string) (storage.ReadObjectCloser
path, err := normalpath.NormalizeAndValidate(
normalpath.Join(
normalpath.Base(path),
normalpath.Normalize(string(data)),
string(data),
),
)
if err != nil {
Expand Down Expand Up @@ -110,6 +114,10 @@ func (b *bucket) Stat(ctx context.Context, path string) (storage.ObjectInfo, err
}

func (b *bucket) Walk(ctx context.Context, prefix string, f func(storage.ObjectInfo) error) error {
prefix, err := storageutil.ValidatePrefix(prefix)
if err != nil {
return err
}
walkChecker := storageutil.NewWalkChecker()
return b.walk(b.root, b.objectReader, prefix, func(path string) error {
if err := walkChecker.Check(ctx); err != nil {
Expand All @@ -125,7 +133,6 @@ func (b *bucket) walk(
prefix string,
walkFn func(string) error,
) error {
prefix = normalpath.Normalize(prefix)
if prefix != "." {
node, err := parent.Descendant(prefix, b.objectReader)
if err != nil {
Expand Down Expand Up @@ -180,10 +187,11 @@ func (b *bucket) walkTree(
return nil
}

// path is expected to be normalized by calling functions
func (b *bucket) newObjectInfo(path string) storage.ObjectInfo {
return storageutil.NewObjectInfo(
normalpath.Normalize(path),
normalpath.Unnormalize(path),
path,
path,
)
}

Expand Down
3 changes: 1 addition & 2 deletions private/pkg/storage/storagegit/storagegit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/git/gittest"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/storage/storagetesting"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestNewBucketAtTreeHash(t *testing.T) {
t,
bucket,
"proto/acme/grocerystore/v1/c.proto",
normalpath.Unnormalize("proto/acme/grocerystore/v1/c.proto"),
"proto/acme/grocerystore/v1/c.proto",
)
storagetesting.AssertNotExist(t, bucket, "random-path")
storagetesting.AssertPathToContent(
Expand Down