Use default branch in wiki repositories

In this commit, we're making wiki repositories use the instance
default branch.

For that we're changing the branch inside HEAD, that later Gitaly
will use to write to.
parent 5696f591
......@@ -87,7 +87,7 @@ class Wiki
end
def create_wiki_repository
repository.create_if_not_exists
change_head_to_default_branch if repository.create_if_not_exists
raise CouldNotCreateWikiError unless repository_exists?
rescue StandardError => err
......@@ -174,6 +174,7 @@ class Wiki
commit = commit_details(:created, message, title)
wiki.write_page(title, format.to_sym, content, commit)
repository.expire_status_cache if repository.empty?
after_wiki_activity
true
......@@ -248,7 +249,9 @@ class Wiki
override :default_branch
def default_branch
wiki.class.default_ref
return 'master' if Feature.disabled?(:wiki_uses_default_branch, user, default_enabled: :yaml)
super || wiki.class.default_ref(container)
end
def wiki_base_path
......@@ -320,6 +323,10 @@ class Wiki
false
end
def change_head_to_default_branch
repository.raw_repository.write_ref('HEAD', "refs/heads/#{default_branch}")
end
end
Wiki.prepend_mod_with('Wiki')
---
name: wiki_uses_default_branch
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64891
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/334754
milestone: '14.1'
type: development
group: group::editor
default_enabled: false
......@@ -4,11 +4,7 @@ require 'spec_helper'
RSpec.describe Gitlab::ImportExport::WikiRepoSaver do
let_it_be(:user) { create(:user) }
let_it_be(:group) do
create(:group, :wiki_repo).tap do |g|
g.add_owner(user)
end
end
let_it_be(:group) { create(:group) }
let(:exportable) { group }
let(:shared) { Gitlab::ImportExport::Shared.new(group) }
......@@ -16,15 +12,19 @@ RSpec.describe Gitlab::ImportExport::WikiRepoSaver do
subject { described_class.new(exportable: exportable, shared: shared) }
describe 'bundles a group wiki Git repo' do
let!(:group_wiki) { GroupWiki.new(group, user) }
let_it_be(:group_wiki) do
create(:group_wiki, user: user).tap do |wiki|
wiki.create_page('index', 'test content')
end
end
let(:group) { group_wiki.group }
let(:export_path) { "#{Dir.tmpdir}/group_tree_saver_spec" }
before do
allow_next_instance_of(Gitlab::ImportExport) do |instance|
allow(instance).to receive(:storage_path).and_return(export_path)
end
group_wiki.create_page('index', 'test content')
end
after do
......@@ -37,8 +37,6 @@ RSpec.describe Gitlab::ImportExport::WikiRepoSaver do
end
context 'when the repo is empty' do
let!(:group) { create(:group) }
it 'bundles the repo successfully' do
expect(subject.save).to be true
end
......
......@@ -3,12 +3,12 @@
require 'spec_helper'
RSpec.describe Elastic::Latest::ProjectWikiClassProxy, :elastic do
let_it_be(:project) { create(:project, :wiki_repo) }
let(:project) { create(:project, :wiki_repo) }
subject { described_class.new(project.wiki.repository) }
describe '#elastic_search_as_wiki_page' do
let_it_be(:page) { create(:wiki_page, wiki: project.wiki) }
let!(:page) { create(:wiki_page, wiki: project.wiki) }
before do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
......
......@@ -33,8 +33,8 @@ RSpec.describe Gitlab::Elastic::ProjectSearchResults, :elastic, :clean_gitlab_re
end
describe "search", :sidekiq_inline do
let_it_be(:project) { create(:project, :public, :repository, :wiki_repo) }
let_it_be(:private_project) { create(:project, :repository, :wiki_repo) }
let(:project) { create(:project, :public, :repository, :wiki_repo) }
let(:private_project) { create(:project, :repository, :wiki_repo) }
before do
[project, private_project].each do |p|
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe ProjectWiki, :elastic do
let_it_be(:project) { create(:project, :wiki_repo) }
let(:project) { create(:project, :wiki_repo) }
before do
stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
......
......@@ -54,8 +54,11 @@ module Gitlab
attr_reader :repository
def self.default_ref
'master'
# TODO remove argument when issue
# https://gitlab.com/gitlab-org/gitlab/-/issues/329190
# is closed.
def self.default_ref(container = nil)
Gitlab::DefaultBranch.value(object: container)
end
# Initialize with a Gitlab::Git::Repository instance
......
......@@ -209,11 +209,10 @@ RSpec.describe Gitlab::ProjectSearchResults do
describe 'wiki search' do
let(:project) { create(:project, :public, :wiki_repo) }
let(:wiki) { build(:project_wiki, project: project) }
before do
wiki.create_page('Files/Title', 'Content')
wiki.create_page('CHANGELOG', 'Files example')
project.wiki.create_page('Files/Title', 'Content')
project.wiki.create_page('CHANGELOG', 'Files example')
end
it_behaves_like 'general blob search', 'wiki', 'wiki_blobs' do
......
......@@ -4,12 +4,11 @@ require 'spec_helper'
RSpec.describe Gitlab::WikiFileFinder do
describe '#find' do
let(:project) { create(:project, :public, :wiki_repo) }
let(:wiki) { build(:project_wiki, project: project) }
before do
wiki.create_page('Files/Title', 'Content')
wiki.create_page('CHANGELOG', 'Files example')
let_it_be(:project) do
create(:project, :public, :wiki_repo).tap do |project|
project.wiki.create_page('Files/Title', 'Content')
project.wiki.create_page('CHANGELOG', 'Files example')
end
end
it_behaves_like 'file finder' do
......
......@@ -5,11 +5,12 @@ require 'spec_helper'
RSpec.describe Git::WikiPushService, services: true do
include RepoHelpers
let_it_be(:current_user) { create(:user) }
let_it_be(:key_id) { create(:key, user: current_user).shell_id }
let_it_be(:wiki) { create(:project_wiki) }
let_it_be(:current_user) { wiki.container.default_owner }
let_it_be(:git_wiki) { wiki.wiki }
let_it_be(:repository) { wiki.repository }
let(:wiki) { create(:project_wiki, user: current_user) }
let(:git_wiki) { wiki.wiki }
let(:repository) { wiki.repository }
describe '#execute' do
it 'executes model-specific callbacks' do
......
......@@ -2,6 +2,7 @@
RSpec.shared_examples 'wiki model' do
let_it_be(:user) { create(:user, :commit_email) }
let(:wiki_container) { raise NotImplementedError }
let(:wiki_container_without_repo) { raise NotImplementedError }
let(:wiki_lfs_enabled) { false }
......@@ -536,4 +537,94 @@ RSpec.shared_examples 'wiki model' do
expect(subject.hook_attrs.keys).to contain_exactly(:web_url, :git_ssh_url, :git_http_url, :path_with_namespace, :default_branch)
end
end
describe '#default_branch' do
subject { wiki.default_branch }
before do
allow(Gitlab::DefaultBranch).to receive(:value).and_return('main')
end
shared_examples 'feature flag wiki_uses_default_branch is disabled' do
it 'returns "master"' do
stub_feature_flags(wiki_uses_default_branch: false)
expect(subject).to eq 'master'
end
end
context 'when repository is not created' do
let(:wiki_container) { wiki_container_without_repo }
it 'returns the instance default branch' do
expect(subject).to eq 'main'
end
it_behaves_like 'feature flag wiki_uses_default_branch is disabled'
end
context 'when repository is empty' do
let(:wiki_container) { wiki_container_without_repo }
before do
wiki.repository.create_if_not_exists
end
it 'returns the instance default branch' do
expect(subject).to eq 'main'
end
it_behaves_like 'feature flag wiki_uses_default_branch is disabled'
end
context 'when repository is not empty' do
it 'returns the repository default branch' do
wiki.create_page('index', 'test content')
expect(subject).to eq wiki.repository.root_ref
end
end
end
describe '#create_wiki_repository' do
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}"
end
end
context 'when repository is empty' do
let(:wiki_container) { wiki_container_without_repo }
it 'does nothing' do
wiki.repository.create_if_not_exists
expect(wiki).not_to receive(:change_head_to_default_branch)
subject
end
end
context 'when repository is not empty' do
it 'does nothing' do
wiki.create_page('index', 'test content')
expect(wiki).not_to receive(:change_head_to_default_branch)
subject
end
end
end
end
......@@ -62,7 +62,7 @@ RSpec.describe 'search/_results' do
let_it_be(:merge_request) { create(:merge_request, title: '*', source_project: project, target_project: project) }
let_it_be(:milestone) { create(:milestone, title: '*', project: project) }
let_it_be(:note) { create(:discussion_note_on_issue, project: project, note: '*') }
let_it_be(:wiki_blob) { create(:wiki_page, project: project, content: '*') }
let_it_be(:wiki_blob) { create(:wiki_page, wiki: project.wiki, content: '*') }
let_it_be(:user) { create(:admin) }
%w[issues merge_requests].each do |search_scope|
......
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