desc "Supports Git repositories"
commands :git => 'git'
+ defaultfor :git => :exists
def create
if !@resource.value(:source)
init_repository(@resource.value(:path))
else
clone_repository(@resource.value(:source), @resource.value(:path))
- reset(@resource.value(:revision)) if @resource.value(:revision)
- end
- end
-
- def exists?
- case @resource.value(:ensure)
- when 'present'
- working_copy_exists?
- when 'bare'
- bare_exists?
- else
- path_exists?
+ if @resource.value(:revision)
+ if @resource.value(:ensure) == :bare
+ notice "Ignoring revision for bare repository"
+ else
+ reset(@resource.value(:revision))
+ end
+ end
end
end
reset(desired)
end
- private
-
def bare_exists?
bare_git_config_exists? && !working_copy_exists?
end
def working_copy_exists?
File.directory?(File.join(@resource.value(:path), '.git'))
end
+
+ def exists?
+ bare_exists? || working_copy_exists?
+ end
+ private
+
def path_exists?
File.directory?(@resource.value(:path))
end
end
def clone_repository(source, path)
- git('clone', source, path)
+ args = ['clone']
+ if @resource.value(:ensure) == :bare
+ args << '--bare'
+ end
+ args.push(source, path)
+ git(*args)
end
def fetch
end
def init_repository(path)
- if @resource.value(:ensure) == 'bare' && working_copy_exists?
+ if @resource.value(:ensure) == :bare && working_copy_exists?
convert_working_copy_to_bare
- elsif @resource.value(:ensure) == 'present' && bare_exists?
+ elsif @resource.value(:ensure) == :present && bare_exists?
convert_bare_to_working_copy
elsif File.directory?(@resource.value(:path))
raise Puppet::Error, "Could not create repository (non-repository at path)"
def normal_init
FileUtils.mkdir(@resource.value(:path))
args = ['init']
- if @resource.value(:ensure) == 'bare'
+ if @resource.value(:ensure) == :bare
+ notice "Creating a bare repository"
args << '--bare'
+ else
+ notice "Creating a working copy repository (#{@resource.value(:ensure).inspect})"
end
at_path do
git(*args)
end
describe 'when creating' do
- context "when a source is given" do
- context "and when a revision is given" do
- it "should execute 'git clone' and 'git reset'" do
- @resource.expects(:value).with(:path).returns(@path).at_least_once
- @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
- @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
- @resource.expects(:value).with(:revision).returns('abcdef').at_least_once
- Dir.expects(:chdir).with(@path).yields
- @provider.expects('git').with('reset', '--hard', 'abcdef')
- @provider.create
- end
+ context "and when a source is given" do
+ before do
+ @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
end
- context "and when a revision is not given" do
- it "should just execute 'git clone'" do
- @resource.expects(:value).with(:path).returns(@path).at_least_once
- @resource.expects(:value).with(:source).returns('git://example.com/repo.git').at_least_once
- @resource.expects(:value).with(:revision).returns(nil).at_least_once
- @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
- @provider.create
- end
+ context "and when ensure = present" do
+ before do
+ @resource.expects(:value).with(:ensure).returns(:present).at_least_once
+ end
+ context "and when a revision is given" do
+ it "should execute 'git clone' and 'git reset'" do
+ @resource.expects(:value).with(:path).returns(@path).at_least_once
+ @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
+ @resource.expects(:value).with(:revision).returns('abcdef').at_least_once
+ Dir.expects(:chdir).with(@path).yields
+ @provider.expects('git').with('reset', '--hard', 'abcdef')
+ @provider.create
+ end
+ end
+ context "and when a revision is not given" do
+ it "should just execute 'git clone'" do
+ @resource.expects(:value).with(:path).returns(@path).at_least_once
+ @resource.expects(:value).with(:revision).returns(nil).at_least_once
+ @provider.expects(:git).with('clone', 'git://example.com/repo.git', @path)
+ @provider.create
+ end
+ end
end
+ context "and when ensure = bare" do
+ before do
+ @resource.expects(:value).with(:ensure).returns(:bare).at_least_once
+ end
+ context "and when a revision is given" do
+ it "should just execute 'git clone --bare'" do
+ @resource.expects(:value).with(:path).returns(@path).at_least_once
+ @resource.expects(:value).with(:revision).returns(nil).at_least_once
+ @provider.expects(:git).with('clone', '--bare', 'git://example.com/repo.git', @path)
+ @provider.create
+ end
+ end
+ context "and when a revision is not given" do
+ it "should just execute 'git clone --bare'" do
+ @resource.expects(:value).with(:path).returns(@path).at_least_once
+ @resource.expects(:value).with(:revision).returns(nil).at_least_once
+ @provider.expects(:git).with('clone', '--bare', 'git://example.com/repo.git', @path)
+ @provider.create
+ end
+ end
+ end
+
end
context "when a source is not given" do
before do
@resource.expects(:value).with(:source).returns(nil)
end
context "when ensure = present" do
- before { @resource.expects(:value).with(:ensure).returns('present').at_least_once }
+ before { @resource.expects(:value).with(:ensure).returns(:present).at_least_once }
context "when the path does not exist" do
it "should execute 'git init'" do
Dir.expects(:mkdir).with(@path)
end
end
context "when ensure = bare" do
- before { @resource.expects(:value).with(:ensure).returns('bare').at_least_once }
+ before { @resource.expects(:value).with(:ensure).returns(:bare).at_least_once }
context "when the path does not exist" do
it "should execute 'git init --bare'" do
Dir.expects(:chdir).with(@path).yields
end
end
- describe "when checking existence" do
- context "when ensure = present" do
- context "when a working copy exists" do
- it "should be true" do
- @resource.expects(:value).with(:ensure).returns('present').at_least_once
- @provider.expects(:working_copy_exists?).returns(true)
- @provider.should be_exists
- end
- end
- context "when a bare repo exists" do
- it "should be " do
- @resource.expects(:value).with(:ensure).returns('present').at_least_once
- @provider.expects(:working_copy_exists?).returns(false)
- @provider.should_not be_exists
- end
- end
- end
- context "when ensure = bare" do
- context "when a working copy exists" do
- it "should be false" do
- @resource.expects(:value).with(:ensure).returns('bare').at_least_once
- @provider.expects(:bare_exists?).returns(false)
- @provider.should_not be_exists
- end
- end
- context "when a bare repo exists" do
- it "should be true" do
- @resource.expects(:value).with(:ensure).returns('bare').at_least_once
- @provider.expects(:bare_exists?).returns(true)
- @provider.should be_exists
- end
- end
- end
- context "when ensure = absent" do
- before { @resource.expects(:value).with(:ensure).returns('absent') }
- context "when the path exists" do
- it "should be true" do
- @resource.expects(:value).with(:path).returns(@path)
- File.expects(:directory?).with(@path).returns(true)
- @provider.should be_exists
- end
- end
- context "when the path does not exist" do
- it "should be false" do
- @resource.expects(:value).with(:path).returns(@path)
- File.expects(:directory?).with(@path).returns(false)
- @provider.should_not be_exists
- end
- end
- end
- end
-
describe "when checking the revision property" do
context "when given a non-SHA ref as the resource revision" do
context "when its SHA is not different than the curent SHA" do