def revision=(desired)
checkout(desired)
- if local_branch_revision?(desired)
+ if local_branch_revision?
# reset instead of pull to avoid merge conflicts. assuming remote is
# authoritative.
# might be worthwhile to have an allow_local_changes param to decide
# whether to reset or pull when we're ensuring latest.
- at_path { git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{desired}") }
+ at_path {
+ git_with_identity('reset', '--hard', "#{@resource.value(:remote)}/#{@resource.value(:revision)}")
+ if detached?
+ git_with_identity('checkout', "#{@resource.value(:revision)}")
+ git_with_identity('pull')
+ end
+ }
end
if @resource.value(:ensure) != :bare
update_submodules
end
def on_branch?
- at_path { git_with_identity('rev-parse', '--abbrev-ref', 'HEAD').chomp }
+ at_path {
+ matches = git_with_identity('branch', '--list').match /\*\s+(.*)/
+ matches[1] unless matches[1].match /detached/
+ }
+ end
+
+ def detached?
+ at_path {
+ git_with_identity('branch', '--list').match /\*\s+\(detached from.*\)/
+ }
end
def tags
it "should use 'git fetch' and 'git reset'" do
resource[:revision] = 'feature/foo'
provider.expects(:update_submodules)
+ provider.expects(:git).with('branch','--list').returns("* master")
provider.expects(:git).with('branch', '-a').returns(resource.value(:revision))
provider.expects(:git).with('checkout', '--force', resource.value(:revision))
provider.expects(:git).with('branch', '-a').returns(resource.value(:revision))
it "should use 'git fetch' and 'git reset'" do
resource[:revision] = 'only/remote'
provider.expects(:update_submodules)
+ provider.expects(:git).with('branch','--list').returns("* master")
provider.expects(:git).with('branch', '-a').returns(resource.value(:revision))
provider.expects(:git).with('checkout', '--force', resource.value(:revision))
provider.expects(:git).with('branch', '-a').returns(resource.value(:revision))
context "retrieving the current revision" do
before do
expects_chdir
- provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns("foo\n")
+ provider.expects(:git).with('branch','--list').returns("* foo")
end
it "will strip trailing newlines" do
end
context 'on master' do
it do
- provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_a))
+ provider.expects(:git).with('branch','--list').returns("* master")
provider.latest.should == 'master'
end
end
context 'no branch' do
it do
- provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_none))
+ provider.expects(:git).with('branch','--list').returns("* master")
+
provider.latest.should == 'master'
end
end
context 'feature/bar' do
it do
- provider.expects(:git).with('rev-parse', '--abbrev-ref', 'HEAD').returns(fixture(:git_branch_feature_bar))
+ provider.expects(:git).with('branch','--list').returns("* master")
provider.latest.should == 'master'
end
end