Class: MiniGit
- Inherits:
-
Object
show all
- Defined in:
- lib/minigit.rb,
lib/minigit/version.rb
Defined Under Namespace
Classes: Capturing, GitError
Constant Summary
collapse
- VERSION =
"0.0.1"
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(where = nil, opts = {}) ⇒ MiniGit
Returns a new instance of MiniGit.
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/minigit.rb', line 60
def initialize(where=nil, opts={})
where, opts = nil, where if where.is_a?(Hash)
@git_command = opts[:git_command] if opts[:git_command]
if where
@git_dir, @git_work_tree = find_git_dir(where)
else
@git_dir = opts[:git_dir] if opts[:git_dir]
@git_work_tree = opts[:git_work_tree] if opts[:git_work_tree]
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
79
80
81
|
# File 'lib/minigit.rb', line 79
def method_missing(meth, *args, &block)
self.git(meth, *args)
end
|
Class Attribute Details
.git_command ⇒ Object
10
11
12
|
# File 'lib/minigit.rb', line 10
def git_command
@git_command || ( (self==::MiniGit) ? 'git' : ::MiniGit.git_command )
end
|
Instance Attribute Details
#git_command ⇒ Object
42
43
44
|
# File 'lib/minigit.rb', line 42
def git_command
@git_command || self.class.git_command
end
|
#git_dir ⇒ Object
Returns the value of attribute git_dir.
40
41
42
|
# File 'lib/minigit.rb', line 40
def git_dir
@git_dir
end
|
#git_work_tree ⇒ Object
Returns the value of attribute git_work_tree.
40
41
42
|
# File 'lib/minigit.rb', line 40
def git_work_tree
@git_work_tree
end
|
Class Method Details
.git(*args) ⇒ Object
18
19
20
|
# File 'lib/minigit.rb', line 18
def git(*args)
_myself.git(*args)
end
|
.method_missing(meth, *args, &block) ⇒ Object
14
15
16
|
# File 'lib/minigit.rb', line 14
def method_missing(meth, *args, &block)
_myself.git(meth, *args)
end
|
Instance Method Details
#capturing ⇒ Object
115
116
117
118
119
|
# File 'lib/minigit.rb', line 115
def capturing
@capturing ||= Capturing.new(:git_command => @git_command,
:git_dir => @git_dir,
:git_work_tree => @git_work_tree)
end
|
#find_git_dir(where) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/minigit.rb', line 46
def find_git_dir(where)
path = Pathname.new(where)
raise ArgumentError, "#{where} does not seem to exist" unless path.exist?
path = path.dirname unless path.directory?
grp = Mixlib::ShellOut.new(
git_command, 'rev-parse', '--git-dir', '--show-toplevel',
:cwd => path.to_s)
grp.run_command
grp.error!
grp.stdout.lines.map { |ln| path.join(Pathname.new(ln.strip)).realpath.to_s }
rescue Mixlib::ShellOut::ShellCommandFailed
raise ArgumentError, "Invalid repository path #{where}; Git said: #{grp.stderr.inspect}"
end
|
#git(*args) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/minigit.rb', line 71
def git(*args)
argv = switches_for(*args)
system(
{'GIT_DIR' => git_dir, 'GIT_WORK_TREE' => git_work_tree},
git_command, *argv)
raise GitError.new(argv, $?) unless $?.success?
end
|
#noncapturing ⇒ Object
121
122
123
|
# File 'lib/minigit.rb', line 121
def noncapturing
self
end
|
#switches_for(*args) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/minigit.rb', line 83
def switches_for(*args)
rv = []
args.each do |arg|
case arg
when Hash
arg.keys.sort_by(&:to_s).each do |k|
short = (k.to_s.length == 1)
switch = short ? "-#{k}" : "--#{k}".gsub('_', '-')
Array(arg[k]).each do |value|
if value == true
rv << switch
elsif short
rv << switch
rv << value.to_s
else
rv << "#{switch}=#{value}"
end
end
end
when String
rv << arg
when Enumerable
rv += switches_for(*arg)
when Symbol
rv << arg.to_s.gsub('_', '-')
else
rv << arg.to_s
end
end
rv
end
|
#system(*args) ⇒ Object
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/minigit.rb', line 126
def system(*args)
return Kernel.system(*args) unless args.first.is_a?(Hash)
begin
env, oenv = args.shift, {}
env.keys.each { |k| oenv[k], ENV[k] = ENV[k], env[k] }
Kernel.system(*args)
ensure
oenv.each { |k,v| if v.nil? then ENV.delete(k) else ENV[k] = v end }
end
end
|