Class: Oxidized::Output::Git

Inherits:
Oxidized::Output show all
Defined in:
lib/oxidized/output/git.rb

Defined Under Namespace

Classes: GitError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGit



15
16
17
18
# File 'lib/oxidized/output/git.rb', line 15

def initialize
  super
  @cfg = Oxidized.config.output.git
end

Instance Attribute Details

#commitrefObject (readonly)

Returns the value of attribute commitref.



13
14
15
# File 'lib/oxidized/output/git.rb', line 13

def commitref
  @commitref
end

Instance Method Details

#fetch(node, group) ⇒ Object

Returns the configuration of group/node_name

#fetch is called by Nodes#fetch Nodes#fetch creates a new Output object each time, so we cannot store the repo index in memory. But as we keep the repo index up to date on disk in #update_repo, we can read it from disk instead of rebuilding it each time.



71
72
73
74
75
76
77
78
79
80
# File 'lib/oxidized/output/git.rb', line 71

def fetch(node, group)
  repo, path = yield_repo_and_path(node, group)
  repo = Rugged::Repository.new repo
  # Read the index from disk
  index = repo.index

  repo.read(index.get(path)[:oid]).data
rescue StandardError
  'node not found'
end

#get_diff(node, group, oid1, oid2) ⇒ Object

give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/oxidized/output/git.rb', line 119

def get_diff(node, group, oid1, oid2)
  diff_commits = nil
  repo, = yield_repo_and_path(node, group)
  repo = Rugged::Repository.new repo
  commit = repo.lookup(oid1)

  if oid2
    commit_old = repo.lookup(oid2)
    diff = repo.diff(commit_old, commit)
    diff.each do |patch|
      if /#{node.name}\s+/ =~ patch.to_s.lines.first
        diff_commits = { patch: patch.to_s, stat: patch.stat }
        break
      end
    end
  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 StandardError
  'no diffs'
end

#get_version(node, group, oid) ⇒ Object

give the blob of a specific revision



110
111
112
113
114
115
116
# File 'lib/oxidized/output/git.rb', line 110

def get_version(node, group, oid)
  repo, path = yield_repo_and_path(node, group)
  repo = Rugged::Repository.new repo
  repo.blob_at(oid, path).content
rescue StandardError
  'version not found'
end

#setupObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oxidized/output/git.rb', line 20

def setup
  if @cfg.empty?
    Oxidized.asetus.user.output.git.user  = 'Oxidized'
    Oxidized.asetus.user.output.git.email = '[email protected]'
    Oxidized.asetus.user.output.git.repo = File.join(Config::ROOT, 'oxidized.git')
    Oxidized.asetus.save :user
    raise NoConfig, "no output git config, edit #{Oxidized::Config.configfile}"
  end

  if @cfg.repo.respond_to?(:each)
    @cfg.repo.each do |group, repo|
      @cfg.repo["#{group}="] = File.expand_path repo
    end
  else
    @cfg.repo = File.expand_path @cfg.repo
  end
end

#store(file, outputs, opt = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oxidized/output/git.rb', line 38

def store(file, outputs, opt = {})
  @msg   = opt[:msg]
  @user  = opt[:user]  || @cfg.user
  @email = opt[:email] || @cfg.email
  @opt   = opt
  @commitref = nil
  repo = @cfg.repo

  outputs.types.each do |type|
    type_cfg = ''
    type_repo = File.join(File.dirname(repo), type + '.git')
    outputs.type(type).each do |output|
      (type_cfg << output; next) unless output.name # rubocop:disable Style/Semicolon
      type_file = file + '--' + output.name
      if @cfg.type_as_directory?
        type_file = type + '/' + type_file
        type_repo = repo
      end
      update type_repo, type_file, output
    end
    update type_repo, file, type_cfg
  end

  update repo, file, outputs.to_cfg
end

#version(node, group) ⇒ Object

give a hash of all oid revision for the given node, and the date of the commit



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/oxidized/output/git.rb', line 83

def version(node, group)
  repo, path = yield_repo_and_path(node, group)

  repo = Rugged::Repository.new repo
  walker = Rugged::Walker.new(repo)
  walker.sorting(Rugged::SORT_DATE)
  walker.push(repo.head.target.oid)
  i = -1
  tab = []
  walker.each do |commit|
    # Diabled rubocop because the suggested .empty? does not work here.
    next if commit.diff(paths: [path]).size.zero? # rubocop:disable Style/ZeroLengthPredicate

    hash = {}
    hash[:date] = commit.time.to_s
    hash[:oid] = commit.oid
    hash[:author] = commit.author
    hash[:message] = commit.message
    tab[i += 1] = hash
  end
  walker.reset
  tab
rescue StandardError
  'node not found'
end