debian-mirror-gitlab/lib/gitlab/submodule_links.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

48 lines
1.3 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
module Gitlab
class SubmoduleLinks
include Gitlab::Utils::StrongMemoize
2020-11-24 15:15:51 +05:30
Urls = Struct.new(:web, :tree, :compare)
2019-09-30 21:07:59 +05:30
def initialize(repository)
@repository = repository
2019-12-21 20:55:43 +05:30
@cache_store = {}
2019-09-30 21:07:59 +05:30
end
2020-11-24 15:15:51 +05:30
def for(submodule, sha, diff_file = nil)
2019-09-30 21:07:59 +05:30
submodule_url = submodule_url_for(sha, submodule.path)
2020-11-24 15:15:51 +05:30
old_submodule_id = old_submodule_id(submodule_url, diff_file)
urls = SubmoduleHelper.submodule_links_for_url(submodule.id, submodule_url, repository, old_submodule_id)
Urls.new(*urls) if urls.any?
2019-09-30 21:07:59 +05:30
end
private
attr_reader :repository
def submodule_urls_for(sha)
2019-12-21 20:55:43 +05:30
@cache_store.fetch(sha) do
submodule_urls = repository.submodule_urls_for(sha)
@cache_store[sha] = submodule_urls
2019-09-30 21:07:59 +05:30
end
end
def submodule_url_for(sha, path)
urls = submodule_urls_for(sha)
urls && urls[path]
end
2020-11-24 15:15:51 +05:30
def old_submodule_id(submodule_url, diff_file)
return unless diff_file&.old_blob && diff_file&.old_content_sha
# if the submodule url has changed from old_sha to sha, a compare link does not make sense
#
old_submodule_url = submodule_url_for(diff_file.old_content_sha, diff_file.old_blob.path)
diff_file.old_blob.id if old_submodule_url == submodule_url
end
2019-09-30 21:07:59 +05:30
end
end