Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only check sha1_checksum when file mtime.to_i == last_event.to_i (so …

…touching file to fire guard is working again)
  • Loading branch information...
commit 8f68778927b7cfe9026f963e17fa567e9d156aa5 1 parent b123175
Thibaud Guillaume-Gentil authored
Showing with 21 additions and 9 deletions.
  1. +19 −7 lib/guard/listener.rb
  2. +2 −2 spec/guard/listener_spec.rb
View
26 lib/guard/listener.rb
@@ -34,7 +34,7 @@ def update_last_event
end
def modified_files(dirs, options = {})
- files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) }
+ files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) }
files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
end
@@ -45,24 +45,36 @@ def potentially_modified_files(dirs, options = {})
Dir.glob(dirs.map { |dir| "#{dir}#{match}" })
end
+ # Depending on the filesystem, mtime is probably only precise to the second, so round
+ # both values down to the second for the comparison.
def file_modified?(path)
- # 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
+ if File.mtime(path).to_i == last_event.to_i
+ file_content_modified?(path, sha1_checksum(path))
+ elsif File.mtime(path).to_i > last_event.to_i
+ set_sha1_checksums_hash(path, sha1_checksum(path))
+ true
+ end
rescue
false
end
- def file_content_modified?(path)
- sha1_checksum = ::Digest::SHA1.file(path).to_s
+ def file_content_modified?(path, sha1_checksum)
if sha1_checksums_hash[path] != sha1_checksum
- @sha1_checksums_hash[path] = sha1_checksum
+ set_sha1_checksums_hash(path, sha1_checksum)
true
else
false
end
end
+ def set_sha1_checksums_hash(path, sha1_checksum)
+ @sha1_checksums_hash[path] = sha1_checksum
+ end
+
+ def sha1_checksum(path)
+ ::Digest::SHA1.file(path).to_s
+ end
+
def self.mac?
Config::CONFIG['target_os'] =~ /darwin/i
end
View
4 spec/guard/listener_spec.rb
@@ -69,7 +69,7 @@
it "ignores the files for the second time" do
FileUtils.touch([file1, file2, file3])
subject.modified_files([@fixture_path.join("folder1/")], {}).should =~ ["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
- sleep 1
+ subject.update_last_event
FileUtils.touch([file1, file2, file3])
subject.modified_files([@fixture_path.join("folder1/")], {}).should == []
sleep 1
@@ -82,7 +82,7 @@
it "identifies the files for the second time" do
FileUtils.touch([file1, file2, file3])
subject.modified_files([@fixture_path.join("folder1/")], {}).should =~ ["spec/fixtures/folder1/deletedfile1.txt", "spec/fixtures/folder1/file1.txt"]
- sleep 1
+ subject.update_last_event
FileUtils.touch([file2, file3])
File.open(file1, "w") { |f| f.write("changed content") }
subject.modified_files([@fixture_path.join("folder1/")], {}).should =~ ["spec/fixtures/folder1/file1.txt"]
Please sign in to comment.
Something went wrong with that request. Please try again.