Commit 3233413c authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch '221159-fj-fix-previous-repository-with-wrong-head' into 'master'

Fix wiki repositories with wrong HEAD

See merge request gitlab-org/gitlab!66232
parents eb7ba00e 4bb1d0e1
......@@ -87,7 +87,8 @@ class Wiki
end
def create_wiki_repository
change_head_to_default_branch if repository.create_if_not_exists
repository.create_if_not_exists
change_head_to_default_branch
raise CouldNotCreateWikiError unless repository_exists?
rescue StandardError => err
......@@ -249,7 +250,7 @@ class Wiki
override :default_branch
def default_branch
super || wiki.class.default_ref(container)
super || Gitlab::Git::Wiki.default_ref(container)
end
def wiki_base_path
......@@ -323,6 +324,12 @@ class Wiki
end
def change_head_to_default_branch
# If the wiki has commits in the 'HEAD' branch means that the current
# HEAD is pointing to the right branch. If not, it could mean that either
# the repo has just been created or that 'HEAD' is pointing
# to the wrong branch and we need to rewrite it
return if repository.raw_repository.commit_count('HEAD') != 0
repository.raw_repository.write_ref('HEAD', "refs/heads/#{default_branch}")
end
end
......
......@@ -21,7 +21,11 @@ module Wikis
end
def create_commit!
wiki.create_wiki_repository
commit_result(create_transformed_commit(@file_content))
rescue Wiki::CouldNotCreateWikiError
raise_error("Error creating the wiki repository")
end
private
......
......@@ -19,6 +19,7 @@ RSpec.describe GroupWiki do
# Don't actually create the repository, because the storage shard doesn't exist.
expect(subject.repository).to receive(:create_if_not_exists)
expect(subject).to receive(:change_head_to_default_branch)
allow(subject).to receive(:repository_exists?).and_return(true)
expect(subject).to receive(:track_wiki_repository).with(shard)
......
......@@ -575,18 +575,21 @@ RSpec.shared_examples 'wiki model' do
end
describe '#create_wiki_repository' do
let(:head_path) { Rails.root.join(TestEnv.repos_path, "#{wiki.disk_path}.git", 'HEAD') }
let(:default_branch) { 'foo' }
before do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return(default_branch)
end
subject { wiki.create_wiki_repository }
context 'when repository is not created' do
let(:wiki_container) { wiki_container_without_repo }
let(:head_path) { Rails.root.join(TestEnv.repos_path, "#{wiki.disk_path}.git", 'HEAD') }
let(:default_branch) { 'foo' }
it 'changes the HEAD reference to the default branch' do
expect(wiki.empty?).to eq true
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return(default_branch)
subject
expect(File.read(head_path).squish).to eq "ref: refs/heads/#{default_branch}"
......@@ -596,23 +599,36 @@ RSpec.shared_examples 'wiki model' do
context 'when repository is empty' do
let(:wiki_container) { wiki_container_without_repo }
it 'does nothing' do
it 'changes the HEAD reference to the default branch' do
wiki.repository.create_if_not_exists
expect(wiki).not_to receive(:change_head_to_default_branch)
wiki.repository.raw_repository.write_ref('HEAD', 'refs/heads/bar')
subject
expect(File.read(head_path).squish).to eq "ref: refs/heads/#{default_branch}"
end
end
context 'when repository is not empty' do
it 'does nothing' do
before do
wiki.create_page('index', 'test content')
end
expect(wiki).not_to receive(:change_head_to_default_branch)
it 'does nothing when HEAD points to the right branch' do
expect(wiki.repository.raw_repository).not_to receive(:write_ref)
subject
end
context 'when HEAD points to the wrong branch' do
it 'rewrites HEAD with the right branch' do
wiki.repository.raw_repository.write_ref('HEAD', 'refs/heads/bar')
subject
expect(File.read(head_path).squish).to eq "ref: refs/heads/#{default_branch}"
end
end
end
end
end
......@@ -25,6 +25,25 @@ RSpec.shared_examples 'Wikis::CreateAttachmentService#execute' do |container_typ
container.add_developer(user)
end
context 'creates wiki repository if it does not exist' do
let(:container) { create(container_type) } # rubocop:disable Rails/SaveBang
it 'creates wiki repository' do
expect { service.execute }.to change { container.wiki.repository.exists? }.to(true)
end
context 'if an error is raised creating the repository' do
it 'catches error and return gracefully' do
allow(container.wiki).to receive(:repository_exists?).and_return(false)
result = service.execute
expect(result[:status]).to eq :error
expect(result[:message]).to eq 'Error creating the wiki repository'
end
end
end
context 'creates branch if it does not exists' do
let(:branch_name) { 'new_branch' }
let(:opts) { file_opts.merge(branch_name: branch_name) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment