diff options
Diffstat (limited to 'lib/oxidized/output/git.rb')
-rw-r--r-- | lib/oxidized/output/git.rb | 88 |
1 files changed, 83 insertions, 5 deletions
diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb index 57b6f62..3757cfc 100644 --- a/lib/oxidized/output/git.rb +++ b/lib/oxidized/output/git.rb @@ -19,6 +19,7 @@ class Git < Output CFGS.save :user raise NoConfig, 'no output git config, edit ~/.config/oxidized/config' end + @cfg.repo = File.expand_path @cfg.repo end def store file, outputs, opt={} @@ -50,18 +51,95 @@ class Git < Output def fetch node, group begin repo = @cfg.repo - if group - repo = File.join File.dirname(repo), group + '.git' - end + repo = File.join File.dirname(repo), group + '.git' if group and not @cfg.single_repo? repo = Rugged::Repository.new repo index = repo.index index.read_tree repo.head.target.tree unless repo.empty? - repo.read(index.get(node)[:oid]).data + file = node + file = File.join(group, node) if group and @cfg.single_repo? + repo.read(index.get(file)[:oid]).data rescue 'node not found' end end + #give a hash of all oid revision for the givin node, and the date of the commit + def version node, group + begin + repo = @cfg.repo + if group + repo = File.join File.dirname(repo), group + '.git' + end + repo = Rugged::Repository.new repo + walker = Rugged::Walker.new(repo) + walker.sorting(Rugged::SORT_DATE) + walker.push(repo.head.target) + i = -1 + tab = [] + walker.each do |commit| + if commit.diff(paths: [node]).size > 0 + hash = {} + hash[:date] = commit.time.to_s + hash[:oid] = commit.oid + hash[:author] = commit.author + hash[:message] = commit.message + tab[i += 1] = hash + end + end + walker.reset + tab + rescue + 'node not found' + end + end + + #give the blob of a specific revision + def get_version node, group, oid + begin + repo = @cfg.repo + if group && group != '' + repo = File.join File.dirname(repo), group + '.git' + end + repo = Rugged::Repository.new repo + repo.blob_at(oid,node).content + rescue + 'version not found' + end + end + + #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 != '' + repo = File.join File.dirname(repo), group + '.git' + end + 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) + 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' + end + end + private def update repo, file, data @@ -104,7 +182,7 @@ class Git < Output :parents => repo.empty? ? [] : [repo.head.target].compact, :update_ref => 'HEAD', ) - + index.write true end |