diff options
author | Francisco Javier López <fjlopez@gitlab.com> | 2021-06-21 19:49:11 +0300 |
---|---|---|
committer | Francisco Javier López <fjlopez@gitlab.com> | 2021-06-25 08:15:45 +0300 |
commit | a1395884165c14f0dbaa21bb59209b026d60edf6 (patch) | |
tree | e6c76a35b3a09ea6e09f1d4d6da755afc890c69c | |
parent | 56aa9a2196f0003bc011e4152bb093b8d32be83d (diff) |
Retrieve ref from unborn HEAD for wikis
In order to allow working with different branches in wikis,
we need to change the way the gollum wiki is instantiated.
We need to pass the default branch in HEAD when we instantiate
the gollum wiki, otherwise it will always default to `master`.
Changelog: added
-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 a05c25757..fa457cf92 100644 --- a/ruby/lib/gitlab/git/repository.rb +++ b/ruby/lib/gitlab/git/repository.rb @@ -440,6 +440,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 |