summaryrefslogtreecommitdiff
path: root/app/models/snippet.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/snippet.rb')
-rw-r--r--app/models/snippet.rb18
1 files changed, 17 insertions, 1 deletions
diff --git a/app/models/snippet.rb b/app/models/snippet.rb
index 817f9d014eb..c4a7c5e25dc 100644
--- a/app/models/snippet.rb
+++ b/app/models/snippet.rb
@@ -20,6 +20,7 @@ class Snippet < ApplicationRecord
extend ::Gitlab::Utils::Override
MAX_FILE_COUNT = 10
+ MASTER_BRANCH = 'master'
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
@@ -82,6 +83,7 @@ class Snippet < ApplicationRecord
scope :inc_author, -> { includes(:author) }
scope :inc_relations_for_view, -> { includes(author: :status) }
scope :with_statistics, -> { joins(:statistics) }
+ scope :inc_projects_namespace_route, -> { includes(project: [:route, :namespace]) }
attr_mentionable :description
@@ -311,13 +313,27 @@ class Snippet < ApplicationRecord
override :default_branch
def default_branch
- super || 'master'
+ super || MASTER_BRANCH
end
def repository_storage
snippet_repository&.shard_name || self.class.pick_repository_storage
end
+ # Repositories are created by default with the `master` branch.
+ # This method changes the `HEAD` file to point to the existing
+ # default branch in case it's not master.
+ def change_head_to_default_branch
+ return unless repository.exists?
+ return if default_branch == MASTER_BRANCH
+ # 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