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

Repo#ls_tree #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 34 additions & 14 deletions lib/rugged_adapter/git_layer_rugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,25 @@ def log(commit = Gollum::Git.default_ref_for_repo(@repo), path = nil, options =
git.log(commit, path, **options)
end

def lstree(sha, options = {})
results = []
@repo.lookup(sha).tree.walk(:postorder) do |root, entry|
results << ::Gollum::Git::Tree.tree_entry_from_rugged_hash(entry, root)
def lstree(sha, path = '/', options = {})

tree = @repo.lookup(sha).tree
if path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

tree = @repo.lookup(sha).tree
tree = tree / path if path

Instead of the if/else?

tree = Gollum::Git::Tree.rugged_tree_path(tree, path)
return nil unless tree
end

if options.fetch(:recursive, true)
results = []
tree.walk(:postorder) do |root, entry|
results << ::Gollum::Git::Tree.tree_entry_from_rugged_hash(entry, root)
end
results
else
tree.map do |entry|
Copy link
Member

@bartkamphorst bartkamphorst Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tree.map { |entry| Gollum::Git::Tree.tree_entry_from_rugged_hash(entry, root) }

Gollum::Git::Tree.tree_entry_from_rugged_hash(entry, path)
end
end
results
end

def path
Expand Down Expand Up @@ -685,6 +698,19 @@ def find_branch(search_list)

class Tree

attr_reader :tree

def self.rugged_tree_path(tree, path)
return tree if path == '/'
begin
obj = tree.path(path)
rescue Rugged::TreeError
return nil
end
return nil if obj.nil?
tree.owner.lookup(obj[:oid])
end

def self.tree_entry_from_rugged_hash(entry, root = '')
{
sha: entry[:oid],
Expand All @@ -711,16 +737,10 @@ def id
@tree.oid
end

def /(file)
return self if file == '/'
begin
obj = @tree.path(file)
rescue Rugged::TreeError
return nil
end
def /(path)
obj = self.class.rugged_tree_path(@tree, path)
return nil if obj.nil?
obj = @tree.owner.lookup(obj[:oid])
obj.is_a?(Rugged::Tree) ? Gollum::Git::Tree.new(obj) : Gollum::Git::Blob.new(obj)
(obj).is_a?(Rugged::Tree) ? Gollum::Git::Tree.new(obj) : Gollum::Git::Blob.new(obj)
end

def blobs
Expand Down
Loading