Skip to content

Commit

Permalink
Add Tree#find_blob. Account for symlink blobs in Tree class. (#59)
Browse files Browse the repository at this point in the history
* Add Tree#find_blob. Account for symlink blobs in Tree class.
* Decomplicate file mode logic by always expecting an integer
  • Loading branch information
Dawa Ometto authored Mar 28, 2023
1 parent 0e619cd commit 68b083c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/rugged_adapter/git_layer_rugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,7 @@ def log(commit = Gollum::Git.default_ref_for_repo(@repo), path = nil, options =
def lstree(sha, options = {})
results = []
@repo.lookup(sha).tree.walk(:postorder) do |root, entry|
entry[:sha] = entry[:oid]
entry[:mode] = entry[:filemode].to_s(8)
entry[:type] = entry[:type].to_s
entry[:path] = "#{root}#{entry[:name]}"
results << entry
results << ::Gollum::Git::Tree.tree_entry_from_rugged_hash(entry, root)
end
results
end
Expand Down Expand Up @@ -689,6 +685,16 @@ def find_branch(search_list)

class Tree

def self.tree_entry_from_rugged_hash(entry, root = '')
{
sha: entry[:oid],
mode: entry[:filemode],
type: entry[:type].to_s,
name: entry[:name],
path: "#{root}#{entry[:name]}"
}
end

def initialize(tree)
@tree = tree
end
Expand Down Expand Up @@ -719,10 +725,22 @@ def /(file)

def blobs
blobs = []
@tree.each_blob {|blob| blobs << Gollum::Git::Blob.new(@tree.owner.lookup(blob[:oid]), blob) }
@tree.each_blob {|blob| blobs << blob_for_tree_entry(blob) }
blobs
end

def find_blob(&block)
return nil unless block_given?
blob = @tree.each_blob.find {|blob| yield blob[:name] }
blob ? blob_for_tree_entry(blob) : nil
end

private

def blob_for_tree_entry(blob)
Gollum::Git::Blob.new(@tree.owner.lookup(blob[:oid]), self.class.tree_entry_from_rugged_hash(blob))
end

end

end
Expand Down

0 comments on commit 68b083c

Please sign in to comment.