From 1e3063f53a1d5abbd5ec1c0eccd357a5941dc50f Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Sun, 15 Jan 2023 22:45:30 +0100 Subject: [PATCH 1/3] Add Tree#find_blob. Account for symlink blobs in Tree class. --- lib/rugged_adapter/git_layer_rugged.rb | 29 ++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/rugged_adapter/git_layer_rugged.rb b/lib/rugged_adapter/git_layer_rugged.rb index 4a79162..bf2a37a 100644 --- a/lib/rugged_adapter/git_layer_rugged.rb +++ b/lib/rugged_adapter/git_layer_rugged.rb @@ -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 @@ -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].to_s(8), + type: entry[:type].to_s, + name: "#{root}#{entry[:name]}", + path: "#{root}#{entry[:path]}", # Repo#lstree is expected to give back an object containing :path rather than :name + } + end + def initialize(tree) @tree = tree end @@ -719,10 +725,21 @@ 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(name) + blob = @tree.each_blob.find {|blob| blob[:name] == 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 From d7881be764940160011e5f9fd39d2bf6a83aa8dd Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 16 Jan 2023 00:35:14 +0100 Subject: [PATCH 2/3] Decomplicate file mode logic by always expecting an integer --- lib/rugged_adapter/git_layer_rugged.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rugged_adapter/git_layer_rugged.rb b/lib/rugged_adapter/git_layer_rugged.rb index bf2a37a..8413137 100644 --- a/lib/rugged_adapter/git_layer_rugged.rb +++ b/lib/rugged_adapter/git_layer_rugged.rb @@ -688,10 +688,10 @@ class Tree def self.tree_entry_from_rugged_hash(entry, root = '') { sha: entry[:oid], - mode: entry[:filemode].to_s(8), + mode: entry[:filemode], type: entry[:type].to_s, - name: "#{root}#{entry[:name]}", - path: "#{root}#{entry[:path]}", # Repo#lstree is expected to give back an object containing :path rather than :name + name: entry[:name], + path: "#{root}#{entry[:name]}" } end From 9d60ba2a8f8f73edf67321e750ff9fa3305c21b9 Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Mon, 16 Jan 2023 01:36:07 +0100 Subject: [PATCH 3/3] Make Tree#find_blob more flexible --- lib/rugged_adapter/git_layer_rugged.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rugged_adapter/git_layer_rugged.rb b/lib/rugged_adapter/git_layer_rugged.rb index 8413137..18bdb2b 100644 --- a/lib/rugged_adapter/git_layer_rugged.rb +++ b/lib/rugged_adapter/git_layer_rugged.rb @@ -729,8 +729,9 @@ def blobs blobs end - def find_blob(name) - blob = @tree.each_blob.find {|blob| blob[:name] == name} + 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