forked from 3ofcoins/minigit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminigit.rb
More file actions
192 lines (162 loc) · 4.42 KB
/
Copy pathminigit.rb
File metadata and controls
192 lines (162 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require 'pathname'
require 'shellwords'
require "minigit/version"
class MiniGit
class << self
attr_accessor :debug
attr_writer :git_command
def git_command
@git_command || ( (self==::MiniGit) ? 'git' : ::MiniGit.git_command )
end
def method_missing(meth, *args, &block)
_myself.git(meth, *args)
end
def git(*args)
_myself.git(*args)
end
def [](arg)
_myself[arg]
end
def []=(key, value)
_myself[key] = value
end
protected
def _myself
@myself ||= self.new
end
end
class GitError < RuntimeError
attr_reader :command, :status, :info
def initialize(command=[], status=nil, info={})
@status = status.dup rescue status.to_s
@command = command
@info = info
super("Failed to run git #{command.join(' ')}: #{@status}")
end
end
attr_accessor :ssh_key
attr_writer :git_command
attr_reader :git_dir, :git_work_tree
def git_command
@git_command || self.class.git_command
end
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?
Dir.chdir(path.to_s) do
out = `#{git_command} rev-parse --git-dir --show-toplevel`
$stderr.puts "+ [#{Dir.pwd}] #{git_command} rev-parse --git-dir --show-toplevel # => #{out.inspect}" if MiniGit.debug
raise ArgumentError, "Invalid repository path #{where}" unless $?.success?
out
end.lines.map { |ln| path.join(Pathname.new(ln.strip)).realpath.to_s }
end
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
def git(*args)
argv = switches_for(*args)
with_git_env do
$stderr.puts "+ #{git_command} #{Shellwords.join(argv)}" if MiniGit.debug
rv = system(git_command, *argv)
raise GitError.new(argv, $?) unless $?.success?
rv
end
end
def method_missing(meth, *args, &block)
self.git(meth, *args)
end
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
def capturing
@capturing ||= Capturing.new(
:git_command => @git_command,
:git_dir => @git_dir,
:git_work_tree => @git_work_tree)
end
def noncapturing
self
end
class Capturing < MiniGit
attr_reader :process
def system(*args)
`#{Shellwords.join(args)}`
end
def capturing
self
end
def noncapturing
@noncapturing ||= MiniGit.new(
:git_command => @git_command,
:git_dir => @git_dir,
:git_work_tree => @git_work_tree)
end
end
def [](arg)
begin
self.capturing.config(arg).strip
rescue MiniGit::GitError
nil
end
end
def []=(key, value)
begin
self.noncapturing.config(key, value)
rescue MiniGit::GitError
nil
end
end
private
def with_git_env
dir, work_tree, git_ssh = ENV['GIT_DIR'], ENV['GIT_WORK_TREE'], ENV['GIT_SSH']
if @ssh_key
gem_root = Gem::Specification.find_by_name("minigit").gem_dir
ENV['GIT_SSH'] = File.join(gem_root, 'bin', 'ssh-git.sh')
ENV['MINIGIT_SSH_KEY'] = @ssh_key
end
ENV['GIT_DIR'] = git_dir
ENV['GIT_WORK_TREE'] = git_work_tree
yield
ensure
if dir then ENV['GIT_DIR'] = dir else ENV.delete('GIT_DIR') end
if work_tree then ENV['GIT_WORK_TREE'] = work_tree else ENV.delete('GIT_WORK_TREE') end
if git_ssh then ENV['GIT_SSH'] = git_ssh else ENV.delete('GIT_SSH') end
ENV.delete('MINIGIT_SSH_KEY')
end
end