Skip to content
Snippets Groups Projects
Commit 8c94537d authored by Robert Speicher's avatar Robert Speicher
Browse files

Merge branch 'fix/relase-tool-repo-check' into 'master'

Abort rake task if repo is not up to date and/or on master

Closes #54

See merge request !97
parents 11caf35f e186ea9c
No related branches found
No related tags found
1 merge request!97Abort rake task if repo is not up to date and/or on master
Pipeline #
require_relative 'init'
require_relative 'lib/support/tasks_helper'
require_relative 'lib/local_repo'
 
begin
require 'rspec/core/rake_task'
Loading
Loading
@@ -11,6 +12,10 @@ rescue LoadError
# no rspec available
end
 
unless Rake.application.top_level_tasks.include?('default') || LocalRepo.ready?
abort('Please use the master branch and make sure you are up to date.'.colorize(:red))
end
desc "Create release"
task :release, [:version] do |_t, args|
version = get_version(args)
Loading
Loading
require 'open3'
class LocalRepo
def self.ready?
new.ready?
end
def ready?
master_branch? && up_to_date?
end
private
def master_branch?
run_git('rev-parse --abbrev-ref HEAD').chomp == 'master'
end
def up_to_date?
!run_git('pull --ff-only').nil?
end
def run_git(command)
out, err, st = Open3.capture3('git ' + command)
raise ScriptError.new(err) unless st.success?
out
end
end
require 'spec_helper'
require 'local_repo'
describe LocalRepo do
let(:fixture) { LocalRepoFixture.new }
let(:repo_path) { File.join('/tmp', fixture.class.repository_name) }
let(:repo_url) { "file://#{fixture.fixture_path}" }
let(:repo_remotes) do
{ origin: repo_url, github: 'https://example.com/foo/bar/baz.git' }
end
let(:repo) { Repository.get(repo_remotes) }
before do
fixture.rebuild_fixture!
end
context 'up to date repository' do
context 'master branch' do
it 'returns true' do
ensure_branch_exists('master')
in_repo_path do
expect(described_class.ready?).to be true
end
end
end
context 'different branch' do
it 'returns false' do
ensure_branch_exists('new-branch')
in_repo_path do
expect(described_class.ready?).to be false
end
end
end
end
context 'outdated repository' do
before do
ensure_branch_exists('master')
in_repo_path do
repo.write_file('test', 'test')
repo.commit('test', 'test commit')
end
end
context 'master branch' do
it 'returns true if fast-forward' do
in_repo_path do
expect(described_class.ready?).to be true
end
end
end
context 'master branch' do
it 'returns false if non fast-forward' do
st = double('st', success?: false)
expect(Open3).to receive(:capture3).with('git rev-parse --abbrev-ref HEAD').and_call_original
expect(Open3).to receive(:capture3).with('git pull --ff-only').and_return(['', 'error', st])
in_repo_path do
expect { described_class.ready? }.to raise_error(ScriptError)
end
end
end
context 'different branch' do
it 'returns false' do
ensure_branch_exists('new-branch')
in_repo_path do
expect(described_class.ready?).to be false
end
end
end
end
def ensure_branch_exists(branch)
repo.ensure_branch_exists(branch)
end
def in_repo_path
Dir.chdir(repo_path) do
yield
end
end
end
require 'fileutils'
require 'rugged'
class LocalRepoFixture
attr_reader :repository
def self.repository_name
'repo'
end
def rebuild_fixture!
wipe_fixture!
@repository = Rugged::Repository.init_at(fixture_path)
commit(
path: 'README.md',
content: 'Sample README.md',
message: 'Add empty README.md'
)
end
def wipe_fixture!
FileUtils.rm_r(fixture_path) if Dir.exist?(fixture_path)
FileUtils.mkdir_p(fixture_path)
end
def fixture_path
File.expand_path(
"../fixtures/repositories/#{self.class.repository_name}",
__dir__
)
end
private
def commit(path:, content:, message:)
index = repository.index
oid = repository.write(content, :blob)
index.add(path: path, oid: oid, mode: 0o100644)
commit = Rugged::Commit.create(repository, {
tree: index.write_tree(repository),
message: message,
parents: repository.empty? ? [] : [repository.head.target].compact,
update_ref: 'HEAD'
})
repository.checkout_head(strategy: :force)
commit
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment