Class: Gollum::Git::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/rugged_adapter/git_layer_rugged.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Git

Rugged does not have a Git class, but the Repository class should allows us to do what’s necessary.



166
167
168
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 166

def initialize(repo)
  @repo = repo
end

Instance Method Details

#cat_file(options, sha) ⇒ Object



196
197
198
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 196

def cat_file(options, sha)
  @repo.lookup(sha).read_raw
end

#checkout(path, ref = 'HEAD', options = {}) ⇒ Object



231
232
233
234
235
236
237
238
239
240
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 231

def checkout(path, ref = 'HEAD', options = {})
  path = path.nil? ? path : [path]
  options = options.merge({:paths => path, :strategy => :force})
  if ref == 'HEAD'
    @repo.checkout_head(**options)
  else
    ref = "refs/heads/#{ref}" unless ref =~ /^refs\/heads\//
    @repo.checkout_tree(sha_from_ref(ref), options)
  end
end

#commit_from_ref(ref) ⇒ Object



307
308
309
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 307

def commit_from_ref(ref)
  sha_or_commit_from_ref(ref, :commit)
end

#exist?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 170

def exist?
  ::File.exists?(@repo.path)
end

#grep(search_terms, options = {}, &block) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 174

def grep(search_terms, options={}, &block)
  ref   = options[:ref] ? options[:ref] : "HEAD"
  tree  = @repo.lookup(sha_from_ref(ref)).tree
  tree  = @repo.lookup(tree[options[:path]][:oid]) if options[:path]
  enc   = options.fetch(:encoding, 'utf-8')
  results = []
  tree.walk_blobs(:postorder) do |root, entry|
    blob  = @repo.lookup(entry[:oid])
    path  = options[:path] ? ::File.join(options[:path], root, entry[:name]) : "#{root}#{entry[:name]}"
    data  = blob.binary? ? nil : blob.content.force_encoding(enc)
    results << yield(path, data)
  end
  results.compact
end

#log(ref = 'refs/heads/master', path = nil, options = {}) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 242

def log(ref = 'refs/heads/master', path = nil, options = {})
  default_options = {
    :limit => options[:max_count] ? options[:max_count] : 10,
    :offset => options[:skip] ? options[:skip] : 0,
    :path => path,
    :follow => false,
    :skip_merges => false
  }
  options = default_options.merge(options)
  options[:limit] ||= 0
  options[:offset] ||= 0
  sha = sha_from_ref(ref)
  return [] if sha.nil?
  begin
    build_log(sha, options)
  rescue Rugged::OdbError, Rugged::InvalidError, Rugged::ReferenceError
  # Return an empty array if the ref wasn't found
    []
  end
end

#lookup(id) ⇒ Object



280
281
282
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 280

def lookup(id)
  @repo.lookup(id)
end

#ls_files(query, options = {}) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 267

def ls_files(query, options = {})
  ref = options[:ref] || "refs/heads/master"
  tree = @repo.lookup(sha_from_ref(ref)).tree
  tree = @repo.lookup(tree[options[:path]][:oid]) if options[:path]
  results = []
  tree.walk_blobs do |root, blob|
    next unless blob[:name] =~ /#{query}/
    path = options[:path] ? ::File.join(options[:path], root, blob[:name]) : "#{root}#{blob[:name]}"
    results << path
  end
  results
end

#pull(remote, branches = nil, options = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 316

def pull(remote, branches = nil, options = {})
  branches = [branches].flatten.map {|branch| "refs/heads/#{branch}" unless branch =~ /^refs\/heads\//}
  r = @repo.remotes[remote]
  r.fetch(branches, options)
  branches.each do |branch|
    branch_name = branch.match(/^refs\/heads\/(.*)/)[1]
    remote_name = remote.match(/^(refs\/heads\/)?(.*)/)[2]
    remote_ref = @repo.branches["#{remote_name}/#{branch_name}"].target
    local_ref = @repo.branches[branch].target
    index = @repo.merge_commits(local_ref, remote_ref)
    options = { author: Actor.default_actor.to_h,
      committer:  Actor.default_actor.to_h,
      message:    "Merged branch #{branch} of #{remote}.",
      parents: [local_ref, remote_ref],
      tree: index.write_tree(@repo),
      update_ref: branch
    }
    Rugged::Commit.create @repo, options
    @repo.checkout(@repo.head.name, :strategy => :force) if !@repo.bare? && branch == @repo.head.name
  end
end

#push(remote, branches = nil, options = {}) ⇒ Object



311
312
313
314
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 311

def push(remote, branches = nil, options = {})
  branches = [branches].flatten.map {|branch| "refs/heads/#{branch}" unless branch =~ /^refs\/heads\//}
  @repo.push(remote, branches, options)
end

#ref_to_sha(query) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 284

def ref_to_sha(query)
  return query if sha?(query)
  query = "refs/heads/#{query}" if !query.nil? && !(query =~ /^refs\/heads\//) && !(query == "HEAD")
  begin
    return @repo.rev_parse_oid(query)
  rescue Rugged::ReferenceError, Rugged::InvalidError
    return nil
  end
end

#revert_commit(sha1, sha2) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 214

def revert_commit(sha1, sha2)
  diff = @repo.diff(sha2, sha1)
  index = @repo.revert_commit(sha2, sha1)
  return false unless index
  paths = []
  diff.each_delta do |delta|
    paths << delta.new_file[:path]
    paths << delta.old_file[:path]
  end
  paths.uniq!
  begin
    return index.write_tree(@repo), paths
  rescue Rugged::IndexError
    return false
  end
end

#revert_path(path, sha1, sha2) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 200

def revert_path(path, sha1, sha2)
  diff = @repo.diff(sha2, sha1, {:paths => [path]}).first.diff
  begin
    result = @repo.apply(diff, {:location => :index, :path => path})
  rescue RuntimeError, Rugged::PathError
    return false
  end
  begin
    return @repo.index.write_tree
  rescue Rugged::IndexError
    return false
  end
end

#rm(path, options = {}) ⇒ Object



189
190
191
192
193
194
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 189

def rm(path, options = {})
  index = @repo.index
  index.write
  to_delete = ::File.join(@repo.workdir, path)
  ::File.unlink to_delete if ::File.exist?(to_delete)
end

#sha_or_commit_from_ref(ref, request_kind = nil) ⇒ Object Also known as: sha_from_ref



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 294

def sha_or_commit_from_ref(ref, request_kind = nil)
  sha = ref_to_sha(ref)
  return nil if sha.nil?
  object = @repo.lookup(sha)
  if object.kind_of?(Rugged::Commit) then
    return Gollum::Git::Commit.new(object) if request_kind == :commit
    sha
  elsif object.respond_to?(:target)
    sha_or_commit_from_ref(object.target.oid, request_kind)
  end
end

#versions_for_path(path = nil, ref = nil, options = {}) ⇒ Object



263
264
265
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 263

def versions_for_path(path = nil, ref = nil, options = {})
  log(ref, path, options)
end