-
Notifications
You must be signed in to change notification settings - Fork 245
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
Add a Git-backed storage.ReadBucket
via storagegit
#2114
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
adcb9fa
Add a Git-backed `storage.ReadBucket` via `storagegit`
saquibmian e1be76c
Fix lint
saquibmian 4abbae1
Fix test (bad stage)
saquibmian 272ef82
Fix lint
saquibmian 880c8bb
Fix Windows test
saquibmian c057bd6
PR feedback
saquibmian 4d06edc
s/TreeEntry/Node
saquibmian 9e154b1
s/entry/node
saquibmian 29ab534
Symlink guarding
saquibmian 90c274b
s/root/parent
saquibmian 41b3a97
Adds some comments on the parsing logic
saquibmian ddc7fcc
Fix test
saquibmian 73ec4cb
Merge branch 'main' into saquib/bsr-1896-implement-storagegit
saquibmian 41b5e6b
Make all tests parallel
saquibmian 07fb6c3
s/FileMode/ObjectMode
saquibmian 45e3331
s/Node/TreeNode
saquibmian 46a3c26
s/Tree.Traverse/Tree.Descendant
saquibmian 05f3da7
Bunch of PR comments
saquibmian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2020-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package git | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type tree struct { | ||
hash Hash | ||
nodes []Node | ||
} | ||
|
||
func parseTree(hash Hash, data []byte) (*tree, error) { | ||
saquibmian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t := &tree{ | ||
hash: hash, | ||
} | ||
/* | ||
data is in the format | ||
<mode><space><name>\0<hash> | ||
repeated | ||
*/ | ||
for len(data) > 0 { | ||
// We can find the \0 character before the <hash> | ||
// and slice to the index of \0 + the length of a hash. | ||
// That gives us a single node. | ||
i := bytes.Index(data, []byte{0}) | ||
if i == -1 { | ||
return nil, errors.New("malformed tree") | ||
} | ||
length := i + 1 + hashLength | ||
node, err := parseTreeNode(data[:length]) | ||
if err != nil { | ||
return nil, fmt.Errorf("malformed tree: %w", err) | ||
saquibmian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
t.nodes = append(t.nodes, node) | ||
data = data[length:] | ||
} | ||
return t, nil | ||
} | ||
|
||
func (t *tree) Hash() Hash { | ||
return t.hash | ||
} | ||
|
||
func (t *tree) Nodes() []Node { | ||
return t.nodes | ||
} | ||
|
||
func (t *tree) Traverse(objectReader ObjectReader, names ...string) (Node, error) { | ||
return traverse(objectReader, t, names...) | ||
} | ||
|
||
func traverse( | ||
objectReader ObjectReader, | ||
root Tree, | ||
names ...string, | ||
) (Node, error) { | ||
// split by the name of the next node we're looking for | ||
// and the names of the descendant nodes | ||
name := names[0] | ||
if len(names) >= 2 { | ||
names = names[1:] | ||
} else { | ||
names = nil | ||
} | ||
saquibmian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Find node with that name in this tree. | ||
var found Node | ||
for _, node := range root.Nodes() { | ||
if node.Name() == name { | ||
found = node | ||
break | ||
} | ||
} | ||
if found == nil { | ||
// No node with that name in this tree. | ||
return nil, ErrSubTreeNotFound | ||
} | ||
if len(names) == 0 { | ||
// No more descendants, we've found our terminal node. | ||
return found, nil | ||
} | ||
if found.Mode() != ModeDir { | ||
// This is an intermediate (non-terminal) node, which are expected to be | ||
// directories. This is node is not a directory, so we fail with a non-found | ||
// errror. | ||
return nil, ErrSubTreeNotFound | ||
} | ||
// TODO: support symlinks (on intermediate dirs) with traverse option | ||
// Descend down and traverse. | ||
tree, err := objectReader.Tree(found.Hash()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return traverse(objectReader, tree, names...) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the left padded zeroes in all of them? I think at least one zero can be removed in all of them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, these are octal literals :)