Class: Grit::Git
Defined Under Namespace
Classes: GitTimeout
Class Attribute Summary collapse
-
.git_binary ⇒ Object
Returns the value of attribute git_binary.
-
.git_max_size ⇒ Object
Returns the value of attribute git_max_size.
-
.git_timeout ⇒ Object
Returns the value of attribute git_timeout.
Instance Attribute Summary collapse
-
#bytes_read ⇒ Object
Returns the value of attribute bytes_read.
-
#git_dir ⇒ Object
Returns the value of attribute git_dir.
Attributes included from GitRuby
#git_file_index, #ruby_git_repo
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(git_dir) ⇒ Git
constructor
A new instance of Git.
-
#method_missing(cmd, options = {}, *args) ⇒ Object
Run the given git command with the specified arguments and return the result as a String
cmd
is the commandoptions
is a hash of Ruby style optionsargs
is the list of arguments (to be joined by spaces). - #run(prefix, cmd, postfix, options, args) ⇒ Object
- #sh(command) ⇒ Object
- #shell_escape(str) ⇒ Object (also: #e)
-
#transform_options(options) ⇒ Object
Transform Ruby style options into git command line options
options
is a hash of Ruby style options. - #wild_sh(command) ⇒ Object
Methods included from GitRuby
#blame_tree, #cat_file, #diff, #file_index, #file_size, #file_type, #init, #ls_tree, read_bytes_until, #rev_list, #rev_parse, #ruby_git
Constructor Details
#initialize(git_dir) ⇒ Git
Returns a new instance of Git.
34 35 36 37 |
# File 'lib/grit/git.rb', line 34 def initialize(git_dir) self.git_dir = git_dir self.bytes_read = 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(cmd, options = {}, *args) ⇒ Object
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
54 55 56 |
# File 'lib/grit/git.rb', line 54 def method_missing(cmd, = {}, *args) run('', cmd, '', , args) end |
Class Attribute Details
.git_binary ⇒ Object
Returns the value of attribute git_binary.
18 19 20 |
# File 'lib/grit/git.rb', line 18 def git_binary @git_binary end |
.git_max_size ⇒ Object
Returns the value of attribute git_max_size.
18 19 20 |
# File 'lib/grit/git.rb', line 18 def git_max_size @git_max_size end |
.git_timeout ⇒ Object
Returns the value of attribute git_timeout.
18 19 20 |
# File 'lib/grit/git.rb', line 18 def git_timeout @git_timeout end |
Instance Attribute Details
#bytes_read ⇒ Object
Returns the value of attribute bytes_read.
32 33 34 |
# File 'lib/grit/git.rb', line 32 def bytes_read @bytes_read end |
#git_dir ⇒ Object
Returns the value of attribute git_dir.
32 33 34 |
# File 'lib/grit/git.rb', line 32 def git_dir @git_dir end |
Class Method Details
.with_timeout(timeout = 10.seconds) ⇒ Object
25 26 27 28 29 30 |
# 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 |
Instance Method Details
#run(prefix, cmd, postfix, options, args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/grit/git.rb', line 58 def run(prefix, cmd, postfix, , args) timeout = .delete(:timeout) rescue nil timeout = true if timeout.nil? opt_args = () 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 |
#sh(command) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 |
#shell_escape(str) ⇒ Object Also known as: e
39 40 41 |
# File 'lib/grit/git.rb', line 39 def shell_escape(str) str.to_s.gsub("'", "\\\\'").gsub(";", '\\;') end |
#transform_options(options) ⇒ Object
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"]
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/grit/git.rb', line 117 def () args = [] .keys.each do |opt| if opt.to_s.size == 1 if [opt] == true args << "-#{opt}" else val = .delete(opt) args << "-#{opt.to_s} '#{e(val)}'" end else if [opt] == true args << "--#{opt.to_s.gsub(/_/, '-')}" else val = .delete(opt) args << "--#{opt.to_s.gsub(/_/, '-')}='#{e(val)}'" end end end args end |
#wild_sh(command) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# 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 |