Commit b10c85a1 authored by Francisco Javier López's avatar Francisco Javier López

Merge branch 'jc-snippets-do-not-change-head' into 'master'

Do not ensure HEAD is default_branch on snippets

See merge request gitlab-org/gitlab!82731
parents 2f30afbb 81f2238e
......@@ -350,20 +350,6 @@ class Snippet < ApplicationRecord
snippet_repository&.shard_name || Repository.pick_storage_shard
end
# Repositories are created with a default branch. This branch
# can be different from the default branch set in the platform.
# This method changes the `HEAD` file to point to the existing
# default branch in case it's different.
def change_head_to_default_branch
return unless repository.exists?
# All snippets must have at least 1 file. Therefore, if
# `HEAD` is empty is because it's pointing to the wrong
# default branch
return unless repository.empty? || list_files('HEAD').empty?
repository.raw_repository.write_ref('HEAD', "refs/heads/#{default_branch}")
end
def create_repository
return if repository_exists? && snippet_repository
......
......@@ -30,10 +30,7 @@ module Gitlab
def check(cmd, changes)
check_snippet_accessibility!
super.tap do |_|
# Ensure HEAD points to the default branch in case it is not master
snippet.change_head_to_default_branch
end
super
end
override :download_ability
......
......@@ -397,38 +397,6 @@ RSpec.describe Gitlab::GitAccessSnippet do
end
end
describe 'HEAD realignment' do
let_it_be(:snippet) { create(:project_snippet, :private, :repository, project: project) }
shared_examples 'HEAD is updated to the snippet default branch' do
let(:actor) { snippet.author }
specify do
expect(snippet).to receive(:change_head_to_default_branch).and_call_original
subject
end
context 'when an error is raised' do
let(:actor) { nil }
it 'does not realign HEAD' do
expect(snippet).not_to receive(:change_head_to_default_branch).and_call_original
expect { subject }.to raise_error(described_class::ForbiddenError)
end
end
end
it_behaves_like 'HEAD is updated to the snippet default branch' do
subject { push_access_check }
end
it_behaves_like 'HEAD is updated to the snippet default branch' do
subject { pull_access_check }
end
end
private
def raise_not_found(message_key)
......
......@@ -893,58 +893,4 @@ RSpec.describe Snippet do
it_behaves_like 'can move repository storage' do
let_it_be(:container) { create(:snippet, :repository) }
end
describe '#change_head_to_default_branch' do
let(:head_path) { Rails.root.join(TestEnv.repos_path, "#{snippet.disk_path}.git", 'HEAD') }
subject { snippet.change_head_to_default_branch }
context 'when repository does not exist' do
let(:snippet) { create(:snippet) }
it 'does nothing' do
expect(snippet.repository_exists?).to eq false
expect(snippet.repository.raw_repository).not_to receive(:write_ref)
subject
end
end
context 'when repository is not empty' do
let(:snippet) { create(:snippet, :empty_repo) }
before do
populate_snippet_repo
end
context 'when HEAD branch is empty' do
it 'changes HEAD to default branch' do
File.write(head_path, 'ref: refs/heads/non_existen_branch')
expect(File.read(head_path).squish).to eq 'ref: refs/heads/non_existen_branch'
subject
expect(File.read(head_path).squish).to eq 'ref: refs/heads/main'
expect(snippet.list_files('HEAD')).not_to be_empty
end
end
context 'when HEAD branch is not empty' do
it 'does nothing' do
File.write(head_path, 'ref: refs/heads/main')
expect(snippet.repository.raw_repository).not_to receive(:write_ref)
subject
end
end
def populate_snippet_repo
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return('main')
data = [{ file_path: 'new_file_test', content: 'bar' }]
snippet.snippet_repository.multi_files_action(snippet.author, data, branch_name: 'main', message: 'foo')
end
end
end
end
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