diff options
-rw-r--r-- | ruby/lib/gitlab/git/repository.rb | 8 | ||||
-rw-r--r-- | ruby/lib/gitlab/git/wiki.rb | 9 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/repository_spec.rb | 18 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/wiki_spec.rb | 32 |
4 files changed, 66 insertions, 1 deletions
diff --git a/ruby/lib/gitlab/git/repository.rb b/ruby/lib/gitlab/git/repository.rb index a4d21f5e2..fad18e5b2 100644 --- a/ruby/lib/gitlab/git/repository.rb +++ b/ruby/lib/gitlab/git/repository.rb @@ -436,6 +436,14 @@ module Gitlab rugged&.close if defined?(@rugged) end + def head_symbolic_ref + message, status = run_git(%w[symbolic-ref HEAD]) + + return 'main' if status.nonzero? + + Ref.extract_branch_name(message.squish) + end + private def sparse_checkout_empty?(output) diff --git a/ruby/lib/gitlab/git/wiki.rb b/ruby/lib/gitlab/git/wiki.rb index 452351afd..506b4489f 100644 --- a/ruby/lib/gitlab/git/wiki.rb +++ b/ruby/lib/gitlab/git/wiki.rb @@ -60,11 +60,18 @@ module Gitlab end def gollum_wiki - @gollum_wiki ||= Gollum::Wiki.new(@repository.path) + options = {} + options[:ref] = gollum_default_ref if gollum_default_ref + + @gollum_wiki ||= Gollum::Wiki.new(@repository.path, options) end private + def gollum_default_ref + @gollum_default_ref ||= @repository.root_ref || @repository.head_symbolic_ref + end + def new_page(gollum_page) Gitlab::Git::WikiPage.new(gollum_page, new_version(gollum_page, gollum_page.version.id)) end diff --git a/ruby/spec/lib/gitlab/git/repository_spec.rb b/ruby/spec/lib/gitlab/git/repository_spec.rb index 70899d4c7..6bbf4bcff 100644 --- a/ruby/spec/lib/gitlab/git/repository_spec.rb +++ b/ruby/spec/lib/gitlab/git/repository_spec.rb @@ -470,6 +470,24 @@ describe Gitlab::Git::Repository do # rubocop:disable Metrics/BlockLength end end + describe '#head_symbolic_ref' do + subject { repository.head_symbolic_ref } + + it 'returns the symbolic ref in HEAD' do + expect(subject).to eq('master') + end + + context 'when repo is empty' do + let(:repository) { gitlab_git_from_gitaly(new_empty_test_repo) } + + it 'returns the symbolic ref in HEAD' do + repository.rugged.head = 'refs/heads/foo' + + expect(subject).to eq('foo') + end + end + end + def create_remote_branch(remote_name, branch_name, source_branch_name) source_branch = repository.branches.find { |branch| branch.name == source_branch_name } repository_rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha) diff --git a/ruby/spec/lib/gitlab/git/wiki_spec.rb b/ruby/spec/lib/gitlab/git/wiki_spec.rb index aca0f0dee..9240ed62c 100644 --- a/ruby/spec/lib/gitlab/git/wiki_spec.rb +++ b/ruby/spec/lib/gitlab/git/wiki_spec.rb @@ -132,6 +132,38 @@ describe Gitlab::Git::Wiki do end end + describe '#gollum_wiki' do + context 'when repository is empty' do + it 'instantiates the gollum wiki with the repo symbolic ref' do + repository.rugged.head = 'refs/heads/foo' + + expect(Gollum::Wiki).to receive(:new).with(repository.path, hash_including(ref: 'foo')) + + subject.gollum_wiki + end + end + + context 'when repository is not empty' do + let(:repository) { gitlab_git_from_gitaly(new_mutable_git_test_repo) } + + it 'instantiates the gollum wiki with the repo root_ref' do + expect(Gollum::Wiki).to receive(:new).with(repository.path, hash_including(ref: repository.root_ref)) + + subject.gollum_wiki + end + end + + context 'when symbolic ref and root ref cannot be found' do + it 'instantiates the gollum wiki without any ref' do + allow(subject).to receive(:gollum_default_ref).and_return(nil) + + expect(Gollum::Wiki).to receive(:new).with(repository.path, {}) + + subject.gollum_wiki + end + end + end + def create_page(name, content) subject.write_page(name, :markdown, content, commit_details(name)) end |