| 1 |
module Grit |
| 2 |
|
| 3 |
class Git |
| 4 |
undef_method :clone |
| 5 |
|
| 6 |
class << self |
| 7 |
attr_accessor :git_binary |
| 8 |
end |
| 9 |
|
| 10 |
self.git_binary = "/usr/bin/env git" |
| 11 |
|
| 12 |
attr_accessor :git_dir |
| 13 |
|
| 14 |
def initialize(git_dir) |
| 15 |
self.git_dir = git_dir |
| 16 |
end |
| 17 |
|
| 18 |
def execute(command) |
| 19 |
`#{command}` |
| 20 |
end |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
def method_missing(cmd, options = {}, *args) |
| 33 |
opt_args = transform_options(options) |
| 34 |
ext_args = args.map { |a| (a == '--' or a =~ /^\s*\|/) ? a : "'#{e(a)}'" } |
| 35 |
|
| 36 |
call = "#{Git.git_binary} --git-dir='#{self.git_dir}' #{cmd.to_s.gsub(/_/, '-')} #{(opt_args + ext_args).join(' ')}" |
| 37 |
puts call if Grit.debug |
| 38 |
response = execute(call) |
| 39 |
puts response if Grit.debug |
| 40 |
response |
| 41 |
end |
| 42 |
|
| 43 |
def shell_escape(str) |
| 44 |
str.to_s.gsub("'", "\\\\'") |
| 45 |
end |
| 46 |
alias_method :e, :shell_escape |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
def transform_options(options) |
| 54 |
args = [] |
| 55 |
options.keys.each do |opt| |
| 56 |
if opt.to_s.size == 1 |
| 57 |
if options[opt] == true |
| 58 |
args << "-#{opt}" |
| 59 |
else |
| 60 |
val = options.delete(opt) |
| 61 |
args << "-#{opt.to_s} '#{e(val)}'" |
| 62 |
end |
| 63 |
else |
| 64 |
if options[opt] == true |
| 65 |
args << "--#{opt.to_s.gsub(/_/, '-')}" |
| 66 |
else |
| 67 |
val = options.delete(opt) |
| 68 |
args << "--#{opt.to_s.gsub(/_/, '-')}='#{e(val)}'" |
| 69 |
end |
| 70 |
end |
| 71 |
end |
| 72 |
args |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
end |