diff options
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r-- | app/models/repository.rb | 137 |
1 files changed, 67 insertions, 70 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index cd761a29618..2e42709452b 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require 'securerandom' +require "securerandom" class Repository - REF_MERGE_REQUEST = 'merge-requests'.freeze - REF_KEEP_AROUND = 'keep-around'.freeze - REF_ENVIRONMENTS = 'environments'.freeze + REF_MERGE_REQUEST = "merge-requests" + REF_KEEP_AROUND = "keep-around" + REF_ENVIRONMENTS = "environments" MAX_DIVERGING_COUNT = 1000 RESERVED_REFS_NAMES = %W[ @@ -35,29 +35,29 @@ class Repository # # For example, for entry `:commit_count` there's a method called `commit_count` which # stores its data in the `commit_count` cache key. - CACHED_METHODS = %i(size commit_count rendered_readme readme_path contribution_guide + CACHED_METHODS = %i[size commit_count rendered_readme readme_path contribution_guide changelog license_blob license_key gitignore gitlab_ci_yml branch_names tag_names branch_count tag_count avatar exists? root_ref has_visible_content? - issue_template_names merge_request_template_names xcode_project?).freeze + issue_template_names merge_request_template_names xcode_project?].freeze # Methods that use cache_method but only memoize the value - MEMOIZED_CACHED_METHODS = %i(license).freeze + MEMOIZED_CACHED_METHODS = %i[license].freeze # Certain method caches should be refreshed when certain types of files are # changed. This Hash maps file types (as returned by Gitlab::FileDetector) to # the corresponding methods to call for refreshing caches. METHOD_CACHES_FOR_FILE_TYPES = { - readme: %i(rendered_readme readme_path), + readme: %i[rendered_readme readme_path], changelog: :changelog, - license: %i(license_blob license_key license), + license: %i[license_blob license_key license], contributing: :contribution_guide, gitignore: :gitignore, gitlab_ci: :gitlab_ci_yml, avatar: :avatar, issue_template: :issue_template_names, merge_request_template: :merge_request_template_names, - xcode_config: :xcode_project? + xcode_config: :xcode_project?, }.freeze def initialize(full_path, project, disk_path: nil, is_wiki: false) @@ -72,7 +72,7 @@ class Repository other.is_a?(self.class) && @disk_path == other.disk_path end - alias_method :eql?, :== + alias eql? == def hash [self.class, @disk_path].hash @@ -84,7 +84,7 @@ class Repository @raw_repository ||= initialize_raw_repository end - alias_method :raw, :raw_repository + alias raw raw_repository # Don't use this! It's going away. Use Gitaly to read or write from repos. def path_to_repo @@ -93,7 +93,7 @@ class Repository storage = Gitlab.config.repositories.storages[@project.repository_storage] File.expand_path( - File.join(storage.legacy_disk_path, disk_path + '.git') + File.join(storage.legacy_disk_path, disk_path + ".git") ) end end @@ -140,7 +140,7 @@ class Repository before: before, follow: Array(path).length == 1, skip_merges: skip_merges, - all: all + all: all, } commits = Gitlab::Git::Commit.where(options) @@ -168,9 +168,9 @@ class Repository return [] end - commits = raw_repository.find_commits_by_message(query, ref, path, limit, offset).map do |c| + commits = raw_repository.find_commits_by_message(query, ref, path, limit, offset).map { |c| commit(c) - end + } CommitCollection.new(project, commits, ref) end @@ -265,16 +265,14 @@ class Repository # to avoid unnecessary syncing. def keep_around(*shas) shas.each do |sha| - begin - next unless sha.present? && commit_by(oid: sha) + next unless sha.present? && commit_by(oid: sha) - next if kept_around?(sha) + next if kept_around?(sha) - # This will still fail if the file is corrupted (e.g. 0 bytes) - raw_repository.write_ref(keep_around_ref_name(sha), sha) - rescue Gitlab::Git::CommandError => ex - Rails.logger.error "Unable to create keep-around reference for repository #{disk_path}: #{ex}" - end + # This will still fail if the file is corrupted (e.g. 0 bytes) + raw_repository.write_ref(keep_around_ref_name(sha), sha) + rescue Gitlab::Git::CommandError => ex + Rails.logger.error "Unable to create keep-around reference for repository #{disk_path}: #{ex}" end end @@ -291,12 +289,13 @@ class Repository raw_repository.diverging_commit_count( @root_ref_hash, branch.dereferenced_target.sha, - max_count: MAX_DIVERGING_COUNT) + max_count: MAX_DIVERGING_COUNT + ) if number_commits_behind + number_commits_ahead >= MAX_DIVERGING_COUNT - { distance: MAX_DIVERGING_COUNT } + {distance: MAX_DIVERGING_COUNT} else - { behind: number_commits_behind, ahead: number_commits_ahead } + {behind: number_commits_behind, ahead: number_commits_ahead} end end end @@ -316,18 +315,18 @@ class Repository end def expire_tags_cache - expire_method_caches(%i(tag_names tag_count)) + expire_method_caches(%i[tag_names tag_count]) @tags = nil end def expire_branches_cache - expire_method_caches(%i(branch_names branch_count has_visible_content?)) + expire_method_caches(%i[branch_names branch_count has_visible_content?]) @local_branches = nil @branch_exists_memo = nil end def expire_statistics_caches - expire_method_caches(%i(size commit_count)) + expire_method_caches(%i[size commit_count]) end def expire_all_method_caches @@ -335,7 +334,7 @@ class Repository end def expire_avatar_cache - expire_method_caches(%i(avatar)) + expire_method_caches(%i[avatar]) end # Refreshes the method caches of this repository. @@ -376,14 +375,14 @@ class Repository end def expire_root_ref_cache - expire_method_caches(%i(root_ref)) + expire_method_caches(%i[root_ref]) end # Expires the cache(s) used to determine if a repository is empty or not. def expire_emptiness_caches return unless empty? - expire_method_caches(%i(has_visible_content?)) + expire_method_caches(%i[has_visible_content?]) raw_repository.expire_has_local_branches_cache end @@ -392,7 +391,7 @@ class Repository end def expire_exists_cache - expire_method_caches(%i(exists?)) + expire_method_caches(%i[exists?]) end # expire cache that doesn't depend on repository data (when expiring) @@ -616,7 +615,7 @@ class Repository def rendered_readme return unless readme - context = { project: project } + context = {project: project} MarkupHelper.markup_unsafe(readme.name, readme.data, context) end @@ -667,7 +666,7 @@ class Repository cache_method :xcode_project? def head_commit - @head_commit ||= commit(self.root_ref) + @head_commit ||= commit(root_ref) end def head_tree @@ -695,8 +694,6 @@ class Repository if last_commit blob_at(last_commit.sha, path) - else - nil end end @@ -722,16 +719,16 @@ class Repository end def next_branch(name, opts = {}) - branch_ids = self.branch_names.map do |n| + branch_ids = branch_names.map { |n| next 1 if n == name result = n.match(/\A#{name}-([0-9]+)\z/) result[1].to_i if result - end.compact + }.compact highest_branch_id = branch_ids.max || 0 - return name if opts[:mild] && 0 == highest_branch_id + return name if opts[:mild] && highest_branch_id == 0 "#{name}-#{highest_branch_id + 1}" end @@ -742,13 +739,13 @@ class Repository def tags_sorted_by(value) case value - when 'name_asc' + when "name_asc" VersionSorter.sort(tags) { |tag| tag.name } - when 'name_desc' + when "name_desc" VersionSorter.rsort(tags) { |tag| tag.name } - when 'updated_desc' + when "updated_desc" tags_sorted_by_committed_date.reverse - when 'updated_asc' + when "updated_asc" tags_sorted_by_committed_date else tags @@ -759,10 +756,10 @@ class Repository # # order_by: name|email|commits # sort: asc|desc default: 'asc' - def contributors(order_by: nil, sort: 'asc') + def contributors(order_by: nil, sort: "asc") commits = self.commits(nil, limit: 2000, offset: 0, skip_merges: true) - commits = commits.group_by(&:author_email).map do |email, commits| + commits = commits.group_by(&:author_email).map { |email, commits| contributor = Gitlab::Contributor.new contributor.email = email @@ -775,7 +772,7 @@ class Repository end contributor - end + } Commit.order_by(collection: commits, order_by: order_by, sort: sort) end @@ -791,20 +788,20 @@ class Repository @local_branches ||= raw_repository.local_branches end - alias_method :branches, :local_branches + alias branches local_branches def tags @tags ||= raw_repository.tags end def create_dir(user, path, **options) - options[:actions] = [{ action: :create_dir, file_path: path }] + options[:actions] = [{action: :create_dir, file_path: path}] multi_action(user, **options) end def create_file(user, path, content, **options) - options[:actions] = [{ action: :create, file_path: path, content: content }] + options[:actions] = [{action: :create, file_path: path, content: content}] multi_action(user, **options) end @@ -813,13 +810,13 @@ class Repository previous_path = options.delete(:previous_path) action = previous_path && previous_path != path ? :move : :update - options[:actions] = [{ action: action, file_path: path, previous_path: previous_path, content: content }] + options[:actions] = [{action: action, file_path: path, previous_path: previous_path, content: content}] multi_action(user, **options) end def delete_file(user, path, **options) - options[:actions] = [{ action: :delete, file_path: path }] + options[:actions] = [{action: :delete, file_path: path}] multi_action(user, **options) end @@ -862,7 +859,7 @@ class Repository def ff_merge(user, source, target_branch, merge_request: nil) their_commit_id = commit(source)&.id - raise 'Invalid merge source' if their_commit_id.nil? + raise "Invalid merge source" if their_commit_id.nil? merge_request&.update(in_progress_merge_commit_sha: their_commit_id) @@ -871,7 +868,8 @@ class Repository def revert( user, commit, branch_name, message, - start_branch_name: nil, start_project: project) + start_branch_name: nil, start_project: project + ) with_cache_hooks do raw_repository.revert( @@ -887,7 +885,8 @@ class Repository def cherry_pick( user, commit, branch_name, message, - start_branch_name: nil, start_project: project) + start_branch_name: nil, start_project: project + ) with_cache_hooks do raw_repository.cherry_pick( @@ -908,8 +907,6 @@ class Repository same_head = branch.target == root_ref_sha merged = ancestor?(branch.target, root_ref_sha) !same_head && merged - else - nil end end @@ -920,9 +917,9 @@ class Repository delegate :merged_branch_names, to: :raw_repository def merge_base(*commits_or_ids) - commit_ids = commits_or_ids.map do |commit_or_id| + commit_ids = commits_or_ids.map { |commit_or_id| commit_or_id.is_a?(::Commit) ? commit_or_id.id : commit_or_id - end + } raw_repository.merge_base(*commit_ids) end @@ -1017,15 +1014,15 @@ class Repository end def route_map_for(sha) - blob_data_at(sha, '.gitlab/route-map.yml') + blob_data_at(sha, ".gitlab/route-map.yml") end - def gitlab_ci_yml_for(sha, path = '.gitlab-ci.yml') + def gitlab_ci_yml_for(sha, path = ".gitlab-ci.yml") blob_data_at(sha, path) end def lfsconfig_for(sha) - blob_data_at(sha, '.lfsconfig') + blob_data_at(sha, ".lfsconfig") end def fetch_ref(source_repository, source_ref:, target_ref:) @@ -1073,10 +1070,10 @@ class Repository # gitlab-org/gitlab-ce#39239 def find_commit(oid_or_ref) commit = if oid_or_ref.is_a?(Gitlab::Git::Commit) - oid_or_ref - else - Gitlab::Git::Commit.find(raw_repository, oid_or_ref) - end + oid_or_ref + else + Gitlab::Git::Commit.find(raw_repository, oid_or_ref) + end ::Commit.new(commit, @project) if commit end @@ -1114,8 +1111,8 @@ class Repository def initialize_raw_repository Gitlab::Git::Repository.new(project.repository_storage, - disk_path + '.git', - Gitlab::GlRepository.gl_repository(project, is_wiki), - project.full_path) + disk_path + ".git", + Gitlab::GlRepository.gl_repository(project, is_wiki), + project.full_path) end end |