Blob of vendor/grit/lib/grit/git.rb (raw blob data)

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 # Run the given git command with the specified arguments and return
23 # the result as a String
24 # +cmd+ is the command
25 # +options+ is a hash of Ruby style options
26 # +args+ is the list of arguments (to be joined by spaces)
27 #
28 # Examples
29 # git.rev_list({:max_count => 10, :header => true}, "master")
30 #
31 # Returns String
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 # Transform Ruby style options into git command line options
49 # +options+ is a hash of Ruby style options
50 #
51 # Returns String[]
52 # e.g. ["--max-count=10", "--header"]
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 # Git
75
76 end # Grit