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
|
module Grack
class Git
attr_reader :repo
def initialize(git_path, repo_path)
@git_path = git_path
@repo = repo_path
end
def update_server_info
execute(%W(update-server-info))
end
def command(cmd)
[@git_path || 'git'] + cmd
end
def capture(cmd)
# _Not_ the same as `IO.popen(...).read`
# By using a block we tell IO.popen to close (wait for) the child process
# after we are done reading its output.
IO.popen(popen_env, cmd, popen_options) { |p| p.read }
end
def execute(cmd)
cmd = command(cmd)
if block_given?
IO.popen(popen_env, cmd, File::RDWR, popen_options) do |pipe|
yield(pipe)
end
else
capture(cmd).chomp
end
end
def popen_options
{ chdir: repo, unsetenv_others: true }
end
def popen_env
{ 'PATH' => ENV['PATH'], 'GL_ID' => ENV['GL_ID'] }
end
def config_setting(service_name)
service_name = service_name.gsub('-', '')
setting = config("http.#{service_name}")
if service_name == 'uploadpack'
setting != 'false'
else
setting == 'true'
end
end
def config(config_name)
execute(%W(config #{config_name}))
end
def valid_repo?
return false unless File.exists?(repo) && File.realpath(repo) == repo
match = execute(%W(rev-parse --git-dir)).match(/\.$|\.git$/)
if match.to_s == '.git'
# Since the parent could be a git repo, we want to make sure the actual repo contains a git dir.
return false unless Dir.entries(repo).include?('.git')
end
match
end
end
end
|