middleman-deploy/lib/middleman-deploy/strategies/git/base.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

module Middleman
module Deploy
module Strategies
module Git
class Base
attr_accessor :branch, :build_dir, :remote, :commit_message, :user_name, :user_email
def initialize(build_dir, remote, branch, commit_message)
self.branch = branch
self.build_dir = build_dir
self.remote = remote
self.commit_message = commit_message
self.user_name = `git config --get user.name`
self.user_email = `git config --get user.email`
end
def process
2015-08-16 20:06:10 +02:00
fail NotImplementedError
end
2014-05-29 11:57:30 +02:00
protected
def add_signature_to_commit_message(base_message)
signature = "#{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}"
time = "#{Time.now.utc}"
"#{base_message} at #{time} by #{signature}"
end
def checkout_branch
# if there is a branch with that name, switch to it, otherwise create a new one and switch to it
2015-08-16 20:06:10 +02:00
if `git branch`.split("\n").any? { |b| b =~ /#{branch}/i }
`git checkout #{branch}`
else
2015-08-16 20:06:10 +02:00
`git checkout -b #{branch}`
end
end
2014-05-29 11:57:30 +02:00
def commit_branch(options = '')
2015-08-16 20:06:10 +02:00
message = commit_message ? commit_message : add_signature_to_commit_message('Automated commit')
2015-08-16 20:06:10 +02:00
run_or_fail('git add -A')
run_or_fail("git commit --allow-empty -am \"#{message}\"")
2015-08-16 20:06:10 +02:00
run_or_fail("git push #{options} origin #{branch}")
end
private
2015-08-16 20:06:10 +02:00
def run_or_fail(command)
2015-08-16 20:06:10 +02:00
system(command) || fail("ERROR running: #{command}")
end
end
end
end
end
end