From de2a9d18a5847439b40adfc0cc4c75e6cbf6262d Mon Sep 17 00:00:00 2001
From: Danilo Sousa <dsgoncalves@uoldiveo.com>
Date: Wed, 20 Jul 2016 07:00:31 -0300
Subject: fixed the #fetch and #verions web problems

apart from the refactorings, now the node path its correct even
when groups are used
---
 lib/oxidized/nodes.rb      | 54 +++++++++++++++++++++-------------------------
 lib/oxidized/output/git.rb | 50 +++++++++++++++++++-----------------------
 2 files changed, 46 insertions(+), 58 deletions(-)

(limited to 'lib')

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?
 
-- 
cgit v1.2.3