Always hide cache-dir contents from Git (#8798)

Merge pull request 8798
This commit is contained in:
Ashwin Maroli 2021-09-16 21:54:42 +05:30 committed by GitHub
parent f0314eec56
commit 891a78685a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -490,10 +490,26 @@ module Jekyll
@site_cleaner ||= Cleaner.new(self)
end
def hide_cache_dir_from_git
@cache_gitignore_path ||= in_source_dir(config["cache_dir"], ".gitignore")
return if File.exist?(@cache_gitignore_path)
cache_dir_path = in_source_dir(config["cache_dir"])
FileUtils.mkdir_p(cache_dir_path) unless File.directory?(cache_dir_path)
File.open(@cache_gitignore_path, "wb") do |file|
file.puts("# ignore everything in this directory\n*")
end
end
# Disable Marshaling cache to disk in Safe Mode
def configure_cache
Jekyll::Cache.cache_dir = in_source_dir(config["cache_dir"], "Jekyll/Cache")
Jekyll::Cache.disable_disk_cache! if safe || config["disable_disk_cache"]
if safe || config["disable_disk_cache"]
Jekyll::Cache.disable_disk_cache!
else
hide_cache_dir_from_git
end
end
def configure_plugins

View File

@ -86,6 +86,28 @@ class TestSite < JekyllUnitTest
site = Site.new(default_configuration)
assert_equal File.join(site.source, ".jekyll-cache"), site.cache_dir
end
should "have the cache_dir hidden from Git" do
site = fixture_site
assert_equal site.source, source_dir
assert_exist source_dir(".jekyll-cache", ".gitignore")
assert_equal(
"# ignore everything in this directory\n*\n",
File.binread(source_dir(".jekyll-cache", ".gitignore"))
)
end
context "with a custom cache_dir configuration" do
should "have the custom cache_dir hidden from Git" do
site = fixture_site("cache_dir" => "../../custom-cache-dir")
refute_exist File.expand_path("../../custom-cache-dir/.gitignore", site.source)
assert_exist source_dir("custom-cache-dir", ".gitignore")
assert_equal(
"# ignore everything in this directory\n*\n",
File.binread(source_dir("custom-cache-dir", ".gitignore"))
)
end
end
end
context "creating sites" do