Class Grit::Git
In: lib/grit/git.rb
Parent: Object

Methods

Included Modules

GitRuby

Classes and Modules

Class Grit::Git::GitTimeout

Attributes

bytes_read  [RW] 
git_binary  [RW] 
git_dir  [RW] 
git_max_size  [RW] 
git_timeout  [RW] 

Public Class methods

[Source]

# File lib/grit/git.rb, line 34
    def initialize(git_dir)
      self.git_dir    = git_dir
      self.bytes_read = 0
    end

[Source]

# File lib/grit/git.rb, line 25
    def self.with_timeout(timeout = 10.seconds)
      old_timeout = Grit::Git.git_timeout
      Grit::Git.git_timeout = timeout
      yield
      Grit::Git.git_timeout = old_timeout
    end

Public Instance methods

e(str)

Alias for shell_escape

Run the given git command with the specified arguments and return the result as a String

  +cmd+ is the command
  +options+ is a hash of Ruby style options
  +args+ is the list of arguments (to be joined by spaces)

Examples

  git.rev_list({:max_count => 10, :header => true}, "master")

Returns String

[Source]

# File lib/grit/git.rb, line 54
    def method_missing(cmd, options = {}, *args)
      run('', cmd, '', options, args)
    end

[Source]

# File lib/grit/git.rb, line 58
    def run(prefix, cmd, postfix, options, args)
      timeout  = options.delete(:timeout) rescue nil
      timeout  = true if timeout.nil?

      opt_args = transform_options(options)
      ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|') ? a : "'#{e(a)}'" }

      call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
      Grit.log(call) if Grit.debug
      response, err = timeout ? sh(call) : wild_sh(call)
      Grit.log(response) if Grit.debug
      Grit.log(err) if Grit.debug
      response
    end

[Source]

# File lib/grit/git.rb, line 73
    def sh(command)
      ret, err = '', ''
      Open3.popen3(command) do |_, stdout, stderr|
        Timeout.timeout(self.class.git_timeout) do
          while tmp = stdout.read(1024)
            ret += tmp
            if (@bytes_read += tmp.size) > self.class.git_max_size
              bytes = @bytes_read
              @bytes_read = 0
              raise GitTimeout.new(command, bytes)
            end
          end
        end

        while tmp = stderr.read(1024)
          err += tmp
        end
      end
      [ret, err]
    rescue Timeout::Error, Grit::Git::GitTimeout
      bytes = @bytes_read
      @bytes_read = 0
      raise GitTimeout.new(command, bytes)
    end

[Source]

# File lib/grit/git.rb, line 39
    def shell_escape(str)
      str.to_s.gsub("'", "\\\\'").gsub(";", '\\;')
    end

Transform Ruby style options into git command line options

  +options+ is a hash of Ruby style options

Returns String[]

  e.g. ["--max-count=10", "--header"]

[Source]

# File lib/grit/git.rb, line 117
    def transform_options(options)
      args = []
      options.keys.each do |opt|
        if opt.to_s.size == 1
          if options[opt] == true
            args << "-#{opt}"
          else
            val = options.delete(opt)
            args << "-#{opt.to_s} '#{e(val)}'"
          end
        else
          if options[opt] == true
            args << "--#{opt.to_s.gsub(/_/, '-')}"
          else
            val = options.delete(opt)
            args << "--#{opt.to_s.gsub(/_/, '-')}='#{e(val)}'"
          end
        end
      end
      args
    end

[Source]

# File lib/grit/git.rb, line 98
    def wild_sh(command)
      ret, err = '', ''
      Open3.popen3(command) do |_, stdout, stderr|
        while tmp = stdout.read(1024)
          ret += tmp
        end

        while tmp = stderr.read(1024)
          err += tmp
        end
      end
      [ret, err]
    end

[Validate]