diff options
author | ytti <saku@ytti.fi> | 2016-07-22 13:18:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-22 13:18:14 +0200 |
commit | dd6f5df411a3ba0e9158d6c1fc5293aa53c47a29 (patch) | |
tree | a3ea3f5c90449c239ee033f71232b1f1f85fe0ae /lib/oxidized | |
parent | 63b1bd788e0807f935e0a69c0e3589f9c041206c (diff) | |
parent | de2a9d18a5847439b40adfc0cc4c75e6cbf6262d (diff) |
Merge pull request #503 from danilopopeye/ds-fixes-and-refactors
fix repository path resolution
Diffstat (limited to 'lib/oxidized')
-rw-r--r-- | lib/oxidized/node.rb | 8 | ||||
-rw-r--r-- | lib/oxidized/nodes.rb | 54 | ||||
-rw-r--r-- | lib/oxidized/output/git.rb | 50 |
3 files changed, 52 insertions, 60 deletions
diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index 9f729ed..3e46b17 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -174,8 +174,12 @@ module Oxidized def resolve_repo remote_repo = Oxidized.config.output.git.repo - if Oxidized.config.output.git.single_repo? || @group.nil? || remote_repo.is_a?(String) - remote_repo + if remote_repo.is_a?(::String) + if Oxidized.config.output.git.single_repo? || @group.nil? + remote_repo + else + File.join(File.dirname(remote_repo), @group + '.git') + end else remote_repo[@group] end diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index cd67007..f5a1ad0 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -56,11 +56,8 @@ module Oxidized end end - def fetch node, group - with_lock do - i = find_node_index node - output = self[i].output.new - raise Oxidized::NotSupported unless output.respond_to? :fetch + def fetch node_name, group + yield_node_output(node_name) do |node, output| output.fetch node, group end end @@ -94,6 +91,24 @@ module Oxidized find_index node or raise Oxidized::NodeNotFound, "unable to find '#{node}'" end + def version node_name, group + yield_node_output(node_name) do |node, output| + output.version node, group + end + end + + def get_version node_name, group, oid + yield_node_output(node_name) do |node, output| + output.get_version node, group, oid + end + end + + def get_diff node_name, group, oid1, oid2 + yield_node_output(node_name) do |node, output| + output.get_diff node, group, oid1, oid2 + end + end + private def initialize opts={} @@ -151,34 +166,13 @@ module Oxidized sort_by! { |x| x.last.nil? ? Time.new(0) : x.last.end } end - public - - def version node, group + def yield_node_output(node_name) with_lock do - i = find_node_index node - output = self[i].output.new + node = find { |n| n.name == node_name } + output = node.output.new raise Oxidized::NotSupported unless output.respond_to? :fetch - output.version node, group - end - end - - def get_version node, group, oid - with_lock do - i = find_node_index node - output = self[i].output.new - raise Oxidized::NotSupported unless output.respond_to? :fetch - output.get_version node, group, oid + yield node, output end end - - def get_diff node, group, oid1, oid2 - with_lock do - i = find_node_index node - output = self[i].output.new - raise Oxidized::NotSupported unless output.respond_to? :fetch - output.get_diff node, group, oid1, oid2 - end - end - end end diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb index 8d9dae1..23bd663 100644 --- a/lib/oxidized/output/git.rb +++ b/lib/oxidized/output/git.rb @@ -41,7 +41,7 @@ class Git < Output outputs.types.each do |type| type_cfg = '' - type_repo = File.join File.dirname(repo), type + '.git' + puts type_repo = File.join(File.dirname(repo), type + '.git') outputs.type(type).each do |output| (type_cfg << output; next) if not output.name type_file = file + '--' + output.name @@ -60,29 +60,21 @@ class Git < Output def fetch node, group begin - repo = @cfg.repo - repo = File.join File.dirname(repo), group + '.git' if group and not @cfg.single_repo? + repo, path = yield_repo_and_path(node, group) repo = Rugged::Repository.new repo index = repo.index index.read_tree repo.head.target.tree unless repo.empty? - file = node - file = File.join(group, node) if group and @cfg.single_repo? - repo.read(index.get(file)[:oid]).data + repo.read(index.get(path)[:oid]).data rescue 'node not found' end end - #give a hash of all oid revision for the given node, and the date of the commit + # give a hash of all oid revision for the given node, and the date of the commit def version node, group begin - repo = @cfg.repo - path = node - if group and @cfg.single_repo? - path = "#{group}/#{node}" - elsif group - repo = File.join File.dirname(repo), group + '.git' - end + repo, path = yield_repo_and_path(node, group) + repo = Rugged::Repository.new repo walker = Rugged::Walker.new(repo) walker.sorting(Rugged::SORT_DATE) @@ -109,14 +101,9 @@ class Git < Output #give the blob of a specific revision def get_version node, group, oid begin - repo = @cfg.repo - if group && group != '' && !@cfg.single_repo? - repo = File.join File.dirname(repo), group + '.git' - elsif group && group != '' - node = File.join group, node - end + repo, path = yield_repo_and_path(node, group) repo = Rugged::Repository.new repo - repo.blob_at(oid,node).content + repo.blob_at(oid,path).content rescue 'version not found' end @@ -125,30 +112,27 @@ class Git < Output #give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines) def get_diff node, group, oid1, oid2 begin - repo = @cfg.repo diff_commits = nil - if group && group != '' && !@cfg.single_repo? - repo = File.join File.dirname(repo), group + '.git' - end + repo, _ = yield_repo_and_path(node, group) repo = Rugged::Repository.new repo commit = repo.lookup(oid1) - #if the second revision is precised + if oid2 commit_old = repo.lookup(oid2) diff = repo.diff(commit_old, commit) diff.each do |patch| - if /#{node}\s+/.match(patch.to_s.lines.first) + if /#{node.name}\s+/.match(patch.to_s.lines.first) diff_commits = {:patch => patch.to_s, :stat => patch.stat} break end end - #else gives the diffs between the first oid and his first parrent else stat = commit.parents[0].diff(commit).stat stat = [stat[1],stat[2]] patch = commit.parents[0].diff(commit).patch diff_commits = {:patch => patch, :stat => stat} end + diff_commits rescue 'no diffs' @@ -157,6 +141,16 @@ class Git < Output private + def yield_repo_and_path(node, group) + repo, path = node.repo, node.name + + if group and @cfg.single_repo? + path = "#{group}/#{node.name}" + end + + [repo, path] + end + def update repo, file, data return if data.empty? |