Skip to content

Commit

Permalink
Make --exclude on 'foo' kill any files like foo/bar/baz - #248
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansissel committed Aug 14, 2012
1 parent dadc31f commit 0b6deb7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/fpm/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,23 +343,43 @@ def exclude
installdir = staging_path
end

exclude_path = proc do |file|
FileUtils.remove_entry_secure(staging_path(file))
Pathname.new(staging_path(file)).parent.ascend do |d|
if (::Dir.entries(d) - %w[ . .. ]).empty?
::Dir.rmdir(d)
@logger.info("Deleting empty directory left by removing exluded file", :path => d)
else
break
end
end
end # exclude_path

attributes[:excludes].each do |wildcard|
@logger.debug("Checking for things to exclude", :wildcard => wildcard)
files.each do |file|
@logger.debug("Checking path against wildcard", :path => file, :wildcard => wildcard)
if File.fnmatch(wildcard, file)
@logger.info("Removing excluded file", :path => file, :matches => wildcard)
FileUtils.remove_entry_secure(staging_path(file))
Pathname.new(staging_path(file)).parent.ascend do |d|
if (::Dir.entries(d) - %w[ . .. ]).empty?
::Dir.rmdir(d)
@logger.info("Deleting empty directory left by removing exluded file", :path => d)
else
break
end
exclude_path.call(file)
next
end

@logger.debug("Checking if path is a child of an excluded directory",
:path => file, :wildcard => wildcard,
:directory? => File.directory?(staging_path(wildcard)))
if File.directory?(staging_path(wildcard))
# issue #248, if the excludes entry is a directory, ignore that
# directory and anything inside it.
exclude_re = Regexp.new("^#{Regexp.escape(wildcard)}($|/)")
if exclude_re.match(file)
@logger.info("Removing excluded file which has a parent excluded directory",
:path => file, :excluded => wildcard)
exclude_path.call(file)
end
end
end
end
end
end # files.each
end # def exclude


Expand Down
10 changes: 10 additions & 0 deletions spec/fpm/package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@
subject.instance_eval { exclude }
insist { subject.files } == ["hello"]
end

it "should obey attributes[:excludes] for child directories" do
Dir.mkdir(subject.staging_path("example"))
Dir.mkdir(subject.staging_path("example/foo"))
File.write(subject.staging_path("example/foo/delete_me"), "Hello!")
File.write(subject.staging_path("keeper"), "Hello!")
subject.attributes[:excludes] = [ "example/foo" ]
subject.instance_eval { exclude }
insist { subject.files } == [ "keeper" ]
end
end

context "#script (internal method)" do
Expand Down

0 comments on commit 0b6deb7

Please sign in to comment.