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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGit

Returns a new instance of Git.



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

Class Method Details

.clean_obsolete_nodes(active_nodes) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/oxidized/output/git.rb', line 213

def self.clean_obsolete_nodes(active_nodes)
  git_config = Oxidized.config.output.git
  repo_path = git_config.repo

  unless git_config.single_repo?
    logger.warn "clean_obsolete_nodes is not implemented for " \
                "multiple git repositories"
    return
  end

  if git_config.type_as_directory?
    logger.warn "clean_obsolete_nodes is not implemented for output " \
                "types as a directory within the git repository"
    return
  end

  # The repo might not exist on the first run
  return unless ::File.directory?(repo_path)

  repo = Rugged::Repository.new repo_path
  return if repo.empty?

  keep_files = active_nodes.map do |n|
    n.group ? ::File.join(n.group, n.name) : n.name
  end

  tree = repo.last_commit.tree
  files_to_delete = []

  tree.walk_blobs do |root, entry|
    file_path = root.empty? ? entry[:name] : ::File.join(root, entry[:name])
    files_to_delete << file_path unless keep_files.include?(file_path)
  end

  return if files_to_delete.empty?

  logger.info "clean_obsolete_nodes: removing " \
              "#{files_to_delete.size} obsolete configs"
  index = repo.index

  files_to_delete.each { |file_path| index.remove(file_path) }

  repo.config['user.name']  = git_config.user
  repo.config['user.email'] = git_config.email
  Rugged::Commit.create(
    repo,
    tree:       index.write_tree(repo),
    message:    "Removing #{files_to_delete.size} obsolete configs",
    parents:    [repo.head.target].compact,
    update_ref: 'HEAD'
  )

  index.write
end

.clear_cacheObject

Currently only used in unit tests



209
210
211
# File 'lib/oxidized/output/git.rb', line 209

def self.clear_cache
  @gitcache = nil
end

.hash_list(node_path, repo_path) ⇒ Object

Return the list of oids for node_path in the repository repo_path



141
142
143
144
# File 'lib/oxidized/output/git.rb', line 141

def self.hash_list(node_path, repo_path)
  update_cache(repo_path)
  @gitcache[repo_path][:nodes][node_path] || []
end

.update_cache(repo_path) ⇒ Object

Update @gitcache, a class instance variable, ensuring persistence by saving the cache independently of object instances



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/oxidized/output/git.rb', line 148

def self.update_cache(repo_path)
  # initialize our cache as a class instance variable
  @gitcache ||= {}
  # When single_repo == false, we have multiple repositories
  unless @gitcache[repo_path]
    @gitcache[repo_path] = {}
    @gitcache[repo_path][:nodes] = {}
    @gitcache[repo_path][:last_commit] = nil
  end

  repo = Rugged::Repository.new repo_path

  walker = Rugged::Walker.new(repo)
  walker.sorting(Rugged::SORT_DATE)
  walker.push(repo.head.target.oid)

  # We store the commits into a temporary cache. It will be prepended
  # to @gitcache to preserve the order of the commits.
  cache = {}
  walker.each do |commit|
    if commit.oid == @gitcache[repo_path][:last_commit]
      # we have reached the last cached commit, so we're done
      break
    end

    commit.diff.each_delta do |delta|
      next unless delta.added? || delta.modified?

      hash = {}
      # We keep :date for reverse compatibility on oxidized-web <= 0.15.1
      hash[:date] = commit.time.to_s
      # date as a Time instance for more flexibility in oxidized-web
      hash[:time] = commit.time
      hash[:oid] = commit.oid
      hash[:author] = commit.author
      hash[:message] = commit.message

      filename = delta.new_file[:path]
      if cache[filename]
        cache[filename].append hash
      else
        cache[filename] = [hash]
      end
    end
  end

  cache.each_pair do |filename, hashlist|
    if @gitcache[repo_path][:nodes][filename]
      # using the splat operator (*) should be OK as hashlist should
      # not be very big when working on deltas
      @gitcache[repo_path][:nodes][filename].prepend(*hashlist)
    else
      @gitcache[repo_path][:nodes][filename] = hashlist
    end
  end

  # Store the most recent commit
  @gitcache[repo_path][:last_commit] = repo.head.target.oid
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 it not easy to cache 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.



81
82
83
84
85
86
87
88
89
90
# File 'lib/oxidized/output/git.rb', line 81

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)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/oxidized/output/git.rb', line 113

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



104
105
106
107
108
109
110
# File 'lib/oxidized/output/git.rb', line 104

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 = 'o@example.com'
    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

file: node name (String) outputs: Oxidized::Models::Outputs opts: dict of optional parameters:

- msg: commit message
- email: committer email
- user: committer name
- group: node group
- significant_changes:
    nil / not set / true: store as usual
    false: skip general config, only store configs where type != nil


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/oxidized/output/git.rb', line 48

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 unless opt[:significant_changes] == false
end

#version(node, group) ⇒ Object

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

Called by Nodes#version



96
97
98
99
100
101
# File 'lib/oxidized/output/git.rb', line 96

def version(node, group)
  repo_path, node_path = yield_repo_and_path(node, group)
  self.class.hash_list(node_path, repo_path)
rescue StandardError
  'node not found'
end