- Handled quick file (<1s) modification

- Avoid to catch modified files without content modification (sha1 checksum)
(Specs needed)
This commit is contained in:
Thibaud Guillaume-Gentil 2011-05-09 09:39:11 +02:00
parent f7140f2b1c
commit 0dcf13d77c
2 changed files with 18 additions and 5 deletions

View File

@ -8,7 +8,7 @@ module Guard
autoload :Polling, 'guard/listeners/polling' autoload :Polling, 'guard/listeners/polling'
class Listener class Listener
attr_reader :last_event attr_reader :last_event, :sha1_checksums_hash
def self.select_and_init def self.select_and_init
if mac? && Darwin.usable? if mac? && Darwin.usable?
@ -24,6 +24,7 @@ module Guard
end end
def initialize def initialize
@sha1_checksums_hash = {}
update_last_event update_last_event
end end
@ -32,7 +33,7 @@ module Guard
end end
def modified_files(dirs, options = {}) def modified_files(dirs, options = {})
files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && recent_file?(path) } files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) }
files.map! { |file| file.gsub("#{Dir.pwd}/", '') } files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
end end
@ -43,12 +44,24 @@ module Guard
Dir.glob(dirs.map { |dir| "#{dir}#{match}" }) Dir.glob(dirs.map { |dir| "#{dir}#{match}" })
end end
def recent_file?(file) def file_modified?(path)
File.mtime(file) >= last_event # Depending on the filesystem, mtime is probably only precise to the second, so round
# both values down to the second for the comparison.
File.mtime(path).to_i >= last_event.to_i
rescue rescue
false false
end end
def file_content_modified?(path)
sha1_checksum = Digest::SHA1.file(path).to_s
if sha1_checksums_hash[path] != sha1_checksum
@sha1_checksums_hash[path] = sha1_checksum
true
else
false
end
end
def self.mac? def self.mac?
Config::CONFIG['target_os'] =~ /darwin/i Config::CONFIG['target_os'] =~ /darwin/i
end end

View File

@ -35,7 +35,7 @@ describe Guard::Listener do
it "updates last_event with time.now" do it "updates last_event with time.now" do
time = Time.now time = Time.now
subject.update_last_event subject.update_last_event
subject.last_event.should >= time subject.last_event.to_i.should >= time.to_i
end end
end end