Refactor caching to be more readable

This commit is contained in:
Bianca Nenciu 2024-09-05 21:53:42 +03:00
parent 99265b1186
commit 865fc42d42
No known key found for this signature in database
GPG Key ID: 07E83B117A6B844D
2 changed files with 7 additions and 13 deletions

View File

@ -58,13 +58,12 @@ RSpec.describe DockerManager::GitRepo do
return if @local_repo && @remote_git_repo
cache_key =
"#{initial_branch}-" + @before_local_repo_clone.map(&:source_location).flatten.join(",")
Digest::SHA1.hexdigest(
"#{initial_branch}-" + @before_local_repo_clone.map(&:source_location).flatten.join(","),
)
@remote_git_repo =
GitHelpers::RemoteGitRepo.new(
initial_branch: initial_branch,
cache_key: Digest::SHA1.hexdigest(cache_key),
) do |repo|
GitHelpers::RemoteGitRepo.new(initial_branch:, cache_key:) do |repo|
repo.commit(
filename: "foo.txt",
commits: [

View File

@ -10,21 +10,16 @@ module GitHelpers
@@caches = {}
def initialize(initial_branch: "main", cache_key: nil, &blk)
def initialize(initial_branch: "main", cache_key: nil, force: false, &blk)
@initial_branch = initial_branch
@local_clone_count = 0
@root_path = Dir.mktmpdir
@@caches[cache_key] ||= begin
@@caches[cache_key] = true
self.class.new(initial_branch: initial_branch, cache_key: cache_key, &blk)
end
@remote_path = File.join(@root_path, "remote.git")
@work_path = File.join(@root_path, "work")
@url = "file://#{@remote_path}"
if @@caches[cache_key] != true
if !force
@@caches[cache_key] ||= self.class.new(initial_branch:, cache_key:, force: true, &blk)
FileUtils.cp_r(@@caches[cache_key].root_path + "/.", @root_path)
Dir.chdir(@work_path) { git "remote remove origin && git remote add origin #{@url}" }
return