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
34:     def initialize(git_dir)
35:       self.git_dir    = git_dir
36:       self.bytes_read = 0
37:     end

[Source]

    # File lib/grit/git.rb, line 25
25:     def self.with_timeout(timeout = 10.seconds)
26:       old_timeout = Grit::Git.git_timeout
27:       Grit::Git.git_timeout = timeout
28:       yield
29:       Grit::Git.git_timeout = old_timeout
30:     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
54:     def method_missing(cmd, options = {}, *args)
55:       run('', cmd, '', options, args)
56:     end

[Source]

    # File lib/grit/git.rb, line 58
58:     def run(prefix, cmd, postfix, options, args)
59:       timeout  = options.delete(:timeout) rescue nil
60:       timeout  = true if timeout.nil?
61: 
62:       opt_args = transform_options(options)
63:       ext_args = args.reject { |a| a.empty? }.map { |a| (a == '--' || a[0].chr == '|') ? a : "'#{e(a)}'" }
64: 
65:       call = "#{prefix}#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}#{e(postfix)}"
66:       Grit.log(call) if Grit.debug
67:       response, err = timeout ? sh(call) : wild_sh(call)
68:       Grit.log(response) if Grit.debug
69:       Grit.log(err) if Grit.debug
70:       response
71:     end

[Source]

    # File lib/grit/git.rb, line 73
73:     def sh(command)
74:       ret, err = '', ''
75:       Open3.popen3(command) do |_, stdout, stderr|
76:         Timeout.timeout(self.class.git_timeout) do
77:           while tmp = stdout.read(1024)
78:             ret += tmp
79:             if (@bytes_read += tmp.size) > self.class.git_max_size
80:               bytes = @bytes_read
81:               @bytes_read = 0
82:               raise GitTimeout.new(command, bytes)
83:             end
84:           end
85:         end
86: 
87:         while tmp = stderr.read(1024)
88:           err += tmp
89:         end
90:       end
91:       [ret, err]
92:     rescue Timeout::Error, Grit::Git::GitTimeout
93:       bytes = @bytes_read
94:       @bytes_read = 0
95:       raise GitTimeout.new(command, bytes)
96:     end

[Source]

    # File lib/grit/git.rb, line 39
39:     def shell_escape(str)
40:       str.to_s.gsub("'", "\\\\'").gsub(";", '\\;')
41:     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
117:     def transform_options(options)
118:       args = []
119:       options.keys.each do |opt|
120:         if opt.to_s.size == 1
121:           if options[opt] == true
122:             args << "-#{opt}"
123:           else
124:             val = options.delete(opt)
125:             args << "-#{opt.to_s} '#{e(val)}'"
126:           end
127:         else
128:           if options[opt] == true
129:             args << "--#{opt.to_s.gsub(/_/, '-')}"
130:           else
131:             val = options.delete(opt)
132:             args << "--#{opt.to_s.gsub(/_/, '-')}='#{e(val)}'"
133:           end
134:         end
135:       end
136:       args
137:     end

[Source]

     # File lib/grit/git.rb, line 98
 98:     def wild_sh(command)
 99:       ret, err = '', ''
100:       Open3.popen3(command) do |_, stdout, stderr|
101:         while tmp = stdout.read(1024)
102:           ret += tmp
103:         end
104: 
105:         while tmp = stderr.read(1024)
106:           err += tmp
107:         end
108:       end
109:       [ret, err]
110:     end

[Validate]