From 7c800d3b0753b76e0880853728ef87abd00e01ca Mon Sep 17 00:00:00 2001 From: edeustace Date: Thu, 23 Aug 2012 12:07:30 +0200 Subject: [PATCH] Added a configuration variable: keep_files (default: ['.git']), based on this pull request: https://github.com/mojombo/jekyll/pull/556 --- bin/jekyll | 5 +++++ jekyll.gemspec | 7 +++++-- lib/jekyll.rb | 1 + lib/jekyll/site.rb | 18 ++++++++++++++++-- test/test_site.rb | 27 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/bin/jekyll b/bin/jekyll index 74cb99b0..07769516 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -145,6 +145,11 @@ opts = OptionParser.new do |opts| puts "Jekyll " + Jekyll::VERSION exit 0 end + + opts.on( "--keep-files filename1,filename2", Array, "Whether to keep files that match the filename (default: .git)") do |names| + puts "keep-files option: #{names}" + options['keep_files'] = names + end end # Read command line options into `options` hash diff --git a/jekyll.gemspec b/jekyll.gemspec index 55201ae8..1c181122 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |s| s.name = 'jekyll' s.version = '0.11.2' - s.date = '2011-12-27' + s.date = '2012-08-23' s.rubyforge_project = 'jekyll' s.summary = "A simple, blog aware, static site generator." @@ -74,10 +74,12 @@ Gem::Specification.new do |s| lib/jekyll/migrators/csv.rb lib/jekyll/migrators/drupal.rb lib/jekyll/migrators/enki.rb + lib/jekyll/migrators/joomla.rb lib/jekyll/migrators/marley.rb lib/jekyll/migrators/mephisto.rb lib/jekyll/migrators/mt.rb lib/jekyll/migrators/posterous.rb + lib/jekyll/migrators/rss.rb lib/jekyll/migrators/textpattern.rb lib/jekyll/migrators/tumblr.rb lib/jekyll/migrators/typo.rb @@ -90,6 +92,7 @@ Gem::Specification.new do |s| lib/jekyll/static_file.rb lib/jekyll/tags/highlight.rb lib/jekyll/tags/include.rb + lib/jekyll/tags/post_url.rb test/helper.rb test/source/.htaccess test/source/_includes/sig.markdown @@ -141,9 +144,9 @@ Gem::Specification.new do |s| test/test_post.rb test/test_rdiscount.rb test/test_redcarpet.rb + test/test_redcloth.rb test/test_site.rb test/test_tags.rb - test/test_redcloth.rb ] # = MANIFEST = diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 65e16cdb..50125b08 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -59,6 +59,7 @@ module Jekyll 'source' => Dir.pwd, 'destination' => File.join(Dir.pwd, '_site'), 'plugins' => File.join(Dir.pwd, '_plugins'), + 'keep_files' => ['.git'], 'layouts' => '_layouts', 'future' => true, diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 8a78e915..b3490264 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -5,7 +5,8 @@ module Jekyll class Site attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude, :include, :source, :dest, :lsi, :pygments, - :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts + :permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts, + :keep_files attr_accessor :converters, :generators @@ -26,6 +27,7 @@ module Jekyll self.include = config['include'] || [] self.future = config['future'] self.limit_posts = config['limit_posts'] || nil + self.keep_files = config['keep_files'] || [] self.reset self.setup @@ -217,7 +219,11 @@ module Jekyll # all files and directories in destination, including hidden ones dest_files = Set.new Dir.glob(File.join(self.dest, "**", "*"), File::FNM_DOTMATCH) do |file| - dest_files << file unless file =~ /\/\.{1,2}$/ + if self.keep_files.length > 0 + dest_files << file unless file =~ /\/\.{1,2}$/ or file =~ keep_file_regex + else + dest_files << file unless file =~ /\/\.{1,2}$/ + end end # files to be written @@ -242,6 +248,14 @@ module Jekyll FileUtils.rm_rf(obsolete_files.to_a) end + # create a regex from the keep_files array + # ['.git','.svn'] => /\/(\.git|\/.svn)/ + def keep_file_regex + or_list = self.keep_files.map.inject("") { |x,y| "#{x}|#{y}" }[1..-1] + pattern = "\/(#{or_list.gsub(".", "\.")})" + Regexp.new pattern + end + # Write static files, pages, and posts. # # Returns nothing. diff --git a/test/test_site.rb b/test/test_site.rb index 712bf359..26bfaa52 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -180,6 +180,13 @@ class TestSite < Test::Unit::TestCase File.open(dest_dir('qux/obsolete.html'), 'w') # empty directory FileUtils.mkdir(dest_dir('quux')) + FileUtils.mkdir(dest_dir('.git')) + FileUtils.mkdir(dest_dir('.svn')) + FileUtils.mkdir(dest_dir('.hg')) + # single file in repository + File.open(dest_dir('.git/HEAD'), 'w') + File.open(dest_dir('.svn/HEAD'), 'w') + File.open(dest_dir('.hg/HEAD'), 'w') end teardown do @@ -187,6 +194,9 @@ class TestSite < Test::Unit::TestCase FileUtils.rm_f(dest_dir('obsolete.html')) FileUtils.rm_rf(dest_dir('qux')) FileUtils.rm_f(dest_dir('quux')) + FileUtils.rm_rf(dest_dir('.git')) + FileUtils.rm_rf(dest_dir('.svn')) + FileUtils.rm_rf(dest_dir('.hg')) end should 'remove orphaned files in destination' do @@ -195,6 +205,23 @@ class TestSite < Test::Unit::TestCase assert !File.exist?(dest_dir('obsolete.html')) assert !File.exist?(dest_dir('qux')) assert !File.exist?(dest_dir('quux')) + assert File.exist?(dest_dir('.git')) + assert File.exist?(dest_dir('.git/HEAD')) + end + + should 'remove orphaned files in destination - keep_files .svn' do + + config = Jekyll::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir, 'keep_files' => ['.svn']}) + @site = Site.new(config) + @site.process + assert !File.exist?(dest_dir('.htpasswd')) + assert !File.exist?(dest_dir('obsolete.html')) + assert !File.exist?(dest_dir('qux')) + assert !File.exist?(dest_dir('quux')) + assert !File.exist?(dest_dir('.git')) + assert !File.exist?(dest_dir('.git/HEAD')) + assert File.exist?(dest_dir('.svn')) + assert File.exist?(dest_dir('.svn/HEAD')) end end