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.



153
154
155
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 153

def initialize(repo)
  @repo = repo
end

Instance Method Details

#apply_patch(head_sha = 'HEAD', patch = nil) ⇒ Object



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

def apply_patch(head_sha = 'HEAD', patch=nil)
  false # Rewrite gollum-lib's revert so that it doesn't require a direct equivalent of Grit's apply_patch
end

#cat_file(options, sha) ⇒ Object



185
186
187
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 185

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

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



198
199
200
201
202
203
204
205
206
207
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 198

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



274
275
276
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 274

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

#exist?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 157

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

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 161

def grep(query, options={})
  ref = options[:ref] ? options[:ref] : "HEAD"
  tree = @repo.lookup(sha_from_ref(ref)).tree
  tree = @repo.lookup(tree[options[:path]][:oid]) if options[:path]
  results = []
  tree.walk_blobs(:postorder) do |root, entry|
    blob = @repo.lookup(entry[:oid])
    count = 0
    blob.content.each_line do |line|
      next unless line.force_encoding("UTF-8").match(/#{Regexp.escape(query)}/i)
      count += 1
    end
    path = options[:path] ? ::File.join(options[:path], root, entry[:name]) : "#{root}#{entry[:name]}"
    results << {:name => path, :count => count} unless count == 0
  end
  results
end

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



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

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



247
248
249
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 247

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

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



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 234

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



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 283

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



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

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



251
252
253
254
255
256
257
258
259
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 251

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(path, sha1, sha2, ref) ⇒ Object



193
194
195
196
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 193

def revert(path, sha1, sha2, ref)
  # FIXME: See https://github.com/gollum/grit_adapter/pull/14
  fail NotImplementedError
end

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



179
180
181
182
183
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 179

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

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



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 261

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



230
231
232
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 230

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