Refactor caching to be more readable
This commit is contained in:
parent
99265b1186
commit
865fc42d42
|
@ -58,13 +58,12 @@ RSpec.describe DockerManager::GitRepo do
|
||||||
return if @local_repo && @remote_git_repo
|
return if @local_repo && @remote_git_repo
|
||||||
|
|
||||||
cache_key =
|
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 =
|
@remote_git_repo =
|
||||||
GitHelpers::RemoteGitRepo.new(
|
GitHelpers::RemoteGitRepo.new(initial_branch:, cache_key:) do |repo|
|
||||||
initial_branch: initial_branch,
|
|
||||||
cache_key: Digest::SHA1.hexdigest(cache_key),
|
|
||||||
) do |repo|
|
|
||||||
repo.commit(
|
repo.commit(
|
||||||
filename: "foo.txt",
|
filename: "foo.txt",
|
||||||
commits: [
|
commits: [
|
||||||
|
|
|
@ -10,21 +10,16 @@ module GitHelpers
|
||||||
|
|
||||||
@@caches = {}
|
@@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
|
@initial_branch = initial_branch
|
||||||
@local_clone_count = 0
|
@local_clone_count = 0
|
||||||
@root_path = Dir.mktmpdir
|
@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")
|
@remote_path = File.join(@root_path, "remote.git")
|
||||||
@work_path = File.join(@root_path, "work")
|
@work_path = File.join(@root_path, "work")
|
||||||
@url = "file://#{@remote_path}"
|
@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)
|
FileUtils.cp_r(@@caches[cache_key].root_path + "/.", @root_path)
|
||||||
Dir.chdir(@work_path) { git "remote remove origin && git remote add origin #{@url}" }
|
Dir.chdir(@work_path) { git "remote remove origin && git remote add origin #{@url}" }
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue