Class: Grit::Git

Inherits:
Object
  • Object
show all
Includes:
GitRuby
Defined in:
lib/grit/git.rb

Defined Under Namespace

Classes: GitTimeout

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from GitRuby

#git_file_index, #ruby_git_repo

Class Method Summary collapse

Instance Method Summary collapse

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, work_tree = nil) ⇒ Git

Returns a new instance of Git.



35
36
37
38
39
# File 'lib/grit/git.rb', line 35

def initialize(git_dir, work_tree=nil)
  self.git_dir    = git_dir
  self.work_tree  = work_tree
  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



56
57
58
# File 'lib/grit/git.rb', line 56

def method_missing(cmd, options = {}, *args)
  run('', cmd, '', options, args)
end

Class Attribute Details

.git_binaryObject

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_sizeObject

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_timeoutObject

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_readObject

Returns the value of attribute bytes_read.



32
33
34
# File 'lib/grit/git.rb', line 32

def bytes_read
  @bytes_read
end

#git_dirObject

Returns the value of attribute git_dir.



32
33
34
# File 'lib/grit/git.rb', line 32

def git_dir
  @git_dir
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



33
34
35
# File 'lib/grit/git.rb', line 33

def last_error
  @last_error
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



33
34
35
# File 'lib/grit/git.rb', line 33

def last_response
  @last_response
end

#work_treeObject

Returns the value of attribute work_tree.



32
33
34
# File 'lib/grit/git.rb', line 32

def work_tree
  @work_tree
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

#git_optionsObject



60
61
62
# File 'lib/grit/git.rb', line 60

def git_options
  { :git_dir => self.git_dir, :work_tree => self.work_tree }.reject { |k, v| v.nil? }
end

#run(prefix, cmd, postfix, options, args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grit/git.rb', line 64

def run(prefix, cmd, postfix, options, args)
  timeout  = options.delete(:timeout) rescue nil
  timeout  = true if timeout.nil?

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

  call = "#{prefix}#{Git.git_binary} #{git_opt_args.join(' ')} #{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
  @last_error, @last_response = err, response
  response
end

#sh(command) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/grit/git.rb', line 81

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



41
42
43
# File 'lib/grit/git.rb', line 41

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"]


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/grit/git.rb', line 125

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

#wild_sh(command) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/grit/git.rb', line 106

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