class Spectre::Git::GitAccess
Public Class Methods
new(cfg, logger)
click to toggle source
# File lib/spectre/git.rb, line 10 def initialize cfg, logger @__logger = logger @__cfg = cfg @__repo_path = nil url(@__cfg['url']) @__cfg['branch'] = 'master' unless @__cfg['branch'] end
Public Instance Methods
add(path)
click to toggle source
# File lib/spectre/git.rb, line 83 def add path run("git add \"#{path}\"") end
add_all()
click to toggle source
# File lib/spectre/git.rb, line 87 def add_all run("git add --all") end
branch(name)
click to toggle source
# File lib/spectre/git.rb, line 49 def branch name @__cfg['branch'] = name end
cert(file_path)
click to toggle source
# File lib/spectre/git.rb, line 45 def cert file_path @__cfg['cert'] = file_path end
cleanup()
click to toggle source
# File lib/spectre/git.rb, line 126 def cleanup FileUtils.rm_rf(@__repo_path) end
clone()
click to toggle source
# File lib/spectre/git.rb, line 53 def clone @__repo_path = File.absolute_path File.join(@__cfg['working_dir'], @__cfg['name']) if File.exist? @__repo_path FileUtils.rm_rf(@__repo_path) @__logger.debug("repo path '#{@__repo_path}' removed") end FileUtils.mkpath(@__repo_path) @__logger.debug("repo path '#{@__repo_path}' created") clone_cmd = ['git', 'clone'] clone_cmd << '--branch' clone_cmd << @__cfg['branch'] if @__cfg['cert'] clone_cmd << '--config' clone_cmd << "http.sslCAInfo=#{@__cfg['cert']}" end clone_cmd << get_url clone_cmd << @__repo_path clone_cmd = clone_cmd.join(' ') @__logger.info("#{clone_cmd.gsub /:([^\:\@\/\/]*)@/, ':*****@'}") run(clone_cmd, log: false) end
commit(message)
click to toggle source
# File lib/spectre/git.rb, line 95 def commit message run("git commit -m \"#{message}\"") end
password(pass)
click to toggle source
# File lib/spectre/git.rb, line 36 def password pass @__cfg['password'] = pass end
pull()
click to toggle source
# File lib/spectre/git.rb, line 103 def pull run("git pull") end
push()
click to toggle source
# File lib/spectre/git.rb, line 99 def push run("git push") end
read_file(path)
click to toggle source
# File lib/spectre/git.rb, line 121 def read_file path full_path = File.join(@__repo_path, path) File.read(full_path) end
run(cmd, log: true)
click to toggle source
# File lib/spectre/git.rb, line 130 def run cmd, log: true stdin, stdout, stderr, wait_thr = Open3.popen3(cmd, chdir: @__repo_path) @__logger.info(cmd) if log output = stdout.gets(nil) stdout.close error = stderr.gets(nil) stderr.close raise error unless wait_thr.value.exitstatus == 0 end
tag(name, message: nil)
click to toggle source
# File lib/spectre/git.rb, line 91 def tag name, message: nil run("git tag -a -m \"#{message}\"") end
url(git_url)
click to toggle source
# File lib/spectre/git.rb, line 19 def url git_url url_match = git_url.match /^(?<scheme>http(?:s)?):\/\/(?:(?<user>[^\/:]*):(?<pass>.*)@)?(?<url>.*\/(?<name>[^\/]*)\.git)$/ raise "invalid git url: '#{git_url}'" unless url_match @__cfg['url_path'] = url_match[:url] @__cfg['scheme'] = url_match[:scheme] @__cfg['username'] = url_match[:user] unless @__cfg['username'] @__cfg['password'] = url_match[:pass] unless @__cfg['password'] @__cfg['name'] = url_match[:name] unless @__cfg['name'] @__cfg['working_dir'] = './tmp' unless @__cfg['working_dir'] end
username(user)
click to toggle source
# File lib/spectre/git.rb, line 32 def username user @__cfg['username'] = user end
working_dir(path)
click to toggle source
# File lib/spectre/git.rb, line 40 def working_dir path @__cfg['working_dir'] = path if path @__cfg['working_dir'] end
write_file(path, content)
click to toggle source
# File lib/spectre/git.rb, line 111 def write_file path, content full_path = File.join(@__repo_path, path) file = File.open(full_path, 'w') file.write(content) file.close full_path end
Private Instance Methods
get_url()
click to toggle source
# File lib/spectre/git.rb, line 146 def get_url cred = @__cfg['username'] ? "#{@__cfg['username']}:#{@__cfg['password']}@" : '' "#{@__cfg['scheme']}://#{cred}#{@__cfg['url_path']}" end