Files

Grit::GitRuby

the functions in this module intercept the calls to git binary made buy the grit objects and attempts to run them in pure ruby if it will be faster, or if the git binary is not available (!!TODO!!)

Attributes

git_file_index[RW]
ruby_git_repo[RW]

Public Class Methods

read_bytes_until(io, char) click to toggle source
# File lib/grit/git-ruby/object.rb, line 164
def self.read_bytes_until(io, char)
  string = ''
  if RUBY_VERSION > '1.9'
    while ((next_char = io.getc) != char) && !io.eof
      string += next_char
    end
  else
    while ((next_char = io.getc.chr) != char) && !io.eof
      string += next_char
    end
  end
  string
end

Public Instance Methods

blame_tree(commit, path = nil) click to toggle source
# File lib/grit/git-ruby.rb, line 200
def blame_tree(commit, path = nil)
  begin
    path = [path].join('/').to_s + '/' if (path && path != '')
    path = '' if !path.is_a? String
    commits = file_index.last_commits(rev_parse({}, commit), looking_for(commit, path))
    clean_paths(commits)
  rescue FileIndex::IndexFileNotFound
    {}
  end
end
cat_file(options, sha) click to toggle source
# File lib/grit/git-ruby.rb, line 21
def cat_file(options, sha)
  if options[:t]
    file_type(sha)
  elsif options[:s]
    file_size(sha)
  elsif options[:p]
    try_run { ruby_git.cat_file(sha) }
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
  ''
end
cat_ref(options, ref) click to toggle source
# File lib/grit/git-ruby.rb, line 33
def cat_ref(options, ref)
  sha = rev_parse({}, ref)
  cat_file(options, sha)
end
diff(options, sha1, sha2 = nil) click to toggle source

git diff –full-index ‘ec037431382e83c3e95d4f2b3d145afbac8ea55d’ ‘f1ec1aea10986159456846b8a05615b87828d6c6’

# File lib/grit/git-ruby.rb, line 47
def diff(options, sha1, sha2 = nil)
  try_run { ruby_git.diff(sha1, sha2, options) }
end
file_index() click to toggle source
# File lib/grit/git-ruby.rb, line 211
def file_index
  @git_file_index ||= FileIndex.new(@git_dir)
end
file_size(ref) click to toggle source
# File lib/grit/git-ruby.rb, line 192
def file_size(ref)
  try_run { ruby_git.cat_file_size(ref).to_s }
end
file_type(ref) click to toggle source
# File lib/grit/git-ruby.rb, line 196
def file_type(ref)
  try_run { ruby_git.cat_file_type(ref).to_s }
end
init(options, *args) click to toggle source
# File lib/grit/git-ruby.rb, line 13
def init(options, *args)
  if options.size == 0
    Grit::GitRuby::Repository.init(@git_dir)
  else
    method_missing('init', options, *args)
  end
end
ls_tree(options, treeish, *paths) click to toggle source

lib/grit/tree.rb:16: output = repo.git.ls_tree({}, treeish, *paths)

# File lib/grit/git-ruby.rb, line 39
def ls_tree(options, treeish, *paths)
  sha = rev_parse({}, treeish)
  ruby_git.ls_tree(sha, paths.flatten, options.delete(:r))
rescue Grit::GitRuby::Repository::NoSuchShaFound
  ''
end
refs(options, prefix) click to toggle source
# File lib/grit/git-ruby.rb, line 111
def refs(options, prefix)
  refs = []
  already = {}
  Dir.chdir(@git_dir) do
    files = Dir.glob(prefix + '/**/*')
    files.each do |ref|
      next if !File.file?(ref)
      id = File.read(ref).chomp
      name = ref.sub("#{prefix}/", '')
      if !already[name]
        refs << "#{name} #{id}"
        already[name] = true
      end
    end

    if File.file?('packed-refs')
      File.readlines('packed-refs').each do |line|
        if m = /^(\w{40}) (.*?)$/.match(line)
          next if !Regexp.new('^' + prefix).match(m[2])
          name = m[2].sub("#{prefix}/", '')
          if !already[name]
            refs << "#{name} #{m[1]}"
            already[name] = true
          end
        end
      end
    end
  end

  refs.join("\n")
end
rev_list(options, ref = 'master') click to toggle source
# File lib/grit/git-ruby.rb, line 51
def rev_list(options, ref = 'master')
  options.delete(:skip) if options[:skip].to_i == 0
  allowed_options = [:max_count, :since, :until, :pretty]  # this is all I can do right now
  if ((options.keys - allowed_options).size > 0)
    return method_missing('rev-list', options, ref)
  elsif (options.size == 0)
    # pure rev-list
    begin
      return file_index.commits_from(rev_parse({}, ref)).join("\n") + "\n"
    rescue
      return method_missing('rev-list', options, ref)
    end
  else
    aref = rev_parse({}, ref)
    if aref.is_a? Array
      return method_missing('rev-list', options, ref)
    else
      return try_run { ruby_git.rev_list(aref, options) }
    end
  end
end
rev_parse(options, string) click to toggle source
# File lib/grit/git-ruby.rb, line 73
def rev_parse(options, string)
  raise RuntimeError, "invalid string: #{string}" unless string.is_a?(String)

  if string =~ /\.\./
    (sha1, sha2) = string.split('..')
    return [rev_parse({}, sha1), rev_parse({}, sha2)]
  end

  if /^[0-9a-f]{40}$/.match(string)  # passing in a sha - just no-op it
    return string.chomp
  end

  head = File.join(@git_dir, 'refs', 'heads', string)
  return File.read(head).chomp if File.file?(head)

  head = File.join(@git_dir, 'refs', 'remotes', string)
  return File.read(head).chomp if File.file?(head)

  head = File.join(@git_dir, 'refs', 'tags', string)
  return File.read(head).chomp if File.file?(head)

  ## check packed-refs file, too
  packref = File.join(@git_dir, 'packed-refs')
  if File.file?(packref)
    File.readlines(packref).each do |line|
      if m = /^(\w{40}) refs\/.+?\/(.*?)$/.match(line)
        next if !Regexp.new(Regexp.escape(string) + '$').match(m[3])
        return m[1].chomp
      end
    end
  end

  ## !! more - partials and such !!

  # revert to calling git - grr
  return method_missing('rev-parse', options, string).chomp
end
ruby_git() click to toggle source
# File lib/grit/git-ruby.rb, line 215
def ruby_git
  @ruby_git_repo ||= Repository.new(@git_dir)
end
tags(options, prefix) click to toggle source
# File lib/grit/git-ruby.rb, line 143
def tags(options, prefix)
  refs = []
  already = {}

  Dir.chdir(repo.path) do
    files = Dir.glob(prefix + '/**/*')

    files.each do |ref|
      next if !File.file?(ref)

      id = File.read(ref).chomp
      name = ref.sub("#{prefix}/", '')

      if !already[name]
        refs << "#{name} #{id}"
        already[name] = true
      end
    end

    if File.file?('packed-refs')
      lines = File.readlines('packed-refs')
      lines.each_with_index do |line, i|
        if m = /^(\w{40}) (.*?)$/.match(line)
          next if !Regexp.new('^' + prefix).match(m[2])
          name = m[2].sub("#{prefix}/", '')

          # Annotated tags in packed-refs include a reference
          # to the commit object on the following line.
          next_line = lines[i + 1]

          id =
          if next_line && next_line[0] == ^^
            next_line[1..-1].chomp
          else
            m[1]
          end

          if !already[name]
            refs << "#{name} #{id}"
            already[name] = true
          end
        end
      end
    end
  end

  refs.join("\n")
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.